Skip to content

Commit

Permalink
Merge pull request #1101 from chromaui/jmhobbs/cap-2210-better-cli-ha…
Browse files Browse the repository at this point in the history
…ndling-of-undefined-branches

Better handling of undefined values in branches
  • Loading branch information
jmhobbs authored Oct 21, 2024
2 parents 9ae5118 + 5d55843 commit 94f914e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 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';
15 changes: 15 additions & 0 deletions node-src/ui/messages/warnings/undefinedBranchOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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/faq/override-branch-name/';

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)}
`);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"cross-spawn": "^7.0.2",
"debug": "^4.3.2",
"dotenv": "^8.2.0",
"env-ci": "^5.0.2",
"env-ci": "^11.1.0",
"eslint": "^9.10.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
Expand Down
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7565,7 +7565,7 @@ __metadata:
cross-spawn: "npm:^7.0.2"
debug: "npm:^4.3.2"
dotenv: "npm:^8.2.0"
env-ci: "npm:^5.0.2"
env-ci: "npm:^11.1.0"
eslint: "npm:^9.10.0"
eslint-config-prettier: "npm:^9.0.0"
eslint-plugin-import: "npm:^2.28.1"
Expand Down Expand Up @@ -8987,7 +8987,17 @@ __metadata:
languageName: node
linkType: hard

"env-ci@npm:^5.0.1, env-ci@npm:^5.0.2":
"env-ci@npm:^11.1.0":
version: 11.1.0
resolution: "env-ci@npm:11.1.0"
dependencies:
execa: "npm:^8.0.0"
java-properties: "npm:^1.0.2"
checksum: 10c0/14f0a597c1fe9ab5585532c01759db62f4c553277b33137d33cb71cdd621833184f182dc67408750973c9f884f5a0d5103fad4f873aabd9e6c4baf65f88bc22a
languageName: node
linkType: hard

"env-ci@npm:^5.0.1":
version: 5.5.0
resolution: "env-ci@npm:5.5.0"
dependencies:
Expand Down Expand Up @@ -10008,7 +10018,7 @@ __metadata:
languageName: node
linkType: hard

"execa@npm:^8.0.1":
"execa@npm:^8.0.0, execa@npm:^8.0.1":
version: 8.0.1
resolution: "execa@npm:8.0.1"
dependencies:
Expand Down Expand Up @@ -12282,7 +12292,7 @@ __metadata:
languageName: node
linkType: hard

"java-properties@npm:^1.0.0":
"java-properties@npm:^1.0.0, java-properties@npm:^1.0.2":
version: 1.0.2
resolution: "java-properties@npm:1.0.2"
checksum: 10c0/be0f58c83b5a852f313de2ea57f7b8b7d46dc062b2ffe487d58838e7034d4660f4d22f2a96aae4daa622af6d734726c0d08b01396e59666ededbcfdc25a694d6
Expand Down

0 comments on commit 94f914e

Please sign in to comment.