Skip to content

Commit 09a827b

Browse files
authored
fix: Pull requests sha evaluation (#40)
The SHA for pull requests are more complicated. The GITHUB_SHA is not always the actual commit to test. Resolves: #35
1 parent 9401830 commit 09a827b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

code-build.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,19 @@ async function waitForBuildEndTime(sdk, { id, logs }, nextToken) {
111111
function githubInputs() {
112112
const projectName = core.getInput("project-name", { required: true });
113113
const { owner, repo } = github.context.repo;
114+
const { payload } = github.context;
114115
// The github.context.sha is evaluated on import.
115116
// This makes it hard to test.
116-
// So I use the raw ENV
117-
const sourceVersion = process.env[`GITHUB_SHA`];
117+
// So I use the raw ENV.
118+
// There is a complexity here because for pull request
119+
// the GITHUB_SHA value is NOT the correct value.
120+
// See: https://github.com/aws-actions/aws-codebuild-run-build/issues/36
121+
const sourceVersion =
122+
process.env[`GITHUB_EVENT_NAME`] === "pull_request"
123+
? (((payload || {}).pull_request || {}).head || {}).sha
124+
: process.env[`GITHUB_SHA`];
125+
126+
assert(sourceVersion, "No source version could be evaluated.");
118127
const buildspecOverride =
119128
core.getInput("buildspec-override", { required: false }) || undefined;
120129

test/code-build-test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,29 @@ describe("logName", () => {
3333

3434
describe("githubInputs", () => {
3535
const OLD_ENV = { ...process.env };
36+
const { context: OLD_CONTEXT } = require("@actions/github");
37+
const { payload: OLD_PAYLOAD, eventName: OLD_EVENT_NAME } = OLD_CONTEXT;
3638
afterEach(() => {
3739
process.env = { ...OLD_ENV };
40+
const { context } = require("@actions/github");
41+
context.eventName = OLD_EVENT_NAME;
42+
context.payload = OLD_PAYLOAD;
3843
});
3944

4045
const projectName = "project_name";
4146
const repoInfo = "owner/repo";
4247
const sha = "1234abcd-12ab-34cd-56ef-1234567890ab";
48+
const pullRequestSha = "181600acb3cfb803f4570d0018928be5d730c00d";
4349

4450
it("build basic parameters for codeBuild.startBuild", () => {
4551
// This is how GITHUB injects its input values.
4652
// It would be nice if there was an easy way to test this...
4753
process.env[`INPUT_PROJECT-NAME`] = projectName;
4854
process.env[`GITHUB_REPOSITORY`] = repoInfo;
4955
process.env[`GITHUB_SHA`] = sha;
56+
// These tests run in pull requests
57+
// so to tests things that are NOT pull request...
58+
process.env[`GITHUB_EVENT_NAME`] = "not_pull_request";
5059
const test = githubInputs();
5160
expect(test).to.haveOwnProperty("projectName").and.to.equal(projectName);
5261
expect(test).to.haveOwnProperty("sourceVersion").and.to.equal(sha);
@@ -84,6 +93,44 @@ describe("githubInputs", () => {
8493
.to.haveOwnProperty("envPassthrough")
8594
.and.to.deep.equal(["one", "two", "three", "four"]);
8695
});
96+
97+
it("can handle pull requests", () => {
98+
// This is how GITHUB injects its input values.
99+
// It would be nice if there was an easy way to test this...
100+
process.env[`INPUT_PROJECT-NAME`] = projectName;
101+
process.env[`GITHUB_REPOSITORY`] = repoInfo;
102+
process.env[`GITHUB_SHA`] = sha;
103+
process.env[`GITHUB_EVENT_NAME`] = "pull_request";
104+
const { context } = require("@actions/github");
105+
context.payload = { pull_request: { head: { sha: pullRequestSha } } };
106+
const test = githubInputs();
107+
expect(test).to.haveOwnProperty("projectName").and.to.equal(projectName);
108+
expect(test)
109+
.to.haveOwnProperty("sourceVersion")
110+
.and.to.equal(pullRequestSha);
111+
expect(test).to.haveOwnProperty("owner").and.to.equal(`owner`);
112+
expect(test).to.haveOwnProperty("repo").and.to.equal(`repo`);
113+
expect(test)
114+
.to.haveOwnProperty("buildspecOverride")
115+
.and.to.equal(undefined);
116+
expect(test).to.haveOwnProperty("envPassthrough").and.to.deep.equal([]);
117+
});
118+
119+
it("will not continue if there is no payload", () => {
120+
// This is how GITHUB injects its input values.
121+
// It would be nice if there was an easy way to test this...
122+
process.env[`INPUT_PROJECT-NAME`] = projectName;
123+
process.env[`GITHUB_REPOSITORY`] = repoInfo;
124+
process.env[`GITHUB_SHA`] = sha;
125+
process.env[`GITHUB_EVENT_NAME`] = "pull_request";
126+
// These tests run in pull requests
127+
// so to tests things that are NOT pull request...
128+
require("@actions/github").context.payload = {};
129+
130+
expect(() => githubInputs()).to.throw(
131+
"No source version could be evaluated."
132+
);
133+
});
87134
});
88135

89136
describe("inputs2Parameters", () => {

0 commit comments

Comments
 (0)