Skip to content

Commit 341d56a

Browse files
harrisoncramergkzejakubbortlik
authored
Release: Bug Fixes and Improvements (#399)
fix: Error messages and run all tests (#381) feat: Automatically open fold under cursor (#380) fix: Discussion ID is not required (#383) chore: Add more emojis (#384) fix: Publish all drafts (#391) fix: Make discussion tree buffers no-modifiable (#394) fix: Incorrect warning about commits (#395) fix: Show draft replies in the correct tree (#396) fix: Cannot choose merge requests (#398) --------- Co-authored-by: George Kontridze <george.kontridze@gmail.com> Co-authored-by: Jakub F. Bortlík <jakub.bortlik@proton.me>
1 parent 38bde8a commit 341d56a

File tree

14 files changed

+174
-53
lines changed

14 files changed

+174
-53
lines changed

cmd/app/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type PluginOptions struct {
1111
GitlabRequest bool `json:"gitlab_request"`
1212
GitlabResponse bool `json:"gitlab_response"`
1313
} `json:"debug"`
14-
ChosenTargetBranch *string `json:"chosen_target_branch,omitempty"`
14+
ChosenMrIID int `json:"chosen_mr_iid"`
1515
ConnectionSettings struct {
1616
Insecure bool `json:"insecure"`
1717
Remote string `json:"remote"`

cmd/app/middleware.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ func (m withMrMiddleware) handle(next http.Handler) http.Handler {
105105
options := gitlab.ListProjectMergeRequestsOptions{
106106
Scope: gitlab.Ptr("all"),
107107
SourceBranch: &m.data.gitInfo.BranchName,
108-
TargetBranch: pluginOptions.ChosenTargetBranch,
108+
}
109+
110+
if pluginOptions.ChosenMrIID != 0 {
111+
options.IIDs = gitlab.Ptr([]int{pluginOptions.ChosenMrIID})
109112
}
110113

111114
mergeRequests, _, err := m.client.ListProjectMergeRequests(m.data.projectInfo.ProjectId, &options)

config/emojis.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6351,6 +6351,38 @@
63516351
],
63526352
"moji": "😮"
63536353
},
6354+
"package": {
6355+
"unicode": "1F4E6",
6356+
"unicode_alternates": [],
6357+
"name": "package",
6358+
"shortname": ":package:",
6359+
"category": "people",
6360+
"aliases": [],
6361+
"aliases_ascii": [],
6362+
"keywords": [
6363+
"release"
6364+
],
6365+
"moji": "📦"
6366+
},
6367+
"party": {
6368+
"unicode": "1F389",
6369+
"unicode_alternates": [],
6370+
"name": "party popper as a 'tada' celebration",
6371+
"shortname": ":tada:",
6372+
"category": "people",
6373+
"aliases": [
6374+
":party_popper:"
6375+
],
6376+
"aliases_ascii": [],
6377+
"keywords": [
6378+
"celebrate",
6379+
"celebration",
6380+
"hooray",
6381+
"hurrah",
6382+
"hurray"
6383+
],
6384+
"moji": "🎉"
6385+
},
63546386
"pensive": {
63556387
"unicode": "1F614",
63566388
"unicode_alternates": [],
@@ -8445,6 +8477,19 @@
84458477
],
84468478
"moji": "🤖"
84478479
},
8480+
"rocket": {
8481+
"unicode": "1F680",
8482+
"unicode_alternates": [],
8483+
"name": "rocket",
8484+
"shortname": ":rocket:",
8485+
"category": "people",
8486+
"aliases": [],
8487+
"aliases_ascii": [],
8488+
"keywords": [
8489+
"space"
8490+
],
8491+
"moji": "🚀"
8492+
},
84488493
"rofl": {
84498494
"unicode": "1F923",
84508495
"unicode_alternates": [],

lua/gitlab/actions/common.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ M.add_empty_titles = function()
8282
{ end_row = linnr - 1, end_col = string.len(v.title), hl_group = "TitleHighlight" }
8383
)
8484
end
85+
M.switch_can_edit_bufs(false, v.bufnr)
8586
end
8687
end
8788
end

lua/gitlab/actions/create_mr.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
--- continue working on it.
4848
---@param args? Mr
4949
M.start = function(args)
50-
if not git.current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
50+
if not git.check_current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
5151
return
5252
end
5353

lua/gitlab/actions/discussions/winbar.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ local function content()
4949
local resolvable_notes, resolved_notes = get_data(state.DISCUSSION_DATA.unlinked_discussions)
5050

5151
local draft_notes = require("gitlab.actions.draft_notes")
52-
local inline_draft_notes = List.new(state.DRAFT_NOTES):filter(draft_notes.has_position)
53-
local unlinked_draft_notes = List.new(state.DRAFT_NOTES):filter(function(note)
54-
return not draft_notes.has_position(note)
52+
local inline_draft_notes, unlinked_draft_notes = List.new(state.DRAFT_NOTES):partition(function(note)
53+
if note.discussion_id == "" then
54+
return draft_notes.has_position(note)
55+
end
56+
for _, discussion in ipairs(state.DISCUSSION_DATA.unlinked_discussions) do
57+
if discussion.id == note.discussion_id then
58+
return false
59+
end
60+
end
61+
return true
5562
end)
5663

5764
local t = {

lua/gitlab/actions/draft_notes/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ end
8484

8585
---Publishes all draft notes and comments. Re-renders all discussion views.
8686
M.confirm_publish_all_drafts = function()
87-
local body = {}
87+
local body = { publish_all = true }
8888
job.run_job("/mr/draft_notes/publish", "POST", body, function(data)
8989
u.notify(data.message, vim.log.levels.INFO)
9090
state.DRAFT_NOTES = {}

lua/gitlab/actions/merge_requests.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ M.choose_merge_request = function(opts)
4545
end
4646

4747
vim.schedule(function()
48-
state.chosen_target_branch = choice.target_branch
48+
state.chosen_mr_iid = choice.iid
4949
require("gitlab.server").restart(function()
5050
if opts.open_reviewer then
5151
require("gitlab").review()

lua/gitlab/actions/summary.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ M.summary = function()
8282
vim.api.nvim_set_current_buf(description_popup.bufnr)
8383
end)
8484

85-
git.current_branch_up_to_date_on_remote(vim.log.levels.WARN)
85+
git.check_current_branch_up_to_date_on_remote(vim.log.levels.WARN)
86+
git.check_mr_in_good_condition()
8687
end
8788

8889
-- Builds a lua list of strings that contain metadata about the current MR. Only builds the

lua/gitlab/git.lua

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,58 @@ M.switch_branch = function(branch)
4343
return run_system({ "git", "checkout", "-q", branch })
4444
end
4545

46+
---Fetches the name of the remote tracking branch for the current branch
47+
---@return string|nil, string|nil
48+
M.get_remote_branch = function()
49+
return run_system({ "git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}" })
50+
end
51+
52+
---Determines whether the tracking branch is ahead of or behind the current branch, and warns the user if so
53+
---@param current_branch string
54+
---@param remote_branch string
55+
---@param log_level number
56+
---@return boolean
57+
M.get_ahead_behind = function(current_branch, remote_branch, log_level)
58+
local u = require("gitlab.utils")
59+
local result, err =
60+
run_system({ "git", "rev-list", "--left-right", "--count", current_branch .. "..." .. remote_branch })
61+
if err ~= nil or result == nil then
62+
u.notify("Could not determine if branch is up-to-date: " .. err, vim.log.levels.ERROR)
63+
return false
64+
end
65+
66+
local ahead, behind = result:match("(%d+)%s+(%d+)")
67+
if ahead == nil or behind == nil then
68+
u.notify("Error parsing ahead/behind information.", vim.log.levels.ERROR)
69+
return false
70+
end
71+
72+
ahead = tonumber(ahead)
73+
behind = tonumber(behind)
74+
75+
if ahead > 0 and behind == 0 then
76+
u.notify(string.format("There are local changes that haven't been pushed to %s yet", remote_branch), log_level)
77+
return false
78+
end
79+
if behind > 0 and ahead == 0 then
80+
u.notify(string.format("There are remote changes on %s that haven't been pulled yet", remote_branch), log_level)
81+
return false
82+
end
83+
84+
if ahead > 0 and behind > 0 then
85+
u.notify(
86+
string.format(
87+
"Your branch and the remote %s have diverged. You need to pull, possibly rebase, and then push.",
88+
remote_branch
89+
),
90+
log_level
91+
)
92+
return false
93+
end
94+
95+
return true -- Checks passed, branch is up-to-date
96+
end
97+
4698
---Return the name of the current branch
4799
---@return string|nil, string|nil
48100
M.get_current_branch = function()
@@ -93,39 +145,45 @@ M.contains_branch = function(current_branch)
93145
return run_system({ "git", "branch", "-r", "--contains", current_branch })
94146
end
95147

96-
---Returns true if `branch` is up-to-date on remote, false otherwise.
148+
---Returns true if `branch` is up-to-date on remote, otherwise false and warns user
97149
---@param log_level integer
98-
---@return boolean|nil
99-
M.current_branch_up_to_date_on_remote = function(log_level)
100-
local state = require("gitlab.state")
101-
local current_branch = M.get_current_branch()
102-
local handle = io.popen("git branch -r --contains " .. current_branch .. " 2>&1")
103-
if not handle then
104-
require("gitlab.utils").notify("Error running 'git branch' command.", vim.log.levels.ERROR)
105-
return nil
150+
---@return boolean
151+
M.check_current_branch_up_to_date_on_remote = function(log_level)
152+
local u = require("gitlab.utils")
153+
154+
-- Get current branch
155+
local current_branch, err_current_branch = M.get_current_branch()
156+
if err_current_branch or not current_branch then
157+
u.notify("Could not get current branch: " .. err_current_branch, vim.log.levels.ERROR)
158+
return false
106159
end
107160

108-
local remote_branches_with_current_head = {}
109-
for line in handle:lines() do
110-
table.insert(remote_branches_with_current_head, line)
161+
-- Get remote tracking branch
162+
local remote_branch, err_remote_branch = M.get_remote_branch()
163+
if err_remote_branch or not remote_branch then
164+
u.notify("Could not get remote branch: " .. err_remote_branch, vim.log.levels.ERROR)
165+
return false
111166
end
112-
handle:close()
113167

114-
local current_head_on_remote = List.new(remote_branches_with_current_head):filter(function(line)
115-
return line == string.format(" %s/", state.settings.connection_settings.remote) .. current_branch
116-
end)
117-
local remote_up_to_date = #current_head_on_remote == 1
168+
return M.get_ahead_behind(current_branch, remote_branch, log_level)
169+
end
118170

119-
if not remote_up_to_date then
120-
require("gitlab.utils").notify(
121-
string.format(
122-
"You have local commits that are not on %s. Have you forgotten to push?",
123-
state.settings.connection_settings.remote
124-
),
125-
log_level
126-
)
171+
---Warns user if the current MR is in a bad state (closed, has conflicts, merged)
172+
M.check_mr_in_good_condition = function()
173+
local state = require("gitlab.state")
174+
local u = require("gitlab.utils")
175+
176+
if state.INFO.has_conflicts then
177+
u.notify("This merge request has conflicts!", vim.log.levels.WARN)
178+
end
179+
180+
if state.INFO.state == "closed" then
181+
u.notify(string.format("This MR was closed %s", u.time_since(state.INFO.closed_at)), vim.log.levels.WARN)
182+
end
183+
184+
if state.INFO.state == "merged" then
185+
u.notify(string.format("This MR was merged %s", u.time_since(state.INFO.merged_at)), vim.log.levels.WARN)
127186
end
128-
return remote_up_to_date
129187
end
130188

131189
return M

0 commit comments

Comments
 (0)