diff --git a/packages/apollo/src/__tests__/git.test.ts b/packages/apollo/src/__tests__/git.test.ts new file mode 100644 index 0000000000..c68a972f1a --- /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).toHaveProperty("commit"); + expect(info).toHaveProperty("branch"); + expect(info).toHaveProperty("message"); + expect(info).toHaveProperty("committer"); + expect(info).toHaveProperty("remoteUrl"); + }); +}); diff --git a/packages/apollo/src/git.ts b/packages/apollo/src/git.ts index ebfa3efa4c..8389b78223 100644 --- a/packages/apollo/src/git.ts +++ b/packages/apollo/src/git.ts @@ -35,12 +35,19 @@ export interface GitContext { } export const gitInfo = async (): Promise => { - 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) { @@ -63,10 +70,25 @@ export const gitInfo = async (): Promise => { 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; };