Skip to content

Commit

Permalink
fix: fix branch parsing regex for GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
provAlyssaKrueger authored and pvdlg committed Sep 30, 2019
1 parent 244ce82 commit 3cafd43
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion services/github.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables

const parseBranch = branch => (/^(?:\/refs\/heads\/)?([^/]+)$/i.exec(branch) || [])[1];
const parseBranch = branch => (/^(?:refs\/heads\/)?([^/]+)$/i.exec(branch) || [])[1];

const getPrEvent = ({env}) => {
try {
Expand Down
20 changes: 10 additions & 10 deletions test/services/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import github from '../../services/github';
const env = {
GITHUB_ACTION: 'action-name',
GITHUB_SHA: '1234',
GITHUB_REF: '/refs/heads/master',
GITHUB_REF: 'refs/heads/master',
GITHUB_REPOSITORY: 'owner/repo',
GITHUB_WORKSPACE: '/workspace',
};
Expand Down Expand Up @@ -41,15 +41,15 @@ test('Push - with short branch name', t => {

test('PR - with event.json file', t => {
const eventFile = tempy.file({extension: 'json'});
const event = {pull_request: {number: '10', base: {ref: '/refs/heads/master'}}};
const event = {pull_request: {number: '10', base: {ref: 'refs/heads/master'}}};
fs.writeFileSync(eventFile, JSON.stringify(event));

t.deepEqual(
github.configuration({
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: '/refs/heads/pr-branch',
GITHUB_REF: 'refs/heads/pr-branch',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand Down Expand Up @@ -77,7 +77,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/heads/pr-branch',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -101,7 +101,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/heads/pr-branch',
GITHUB_EVENT_PATH: '/tmp/null',
},
}),
Expand All @@ -122,7 +122,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/heads/pr-branch'},
}),
{
name: 'GitHub Actions',
Expand All @@ -148,7 +148,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/heads/pr-branch',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand Down Expand Up @@ -176,7 +176,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/heads/pr-branch',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand All @@ -196,15 +196,15 @@ test('PR - with missing "pull_request.base" in event.json file', t => {

test('PR - with erronous branch names', t => {
const eventFile = tempy.file({extension: 'json'});
const event = {pull_request: {number: '10', base: {ref: '/refs/tags/master'}}};
const event = {pull_request: {number: '10', base: {ref: 'refs/tags/master'}}};
fs.writeFileSync(eventFile, JSON.stringify(event));

t.deepEqual(
github.configuration({
env: {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_REF: '/refs/tags/pr-branch',
GITHUB_REF: 'refs/tags/pr-branch',
GITHUB_EVENT_PATH: eventFile,
},
}),
Expand Down

0 comments on commit 3cafd43

Please sign in to comment.