Skip to content

Commit 9ec134d

Browse files
fix: draft note edits + reply keybinding (#279)
fix: issue with editing draft notes; jumping to correct position for draft notes; and replying (not supported) to draft notes.
1 parent 2c2d0eb commit 9ec134d

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

cmd/draft_notes.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type PostDraftNoteRequest struct {
2222
}
2323

2424
type UpdateDraftNoteRequest struct {
25-
Note string `json:"note"`
25+
Note string `json:"note"`
26+
Position gitlab.PositionOptions
2627
}
2728

2829
type DraftNotePublishRequest struct {
@@ -242,7 +243,8 @@ func (a *api) updateDraftNote(w http.ResponseWriter, r *http.Request) {
242243
}
243244

244245
opt := gitlab.UpdateDraftNoteOptions{
245-
Note: &updateDraftNoteRequest.Note,
246+
Note: &updateDraftNoteRequest.Note,
247+
Position: &updateDraftNoteRequest.Position,
246248
}
247249

248250
draftNote, res, err := a.client.UpdateDraftNote(a.projectInfo.ProjectId, a.projectInfo.MergeId, id, &opt)

cmd/draft_notes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func updateDraftNote(pid interface{}, mergeRequest int, note int, opt *gitlab.Up
106106

107107
func TestEditDraftNote(t *testing.T) {
108108
t.Run("Edits draft note", func(t *testing.T) {
109-
request := makeRequest(t, http.MethodPatch, "/mr/draft_notes/3", UpdateDraftNoteRequest{Note: "Some new note"})
109+
request := makeRequest(t, http.MethodPatch, "/mr/draft_notes/3", UpdateDraftNoteRequest{Note: "Some new note", Position: gitlab.PositionOptions{}})
110110
server, _ := createRouterAndApi(fakeClient{updateDraftNote: updateDraftNote})
111111
data := serveRequest(t, server, request, SuccessResponse{})
112112
assert(t, data.Message, "Draft note updated")

lua/gitlab/actions/common.lua

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
local List = require("gitlab.utils.list")
55
local u = require("gitlab.utils")
66
local reviewer = require("gitlab.reviewer")
7+
local indicators_common = require("gitlab.indicators.common")
78
local common_indicators = require("gitlab.indicators.common")
89
local state = require("gitlab.state")
910
local M = {}
@@ -176,7 +177,7 @@ end
176177

177178
---Takes a node and returns the line where the note is positioned in the new SHA. If
178179
---the line is not in the new SHA, returns nil
179-
---@param node any
180+
---@param node NuiTree.Node
180181
---@return number|nil
181182
local function get_new_line(node)
182183
---@type GitlabLineRange|nil
@@ -191,7 +192,7 @@ end
191192

192193
---Takes a node and returns the line where the note is positioned in the old SHA. If
193194
---the line is not in the old SHA, returns nil
194-
---@param node any
195+
---@param node NuiTree.Node
195196
---@return number|nil
196197
local function get_old_line(node)
197198
---@type GitlabLineRange|nil
@@ -204,6 +205,36 @@ local function get_old_line(node)
204205
return start_old_line
205206
end
206207

208+
---@param id string|integer
209+
---@return integer|nil
210+
M.get_line_number = function(id)
211+
---@type Discussion|DraftNote|nil
212+
local d_or_n
213+
d_or_n = List.new(state.DISCUSSION_DATA.discussions or {}):find(function(d)
214+
return d.id == id
215+
end) or List.new(state.DRAFT_NOTES or {}):find(function(d)
216+
return d.id == id
217+
end)
218+
219+
if d_or_n == nil then
220+
return
221+
end
222+
223+
local first_note = indicators_common.get_first_note(d_or_n)
224+
return (indicators_common.is_new_sha(d_or_n) and first_note.position.new_line or first_note.position.old_line) or 1
225+
end
226+
227+
---@param root_node NuiTree.Node
228+
---@return integer|nil
229+
M.get_line_number_from_node = function(root_node)
230+
if root_node.range then
231+
local start_old_line, start_new_line = common_indicators.parse_line_code(root_node.range.start.line_code)
232+
return root_node.old_line and start_old_line or start_new_line
233+
else
234+
return M.get_line_number(root_node.id)
235+
end
236+
end
237+
207238
-- This function (settings.discussion_tree.jump_to_reviewer) will jump the cursor to the reviewer's location associated with the note. The implementation depends on the reviewer
208239
M.jump_to_reviewer = function(tree, callback)
209240
local node = tree:get_node()
@@ -212,10 +243,10 @@ M.jump_to_reviewer = function(tree, callback)
212243
u.notify("Could not get discussion node", vim.log.levels.ERROR)
213244
return
214245
end
215-
local line_number = (root_node.new_line or root_node.old_line or 1)
216-
if root_node.range then
217-
local start_old_line, start_new_line = common_indicators.parse_line_code(root_node.range.start.line_code)
218-
line_number = root_node.old_line and start_old_line or start_new_line
246+
local line_number = M.get_line_number_from_node(root_node)
247+
if line_number == nil then
248+
u.notify("Could not get line number", vim.log.levels.ERROR)
249+
return
219250
end
220251
reviewer.jump(root_node.file_name, line_number, root_node.old_line == nil)
221252
callback()

lua/gitlab/actions/discussions/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ end
215215

216216
-- The reply popup will mount in a window when you trigger it (settings.discussion_tree.reply) when hovering over a node in the discussion tree.
217217
M.reply = function(tree)
218+
if M.is_draft_note(tree) then
219+
u.notify("Gitlab does not support replying to draft notes", vim.log.levels.WARN)
220+
return
221+
end
218222
local reply_popup = Popup(u.create_popup_state("Reply", state.settings.popup.reply))
219223
local node = tree:get_node()
220224
local discussion_node = common.get_root_node(tree, node)

lua/gitlab/actions/draft_notes/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ end
8585
---Send edits will actually send the edits to Gitlab and refresh the draft_notes tree
8686
M.send_edits = function(note_id)
8787
return function(text)
88-
local body = { note = text }
88+
local all_notes = List.new(state.DRAFT_NOTES)
89+
local the_note = all_notes:find(function(note)
90+
return note.id == note_id
91+
end)
92+
local body = { note = text, position = the_note.position }
8993
job.run_job(string.format("/mr/draft_notes/%d", note_id), "PATCH", body, function(data)
9094
u.notify(data.message, vim.log.levels.INFO)
9195
local has_position = false
92-
local new_draft_notes = List.new(state.DRAFT_NOTES):map(function(note)
96+
local new_draft_notes = all_notes:map(function(note)
9397
if note.id == note_id then
9498
has_position = M.has_position(note)
9599
note.note = text

lua/gitlab/indicators/diagnostics.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ end
5151
---@param d_or_n Discussion|DraftNote
5252
---@return Diagnostic
5353
local create_single_line_diagnostic = function(d_or_n)
54-
local first_note = indicators_common.get_first_note(d_or_n)
55-
local linnr = (indicators_common.is_new_sha(d_or_n) and first_note.position.new_line or first_note.position.old_line)
56-
or 1
54+
local linnr = actions_common.get_line_number(d_or_n.id)
5755
return create_diagnostic({
5856
lnum = linnr - 1,
5957
}, d_or_n)

0 commit comments

Comments
 (0)