Skip to content

Commit d05447d

Browse files
committed
Add fallback for git branch calculation
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
1 parent d90a883 commit d05447d

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { gitInfo } from "../git";
2+
3+
describe("Git integration", () => {
4+
it("Returns commit, branch, message, committer, and remoteUrl", async () => {
5+
// Currently these tests are too granular and would be better as
6+
// service:push tests when they are uncommented
7+
const info = await gitInfo();
8+
9+
expect(info.commit).toBeDefined();
10+
expect(info.committer).toBeDefined();
11+
expect(info.remoteUrl).toBeDefined();
12+
expect(info.message).toBeDefined();
13+
expect(info.branch).toBeDefined();
14+
});
15+
});

packages/apollo/src/git.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ export interface GitContext {
3535
}
3636

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

4147
if (!commit) return;
4248

4349
let committer;
50+
let branch = ciBranch || prBranch;
4451
let remoteUrl = slug;
4552
let message;
53+
54+
// In order to use git-parse and git-rev-sync, we must ensure that a git context is
55+
// accessible. Without this check, the commands would throw
4656
if (gitLoc) {
4757
const { authorName, authorEmail, ...commit } = await gitToJs(gitLoc)
4858
.then((commits: Commit[]) =>
@@ -63,10 +73,25 @@ export const gitInfo = async (): Promise<GitContext | undefined> => {
6373
remoteUrl = git.remoteUrl();
6474
} catch (e) {}
6575
}
76+
77+
// The ci and pr branches pulled from the ci's environment can be undefined,
78+
// so we fallback on the git context.
79+
//
80+
// See https://github.com/pvdlg/env-ci#caveats for a detailed list of when
81+
// branch can be undefined
82+
if (!branch) {
83+
branch = git.branch();
84+
}
6685
}
6786

6887
return pickBy(
69-
{ committer, commit, remoteUrl, message, branch },
88+
{
89+
committer,
90+
commit,
91+
remoteUrl,
92+
message,
93+
branch
94+
},
7095
identity
7196
) as GitContext;
7297
};

0 commit comments

Comments
 (0)