Skip to content

Commit

Permalink
fix: use GITHUB_REF as documented on docs.github.com/actions (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
KengoTODA committed Oct 29, 2021
1 parent 9a8dcd4 commit 0baab54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
9 changes: 8 additions & 1 deletion services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ const getPrEvent = ({env}) => {
return {pr: undefined, branch: undefined};
};

const getPrNumber = (env) => {
const event = env.GITHUB_EVENT_PATH ? require(env.GITHUB_EVENT_PATH) : undefined;
return event && event.pull_request ? event.pull_request.number : undefined;
};

module.exports = {
detect({env}) {
return Boolean(env.GITHUB_ACTIONS);
},
configuration({env, cwd}) {
const isPr = env.GITHUB_EVENT_NAME === 'pull_request' || env.GITHUB_EVENT_NAME === 'pull_request_target';
const branch = parseBranch(env.GITHUB_REF);
const branch = parseBranch(
env.GITHUB_EVENT_NAME === 'pull_request_target' ? `refs/pull/${getPrNumber(env)}/merge` : env.GITHUB_REF
);

return {
name: 'GitHub Actions',
Expand Down
28 changes: 14 additions & 14 deletions test/services/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test('PR - with event.json file', (t) => {
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_REF: 'refs/pull/10/merge',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -63,7 +63,7 @@ test('PR - with event.json file', (t) => {
build: '1246789',
branch: 'master',
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: '10',
root: '/workspace',
slug: 'owner/repo',
Expand All @@ -81,7 +81,7 @@ test('PR - target', (t) => {
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request_target',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_REF: 'refs/heads/master',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -92,7 +92,7 @@ test('PR - target', (t) => {
build: '1246789',
branch: 'master',
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: '10',
root: '/workspace',
slug: 'owner/repo',
Expand All @@ -110,7 +110,7 @@ test('PR - with event.json file and short branch name', (t) => {
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_REF: 'refs/pull/10/merge',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -121,7 +121,7 @@ test('PR - with event.json file and short branch name', (t) => {
build: '1246789',
branch: 'master',
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: '10',
root: '/workspace',
slug: 'owner/repo',
Expand All @@ -135,7 +135,7 @@ test('PR - with missing event.json file', (t) => {
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_REF: 'refs/pull/10/merge',
GITHUB_EVENT_PATH: '/tmp/null',
},
}),
Expand All @@ -146,7 +146,7 @@ test('PR - with missing event.json file', (t) => {
build: '1246789',
branch: undefined,
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: undefined,
root: '/workspace',
slug: 'owner/repo',
Expand All @@ -157,7 +157,7 @@ test('PR - with missing event.json file', (t) => {
test('PR - with missing event.json file path', (t) => {
t.deepEqual(
github.configuration({
env: {...env, GITHUB_EVENT_NAME: 'pull_request', GITHUB_REF: 'refs/heads/pr-branch'},
env: {...env, GITHUB_EVENT_NAME: 'pull_request', GITHUB_REF: 'refs/pull/10/merge'},
}),
{
name: 'GitHub Actions',
Expand All @@ -166,7 +166,7 @@ test('PR - with missing event.json file path', (t) => {
build: '1246789',
branch: undefined,
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: undefined,
root: '/workspace',
slug: 'owner/repo',
Expand All @@ -184,7 +184,7 @@ test('PR - with missing "pull_request" in event.json file', (t) => {
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_REF: 'refs/pull/10/merge',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -195,7 +195,7 @@ test('PR - with missing "pull_request" in event.json file', (t) => {
build: '1246789',
branch: undefined,
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: undefined,
root: '/workspace',
slug: 'owner/repo',
Expand All @@ -213,7 +213,7 @@ test('PR - with missing "pull_request.base" in event.json file', (t) => {
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_REF: 'refs/pull/10/merge',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -224,7 +224,7 @@ test('PR - with missing "pull_request.base" in event.json file', (t) => {
build: '1246789',
branch: undefined,
isPr: true,
prBranch: 'pr-branch',
prBranch: 'refs/pull/10/merge',
pr: '10',
root: '/workspace',
slug: 'owner/repo',
Expand Down

0 comments on commit 0baab54

Please sign in to comment.