Skip to content

Commit a260f64

Browse files
Release (#490)
Fix: Jumping to renamed files (#484) Fix: Store reviewer data before creating comment popup (#476) Fix: Make publishing drafts more robust (#483) Fix: Swap file_name and old_file_name in reviewer data (#485) --------- Co-authored-by: Jakub F. Bortlík <[email protected]>
1 parent 9f898aa commit a260f64

File tree

7 files changed

+79
-38
lines changed

7 files changed

+79
-38
lines changed

cmd/app/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func NewClient() (*Client, error) {
6868

6969
retryClient := retryablehttp.NewClient()
7070
retryClient.HTTPClient.Transport = tr
71-
retryClient.RetryMax = 0
7271
gitlabOptions = append(gitlabOptions, gitlab.WithHTTPClient(retryClient.HTTPClient))
72+
gitlabOptions = append(gitlabOptions, gitlab.WithoutRetries())
7373

7474
client, err := gitlab.NewClient(pluginOptions.AuthToken, gitlabOptions...)
7575

cmd/app/comment_helpers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@ type RequestWithPosition interface {
4242
func buildCommentPosition(commentWithPositionData RequestWithPosition) *gitlab.PositionOptions {
4343
positionData := commentWithPositionData.GetPositionData()
4444

45+
// If the file has been renamed, then this is a relevant part of the payload
46+
oldFileName := positionData.OldFileName
47+
if oldFileName == "" {
48+
oldFileName = positionData.FileName
49+
}
50+
4551
opt := &gitlab.PositionOptions{
4652
PositionType: &positionData.Type,
4753
StartSHA: &positionData.StartCommitSHA,
4854
HeadSHA: &positionData.HeadCommitSHA,
4955
BaseSHA: &positionData.BaseCommitSHA,
5056
NewPath: &positionData.FileName,
51-
OldPath: &positionData.OldFileName,
57+
OldPath: &oldFileName,
5258
NewLine: positionData.NewLine,
5359
OldLine: positionData.OldLine,
5460
}

lua/gitlab/actions/comment.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ M.create_comment_layout = function(opts)
153153
title = "Note"
154154
user_settings = popup_settings.note
155155
else
156-
-- TODO: investigate why `old_file_name` is in fact the new name for renamed files!
157-
local file_name = M.location.reviewer_data.old_file_name ~= "" and M.location.reviewer_data.old_file_name
158-
or M.location.reviewer_data.file_name
156+
local file_name = (M.location.reviewer_data.new_sha_focused or M.location.reviewer_data.old_file_name == "")
157+
and M.location.reviewer_data.file_name
158+
or M.location.reviewer_data.old_file_name
159159
title =
160160
popup.create_title("Comment", file_name, M.location.visual_range.start_line, M.location.visual_range.end_line)
161161
user_settings = popup_settings.comment

lua/gitlab/actions/draft_notes/init.lua

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
-- under lua/gitlab/actions/discussions/init.lua
55
local common = require("gitlab.actions.common")
66
local discussion_tree = require("gitlab.actions.discussions.tree")
7+
local git = require("gitlab.git")
78
local job = require("gitlab.job")
89
local NuiTree = require("nui.tree")
910
local List = require("gitlab.utils.list")
@@ -85,12 +86,20 @@ end
8586

8687
---Publishes all draft notes and comments. Re-renders all discussion views.
8788
M.confirm_publish_all_drafts = function()
89+
if not git.check_current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
90+
return
91+
end
8892
local body = { publish_all = true }
8993
job.run_job("/mr/draft_notes/publish", "POST", body, function(data)
9094
u.notify(data.message, vim.log.levels.INFO)
9195
state.DRAFT_NOTES = {}
92-
local discussions = require("gitlab.actions.discussions")
93-
discussions.rebuild_view(false, true)
96+
require("gitlab.actions.discussions").rebuild_view(false, true)
97+
end, function()
98+
require("gitlab.actions.discussions").rebuild_view(false, true)
99+
u.notify(
100+
"Draft(s) may have been published despite the error. Check the discussion tree. Try publishing drafts individually.",
101+
vim.log.levels.WARN
102+
)
94103
end)
95104
end
96105

@@ -99,6 +108,9 @@ end
99108
---and re-render it.
100109
---@param tree NuiTree
101110
M.confirm_publish_draft = function(tree)
111+
if not git.check_current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
112+
return
113+
end
102114
local current_node = tree:get_node()
103115
local note_node = common.get_note_node(tree, current_node)
104116
local root_node = common.get_root_node(tree, current_node)
@@ -111,12 +123,13 @@ M.confirm_publish_draft = function(tree)
111123
---@type integer
112124
local note_id = note_node.is_root and root_node.id or note_node.id
113125
local body = { note = note_id }
126+
local unlinked = tree.bufnr == require("gitlab.actions.discussions").unlinked_bufnr
114127
job.run_job("/mr/draft_notes/publish", "POST", body, function(data)
115128
u.notify(data.message, vim.log.levels.INFO)
116-
117-
local discussions = require("gitlab.actions.discussions")
118-
local unlinked = tree.bufnr == discussions.unlinked_bufnr
119129
M.rebuild_view(unlinked)
130+
end, function()
131+
M.rebuild_view(unlinked)
132+
u.notify("Draft may have been published despite the error. Check the discussion tree.", vim.log.levels.WARN)
120133
end)
121134
end
122135

lua/gitlab/git.lua

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ local M = {}
66
---@param command table
77
---@return string|nil, string|nil
88
local run_system = function(command)
9-
-- Load here to prevent loop
10-
local u = require("gitlab.utils")
119
local result = vim.fn.trim(vim.fn.system(command))
1210
if vim.v.shell_error ~= 0 then
13-
u.notify(result, vim.log.levels.ERROR)
11+
require("gitlab.utils").notify(result, vim.log.levels.ERROR)
1412
return nil, result
1513
end
1614
return result, nil
@@ -52,10 +50,28 @@ M.switch_branch = function(branch)
5250
return run_system({ "git", "checkout", "-q", branch })
5351
end
5452

55-
---Fetches the name of the remote tracking branch for the current branch
56-
---@return string|nil, string|nil
53+
---Returns the name of the remote-tracking branch for the current branch or nil if it can't be found
54+
---@return string|nil
5755
M.get_remote_branch = function()
58-
return run_system({ "git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}" })
56+
local remote_branch, err = run_system({ "git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}" })
57+
if err or remote_branch == "" then
58+
require("gitlab.utils").notify("Could not get remote branch: " .. err, vim.log.levels.ERROR)
59+
return nil
60+
end
61+
return remote_branch
62+
end
63+
64+
---Fetch the remote branch
65+
---@param remote_branch string The name of the repo and branch to fetch (e.g., "origin/some_branch")
66+
---@return boolean fetch_successfull False if an error occurred while fetching, true otherwise.
67+
M.fetch_remote_branch = function(remote_branch)
68+
local remote, branch = string.match(remote_branch, "([^/]+)/(.*)")
69+
local _, fetch_err = run_system({ "git", "fetch", remote, branch })
70+
if fetch_err ~= nil then
71+
require("gitlab.utils").notify("Error fetching remote-tracking branch: " .. fetch_err, vim.log.levels.ERROR)
72+
return false
73+
end
74+
return true
5975
end
6076

6177
---Determines whether the tracking branch is ahead of or behind the current branch, and warns the user if so
@@ -64,6 +80,10 @@ end
6480
---@param log_level number
6581
---@return boolean
6682
M.get_ahead_behind = function(current_branch, remote_branch, log_level)
83+
if not M.fetch_remote_branch(remote_branch) then
84+
return false
85+
end
86+
6787
local u = require("gitlab.utils")
6888
local result, err =
6989
run_system({ "git", "rev-list", "--left-right", "--count", current_branch .. "..." .. remote_branch })
@@ -104,17 +124,22 @@ M.get_ahead_behind = function(current_branch, remote_branch, log_level)
104124
return true -- Checks passed, branch is up-to-date
105125
end
106126

107-
---Return the name of the current branch
108-
---@return string|nil, string|nil
127+
---Return the name of the current branch or nil if it can't be retrieved
128+
---@return string|nil
109129
M.get_current_branch = function()
110-
return run_system({ "git", "branch", "--show-current" })
130+
local current_branch, err = run_system({ "git", "branch", "--show-current" })
131+
if err or current_branch == "" then
132+
require("gitlab.utils").notify("Could not get current branch: " .. err, vim.log.levels.ERROR)
133+
return nil
134+
end
135+
return current_branch
111136
end
112137

113138
---Return the list of possible merge targets.
114139
---@return table|nil
115140
M.get_all_merge_targets = function()
116-
local current_branch, err = M.get_current_branch()
117-
if not current_branch or err ~= nil then
141+
local current_branch = M.get_current_branch()
142+
if current_branch == nil then
118143
return
119144
end
120145
return List.new(M.get_all_remote_branches()):filter(function(branch)
@@ -158,19 +183,13 @@ end
158183
---@param log_level integer
159184
---@return boolean
160185
M.check_current_branch_up_to_date_on_remote = function(log_level)
161-
local u = require("gitlab.utils")
162-
163-
-- Get current branch
164-
local current_branch, err_current_branch = M.get_current_branch()
165-
if err_current_branch or not current_branch then
166-
u.notify("Could not get current branch: " .. err_current_branch, vim.log.levels.ERROR)
186+
local current_branch = M.get_current_branch()
187+
if current_branch == nil then
167188
return false
168189
end
169190

170-
-- Get remote tracking branch
171-
local remote_branch, err_remote_branch = M.get_remote_branch()
172-
if err_remote_branch or not remote_branch then
173-
u.notify("Could not get remote branch: " .. err_remote_branch, vim.log.levels.ERROR)
191+
local remote_branch = M.get_remote_branch()
192+
if remote_branch == nil then
174193
return false
175194
end
176195

lua/gitlab/job.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local Job = require("plenary.job")
44
local u = require("gitlab.utils")
55
local M = {}
66

7-
M.run_job = function(endpoint, method, body, callback)
7+
M.run_job = function(endpoint, method, body, callback, on_error_callback)
88
local state = require("gitlab.state")
99
local args = { "-s", "-X", (method or "POST"), string.format("localhost:%s", state.settings.port) .. endpoint }
1010

@@ -16,7 +16,8 @@ M.run_job = function(endpoint, method, body, callback)
1616

1717
-- This handler will handle all responses from the Go server. Anything with a successful
1818
-- status will call the callback (if it is supplied for the job). Otherwise, it will print out the
19-
-- success message or error message and details from the Go server.
19+
-- success message or error message and details from the Go server and run the on_error_callback
20+
-- (if supplied for the job).
2021
local stderr = {}
2122
Job:new({
2223
command = "curl",
@@ -53,6 +54,9 @@ M.run_job = function(endpoint, method, body, callback)
5354
-- Handle error case
5455
local message = string.format("%s: %s", data.message, data.details)
5556
u.notify(message, vim.log.levels.ERROR)
57+
if on_error_callback then
58+
on_error_callback(data)
59+
end
5660
end
5761
end, 0)
5862
end,

lua/gitlab/reviewer/init.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ M.jump = function(file_name, old_file_name, line_number, new_buffer)
123123

124124
local files = view.panel:ordered_file_list()
125125
local file = List.new(files):find(function(f)
126-
return new_buffer and f.path == file_name or f.oldpath == old_file_name
126+
local oldpath = f.oldpath ~= nil and f.oldpath or f.path
127+
return new_buffer and f.path == file_name or oldpath == old_file_name
127128
end)
128129
if file == nil then
129130
u.notify(
@@ -195,10 +196,8 @@ M.get_reviewer_data = function(current_win)
195196
local opposite_bufnr = new_sha_focused and layout.a.file.bufnr or layout.b.file.bufnr
196197

197198
return {
198-
-- TODO: swap 'a' and 'b' to fix lua/gitlab/actions/comment.lua:158, and hopefully also
199-
-- lua/gitlab/indicators/diagnostics.lua:129.
200-
file_name = layout.a.file.path,
201-
old_file_name = M.is_file_renamed() and layout.b.file.path or "",
199+
old_file_name = M.is_file_renamed() and layout.a.file.path or "",
200+
file_name = layout.b.file.path,
202201
old_line_from_buf = old_line,
203202
new_line_from_buf = new_line,
204203
modification_type = modification_type,

0 commit comments

Comments
 (0)