Skip to content

Commit e805305

Browse files
authored
apollo: fix remoteUrl in service:check (#1121)
The remoteUrl can no longer be the slug, since we can't assume that the feature is going to be used exclusively in GitHub. Additionally we add some logging in order to help debug the gitInfo() function
1 parent a92eaff commit e805305

6 files changed

Lines changed: 23 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Upcoming
44

5+
- `apollo`
6+
- fix remoteUrl(remove slug) for service:check [#1121](https://github.com/apollographql/apollo-tooling/pull/1121)
7+
58
## `apollo-graphql@0.2.0`
69

710
- `apollo-graphql@0.2.0`

packages/apollo/src/__tests__/git.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ describe("Git integration", () => {
44
it("Returns commit, branch, message, committer, and remoteUrl", async () => {
55
// Currently these tests are too granular and would be better as
66
// service:push tests when they are uncommented
7-
const info = await gitInfo();
7+
const info = await gitInfo(console.log);
88

99
expect(info.commit).toBeDefined();
1010
expect(info.committer).toBeDefined();
1111
expect(info.remoteUrl).toBeDefined();
12+
// Match both ssh and http/s remotes
13+
expect(info.remoteUrl).toMatch(
14+
/(https?:\/\/|git@)github.com(\/|:)apollographql\/apollo-tooling(.git)?/
15+
);
1216
expect(info.message).toBeDefined();
1317
expect(info.branch).toBeDefined();
1418
});

packages/apollo/src/commands/client/check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class ClientCheck extends ClientCommand {
3838
if (!config.name) {
3939
throw new Error("No service found to link to Engine");
4040
}
41-
ctx.gitContext = await gitInfo();
41+
ctx.gitContext = await gitInfo(this.log);
4242

4343
ctx.operations = Object.entries(
4444
this.project.mergedOperationsAndFragmentsForService

packages/apollo/src/commands/service/check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default class ServiceCheck extends ProjectCommand {
7474

7575
const tag = flags.tag || config.tag || "current";
7676
const schema = await project.resolveSchema({ tag });
77-
ctx.gitContext = await gitInfo();
77+
ctx.gitContext = await gitInfo(this.log);
7878

7979
const historicParameters = validateHistoricParams({
8080
validationPeriod: flags.validationPeriod,

packages/apollo/src/commands/service/push.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class ServicePush extends ProjectCommand {
3333
}
3434

3535
const schema = await project.resolveSchema({ tag: flags.tag });
36-
gitContext = await gitInfo();
36+
gitContext = await gitInfo(this.log);
3737

3838
const { tag, code } = await project.engine.uploadSchema({
3939
id: config.name,

packages/apollo/src/git.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as ci from "env-ci";
44
import { gitToJs } from "git-parse";
55
import * as git from "git-rev-sync";
66
import { pickBy, identity } from "lodash";
7+
import Command from "@oclif/command";
78

89
const findGitRoot = (start?: string | string[]): string | void => {
910
start = start || process.cwd();
@@ -34,22 +35,22 @@ export interface GitContext {
3435
branch?: string;
3536
}
3637

37-
export const gitInfo = async (): Promise<GitContext | undefined> => {
38+
export const gitInfo = async (
39+
log: Command["log"]
40+
): Promise<GitContext | undefined> => {
3841
// Occasionally `branch` will be undefined depending on the environment, so
3942
// we need to fallback on `prBranch`. However in some cases, we are not able
4043
// to get to the branch at all. For more information, see
4144
// 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();
45+
const { commit, branch: ciBranch, root, prBranch } = ci();
4546
const gitLoc = root ? root : findGitRoot();
4647

4748
if (!commit) return;
4849

4950
let committer;
5051
let branch = ciBranch || prBranch;
5152
// BUILD_REPOSITORY_ID is for azure pipelines
52-
let remoteUrl = slug || process.env.BUILD_REPOSITORY_ID;
53+
let remoteUrl = process.env.BUILD_REPOSITORY_ID;
5354
let message;
5455

5556
// In order to use git-parse and git-rev-sync, we must ensure that a git context is
@@ -69,10 +70,12 @@ export const gitInfo = async (): Promise<GitContext | undefined> => {
6970

7071
message = commit.message;
7172

72-
if (!isCi) {
73-
try {
74-
remoteUrl = git.remoteUrl();
75-
} catch (e) {}
73+
// The remoteUrl call can fail and throw an error
74+
// https://github.com/kurttheviking/git-rev-sync-js#gitremoteurl--string
75+
try {
76+
remoteUrl = git.remoteUrl();
77+
} catch (e) {
78+
log(["Unable to retrieve remote url, failed with:", e].join("\n\n"));
7679
}
7780

7881
// The ci and pr branches pulled from the ci's environment can be undefined,

0 commit comments

Comments
 (0)