Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-44] Get commit SHA from pull_request.merge_commit_sha #72

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as core from '@actions/core'
import * as io from '@actions/io'
import * as exec from '@actions/exec'
import * as utils from './utils'
import * as github from '@actions/github'
import {Inputs, createPullRequest} from './github-helper'
import { PullRequest } from '@octokit/webhooks-definitions/schema'

const CHERRYPICK_EMPTY =
'The previous cherry-pick is now empty, possibly due to conflict resolution.'
Expand All @@ -27,7 +29,9 @@ export async function run(): Promise<void> {

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

const githubSha = process.env.GITHUB_SHA
// the value of merge_commit_sha changes depending on the status of the pull request
// see https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request
const githubSha = (github.context.payload.pull_request as PullRequest).merge_commit_sha
const prBranch = inputs.cherryPickBranch
? inputs.cherryPickBranch
: `cherry-pick-${inputs.branch}-${githubSha}`
Expand Down
20 changes: 16 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import {run} from '../src/index'
import {createPullRequest, Inputs} from '../src/github-helper'
import {createPullRequest} from '../src/github-helper'
import { PullRequest } from '@octokit/webhooks-definitions/schema'

const defaultMockedGetInputData: any = {
token: 'whatever',
Expand Down Expand Up @@ -43,6 +44,18 @@ jest.mock('@actions/exec', () => {
}
})

jest.mock('@actions/github', () => {
return {
context: {
payload: {
pull_request: {
merge_commit_sha: 'XXXXXX'
} as PullRequest
}
}
}
})

jest.mock('../src/github-helper', () => {
return {
createPullRequest: jest.fn().mockImplementation(() => {
Expand All @@ -53,7 +66,6 @@ jest.mock('../src/github-helper', () => {

describe('run main', () => {
beforeEach(() => {
process.env.GITHUB_SHA = 'xxxxxxxxxx'
mockedGetInputData = defaultMockedGetInputData
})

Expand Down Expand Up @@ -86,7 +98,7 @@ describe('run main', () => {
test('valid execution with default new branch', async () => {
await run()

commonChecks('target-branch', 'cherry-pick-target-branch-xxxxxxxxxx')
commonChecks('target-branch', 'cherry-pick-target-branch-XXXXXX')

expect(createPullRequest).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -99,7 +111,7 @@ describe('run main', () => {
reviewers: [],
cherryPickBranch: ''
}),
'cherry-pick-target-branch-xxxxxxxxxx'
'cherry-pick-target-branch-XXXXXX'
)
})

Expand Down