diff --git a/packages/apollo/src/__tests__/git.test.ts b/packages/apollo/src/__tests__/git.test.ts new file mode 100644 index 0000000000..1bd52a129d --- /dev/null +++ b/packages/apollo/src/__tests__/git.test.ts @@ -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(); + }); +}); diff --git a/packages/apollo/src/git.ts b/packages/apollo/src/git.ts index ebfa3efa4c..d4dabe8b19 100644 --- a/packages/apollo/src/git.ts +++ b/packages/apollo/src/git.ts @@ -35,14 +35,24 @@ export interface GitContext { } export const gitInfo = async (): Promise => { - 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[]) => @@ -63,10 +73,25 @@ export const gitInfo = async (): Promise => { 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; };