Skip to content

Commit

Permalink
Merge pull request #4 from Vendic/feature/support-pull-number-input
Browse files Browse the repository at this point in the history
Feature support optional pull number input
  • Loading branch information
Tjitse-E authored Feb 1, 2023
2 parents 0024377 + 8609688 commit d7b82a3
Show file tree
Hide file tree
Showing 9 changed files with 7,642 additions and 6,635 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.idea
*~
*~
coverage/
67 changes: 58 additions & 9 deletions __tests__/extract_task_ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,54 @@ import {WebhookPayload} from "@actions/github/lib/interfaces";
import nock from "nock";
import {expect, test} from '@jest/globals'

test('Extract 4 task ids with manual pull_number input', async () => {
process.env['INPUT_PULL_NUMBER'] = '2';

const infoMock = jest.spyOn(core, 'info')
const setOutputMock = jest.spyOn(core, 'setOutput')

const pullsApiCallOutputPath = path.join(__dirname, 'pull_request_api_response.json')
const pullsApiCallOutput: JSON = JSON.parse(fs.readFileSync(pullsApiCallOutputPath, 'utf-8'))
nock('https://api.github.com')
.persist()
.get('/repos/foo/bar/pulls/2')
.reply(200, pullsApiCallOutput)

const apiOutputPath = path.join(__dirname, 'api_output.json')
const apiOutput: JSON = JSON.parse(fs.readFileSync(apiOutputPath, 'utf-8'))
nock('https://api.github.com')
.persist()
.get('/repos/foo/bar/pulls/2/commits')
.reply(200, apiOutput)

await run()

// Assertions
expect(infoMock).toHaveBeenCalledWith('Found task id ABCDEFGH-14')
expect(infoMock).toHaveBeenCalledWith('Found task id ABC-100')
expect(infoMock).toHaveBeenCalledWith('Found task id DEV-1234')
expect(infoMock).toHaveBeenCalledWith('Found task id XYZ-123')
expect(setOutputMock).toHaveBeenCalledWith('task_ids', 'ABCDEFGH-14\nABC-100\nDEV-1234\nXYZ-123\nREF-1234\nREF-12345')
})

