Skip to content

Commit 4081bf9

Browse files
Merge pull request #846 from actions/merge-group-bug-fix
Fix for merge_group event bug
2 parents a6993e2 + 03e585e commit 4081bf9

File tree

5 files changed

+83
-25
lines changed

5 files changed

+83
-25
lines changed

__tests__/config.test.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ test('it raises an error when no refs are provided and the event is not a pull r
124124
).toThrow()
125125
})
126126

127-
const pullRequestLikeEvents = [
128-
'pull_request',
129-
'pull_request_target',
130-
'merge_group'
131-
]
127+
const pullRequestLikeEvents = ['pull_request', 'pull_request_target']
132128

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

154150
test.each(pullRequestLikeEvents)(
155-
'it uses the event refs when the event is %s and the no refs are input',
151+
'it uses the event refs when the event is %s and no refs are provided in config',
156152
async eventName => {
157153
const refs = getRefs(await readConfig(), {
158154
payload: {
@@ -169,6 +165,37 @@ test.each(pullRequestLikeEvents)(
169165
}
170166
)
171167

168+
test('it uses the given refs even when the event is merge_group', async () => {
169+
setInput('base-ref', 'a-custom-base-ref')
170+
setInput('head-ref', 'a-custom-head-ref')
171+
172+
const refs = getRefs(await readConfig(), {
173+
payload: {
174+
merge_group: {
175+
base_sha: 'pr-base-ref',
176+
head_sha: 'pr-head-ref'
177+
}
178+
},
179+
eventName: 'merge_group'
180+
})
181+
expect(refs.base).toEqual('a-custom-base-ref')
182+
expect(refs.head).toEqual('a-custom-head-ref')
183+
})
184+
185+
test('it uses the event refs when the event is merge_group and no refs are provided in config', async () => {
186+
const refs = getRefs(await readConfig(), {
187+
payload: {
188+
merge_group: {
189+
base_sha: 'pr-base-ref',
190+
head_sha: 'pr-head-ref'
191+
}
192+
},
193+
eventName: 'merge_group'
194+
})
195+
expect(refs.base).toEqual('pr-base-ref')
196+
expect(refs.head).toEqual('pr-head-ref')
197+
})
198+
172199
test('it defaults to runtime scope', async () => {
173200
const config = await readConfig()
174201
expect(config.fail_on_scopes).toEqual(['runtime'])

dist/index.js

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git-refs.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
import {PullRequestSchema, ConfigurationOptions} from './schemas'
1+
import {
2+
PullRequestSchema,
3+
ConfigurationOptions,
4+
MergeGroupSchema
5+
} from './schemas'
26

37
export function getRefs(
48
config: ConfigurationOptions,
5-
context: {payload: {pull_request?: unknown}; eventName: string}
9+
context: {
10+
payload: {pull_request?: unknown; merge_group?: unknown}
11+
eventName: string
12+
}
613
): {base: string; head: string} {
714
let base_ref = config.base_ref
815
let head_ref = config.head_ref
916

1017
// If possible, source default base & head refs from the GitHub event.
1118
// The base/head ref from the config take priority, if provided.
12-
if (
13-
context.eventName === 'pull_request' ||
14-
context.eventName === 'pull_request_target' ||
15-
context.eventName === 'merge_group'
16-
) {
17-
const pull_request = PullRequestSchema.parse(context.payload.pull_request)
18-
base_ref = base_ref || pull_request.base.sha
19-
head_ref = head_ref || pull_request.head.sha
19+
if (!base_ref && !head_ref) {
20+
if (
21+
context.eventName === 'pull_request' ||
22+
context.eventName === 'pull_request_target'
23+
) {
24+
const pull_request = PullRequestSchema.parse(context.payload.pull_request)
25+
base_ref = base_ref || pull_request.base.sha
26+
head_ref = head_ref || pull_request.head.sha
27+
} else if (context.eventName === 'merge_group') {
28+
const merge_group = MergeGroupSchema.parse(context.payload.merge_group)
29+
base_ref = base_ref || merge_group.base_sha
30+
head_ref = head_ref || merge_group.head_sha
31+
}
2032
}
2133

2234
if (!base_ref && !head_ref) {

src/schemas.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export const PullRequestSchema = z.object({
9191
head: z.object({sha: z.string()})
9292
})
9393

94+
export const MergeGroupSchema = z.object({
95+
base_sha: z.string(),
96+
head_sha: z.string()
97+
})
98+
9499
export const ConfigurationOptionsSchema = z
95100
.object({
96101
fail_on_severity: SeveritySchema,

0 commit comments

Comments
 (0)