diff --git a/src/git-refs.ts b/src/git-refs.ts index 47fdbeb3d..670fee483 100644 --- a/src/git-refs.ts +++ b/src/git-refs.ts @@ -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) { diff --git a/src/schemas.ts b/src/schemas.ts index b6adbcf1b..d1f81d442 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -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,