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 3, 2019
1 parent d90a883 commit 5b5fe71
Show file tree
Hide file tree
Showing 2 changed files with 39 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();
});
});
26 changes: 24 additions & 2 deletions packages/apollo/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ export interface GitContext {
}

export const gitInfo = async (): Promise<GitContext | undefined> => {
const { isCi, commit, branch, slug, root } = ci();
// Occassionally `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;
if (gitLoc) {
Expand All @@ -63,10 +70,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 undefined,
// so we fallback on the git context
//
// To determine the cases when branch could be undefined, see
// https://github.com/pvdlg/env-ci#caveats
if (!branch) {
branch = git.branch();
}
}

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

0 comments on commit 5b5fe71

Please sign in to comment.