Skip to content

Commit

Permalink
feat: support for pull_request event (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinastur committed Mar 13, 2023
1 parent b0d835d commit 3c31ff9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 26 deletions.
41 changes: 29 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9517,7 +9517,7 @@ const cp = __nccwpck_require__(2081);
const core = __nccwpck_require__(2186);
const fs = __nccwpck_require__(7147);
const { context } = __nccwpck_require__(5438);
const { isGitHubTag, isBranch } = __nccwpck_require__(8396);
const { isGitHubTag, isBranch, isPullRequest, branchRefToSlug, prRefToSlug, tagRefToSlug } = __nccwpck_require__(8396);
const { timestamp, cpOptions } = __nccwpck_require__(1608);

const GITHUB_REGISTRY_URLS = ['docker.pkg.github.com', 'ghcr.io'];
Expand All @@ -9537,25 +9537,26 @@ const createTags = (addLatest, addTimestamp) => {
const ref = context.ref.toLowerCase();
const shortSha = sha.substring(0, 7);
const dockerTags = [];

if (isGitHubTag(ref)) {
// If GitHub tag exists, use it as the Docker tag
const tag = ref.replace('refs/tags/', '');
dockerTags.push(tag);
dockerTags.push(tagRefToSlug(ref));
} else if (isBranch(ref)) {
// If we're not building a tag, use branch-prefix-{GIT_SHORT_SHA) as the Docker tag
// refs/heads/jira-123/feature/something
const branchName = ref.replace('refs/heads/', '');
const safeBranchName = branchName
.replace(/[^\w.-]+/g, '-')
.replace(/^[^\w]+/, '')
.substring(0, 120);
const safeBranchName = branchRefToSlug(ref);
const baseTag = `${safeBranchName}-${shortSha}`;
const tag = addTimestamp ? `${baseTag}-${timestamp()}` : baseTag;
dockerTags.push(tag);
} else if (isPullRequest(ref)) {
// pull_request event, use pr-{PR_NUMBER)-{GIT_SHORT_SHA) as the Docker tag
const safePrRefName = prRefToSlug(ref);
const baseTag = `pr-${safePrRefName}-${shortSha}`;
const tag = addTimestamp ? `${baseTag}-${timestamp()}` : baseTag;
dockerTags.push(tag);
} else {
core.setFailed(
'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push-event-push'
'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push or' +
' pull https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request'
);
}

Expand Down Expand Up @@ -9683,6 +9684,8 @@ const isGitHubTag = ref => ref && ref.includes('refs/tags/');

const isBranch = ref => ref && ref.includes('refs/heads/');

const isPullRequest = ref => ref && ref.includes('refs/pull/');

// Returns owning organization of the repo where the Action is run
const getDefaultOwner = () => {
let owner;
Expand All @@ -9696,10 +9699,24 @@ const getDefaultOwner = () => {
return owner;
};

const refToSlug = githubRef =>
githubRef
.replace(/[^\w.-]+/g, '-')
.replace(/^[^\w]+/, '')
.substring(0, 120);

const tagRefToSlug = githubRef => refToSlug(githubRef.replace('refs/tags/', ''));
const branchRefToSlug = githubRef => refToSlug(githubRef.replace('refs/heads/', ''));
const prRefToSlug = githubRef => refToSlug(githubRef.replace('refs/pull/', '').split('/').shift());

module.exports = {
isGitHubTag,
branchRefToSlug,
getDefaultOwner,
isBranch,
getDefaultOwner
isGitHubTag,
isPullRequest,
prRefToSlug,
tagRefToSlug
};


Expand Down
20 changes: 11 additions & 9 deletions src/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const cp = require('child_process');
const core = require('@actions/core');
const fs = require('fs');
const { context } = require('@actions/github');
const { isGitHubTag, isBranch } = require('./github');
const { isGitHubTag, isBranch, isPullRequest, branchRefToSlug, prRefToSlug, tagRefToSlug } = require('./github');
const { timestamp, cpOptions } = require('./utils');

const GITHUB_REGISTRY_URLS = ['docker.pkg.github.com', 'ghcr.io'];
Expand All @@ -25,22 +25,24 @@ const createTags = (addLatest, addTimestamp) => {

if (isGitHubTag(ref)) {
// If GitHub tag exists, use it as the Docker tag
const tag = ref.replace('refs/tags/', '');
dockerTags.push(tag);
dockerTags.push(tagRefToSlug(ref));
} else if (isBranch(ref)) {
// If we're not building a tag, use branch-prefix-{GIT_SHORT_SHA) as the Docker tag
// refs/heads/jira-123/feature/something
const branchName = ref.replace('refs/heads/', '');
const safeBranchName = branchName
.replace(/[^\w.-]+/g, '-')
.replace(/^[^\w]+/, '')
.substring(0, 120);
const safeBranchName = branchRefToSlug(ref);
const baseTag = `${safeBranchName}-${shortSha}`;
const tag = addTimestamp ? `${baseTag}-${timestamp()}` : baseTag;
dockerTags.push(tag);
} else if (isPullRequest(ref)) {
// pull_request event, use pr-{PR_NUMBER)-{GIT_SHORT_SHA) as the Docker tag
const safePrRefName = prRefToSlug(ref);
const baseTag = `pr-${safePrRefName}-${shortSha}`;
const tag = addTimestamp ? `${baseTag}-${timestamp()}` : baseTag;
dockerTags.push(tag);
} else {
core.setFailed(
'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push-event-push'
'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push or' +
' pull https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request'
);
}

Expand Down
20 changes: 18 additions & 2 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const isGitHubTag = ref => ref && ref.includes('refs/tags/');

const isBranch = ref => ref && ref.includes('refs/heads/');

const isPullRequest = ref => ref && ref.includes('refs/pull/');

// Returns owning organization of the repo where the Action is run
const getDefaultOwner = () => {
let owner;
Expand All @@ -18,8 +20,22 @@ const getDefaultOwner = () => {
return owner;
};

const refToSlug = githubRef =>
githubRef
.replace(/[^\w.-]+/g, '-')
.replace(/^[^\w]+/, '')
.substring(0, 120);

const tagRefToSlug = githubRef => refToSlug(githubRef.replace('refs/tags/', ''));
const branchRefToSlug = githubRef => refToSlug(githubRef.replace('refs/heads/', ''));
const prRefToSlug = githubRef => refToSlug(githubRef.replace('refs/pull/', '').split('/').shift());

module.exports = {
isGitHubTag,
branchRefToSlug,
getDefaultOwner,
isBranch,
getDefaultOwner
isGitHubTag,
isPullRequest,
prRefToSlug,
tagRefToSlug
};
32 changes: 29 additions & 3 deletions tests/docker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ describe('Create Docker image tags', () => {
let addTimestamp;

beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2022-01-01T00:00:00+0000').getTime());

addLatest = false;
addTimestamp = false;
});

afterEach(() => jest.useRealTimers());

test('Create from tag push', () => {
context.ref = 'refs/tags/v1.0';
context.sha = '8d93430eddafb926c668181c71f579556f68668c';
Expand Down Expand Up @@ -130,16 +134,38 @@ describe('Create Docker image tags', () => {
expect(tags.length).toEqual(1);
});

test('Create from pull request push (not supported)', () => {
context.ref = 'refs/pull/1';
test('Create from pull request', () => {
context.ref = 'refs/pull/1/merge';
context.sha = '89977b79ba5102dab6f3687e6c3b9c1cda878d0a';
core.setFailed = jest.fn();

const tags = docker.createTags(addLatest, false);

expect(tags.length).toEqual(1);
expect(tags[0]).toBe('pr-1-89977b7');
});

test('Create from pull request with timestamp', () => {
context.ref = 'refs/pull/1/merge';
context.sha = '89977b79ba5102dab6f3687e6c3b9c1cda878d0a';
core.setFailed = jest.fn();

const tags = docker.createTags(addLatest, true);

expect(tags.length).toEqual(1);
expect(tags[0]).toBe('pr-1-89977b7-2022-01-01.000100');
});

test('Create from unknown event (not supported)', () => {
context.ref = 'refs/unknown/';
context.sha = '89977b79ba5102dab6f3687e6c3b9c1cda878d0a';
core.setFailed = jest.fn();

const tags = docker.createTags(addLatest, addTimestamp);

expect(tags.length).toEqual(0);
expect(core.setFailed).toHaveBeenCalledWith(
'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push-event-push'
'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push or pull https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request'
);
});
});
Expand Down
31 changes: 31 additions & 0 deletions tests/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ jest.mock('@actions/core');

const core = require('@actions/core');
const github = require('../src/github');
const { branchRefToSlug, prRefToSlug, tagRefToSlug } = require('../src/github');

describe('Get default repo & owner name', () => {
test('Returns the environment variable', () => {
Expand All @@ -20,4 +21,34 @@ describe('Get default repo & owner name', () => {
github.getDefaultOwner();
expect(core.setFailed).toHaveBeenCalledWith(error);
});

describe('branchRefToSlug', () => {
test.each([
['refs/heads/jira-123/feature/something', 'jira-123-feature-something'],
['refs/heads/SOME-mixed-CASE-Branch', 'SOME-mixed-CASE-Branch'],
['refs/heads/feat/mybranch', 'feat-mybranch'],
['refs/heads/chore_mybranch--with--hyphens-', 'chore_mybranch--with--hyphens-']
])('branchRefToSlug for "%s" should return: %s', (d, expected) => {
expect(branchRefToSlug(d)).toBe(expected);
});
});

describe('prRefToSlug', () => {
test.each([
['refs/pull/1', '1'],
['refs/pull/1/merge', '1'],
['refs/pull/123/head', '123']
])('prRefToSlug for "%s" should return: %s', (d, expected) => {
expect(prRefToSlug(d)).toBe(expected);
});
});

describe('tagRefToSlug', () => {
test.each([
['refs/tags/v1.0', 'v1.0'],
['refs/tags/some-other-tag', 'some-other-tag']
])('tagRefToSlug for "%s" should return: %s', (d, expected) => {
expect(tagRefToSlug(d)).toBe(expected);
});
});
});

0 comments on commit 3c31ff9

Please sign in to comment.