generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from lampajr/issue-18_cp_branch_name
[ISSUE-18] Customize cherry pick branch name
- Loading branch information
Showing
7 changed files
with
169 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import * as core from '@actions/core' | ||
import * as exec from '@actions/exec' | ||
import {run} from '../src/main' | ||
import {createPullRequest} from '../src/github-helper' | ||
|
||
const defaultMockedGetInputData: any = { | ||
token: 'whatever', | ||
author: 'Me <[email protected]>', | ||
committer: 'Someone <[email protected]>', | ||
branch: 'target-branch', | ||
'cherry-pick-branch': '' | ||
} | ||
|
||
let mockedGetInputData: any = defaultMockedGetInputData | ||
|
||
// default mock | ||
jest.mock('@actions/core', () => { | ||
return { | ||
info: jest.fn(), | ||
setFailed: jest.fn().mockImplementation(msg => { | ||
throw new Error(msg) | ||
}), | ||
// redirect to stdout | ||
startGroup: jest.fn().mockImplementation(console.log), | ||
endGroup: jest.fn(), | ||
getInput: jest.fn().mockImplementation((name: string) => { | ||
return name in mockedGetInputData ? mockedGetInputData[name] : '' | ||
}) | ||
} | ||
}) | ||
|
||
jest.mock('@actions/exec', () => { | ||
return { | ||
// 0 -> success | ||
exec: jest.fn().mockResolvedValue(0) | ||
} | ||
}) | ||
|
||
jest.mock('../src/github-helper') | ||
|
||
describe('run main', () => { | ||
beforeEach(() => { | ||
process.env.GITHUB_SHA = 'xxxxxxxxxx' | ||
mockedGetInputData = defaultMockedGetInputData | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
const commonChecks = (targetBranch: string, cherryPickBranch: string) => { | ||
expect(core.startGroup).toBeCalledTimes(6) | ||
expect(core.startGroup).toHaveBeenCalledWith( | ||
'Configuring the committer and author' | ||
) | ||
expect(core.startGroup).toHaveBeenCalledWith('Fetch all branchs') | ||
expect(core.startGroup).toHaveBeenCalledWith( | ||
`Create new branch ${cherryPickBranch} from ${targetBranch}` | ||
) | ||
expect(core.startGroup).toHaveBeenCalledWith('Cherry picking') | ||
expect(core.startGroup).toHaveBeenCalledWith('Push new branch to remote') | ||
expect(core.startGroup).toHaveBeenCalledWith('Opening pull request') | ||
|
||
expect(core.endGroup).toBeCalledTimes(6) | ||
|
||
// TODO check params | ||
expect(exec.exec).toBeCalledTimes(7) | ||
|
||
// TODO check params | ||
expect(createPullRequest).toBeCalledTimes(1) | ||
} | ||
|
||
test('valid execution with default new branch', async () => { | ||
await run() | ||
|
||
commonChecks('target-branch', 'cherry-pick-target-branch-xxxxxxxxxx') | ||
|
||
expect(createPullRequest).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
author: 'Me <[email protected]>', | ||
committer: 'Someone <[email protected]>', | ||
branch: 'target-branch', | ||
title: '', | ||
body: '', | ||
labels: [], | ||
reviewers: [], | ||
cherryPickBranch: '' | ||
}), | ||
'cherry-pick-target-branch-xxxxxxxxxx' | ||
) | ||
}) | ||
|
||
test('valid execution with customized branch', async () => { | ||
mockedGetInputData['cherry-pick-branch'] = 'my-custom-branch' | ||
|
||
await run() | ||
|
||
commonChecks('target-branch', 'my-custom-branch') | ||
|
||
expect(createPullRequest).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
author: 'Me <[email protected]>', | ||
committer: 'Someone <[email protected]>', | ||
branch: 'target-branch', | ||
title: '', | ||
body: '', | ||
labels: [], | ||
reviewers: [], | ||
cherryPickBranch: 'my-custom-branch' | ||
}), | ||
'my-custom-branch' | ||
) | ||
}) | ||
|
||
test('valid execution with pr overrides', async () => { | ||
mockedGetInputData['cherry-pick-branch'] = 'my-custom-branch' | ||
mockedGetInputData['title'] = 'new title' | ||
mockedGetInputData['body'] = 'new body' | ||
mockedGetInputData['labels'] = 'label1,label2' | ||
mockedGetInputData['reviewers'] = 'user1,user2,user3' | ||
|
||
await run() | ||
|
||
commonChecks('target-branch', 'my-custom-branch') | ||
|
||
expect(createPullRequest).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
author: 'Me <[email protected]>', | ||
committer: 'Someone <[email protected]>', | ||
branch: 'target-branch', | ||
title: 'new title', | ||
body: 'new body', | ||
labels: ['label1', 'label2'], | ||
reviewers: ['user1', 'user2', 'user3'], | ||
cherryPickBranch: 'my-custom-branch' | ||
}), | ||
'my-custom-branch' | ||
) | ||
}) | ||
}) |