Skip to content

Commit 5b5fe71

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 5b5fe71

2 files changed

Lines changed: 39 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: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ export interface GitContext {
3535
}
3636

3737
export const gitInfo = async (): Promise<GitContext | undefined> => {
38-
const { isCi, commit, branch, slug, root } = ci();
38+
// Occassionally `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;
4653
if (gitLoc) {
@@ -63,10 +70,25 @@ export const gitInfo = async (): Promise<GitContext | undefined> => {
6370
remoteUrl = git.remoteUrl();
6471
} catch (e) {}
6572
}
73+
74+
// The ci and pr branches pulled from the ci's environment can undefined,
75+
// so we fallback on the git context
76+
//
77+
// To determine the cases when branch could be undefined, see
78+
// https://github.com/pvdlg/env-ci#caveats
79+
if (!branch) {
80+
branch = git.branch();
81+
}
6682
}
6783

6884
return pickBy(
69-
{ committer, commit, remoteUrl, message, branch },
85+
{
86+
committer,
87+
commit,
88+
remoteUrl,
89+
message,
90+
branch
91+
},
7092
identity
7193
) as GitContext;
7294
};

0 commit comments

Comments
 (0)