Skip to content

Commit fcdb289

Browse files
committed
feat!: implement AutoMerge option to AcceptMergeRequest #518
This PR also fixes a bug when the `Delete source branch` setting of the MR was not respected due to incorrect reference in `lua/gitlab/actions/merge.lua` to `state.INFO.delete_branch` instead of `state.INFO.force_remove_source_branch`. This PR also changes the behaviour of how the `opts` in `gitlab.merge(opts)` behave: - Before this PR: - when `lua require("gitlab").merge()` was run without parameters, the values visible in the Summary view were used (except for the bug mentioned above). - specifying one of `squash` or `delete_branch` in `opts` caused the other parameter to default to `false` even if Summary view would show it's set to `true`. - After this PR: - running `lua require("gitlab").merge()` without parameters doesn't change. - specifying any of the `opts` values (`auto_merge`, `squash`, `delete_branch`), has no effect on the other options - if not specified in the `gitlab.merge()` call, the values from the Summary are used. This is a breaking change, since theoretically, before, a user could rely on the fact that ignoring one of the options would set it to `false` which now is not guaranteed (the value depends on the existing MR settings).
1 parent 5a210ff commit fcdb289

6 files changed

Lines changed: 39 additions & 17 deletions

File tree

cmd/app/merge_mr.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
type AcceptMergeRequestRequest struct {
11+
AutoMerge bool `json:"auto_merge"`
1112
DeleteBranch bool `json:"delete_branch"`
1213
SquashMessage string `json:"squash_message"`
1314
Squash bool `json:"squash"`
@@ -27,6 +28,7 @@ func (a mergeRequestAccepterService) ServeHTTP(w http.ResponseWriter, r *http.Re
2728
payload := r.Context().Value(payload("payload")).(*AcceptMergeRequestRequest)
2829

2930
opts := gitlab.AcceptMergeRequestOptions{
31+
AutoMerge: &payload.AutoMerge,
3032
Squash: &payload.Squash,
3133
ShouldRemoveSourceBranch: &payload.DeleteBranch,
3234
}
@@ -47,7 +49,13 @@ func (a mergeRequestAccepterService) ServeHTTP(w http.ResponseWriter, r *http.Re
4749
return
4850
}
4951

50-
response := SuccessResponse{Message: "MR merged successfully"}
52+
var message string
53+
if payload.AutoMerge {
54+
message = "MR set to be merged when all checks pass"
55+
} else {
56+
message = "MR merged successfully"
57+
}
58+
response := SuccessResponse{Message: message}
5159

5260
w.WriteHeader(http.StatusOK)
5361

doc/gitlab.nvim.txt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Table of Contents *gitlab.nvim.table-of-contents*
99
- Connecting to Gitlab |gitlab.nvim.connecting-to-gitlab|
1010
- Configuring the Plugin |gitlab.nvim.configuring-the-plugin|
1111
- Usage |gitlab.nvim.usage|
12-
- The Summary view |gitlab.nvim.the-summary-view|
12+
- The Summary view |gitlab.nvim.summary-view|
1313
- Reviewing an MR |gitlab.nvim.reviewing-an-mr|
1414
- Temporary registers |gitlab.nvim.temp-registers|
1515
- Discussions and Notes |gitlab.nvim.discussions-and-notes|
@@ -308,6 +308,7 @@ you call this function with no values the defaults will be used:
308308
"pipeline",
309309
"branch",
310310
"target_branch",
311+
"auto_merge",
311312
"delete_branch",
312313
"squash",
313314
"labels",
@@ -382,7 +383,7 @@ Then open Neovim. To begin, try running the `summary` command or the `review`
382383
command.
383384

384385

385-
THE SUMMARY VIEW *gitlab.nvim.the-summary-view*
386+
THE SUMMARY VIEW *gitlab.nvim.summary-view*
386387

387388
The `summary` action will open the MR title and description:
388389
>lua
@@ -1025,18 +1026,25 @@ Copies the URL of the current MR to system clipboard.
10251026
*gitlab.nvim.merge*
10261027
gitlab.merge({opts}) ~
10271028

1028-
Merges the merge request into the target branch. When run without any
1029-
arguments, the `merge` action will respect the "Squash commits" and "Delete
1030-
source branch" settings set by `require("gitlab").create_mr()` or set in
1031-
Gitlab online. You can see the current settings in the Summary view, see
1032-
|gitlab.nvim.the-summary-view|.
1029+
Merges a mergeable merge request into the target branch. The behaviour can be
1030+
configured with the `opts` parameter. By default, the `merge` action respects
1031+
the "Auto-merge" setting, and the "Squash commits" and "Delete source branch"
1032+
settings set by `require("gitlab").create_mr()` (or in Gitlab's "Edit merge
1033+
request" page). You can see the current settings in the Summary view, see
1034+
|gitlab.nvim.summary-view|.
10331035
>lua
10341036
require("gitlab").merge()
1035-
require("gitlab").merge({ squash = false, delete_branch = true })
1037+
require("gitlab").merge({
1038+
auto_merge = true, squash = false, delete_branch = false
1039+
})
10361040
<
10371041
Parameters: ~
10381042
{opts}: (table|nil) Keyword arguments that can be used to override
1039-
default behavior.
1043+
individual current settings.
1044+
• {auto_merge}: (bool) If true, the merge request is set to merge
1045+
automatically when the pipeline succeeds. If false, an immediate
1046+
merge is attempted. Currently, the plugin doesn't allow
1047+
cancelling the auto-merge.
10401048
• {delete_branch}: (bool) If true, the source branch will be
10411049
deleted.
10421050
{squash}: (bool) If true, the commits will be squashed. If

lua/gitlab/actions/merge.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ local function create_squash_message_popup()
1212
end
1313

1414
---@class MergeOpts
15+
---@field auto_merge boolean?
1516
---@field delete_branch boolean?
1617
---@field squash boolean?
1718
---@field squash_message string?
1819

1920
---@param opts MergeOpts
2021
M.merge = function(opts)
21-
local merge_body = { squash = state.INFO.squash, delete_branch = state.INFO.delete_branch }
22-
if opts then
23-
merge_body.squash = opts.squash ~= nil and opts.squash
24-
merge_body.delete_branch = opts.delete_branch ~= nil and opts.delete_branch
22+
local merge_body = {
23+
auto_merge = state.INFO.merge_when_pipeline_succeeds,
24+
squash = state.INFO.squash,
25+
delete_branch = state.INFO.force_remove_source_branch,
26+
}
27+
for key, val in pairs(opts or {}) do
28+
merge_body[key] = val
2529
end
2630

27-
if state.INFO.detailed_merge_status ~= "mergeable" then
31+
if state.INFO.detailed_merge_status ~= "mergeable" and not merge_body.auto_merge then
2832
u.notify(string.format("MR not mergeable, currently '%s'", state.INFO.detailed_merge_status), vim.log.levels.ERROR)
2933
return
3034
end

lua/gitlab/actions/summary.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ M.build_info_lines = function()
124124
branch = { title = "Branch", content = info.source_branch },
125125
labels = { title = "Labels", content = table.concat(info.labels, ", ") },
126126
target_branch = { title = "Target Branch", content = info.target_branch },
127+
auto_merge = { title = "Auto-merge", content = (info.merge_when_pipeline_succeeds and "Yes" or "No") },
127128
delete_branch = {
128129
title = "Delete Source Branch",
129130
content = (info.force_remove_source_branch and "Yes" or "No"),
@@ -271,7 +272,7 @@ M.color_details = function(bufnr)
271272
vim.api.nvim_buf_add_highlight(bufnr, details_namespace, ("label" .. j), i - 1, start_idx - 1, end_idx)
272273
end
273274
end
274-
elseif v == "delete_branch" or v == "squash" or v == "draft" or v == "conflicts" then
275+
elseif v == "auto_merge" or v == "delete_branch" or v == "squash" or v == "draft" or v == "conflicts" then
275276
local line_content = u.get_line_content(bufnr, i)
276277
local start_idx, end_idx = line_content:find("%S-$")
277278
if start_idx ~= nil and end_idx ~= nil then

lua/gitlab/annotations.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@
252252

253253
---@class InfoSettings
254254
---@field horizontal? boolean -- Display metadata to the left of the summary rather than underneath
255-
---@field fields? ("author" | "created_at" | "updated_at" | "merge_status" | "draft" | "conflicts" | "assignees" | "reviewers" | "pipeline" | "branch" | "target_branch" | "delete_branch" | "squash" | "labels")[]
255+
---@field fields? ("author" | "created_at" | "updated_at" | "merge_status" | "draft" | "conflicts" | "assignees" | "reviewers" | "pipeline" | "branch" | "target_branch" | "auto_merge" | "delete_branch" | "squash" | "labels")[]
256256

257257
---@class DiscussionSettings: table
258258
---@field expanders? ExpanderOpts -- Customize the expander icons in the discussion tree

lua/gitlab/state.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ M.settings = {
214214
"pipeline",
215215
"branch",
216216
"target_branch",
217+
"auto_merge",
217218
"delete_branch",
218219
"squash",
219220
"labels",

0 commit comments

Comments
 (0)