diff --git a/node-src/tasks/gitInfo.test.ts b/node-src/tasks/gitInfo.test.ts index cab1e6d96..ba122dbe7 100644 --- a/node-src/tasks/gitInfo.test.ts +++ b/node-src/tasks/gitInfo.test.ts @@ -154,4 +154,14 @@ describe('setGitInfo', () => { expect(ctx.rebuildForBuild).toEqual(lastBuild); expect(ctx.storybookUrl).toEqual(lastBuild.storybookUrl); }); + + it('removes undefined owner prefix from branch', async () => { + const ctx = { log, options: {}, client } as any; + getCommitAndBranch.mockResolvedValue({ + ...commitInfo, + branch: 'undefined:repo', + }); + await setGitInfo(ctx, {} as any); + expect(ctx.git.branch).toBe('repo'); + }); }); diff --git a/node-src/tasks/gitInfo.ts b/node-src/tasks/gitInfo.ts index a4e6eb843..1772c1d4c 100644 --- a/node-src/tasks/gitInfo.ts +++ b/node-src/tasks/gitInfo.ts @@ -15,6 +15,7 @@ import replacedBuild from '../ui/messages/info/replacedBuild'; import externalsChanged from '../ui/messages/warnings/externalsChanged'; import invalidChangedFiles from '../ui/messages/warnings/invalidChangedFiles'; import isRebuild from '../ui/messages/warnings/isRebuild'; +import undefinedBranchOwner from '../ui/messages/warnings/undefinedBranchOwner'; import { initial, pending, @@ -25,6 +26,8 @@ import { success, } from '../ui/tasks/gitInfo'; +const UNDEFINED_BRANCH_PREFIX_REGEXP = /^undefined:/; + const SkipBuildMutation = ` mutation SkipBuildMutation($commit: String!, $branch: String, $slug: String) { skipBuild(commit: $commit, branch: $branch, slug: $slug) @@ -99,6 +102,10 @@ export const setGitInfo = async (ctx: Context, task: Task) => { if (ownerName) { ctx.git.branch = ctx.git.branch.replace(/[^:]+:/, ''); ctx.git.slug = repositorySlug || ctx.git.slug?.replace(/[^/]+/, ownerName); + } else if (UNDEFINED_BRANCH_PREFIX_REGEXP.test(ctx.git.branch)) { + // Strip off `undefined:` owner prefix that we have seen in some CI systems. + ctx.log.warn(undefinedBranchOwner()); + ctx.git.branch = ctx.git.branch.replace(UNDEFINED_BRANCH_PREFIX_REGEXP, ''); } const { branch, commit, slug } = ctx.git; diff --git a/node-src/ui/messages/warnings/undefinedBranchOwner.stories.ts b/node-src/ui/messages/warnings/undefinedBranchOwner.stories.ts new file mode 100644 index 000000000..ad817360f --- /dev/null +++ b/node-src/ui/messages/warnings/undefinedBranchOwner.stories.ts @@ -0,0 +1,5 @@ +export default { + title: 'CLI/Messages/Warnings', +}; + +export { default as UndefinedBranchOwner } from './undefinedBranchOwner'; diff --git a/node-src/ui/messages/warnings/undefinedBranchOwner.ts b/node-src/ui/messages/warnings/undefinedBranchOwner.ts new file mode 100644 index 000000000..4fc82238f --- /dev/null +++ b/node-src/ui/messages/warnings/undefinedBranchOwner.ts @@ -0,0 +1,16 @@ +import chalk from 'chalk'; +import { dedent } from 'ts-dedent'; + +import { info, warning } from '../../components/icons'; +import link from '../../components/link'; + +const docsUrl = + 'https://www.chromatic.com/docs/custom-ci-provider/#overriding-chromatics-branch-detection'; + +export default () => { + return dedent(chalk` + ${warning} Removing unknown owner prefix from branch name. + You may wish to set the branch directly to avoid incorrect values. + ${info} Read more at ${link(docsUrl)} + `); +};