Skip to content

Commit

Permalink
Merge pull request #846 from actions/merge-group-bug-fix
Browse files Browse the repository at this point in the history
Fix for merge_group event bug
  • Loading branch information
Ahmed3lmallah authored Oct 28, 2024
2 parents a6993e2 + 03e585e commit 4081bf9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 25 deletions.
39 changes: 33 additions & 6 deletions __tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ test('it raises an error when no refs are provided and the event is not a pull r
).toThrow()
})

const pullRequestLikeEvents = [
'pull_request',
'pull_request_target',
'merge_group'
]
const pullRequestLikeEvents = ['pull_request', 'pull_request_target']

test.each(pullRequestLikeEvents)(
'it uses the given refs even when the event is %s',
Expand All @@ -152,7 +148,7 @@ test.each(pullRequestLikeEvents)(
)

test.each(pullRequestLikeEvents)(
'it uses the event refs when the event is %s and the no refs are input',
'it uses the event refs when the event is %s and no refs are provided in config',
async eventName => {
const refs = getRefs(await readConfig(), {
payload: {
Expand All @@ -169,6 +165,37 @@ test.each(pullRequestLikeEvents)(
}
)

test('it uses the given refs even when the event is merge_group', async () => {
setInput('base-ref', 'a-custom-base-ref')
setInput('head-ref', 'a-custom-head-ref')

const refs = getRefs(await readConfig(), {
payload: {
merge_group: {
base_sha: 'pr-base-ref',
head_sha: 'pr-head-ref'
}
},
eventName: 'merge_group'
})
expect(refs.base).toEqual('a-custom-base-ref')
expect(refs.head).toEqual('a-custom-head-ref')
})

test('it uses the event refs when the event is merge_group and no refs are provided in config', async () => {
const refs = getRefs(await readConfig(), {
payload: {
merge_group: {
base_sha: 'pr-base-ref',
head_sha: 'pr-head-ref'
}
},
eventName: 'merge_group'
})
expect(refs.base).toEqual('pr-base-ref')
expect(refs.head).toEqual('pr-head-ref')
})

test('it defaults to runtime scope', async () => {
const config = await readConfig()
expect(config.fail_on_scopes).toEqual(['runtime'])
Expand Down
30 changes: 22 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.

32 changes: 22 additions & 10 deletions src/git-refs.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import {PullRequestSchema, ConfigurationOptions} from './schemas'
import {
PullRequestSchema,
ConfigurationOptions,
MergeGroupSchema
} from './schemas'

export function getRefs(
config: ConfigurationOptions,
context: {payload: {pull_request?: unknown}; eventName: string}
context: {
payload: {pull_request?: unknown; merge_group?: unknown}
eventName: string
}
): {base: string; head: string} {
let base_ref = config.base_ref
let head_ref = config.head_ref

// If possible, source default base & head refs from the GitHub event.
// The base/head ref from the config take priority, if provided.
if (
context.eventName === 'pull_request' ||
context.eventName === 'pull_request_target' ||
context.eventName === 'merge_group'
) {
const pull_request = PullRequestSchema.parse(context.payload.pull_request)
base_ref = base_ref || pull_request.base.sha
head_ref = head_ref || pull_request.head.sha
if (!base_ref && !head_ref) {
if (
context.eventName === 'pull_request' ||
context.eventName === 'pull_request_target'
) {
const pull_request = PullRequestSchema.parse(context.payload.pull_request)
base_ref = base_ref || pull_request.base.sha
head_ref = head_ref || pull_request.head.sha
} else if (context.eventName === 'merge_group') {
const merge_group = MergeGroupSchema.parse(context.payload.merge_group)
base_ref = base_ref || merge_group.base_sha
head_ref = head_ref || merge_group.head_sha
}
}

if (!base_ref && !head_ref) {
Expand Down
5 changes: 5 additions & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export const PullRequestSchema = z.object({
head: z.object({sha: z.string()})
})

export const MergeGroupSchema = z.object({
base_sha: z.string(),
head_sha: z.string()
})

export const ConfigurationOptionsSchema = z
.object({
fail_on_severity: SeveritySchema,
Expand Down

0 comments on commit 4081bf9

Please sign in to comment.