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

Add fallback for git branch calculation #871

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};