-
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: Add support for auto-merge and squash options in merge requests #235
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "changesets-gitlab": minor | ||
| --- | ||
|
|
||
| feat: add optional INPUT_MERGE_WHEN_PIPELINE_SUCCEEDS, INPUT_SQUASH, and INPUT_SQUASH_COMMIT_MESSAGE inputs to support auto-merge and squash settings for created merge requests |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,12 @@ import fs from 'node:fs/promises' | |
| import path from 'node:path' | ||
|
|
||
| import { exec } from '@actions/exec' | ||
| import type { Gitlab } from '@gitbeaker/core' | ||
| import type { | ||
| Gitlab, | ||
| MergeRequestSchema, | ||
| EditMergeRequestOptions, | ||
| AcceptMergeRequestOptions, | ||
| } from '@gitbeaker/core' | ||
| import type { Package } from '@manypkg/get-packages' | ||
| import { getPackages } from '@manypkg/get-packages' | ||
| import pLimit from 'p-limit' | ||
|
|
@@ -207,6 +212,9 @@ export interface VersionOptions { | |
| cwd?: string | ||
| mrTitle?: string | ||
| removeSourceBranch?: boolean | ||
| mergeWhenPipelineSucceeds?: boolean | ||
| squash?: boolean | ||
| squashCommitMessage?: string | ||
| mrTargetBranch?: string | ||
| commitMessage?: string | ||
| hasPublishScript?: boolean | ||
|
|
@@ -220,6 +228,9 @@ export async function runVersion({ | |
| mrTargetBranch = context.ref, | ||
| commitMessage = 'Version Packages', | ||
| removeSourceBranch = false, | ||
| mergeWhenPipelineSucceeds, | ||
| squash, | ||
| squashCommitMessage, | ||
| hasPublishScript = false, | ||
| }: VersionOptions) { | ||
| const currentBranch = context.ref | ||
|
|
@@ -322,28 +333,59 @@ ${ | |
| perPage: 1, | ||
| }) | ||
| console.log(JSON.stringify(searchResult, null, 2)) | ||
|
|
||
| let mergeRequest: MergeRequestSchema | ||
| const mergeRequestOptions: EditMergeRequestOptions = { | ||
| description: await mrBodyPromise, | ||
| removeSourceBranch, | ||
| labels, | ||
| squash, | ||
| } | ||
| if (searchResult.length === 0) { | ||
| console.log( | ||
| `creating merge request from ${versionBranch} to ${mrTargetBranch}.`, | ||
| ) | ||
| await api.MergeRequests.create( | ||
| mergeRequest = await api.MergeRequests.create( | ||
| context.projectId, | ||
| versionBranch, | ||
| mrTargetBranch, | ||
| finalMrTitle, | ||
| { | ||
| description: await mrBodyPromise, | ||
| removeSourceBranch, | ||
| labels, | ||
| }, | ||
| mergeRequestOptions, | ||
| ) | ||
| } else { | ||
| console.log(`updating found merge request !${searchResult[0].iid}`) | ||
| await api.MergeRequests.edit(context.projectId, searchResult[0].iid, { | ||
| title: finalMrTitle, | ||
| description: await mrBodyPromise, | ||
| removeSourceBranch, | ||
| labels, | ||
| }) | ||
| mergeRequestOptions.title = finalMrTitle | ||
| mergeRequest = await api.MergeRequests.edit( | ||
| context.projectId, | ||
| searchResult[0].iid, | ||
| mergeRequestOptions, | ||
| ) | ||
| } | ||
|
|
||
| const acceptRequest: AcceptMergeRequestOptions = {} | ||
| if ( | ||
| mergeWhenPipelineSucceeds !== undefined && | ||
| mergeRequest.merge_when_pipeline_succeeds !== mergeWhenPipelineSucceeds | ||
| ) { | ||
| acceptRequest.mergeWhenPipelineSucceeds = mergeWhenPipelineSucceeds | ||
| } | ||
| if ( | ||
| squash === true && | ||
| squashCommitMessage !== undefined && | ||
| mergeRequest.squash_commit_message !== squashCommitMessage | ||
|
kriakiku marked this conversation as resolved.
|
||
| ) { | ||
| acceptRequest.squashCommitMessage = squashCommitMessage | ||
| } | ||
|
|
||
| if (Object.keys(acceptRequest).length > 0) { | ||
| console.log( | ||
| `updating merge request !${mergeRequest.iid} with options:`, | ||
| acceptRequest, | ||
| ) | ||
| await api.MergeRequests.accept( | ||
| context.projectId, | ||
| mergeRequest.iid, | ||
| acceptRequest, | ||
| ) | ||
| } | ||
|
Comment on lines
+365
to
390
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling GitLab's accept endpoint (
Suggested guards:
🔒 Proposed guard- const acceptRequest: AcceptMergeRequestOptions = {}
- if (
- mergeWhenPipelineSucceeds !== undefined &&
- mergeRequest.merge_when_pipeline_succeeds !== mergeWhenPipelineSucceeds
- ) {
- acceptRequest.mergeWhenPipelineSucceeds = mergeWhenPipelineSucceeds
- }
- if (
- squash === true &&
- squashCommitMessage !== undefined &&
- mergeRequest.squash_commit_message !== squashCommitMessage
- ) {
- acceptRequest.squashCommitMessage = squashCommitMessage
- }
-
- if (Object.keys(acceptRequest).length > 0) {
+ // Only invoke accept when explicitly opting into auto-merge; accept will
+ // otherwise merge the MR immediately.
+ if (
+ mergeWhenPipelineSucceeds === true &&
+ mergeRequest.merge_when_pipeline_succeeds !== true
+ ) {
+ const acceptRequest: AcceptMergeRequestOptions = {
+ mergeWhenPipelineSucceeds: true,
+ ...(squash === true && squashCommitMessage !== undefined
+ ? { squashCommitMessage }
+ : {}),
+ }
console.log(
`updating merge request !${mergeRequest.iid} with options:`,
- acceptRequest,
+ acceptRequest,
)
await api.MergeRequests.accept(
context.projectId,
mergeRequest.iid,
acceptRequest,
)
}🤖 Prompt for AI Agents
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kriakiku Is this true? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the only way to specify squashCommitMessage and mergeWhenPipelineSucceeds is by calling the Accept endpoint. This may trigger an immediate merge of the request if no additional rules are set up in the repository (such as requiring an approval from someone other than the author). The GitLab API doesn’t allow doing this any other way. Should I update the README to clarify that setting these parameters via the Accept endpoint might result in an immediate merge?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, this is a bit confusing to me. |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.