Skip to content

Commit

Permalink
Remove undefined origin values in branch names.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhobbs committed Oct 18, 2024
1 parent b8d4cc8 commit 925b5f0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions node-src/tasks/gitInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
7 changes: 7 additions & 0 deletions node-src/tasks/gitInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions node-src/ui/messages/warnings/undefinedBranchOwner.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
title: 'CLI/Messages/Warnings',
};

export { default as UndefinedBranchOwner } from './undefinedBranchOwner';
16 changes: 16 additions & 0 deletions node-src/ui/messages/warnings/undefinedBranchOwner.ts
Original file line number Diff line number Diff line change
@@ -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)}
`);
};

0 comments on commit 925b5f0

Please sign in to comment.