Skip to content

Commit

Permalink
[ISSUE-18] Customize cherry pick branch name
Browse files Browse the repository at this point in the history
lampajr committed Dec 1, 2022
1 parent 6523ab0 commit 682f378
Showing 7 changed files with 169 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -98,6 +98,7 @@ Mor informatoin can be found in the [GitHub Blog](https://github.blog/2020-08-03
| `team-reviewers` | A comma or newline-separated list of GitHub teams to request a review from. Note that a `repo` scoped [PAT](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) may be required. | |
| `title` | Title of the new pull request, the special string `{old_title}` will be substituted for the title of the pull request which triggered the action | [Triggering pull request title] |
| `body` | Body of the new pull request, the special string `{old_pull_request_id}` will be substituted for the ID of the pull request which triggered the action | [Triggering pull request body] |
| `cherry-pick-branch` | Name of the new cherry pick branch | `cherry-pick-${inputs.branch}-${commitSha}` |

## License

3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -48,6 +48,9 @@ inputs:
A comma or newline separated list of GitHub teams to request a review from.
Note that a `repo` scoped Personal Access Token (PAT) may be required.
required: false
cherry-pick-branch:
description: "Name of the new cherry pick branch."
required: false
runs:
using: node12
main: dist/index.js
21 changes: 13 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/github-helper.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ export interface Inputs {
assignees: string[]
reviewers: string[]
teamReviewers: string[]
cherryPickBranch?: string
}

export async function createPullRequest(
14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -20,13 +20,16 @@ export async function run(): Promise<void> {
inherit_labels: utils.getInputAsBoolean('inherit_labels'),
assignees: utils.getInputAsArray('assignees'),
reviewers: utils.getInputAsArray('reviewers'),
teamReviewers: utils.getInputAsArray('teamReviewers')
teamReviewers: utils.getInputAsArray('teamReviewers'),
cherryPickBranch: core.getInput('cherry-pick-branch')
}

core.info(`Cherry pick into branch ${inputs.branch}!`)

const githubSha = process.env.GITHUB_SHA
const prBranch = `cherry-pick-${inputs.branch}-${githubSha}`
const prBranch = inputs.cherryPickBranch
? inputs.cherryPickBranch
: `cherry-pick-${inputs.branch}-${githubSha}`

// Configure the committer and author
core.startGroup('Configuring the committer and author')
@@ -51,7 +54,7 @@ export async function run(): Promise<void> {
core.endGroup()

// Create branch new branch
core.startGroup(`Create new branch from ${inputs.branch}`)
core.startGroup(`Create new branch ${prBranch} from ${inputs.branch}`)
await gitExecution(['checkout', '-b', prBranch, `origin/${inputs.branch}`])
core.endGroup()

@@ -120,4 +123,7 @@ class GitOutput {
exitCode = 0
}

run()
// do not run if imported as module
if (require.main === module) {
run()
}
140 changes: 140 additions & 0 deletions test/main.test.ts
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'
)
})
})

0 comments on commit 682f378

Please sign in to comment.