diff --git a/README.md b/README.md index b8474e7..d9cda87 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,14 @@ And a lot more! https://github.com/harrisoncramer/gitlab.nvim/assets/32515581/dc5c07de-4ae6-4335-afe1-d554e3804372 -## Table of Contents +## Table of Contents -- [Requirements](#requirements) -- [Quick Start](#quick-start) -- [Installation](#installation) -- [Connecting to Gitlab](#connecting-to-gitlab) -- [Configuring the Plugin](#configuring-the-plugin) -- [Usage](#usage) +- [Requirements](#requirements) +- [Quick Start](#quick-start) +- [Installation](#installation) +- [Connecting to Gitlab](#connecting-to-gitlab) +- [Configuring the Plugin](#configuring-the-plugin) +- [Usage](#usage) - [The summary command](#summary) - [Reviewing Diffs](#reviewing-diffs) - [Discussions and Notes](#discussions-and-notes) @@ -125,7 +125,7 @@ require("gitlab").setup({ delete_comment = "dd", -- Delete comment reply = "r", -- Reply to comment toggle_node = "t", -- Opens or closes the discussion - toggle_resolved = "p", -- Toggles the resolved status of the discussion + toggle_resolved = "p" -- Toggles the resolved status of the whole discussion position = "left", -- "top", "right", "bottom" or "left" size = "20%", -- Size of split relative = "editor", -- Position of tree split relative to "editor" or "window" diff --git a/cmd/comment.go b/cmd/comment.go index ae0c27a..46a58dd 100644 --- a/cmd/comment.go +++ b/cmd/comment.go @@ -214,15 +214,8 @@ func EditComment(w http.ResponseWriter, r *http.Request) { options := gitlab.UpdateMergeRequestDiscussionNoteOptions{} - /* The PATCH can either be to the resolved status of - the discussion or or the text of the comment */ msg := "edit comment" - if editCommentRequest.Comment == "" { - options.Resolved = &editCommentRequest.Resolved - msg = "update discussion status" - } else { - options.Body = gitlab.String(editCommentRequest.Comment) - } + options.Body = gitlab.String(editCommentRequest.Comment) note, res, err := c.git.Discussions.UpdateMergeRequestDiscussionNote(c.projectId, c.mergeId, editCommentRequest.DiscussionId, editCommentRequest.NoteId, &options) diff --git a/cmd/main.go b/cmd/main.go index a476c1f..bd930ae 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -36,6 +36,7 @@ func main() { m.Handle("/revoke", withGitlabContext(http.HandlerFunc(RevokeHandler), c)) m.Handle("/info", withGitlabContext(http.HandlerFunc(InfoHandler), c)) m.Handle("/discussions", withGitlabContext(http.HandlerFunc(ListDiscussionsHandler), c)) + m.Handle("/discussion/resolve", withGitlabContext(http.HandlerFunc(DiscussionResolveHandler), c)) m.Handle("/comment", withGitlabContext(http.HandlerFunc(CommentHandler), c)) m.Handle("/reply", withGitlabContext(http.HandlerFunc(ReplyHandler), c)) m.Handle("/members", withGitlabContext(http.HandlerFunc(ProjectMembersHandler), c)) diff --git a/cmd/resolve_discussion.go b/cmd/resolve_discussion.go new file mode 100644 index 0000000..5e4c3c1 --- /dev/null +++ b/cmd/resolve_discussion.go @@ -0,0 +1,69 @@ +package main + +import ( + "encoding/json" + "io" + "net/http" + + "github.com/xanzy/go-gitlab" +) + +type DiscussionResolveRequest struct { + DiscussionID string `json:"discussion_id"` + Resolved bool `json:"resolved"` +} + +func DiscussionResolveHandler(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPut: + DiscussionResolve(w, r) + default: + w.WriteHeader(http.StatusMethodNotAllowed) + } +} +func DiscussionResolve(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + c := r.Context().Value("client").(Client) + + body, err := io.ReadAll(r.Body) + if err != nil { + c.handleError(w, err, "Could not read request body", http.StatusBadRequest) + return + } + defer r.Body.Close() + var resolveDiscussionRequest DiscussionResolveRequest + err = json.Unmarshal(body, &resolveDiscussionRequest) + if err != nil { + c.handleError(w, err, "Could not read JSON from request", http.StatusBadRequest) + return + } + + _, res, err := c.git.Discussions.ResolveMergeRequestDiscussion( + c.projectId, + c.mergeId, + resolveDiscussionRequest.DiscussionID, + &gitlab.ResolveMergeRequestDiscussionOptions{Resolved: &resolveDiscussionRequest.Resolved}, + ) + + if err != nil { + c.handleError(w, err, "Could not update resolve status of discussion", res.StatusCode) + return + } + + w.WriteHeader(http.StatusOK) + var message string + if resolveDiscussionRequest.Resolved { + message = "Discussion resolved" + } else { + message = "Discussion unresolved" + } + response := SuccessResponse{ + Message: message, + Status: http.StatusOK, + } + + err = json.NewEncoder(w).Encode(response) + if err != nil { + c.handleError(w, err, "Could not encode response", http.StatusInternalServerError) + } +} diff --git a/lua/gitlab/actions/discussions.lua b/lua/gitlab/actions/discussions.lua index dbf7aaf..1032027 100644 --- a/lua/gitlab/actions/discussions.lua +++ b/lua/gitlab/actions/discussions.lua @@ -633,8 +633,8 @@ M.send_edits = function(discussion_id, note_id, unlinked) end end --- This comment (settings.discussion_tree.toggle_resolved) will toggle the resolved status of the current discussion and send the change to the Go server -M.toggle_resolved = function(tree) +-- This function (settings.discussion_tree.toggle_discussion_resolved) will toggle the resolved status of the current discussion and send the change to the Go server +M.toggle_discussion_resolved = function(tree) local note = tree:get_node() if not note or not note.resolvable then return @@ -642,11 +642,10 @@ M.toggle_resolved = function(tree) local body = { discussion_id = note.id, - note_id = note.root_note_id, resolved = not note.resolved, } - job.run_job("/comment", "PATCH", body, function(data) + job.run_job("/discussion/resolve", "PUT", body, function(data) u.notify(data.message, vim.log.levels.INFO) M.redraw_resolved_status(tree, note, not note.resolved) end) @@ -802,10 +801,10 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked) M.delete_comment(tree, unlinked) end, { buffer = bufnr }) vim.keymap.set("n", state.settings.discussion_tree.toggle_resolved, function() - M.toggle_resolved(tree) + M.toggle_discussion_resolved(tree) end, { buffer = bufnr }) vim.keymap.set("n", state.settings.discussion_tree.toggle_node, function() - M.toggle_node(tree, unlinked) + M.toggle_node(tree) end, { buffer = bufnr }) vim.keymap.set("n", state.settings.discussion_tree.reply, function() M.reply(tree) diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index e059a46..971aded 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -47,7 +47,7 @@ return { toggle_discussions = async.sequence({ info }, discussions.toggle), edit_comment = async.sequence({ info }, discussions.edit_comment), delete_comment = async.sequence({ info }, discussions.delete_comment), - toggle_resolved = async.sequence({ info }, discussions.toggle_resolved), + toggle_resolved = async.sequence({ info }, discussions.toggle_discussion_resolved), reply = async.sequence({ info }, discussions.reply), -- Other functions 🤷 state = state,