Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record the repository slug to support builds from forks #193

Merged
merged 14 commits into from Jan 12, 2021
36 changes: 23 additions & 13 deletions bin/git/getCommitAndBranch.js
Expand Up @@ -74,40 +74,50 @@ 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,
committedAt,
committerEmail,
committerName,
branch,
slug,
isTravisPrBuild,
fromCI,
})}`
);
return { commit, committedAt, committerEmail, committerName, branch, isTravisPrBuild, fromCI };

return {
commit,
committedAt,
committerEmail,
committerName,
branch,
slug,
isTravisPrBuild,
fromCI,
};
}
6 changes: 6 additions & 0 deletions bin/git/git.js
Expand Up @@ -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)?/) || [];
ghengeveld marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
1 change: 1 addition & 0 deletions bin/main.test.js
Expand Up @@ -135,6 +135,7 @@ jest.mock('./git/git', () => ({
}),
getBranch: () => 'branch',
getBaselineCommits: () => ['baseline'],
getSlug: () => 'user/repo',
getVersion: () => '2.24.1',
}));

Expand Down
3 changes: 2 additions & 1 deletion 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,
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion 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');
Expand All @@ -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, {});
Expand Down