Skip to content

Commit

Permalink
ignore pull request event for github actions, use speculative branch …
Browse files Browse the repository at this point in the history
…selection feature
  • Loading branch information
krzkaczor committed Sep 4, 2019
1 parent 4191915 commit 808cd20
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"repository": "codechecks/monorepo",
"author": "Chris Kaczor <[email protected]>",
"version": "0.1.8-beta.3",
"version": "0.1.8-beta.4",
"main": "dist/index.js",
"bin": {
"codechecks": "dist/runner.js"
Expand Down
28 changes: 5 additions & 23 deletions packages/client/src/ci-providers/GithubActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Env, CiProvider } from "./types";
import { crash } from "../utils/errors";

// github actions have explicit event for PR creation but we cant use it since it fired only once
// in future we need to build a mechanism to get pull request information using API since it's impossible to get from the inside the PR

export class GithubActions implements CiProvider {
constructor(private readonly env: Env) {}

Expand All @@ -9,27 +12,7 @@ export class GithubActions implements CiProvider {
}

getPullRequestID(): number | undefined {
if (this.env["GITHUB_EVENT_NAME"] !== "pull_request") {
return undefined;
}
const eventPayload = this.getEventPayload();
const prNumber = eventPayload.number;

if (!prNumber || isNaN(prNumber)) {
return undefined;
}

return prNumber;
}

private getEventPayload(): { number: number } {
const eventPath = this.env["GITHUB_EVENT_PATH"] || "";

try {
return require(eventPath);
} catch (error) {
throw crash(`Could not parse Github event JSON file: ${error}`);
}
return undefined;
}

getCurrentSha(): string {
Expand Down Expand Up @@ -58,7 +41,6 @@ export class GithubActions implements CiProvider {
}

public supportsSpeculativeBranchSelection(): boolean {
// github actions have explicit event for PR creation so we don't need speculative branch selection
return false;
return true;
}
}
18 changes: 2 additions & 16 deletions packages/client/src/ci-providers/__tests__/GithubActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ import { GithubActions } from "../GithubActions";
describe("Github Actions", () => {
const env = readEnvFile(join(__dirname, "__fixtures__", "github/pr.env"));
const envFork = readEnvFile(join(__dirname, "__fixtures__", "github/fork.env"));
const envNoPR = readEnvFile(join(__dirname, "__fixtures__", "github/nopr.env"));

// We have to stub the path here because we cannot do this in the .env fixture. Github
// will give us an absolute path.
// @see https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-variables
env.GITHUB_EVENT_PATH = join(__dirname, env.GITHUB_EVENT_PATH || "");
envFork.GITHUB_EVENT_PATH = join(__dirname, envFork.GITHUB_EVENT_PATH || "");
envNoPR.GITHUB_EVENT_PATH = join(__dirname, envNoPR.GITHUB_EVENT_PATH || "");

it("should detect github", () => {
const provider = new GithubActions(env);
Expand All @@ -27,16 +19,10 @@ describe("Github Actions", () => {
expect(provider.isCurrentlyRunning()).toBe(false);
});

it("should get pull request id", () => {
it("should not get pull request id", () => {
const provider = new GithubActions(env);

expect(provider.getPullRequestID()).toBe(2);
});

it("should not get pull request id if not running in PR context", () => {
const provider = new GithubActions(envNoPR);

expect(provider.getPullRequestID()).toBeUndefined();
expect(provider.getPullRequestID()).toBe(undefined);
});

it("should get target SHA", () => {
Expand Down

0 comments on commit 808cd20

Please sign in to comment.