Merge pull request #846 from actions/merge-group-bug-fix

Fix for merge_group event bug
This commit is contained in:
Ahmed ElMallah
2024-10-28 11:42:18 -07:00
committed by GitHub
5 changed files with 83 additions and 25 deletions
+33 -6
View File
@@ -124,11 +124,7 @@ test('it raises an error when no refs are provided and the event is not a pull r
).toThrow() ).toThrow()
}) })
const pullRequestLikeEvents = [ const pullRequestLikeEvents = ['pull_request', 'pull_request_target']
'pull_request',
'pull_request_target',
'merge_group'
]
test.each(pullRequestLikeEvents)( test.each(pullRequestLikeEvents)(
'it uses the given refs even when the event is %s', 'it uses the given refs even when the event is %s',
@@ -152,7 +148,7 @@ test.each(pullRequestLikeEvents)(
) )
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 => { async eventName => {
const refs = getRefs(await readConfig(), { const refs = getRefs(await readConfig(), {
payload: { payload: {
@@ -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 () => { test('it defaults to runtime scope', async () => {
const config = await readConfig() const config = await readConfig()
expect(config.fail_on_scopes).toEqual(['runtime']) expect(config.fail_on_scopes).toEqual(['runtime'])
Generated Vendored
+22 -8
View File
@@ -314,12 +314,18 @@ function getRefs(config, context) {
let head_ref = config.head_ref; let head_ref = config.head_ref;
// If possible, source default base & head refs from the GitHub event. // If possible, source default base & head refs from the GitHub event.
// The base/head ref from the config take priority, if provided. // The base/head ref from the config take priority, if provided.
if (context.eventName === 'pull_request' || if (!base_ref && !head_ref) {
context.eventName === 'pull_request_target' || if (context.eventName === 'pull_request' ||
context.eventName === 'merge_group') { context.eventName === 'pull_request_target') {
const pull_request = schemas_1.PullRequestSchema.parse(context.payload.pull_request); const pull_request = schemas_1.PullRequestSchema.parse(context.payload.pull_request);
base_ref = base_ref || pull_request.base.sha; base_ref = base_ref || pull_request.base.sha;
head_ref = head_ref || pull_request.head.sha; head_ref = head_ref || pull_request.head.sha;
}
else if (context.eventName === 'merge_group') {
const merge_group = schemas_1.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) { if (!base_ref && !head_ref) {
throw new Error('Both a base ref and head ref must be provided, either via the `base_ref`/`head_ref` ' + throw new Error('Both a base ref and head ref must be provided, either via the `base_ref`/`head_ref` ' +
@@ -1023,7 +1029,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.ScorecardSchema = exports.ScorecardApiSchema = exports.ComparisonResponseSchema = exports.ChangesSchema = exports.ConfigurationOptionsSchema = exports.PullRequestSchema = exports.ChangeSchema = exports.SeveritySchema = exports.SCOPES = exports.SEVERITIES = void 0; exports.ScorecardSchema = exports.ScorecardApiSchema = exports.ComparisonResponseSchema = exports.ChangesSchema = exports.ConfigurationOptionsSchema = exports.MergeGroupSchema = exports.PullRequestSchema = exports.ChangeSchema = exports.SeveritySchema = exports.SCOPES = exports.SEVERITIES = void 0;
const z = __importStar(__nccwpck_require__(3301)); const z = __importStar(__nccwpck_require__(3301));
const purl_1 = __nccwpck_require__(3609); const purl_1 = __nccwpck_require__(3609);
exports.SEVERITIES = ['critical', 'high', 'moderate', 'low']; exports.SEVERITIES = ['critical', 'high', 'moderate', 'low'];
@@ -1107,6 +1113,10 @@ exports.PullRequestSchema = z.object({
base: z.object({ sha: z.string() }), base: z.object({ sha: z.string() }),
head: z.object({ sha: z.string() }) head: z.object({ sha: z.string() })
}); });
exports.MergeGroupSchema = z.object({
base_sha: z.string(),
head_sha: z.string()
});
exports.ConfigurationOptionsSchema = z exports.ConfigurationOptionsSchema = z
.object({ .object({
fail_on_severity: exports.SeveritySchema, fail_on_severity: exports.SeveritySchema,
@@ -49922,7 +49932,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.ScorecardSchema = exports.ScorecardApiSchema = exports.ComparisonResponseSchema = exports.ChangesSchema = exports.ConfigurationOptionsSchema = exports.PullRequestSchema = exports.ChangeSchema = exports.SeveritySchema = exports.SCOPES = exports.SEVERITIES = void 0; exports.ScorecardSchema = exports.ScorecardApiSchema = exports.ComparisonResponseSchema = exports.ChangesSchema = exports.ConfigurationOptionsSchema = exports.MergeGroupSchema = exports.PullRequestSchema = exports.ChangeSchema = exports.SeveritySchema = exports.SCOPES = exports.SEVERITIES = void 0;
const z = __importStar(__nccwpck_require__(3301)); const z = __importStar(__nccwpck_require__(3301));
const purl_1 = __nccwpck_require__(4498); const purl_1 = __nccwpck_require__(4498);
exports.SEVERITIES = ['critical', 'high', 'moderate', 'low']; exports.SEVERITIES = ['critical', 'high', 'moderate', 'low'];
@@ -50006,6 +50016,10 @@ exports.PullRequestSchema = z.object({
base: z.object({ sha: z.string() }), base: z.object({ sha: z.string() }),
head: z.object({ sha: z.string() }) head: z.object({ sha: z.string() })
}); });
exports.MergeGroupSchema = z.object({
base_sha: z.string(),
head_sha: z.string()
});
exports.ConfigurationOptionsSchema = z exports.ConfigurationOptionsSchema = z
.object({ .object({
fail_on_severity: exports.SeveritySchema, fail_on_severity: exports.SeveritySchema,
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+22 -10
View File
@@ -1,22 +1,34 @@
import {PullRequestSchema, ConfigurationOptions} from './schemas' import {
PullRequestSchema,
ConfigurationOptions,
MergeGroupSchema
} from './schemas'
export function getRefs( export function getRefs(
config: ConfigurationOptions, config: ConfigurationOptions,
context: {payload: {pull_request?: unknown}; eventName: string} context: {
payload: {pull_request?: unknown; merge_group?: unknown}
eventName: string
}
): {base: string; head: string} { ): {base: string; head: string} {
let base_ref = config.base_ref let base_ref = config.base_ref
let head_ref = config.head_ref let head_ref = config.head_ref
// If possible, source default base & head refs from the GitHub event. // If possible, source default base & head refs from the GitHub event.
// The base/head ref from the config take priority, if provided. // The base/head ref from the config take priority, if provided.
if ( if (!base_ref && !head_ref) {
context.eventName === 'pull_request' || if (
context.eventName === 'pull_request_target' || context.eventName === 'pull_request' ||
context.eventName === 'merge_group' context.eventName === 'pull_request_target'
) { ) {
const pull_request = PullRequestSchema.parse(context.payload.pull_request) const pull_request = PullRequestSchema.parse(context.payload.pull_request)
base_ref = base_ref || pull_request.base.sha base_ref = base_ref || pull_request.base.sha
head_ref = head_ref || pull_request.head.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) { if (!base_ref && !head_ref) {
+5
View File
@@ -91,6 +91,11 @@ export const PullRequestSchema = z.object({
head: z.object({sha: z.string()}) head: z.object({sha: z.string()})
}) })
export const MergeGroupSchema = z.object({
base_sha: z.string(),
head_sha: z.string()
})
export const ConfigurationOptionsSchema = z export const ConfigurationOptionsSchema = z
.object({ .object({
fail_on_severity: SeveritySchema, fail_on_severity: SeveritySchema,