From 3b40f4c17a87cf1f727fc3ec9497f9ee65cc76e1 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Wed, 14 Oct 2020 17:14:23 +0200 Subject: [PATCH 1/9] Record the repository slug (owner/repo) to support forks. --- bin/git/getCommitAndBranch.js | 36 ++++++++++++++++++++++------------- bin/git/git.js | 6 ++++++ bin/main.test.js | 1 + bin/tasks/gitInfo.js | 3 ++- bin/tasks/gitInfo.test.js | 3 ++- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/bin/git/getCommitAndBranch.js b/bin/git/getCommitAndBranch.js index 0c4991937..c89a66c7c 100644 --- a/bin/git/getCommitAndBranch.js +++ b/bin/git/getCommitAndBranch.js @@ -74,30 +74,29 @@ export async function getCommitAndBranch({ patchBaseRef, inputFromCI, log } = {} } } + const { isCi, prBranch, branch: ciBranch, slug } = envCi(); + // On certain CI systems, a branch is not checked out // (instead a detached head is used for the commit). if (!notHead(branch)) { - const { prBranch: prBranchFromEnvCi, branch: branchFromEnvCi } = envCi(); - - // $HEAD is for netlify: https://www.netlify.com/docs/continuous-deployment/ - // $GERRIT_BRANCH is for Gerrit/Jenkins: https://wiki.jenkins.io/display/JENKINS/Gerrit+Trigger - // $CI_BRANCH is a general setting that lots of systems use branch = - notHead(prBranchFromEnvCi) || - notHead(branchFromEnvCi) || - notHead(process.env.HEAD) || - notHead(process.env.GERRIT_BRANCH) || + notHead(prBranch) || + notHead(ciBranch) || + notHead(process.env.HEAD) || // https://www.netlify.com/docs/continuous-deployment/ + notHead(process.env.GERRIT_BRANCH) || // https://wiki.jenkins.io/display/JENKINS/Gerrit+Trigger + notHead(process.env.GITHUB_REF) || // https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables notHead(process.env.CI_BRANCH) || - notHead(process.env.GITHUB_REF) || notHead(branch) || 'HEAD'; } - // REPOSITORY_URL is for netlify: https://www.netlify.com/docs/continuous-deployment/ + const fromCI = + isCi || !!inputFromCI || !!process.env.CI || - !!process.env.REPOSITORY_URL || + !!process.env.REPOSITORY_URL || // https://www.netlify.com/docs/continuous-deployment/ !!process.env.GITHUB_REPOSITORY; + log.debug( `git info: ${JSON.stringify({ commit, @@ -105,9 +104,20 @@ export async function getCommitAndBranch({ patchBaseRef, inputFromCI, log } = {} committerEmail, committerName, branch, + slug, isTravisPrBuild, fromCI, })}` ); - return { commit, committedAt, committerEmail, committerName, branch, isTravisPrBuild, fromCI }; + + return { + commit, + committedAt, + committerEmail, + committerName, + branch, + slug, + isTravisPrBuild, + fromCI, + }; } diff --git a/bin/git/git.js b/bin/git/git.js index 53def3be0..4d9c6c04c 100644 --- a/bin/git/git.js +++ b/bin/git/git.js @@ -78,6 +78,12 @@ export async function getVersion() { return result.replace('git version ', ''); } +export async function getSlug() { + const result = await execGitCommand(`git config --get remote.origin.url`); + const [, slug] = result.match(/([^/:]+\/[^/]+?)(\.git)?/) || []; + return slug; +} + // NOTE: At some point we should check that the commit has been pushed to the // remote and the branch matches with origin/REF, but for now we are naive about // adhoc builds. diff --git a/bin/main.test.js b/bin/main.test.js index 8941b1389..31c8494e2 100644 --- a/bin/main.test.js +++ b/bin/main.test.js @@ -135,6 +135,7 @@ jest.mock('./git/git', () => ({ }), getBranch: () => 'branch', getBaselineCommits: () => ['baseline'], + getSlug: () => 'user/repo', getVersion: () => '2.24.1', })); diff --git a/bin/tasks/gitInfo.js b/bin/tasks/gitInfo.js index 28cddd4f5..160099145 100644 --- a/bin/tasks/gitInfo.js +++ b/bin/tasks/gitInfo.js @@ -1,7 +1,7 @@ import picomatch from 'picomatch'; import { getCommitAndBranch } from '../git/getCommitAndBranch'; -import { getBaselineCommits, getVersion } from '../git/git'; +import { getBaselineCommits, getSlug, getVersion } from '../git/git'; import { createTask, transitionTo } from '../lib/tasks'; import { initial, @@ -22,6 +22,7 @@ export const setGitInfo = async (ctx, task) => { const { patchBaseRef, fromCI, ignoreLastBuildOnBranch, skip } = ctx.options; ctx.git = await getCommitAndBranch({ patchBaseRef, inputFromCI: fromCI, log: ctx.log }); + ctx.git.slug = ctx.git.slug || (await getSlug()); ctx.git.version = await getVersion(); const { branch, commit } = ctx.git; diff --git a/bin/tasks/gitInfo.test.js b/bin/tasks/gitInfo.test.js index 8ba375920..0a16cff02 100644 --- a/bin/tasks/gitInfo.test.js +++ b/bin/tasks/gitInfo.test.js @@ -1,5 +1,5 @@ import { getCommitAndBranch } from '../git/getCommitAndBranch'; -import { getBaselineCommits, getVersion } from '../git/git'; +import { getBaselineCommits, getSlug, getVersion } from '../git/git'; import { setGitInfo } from './gitInfo'; jest.mock('../git/getCommitAndBranch'); @@ -11,6 +11,7 @@ describe('setGitInfo', () => { it('sets the git info on context', async () => { getCommitAndBranch.mockReturnValue({ commit: '123asdf', branch: 'something' }); getBaselineCommits.mockReturnValue(['asd2344']); + getSlug.mockReturnValue('user/repo'); getVersion.mockReturnValue('Git v1.0.0'); const ctx = { log, options: {} }; await setGitInfo(ctx, {}); From 6ae06ce5558d26a892faba3f21353ce8f85f02dc Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 16 Oct 2020 10:00:24 +0200 Subject: [PATCH 2/9] Add comment on what slug means. --- bin/git/git.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/git/git.js b/bin/git/git.js index 4d9c6c04c..08b498100 100644 --- a/bin/git/git.js +++ b/bin/git/git.js @@ -78,6 +78,9 @@ export async function getVersion() { return result.replace('git version ', ''); } +// The slug consists of the last two parts of the URL, at least for GitHub, GitLab and Bitbucket, +// and is typically followed by `.git`. The regex matches the last two parts between slashes, and +// ignores the `.git` suffix if it exists, so it matches something like `ownername/reponame`. export async function getSlug() { const result = await execGitCommand(`git config --get remote.origin.url`); const [, slug] = result.match(/([^/:]+\/[^/]+?)(\.git)?/) || []; From f1139f06b2fe1f44de4d3bbf50eebf72b7bc81c1 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 16 Oct 2020 21:15:03 +0200 Subject: [PATCH 3/9] Update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 894da95e0..d063d3c8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 5.3.0 - unreleased + +- Record the repository slug (owner/repo) to support forks. +- Retrieve branch name using more modern git commands, if available. +- Auto-detect buildScriptName from available scripts. +- Improve various log messages. + # 5.2.0 - 2020-09-14 - Keep track of baselines when doing squash or rebase merges. From d98c10a771c2d13ee9fe375fd335ff02e6ca849c Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 16 Oct 2020 21:15:34 +0200 Subject: [PATCH 4/9] 5.3.0-alpha.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 00faea947..4367ef78f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromatic", - "version": "5.2.0", + "version": "5.3.0-alpha.0", "description": "Visual Testing for Storybook", "homepage": "https://www.chromatic.com", "bugs": { From 9d4c679ed781d9b66e99fc4ca77b43f3dfb8a7f9 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 16 Oct 2020 21:47:13 +0200 Subject: [PATCH 5/9] Fix slug regex and add tests. --- bin/git/git.js | 2 +- bin/git/git.test.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/bin/git/git.js b/bin/git/git.js index c96a42719..9813ab69d 100644 --- a/bin/git/git.js +++ b/bin/git/git.js @@ -83,7 +83,7 @@ export async function getVersion() { // ignores the `.git` suffix if it exists, so it matches something like `ownername/reponame`. export async function getSlug() { const result = await execGitCommand(`git config --get remote.origin.url`); - const [, slug] = result.match(/([^/:]+\/[^/]+?)(\.git)?/) || []; + const [, slug] = result.match(/([^/:]+\/[^/]+?)(\.git)?$/) || []; return slug; } diff --git a/bin/git/git.test.js b/bin/git/git.test.js index ad446ee79..592571d3c 100644 --- a/bin/git/git.test.js +++ b/bin/git/git.test.js @@ -1,11 +1,12 @@ /* eslint-disable jest/expect-expect */ import { exec } from 'child_process'; +import execa from 'execa'; import process from 'process'; import { dirSync } from 'tmp'; import { promisify } from 'util'; import generateGitRepository from './generateGitRepository'; -import { getBaselineCommits } from './git'; +import { getBaselineCommits, getSlug } from './git'; import longLineDescription from './mocks/long-line'; import longLoopDescription from './mocks/long-loop'; import createMockIndex from './mocks/mock-index'; @@ -13,6 +14,8 @@ import simpleLoopDescription from './mocks/simple-loop'; import threeParentsDescription from './mocks/three-parents'; import twoRootsDescription from './mocks/two-roots'; +const execaCommand = jest.spyOn(execa, 'command'); + // Bumping up the Jest timeout for this file because it is timing out sometimes // I think this just a bit of a slow file due to git stuff, takes ~2-3s on my computer. jest.setTimeout(30 * 1000); @@ -415,3 +418,16 @@ describe('getBaselineCommits', () => { }); }); }); + +describe('getSlug', () => { + it('returns the slug portion of the git url', async () => { + execaCommand.mockImplementation(() => ({ all: 'git@github.com:chromaui/chromatic-cli.git' })); + expect(await getSlug()).toBe('chromaui/chromatic-cli'); + + execaCommand.mockImplementation(() => ({ all: 'https://github.com/chromaui/chromatic-cli' })); + expect(await getSlug()).toBe('chromaui/chromatic-cli'); + + execaCommand.mockImplementation(() => ({ all: 'https://gitlab.com/foo/bar.baz.git' })); + expect(await getSlug()).toBe('foo/bar.baz'); + }); +}); From 0032a8792e6a9b3e99c3135447579034fdc7143f Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 16 Oct 2020 21:47:39 +0200 Subject: [PATCH 6/9] 5.3.0-alpha.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4367ef78f..dc6851f42 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromatic", - "version": "5.3.0-alpha.0", + "version": "5.3.0-alpha.1", "description": "Visual Testing for Storybook", "homepage": "https://www.chromatic.com", "bugs": { From bf3d9db92be4ee3741dfe6a43572455032884d0b Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Sun, 8 Nov 2020 20:16:21 +0100 Subject: [PATCH 7/9] 5.4.0-alpha.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dc6851f42..b57a78572 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromatic", - "version": "5.3.0-alpha.1", + "version": "5.4.0-alpha.0", "description": "Visual Testing for Storybook", "homepage": "https://www.chromatic.com", "bugs": { From e037e3f9f80b608e3aceed0a57b2556f19f48b1f Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 21 Dec 2020 14:05:11 +0100 Subject: [PATCH 8/9] Avoid eslint oddities. --- bin/lib/logSerializers.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/lib/logSerializers.test.js b/bin/lib/logSerializers.test.js index 1977aebab..b6a17caad 100644 --- a/bin/lib/logSerializers.test.js +++ b/bin/lib/logSerializers.test.js @@ -1,14 +1,15 @@ -/* eslint-disable jest/no-try-expect */ import { execSync } from 'child_process'; import { errorSerializer } from './logSerializers'; it('strips off envPairs', () => { + let err; try { execSync('some hot garbage'); - } catch (err) { - expect(errorSerializer(err).envPairs).toBeUndefined(); + } catch (e) { + err = e; } + expect(errorSerializer(err).envPairs).toBeUndefined(); }); it('does not add random things to the error', () => { From 4c5ab2f106ce1058de1698b616357cce51a6baf6 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Tue, 22 Dec 2020 16:27:12 +0100 Subject: [PATCH 9/9] 5.6.0-alpha.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b49a262e4..138143cd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromatic", - "version": "5.5.0", + "version": "5.6.0-alpha.0", "description": "Visual Testing for Storybook", "homepage": "https://www.chromatic.com", "bugs": {