Skip to content

Commit

Permalink
Add fallback for git branch calculation
Browse files Browse the repository at this point in the history
In some ci's, the git branch will not be present in the environment with
the default that env-ci uses, so we need to provide a fallback. The
fallback is either the PR's branch if present or the .git branch. This
branch is used inside of the Engine frontend to display more information
about the schema changes.

See https://github.com/pvdlg/env-ci#caveats for a longer list of cases
when the branch will not be present in the ci environment
  • Loading branch information
evans committed Jan 4, 2019
1 parent d90a883 commit d05447d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/apollo/src/__tests__/git.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { gitInfo } from "../git";

describe("Git integration", () => {
it("Returns commit, branch, message, committer, and remoteUrl", async () => {
// Currently these tests are too granular and would be better as
// service:push tests when they are uncommented
const info = await gitInfo();

expect(info.commit).toBeDefined();
expect(info.committer).toBeDefined();
expect(info.remoteUrl).toBeDefined();
expect(info.message).toBeDefined();
expect(info.branch).toBeDefined();
});
});
29 changes: 27 additions & 2 deletions packages/apollo/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ export interface GitContext {
}

export const gitInfo = async (): Promise<GitContext | undefined> => {
const { isCi, commit, branch, slug, root } = ci();
// Occasionally `branch` will be undefined depending on the environment, so
// we need to fallback on `prBranch`. However in some cases, we are not able
// to get to the branch at all. For more information, see
// https://github.com/pvdlg/env-ci#caveats
//
// slug is formatted as follows: ${organization}/${repository name}
const { isCi, commit, branch: ciBranch, slug, root, prBranch } = ci();
const gitLoc = root ? root : findGitRoot();

if (!commit) return;

let committer;
let branch = ciBranch || prBranch;
let remoteUrl = slug;
let message;

// In order to use git-parse and git-rev-sync, we must ensure that a git context is
// accessible. Without this check, the commands would throw
if (gitLoc) {
const { authorName, authorEmail, ...commit } = await gitToJs(gitLoc)
.then((commits: Commit[]) =>
Expand All @@ -63,10 +73,25 @@ export const gitInfo = async (): Promise<GitContext | undefined> => {
remoteUrl = git.remoteUrl();
} catch (e) {}
}

// The ci and pr branches pulled from the ci's environment can be undefined,
// so we fallback on the git context.
//
// See https://github.com/pvdlg/env-ci#caveats for a detailed list of when
// branch can be undefined
if (!branch) {
branch = git.branch();
}
}

return pickBy(
{ committer, commit, remoteUrl, message, branch },
{
committer,
commit,
remoteUrl,
message,
branch
},
identity
) as GitContext;
};

0 comments on commit d05447d

Please sign in to comment.