test('Extract 4 task ids after PR opened', async () => {
process.env['INPUT_PULL_NUMBER'] = undefined;

// Mocks
const infoMock = jest.spyOn(core, 'info')
const setOutputMock = jest.spyOn(core, 'setOutput')
const apiOutputPath = path.join(__dirname, 'api_output.json')
const payloadPath = path.join(__dirname, 'pull_request_context.json');
const payload = JSON.parse(fs.readFileSync(payloadPath, 'utf8'))
github.context.payload = payload as WebhookPayload

const pullsApiCallOutputPath = path.join(__dirname, 'pull_request_api_response.json')
const pullsApiCallOutput: JSON = JSON.parse(fs.readFileSync(pullsApiCallOutputPath, 'utf-8'))
nock('https://api.github.com')
.persist()
.get('/repos/foo/bar/pulls/2')
.reply(200, pullsApiCallOutput)

const apiOutputPath = path.join(__dirname, 'api_output.json')
const apiOutput: JSON = JSON.parse(fs.readFileSync(apiOutputPath, 'utf-8'))
nock('https://api.github.com')
.persist()
Expand All @@ -24,46 +64,55 @@ test('Extract 4 task ids after PR opened', async () => {
await run()

// Assertions
expect(infoMock).toHaveBeenCalledWith('Found task id ABC-100')
expect(infoMock).toHaveBeenCalledWith('Found task id ABCDEFGH-14')
expect(infoMock).toHaveBeenCalledWith('Found task id ABC-100')
expect(infoMock).toHaveBeenCalledWith('Found task id DEV-1234')
expect(infoMock).toHaveBeenCalledWith('Found task id ABC-123')
expect(setOutputMock).toHaveBeenCalledWith('task_ids', 'ABCDEFGH-14\nABC-100\nDEV-1234\nABC-123')
expect(infoMock).toHaveBeenCalledWith('Found task id XYZ-123')
expect(setOutputMock).toHaveBeenCalledWith('task_ids', 'ABCDEFGH-14\nABC-100\nDEV-1234\nXYZ-123\nREF-1234\nREF-12345')
})

test('Extract 4 task ids after PR review submitted', async () => {
// Mocks
const infoMock = jest.spyOn(core, 'info')
const setOutputMock = jest.spyOn(core, 'setOutput')
const apiOutputPath = path.join(__dirname, 'api_output.json')
const payloadPath = path.join(__dirname, 'pull_request_review_context.json');
const payload = JSON.parse(fs.readFileSync(payloadPath, 'utf8'))
github.context.payload = payload as WebhookPayload

const apiOutputPath = path.join(__dirname, 'api_output.json')
const apiOutput: JSON = JSON.parse(fs.readFileSync(apiOutputPath, 'utf-8'))
nock('https://api.github.com')
.persist()
.get('/repos/foo/bar/pulls/2/commits')
.reply(200, apiOutput)

const pullsApiCallOutputPath = path.join(__dirname, 'pull_request_api_response.json')
const pullsApiCallOutput: JSON = JSON.parse(fs.readFileSync(pullsApiCallOutputPath, 'utf-8'))
nock('https://api.github.com')
.persist()
.get('/repos/foo/bar/pulls/2')
.reply(200, pullsApiCallOutput)

await run()

// Assertions
expect(infoMock).toHaveBeenCalledWith('Found task id DEV-1234')
expect(infoMock).toHaveBeenCalledWith('Found task id ABCDEFGH-14')
expect(infoMock).toHaveBeenCalledWith('Found task id ABC-100')
expect(infoMock).toHaveBeenCalledWith('Found task id DEV-1234')
expect(infoMock).toHaveBeenCalledWith('Found task id ABC-123')
expect(setOutputMock).toHaveBeenCalledWith('task_ids', 'ABCDEFGH-14\nABC-100\nDEV-1234\nABC-123')
expect(infoMock).toHaveBeenCalledWith('Found task id XYZ-123')
expect(setOutputMock).toHaveBeenCalledWith('task_ids', 'ABCDEFGH-14\nABC-100\nDEV-1234\nXYZ-123\nREF-1234\nREF-12345')
})

beforeEach(() => {
jest.resetModules()
process.env['INPUT_TOKEN'] = 'xyz'
process.env['GITHUB_REPOSITORY'] = 'foo/bar';
process.env['INPUT_TASK_ID_PATTERN'] = '[A-Z]{2,}-[0-9]{1,5}'
process.env['INPUT_TASK_ID_PATTERN'] = '[A-Z]{2,}-[0-9]{1,5}';
})

afterEach(() => {
delete process.env['GITHUB_REPOSITORY']
delete process.env['INPUT_TOKEN']
delete process.env['INPUT_TASK_ID_PATTERN']
delete process.env['INPUT_PULL_NUMBER']
})
22 changes: 22 additions & 0 deletions __tests__/pull_request_api_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"title": "REF-12345 Test github extract task id",
"body": "XYZ-123 ## Description\r\n<!-- Please include a summary of the changes and the related issue. Please also include relevant context. -->\r\n\r\n## How Has This Been Tested?\r\n<!-- Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration -->\r\n\r\n## Checklist:\r\n- [ ] My code is up to date with the development branch, i've pulled in the latest changes\r\n- [ ] I've added the build label to my PR\r\n\r\n## Changelog\r\n<!-- Available topics: Added, Changed, Deprecated, Removed, Fixed, Security -->\r\n### Changed\r\n<!-- Add bullet list of changes here -->\r\n\r\n### Fixed\r\n<!-- Add bullet list of fixes here -->\r\n",
"head": {
"ref": "feature/REF-1234-test-feature-preview"
},
"author_association": "CONTRIBUTOR",
"auto_merge": null,
"active_lock_reason": null,
"merged": false,
"mergeable": true,
"rebaseable": true,
"mergeable_state": "unstable",
"merged_by": null,
"comments": 0,
"review_comments": 0,
"maintainer_can_modify": false,
"commits": 9,
"additions": 348,
"deletions": 598,
"changed_files": 15
}
9 changes: 1 addition & 8 deletions __tests__/pull_request_context.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
{
"action": "opened",
"number": 2,
"pull_request": {
"body": "Hello world",
"title": "ABC-100 Your pull request (automerge)",
"head": {
"ref": "feature/ABC-123-hello-world"
}
}
"number": 2
}
6 changes: 4 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'github-extract-task-ids-action'
description: 'Extract task ids from commit messages, branch and pull request title using regular expressions'
author: 'Tjitse-E'
runs:
using: 'node12'
using: 'node16'
main: 'dist/index.js'
inputs:
token:
Expand All @@ -12,4 +12,6 @@ inputs:
description: The regex patteren of your task id
required: true
default: '[A-Z]{2,}-[0-9]{1,5}'

pull_number:
description: The pull request number
required: false
Loading

0 comments on commit d7b82a3

Please sign in to comment.