diff --git a/services/github.js b/services/github.js index e951430..cba02c7 100644 --- a/services/github.js +++ b/services/github.js @@ -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', diff --git a/test/services/github.test.js b/test/services/github.test.js index 5cbe3cd..e6bbf12 100644 --- a/test/services/github.test.js +++ b/test/services/github.test.js @@ -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, }, }), @@ -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', @@ -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, }, }), @@ -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', @@ -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, }, }), @@ -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', @@ -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', }, }), @@ -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', @@ -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', @@ -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', @@ -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, }, }), @@ -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', @@ -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, }, }), @@ -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',