Feat: Edit PR Description (#22)

This MR makes it possible to edit the description. Non-breaking, will happen within the normal description popup.
This commit is contained in:
Harrison (Harry) Cramer
2023-08-14 20:37:36 -04:00
committed by GitHub
parent 35d5b619ce
commit 2028be2154
6 changed files with 107 additions and 20 deletions

View File

@@ -2,11 +2,9 @@
This Neovim plugin is designed to make it easy to review Gitlab MRs from within the editor. This means you can do things like: This Neovim plugin is designed to make it easy to review Gitlab MRs from within the editor. This means you can do things like:
- Create, edit, and delete comments on an MR - Create, edit, delete, and reply to comments on an MR
- Reply to exisiting comments - Read and Edit an MR description
- Read MR summaries - Approve/Revoke Approval for an MR
- Approve an MR
- Revoke approval for an MR
https://github.com/harrisoncramer/gitlab.nvim/assets/32515581/dfd3aa8a-6fc4-4e43-8d2f-489df0745822 https://github.com/harrisoncramer/gitlab.nvim/assets/32515581/dfd3aa8a-6fc4-4e43-8d2f-489df0745822
@@ -58,7 +56,7 @@ This plugin requires a `.gitlab.nvim` file in the root of the local Gitlab direc
``` ```
project_id=112415 project_id=112415
auth_token=your_gitlab_token auth_token=your_gitlab_token
gitlab_url=https://my-personal-gitlab-instance.com gitlab_url=https://my-personal-gitlab-instance.com/
``` ```
If you don't want to write your authentication token into a dotfile, you may provide it as a shell variable. For instance in your `.bashrc` or `.zshrc` file: If you don't want to write your authentication token into a dotfile, you may provide it as a shell variable. For instance in your `.bashrc` or `.zshrc` file:
@@ -72,7 +70,7 @@ By default, the plugin will interact with MRs against a "main" branch. You can c
``` ```
project_id=112415 project_id=112415
auth_token=your_gitlab_token auth_token=your_gitlab_token
gitlab_url=https://my-personal-gitlab-instance.com gitlab_url=https://my-personal-gitlab-instance.com/
base_branch=master base_branch=master
``` ```
@@ -88,7 +86,7 @@ require("gitlab").setup({
keymaps = { keymaps = {
popup = { -- The popup for comment creation, editing, and replying popup = { -- The popup for comment creation, editing, and replying
exit = "<Esc>", exit = "<Esc>",
perform_action = "<leader>s", -- Once in normal mode, does action perform_action = "<leader>s", -- Once in normal mode, does action (like saving comment or editing description, etc)
}, },
discussion_tree = { -- The discussion tree that holds all comments discussion_tree = { -- The discussion tree that holds all comments
jump_to_location = "o", jump_to_location = "o",
@@ -118,12 +116,15 @@ First, check out the branch that you want to review locally.
git checkout feature-branch git checkout feature-branch
``` ```
Then open Neovim and the reviewer will be initialized. The `project_id` you specify in your configuration file must match the project_id of the Gitlab project your terminal is inside of. The `summary` command will pull down the MR description into a buffer so that you can read it: Then open Neovim and the reviewer will be initialized. The `project_id` you specify in your configuration file must match the project_id of the Gitlab project your terminal is inside of.
The `summary` command will pull down the MR description into a buffer so that you can read it. To edit the description, edit the buffer and press the `perform_action` keybinding when in normal mode (it's `<leader>s` by default):
```lua ```lua
require("gitlab").summary() require("gitlab").summary()
``` ```
The `approve` command will approve the merge request for the current branch: The `approve` command will approve the merge request for the current branch:
```lua ```lua

View File

@@ -29,6 +29,7 @@ func main() {
} }
m := http.NewServeMux() m := http.NewServeMux()
m.Handle("/mr", withGitlabContext(http.HandlerFunc(UpdateHandler), c))
m.Handle("/approve", withGitlabContext(http.HandlerFunc(ApproveHandler), c)) m.Handle("/approve", withGitlabContext(http.HandlerFunc(ApproveHandler), c))
m.Handle("/revoke", withGitlabContext(http.HandlerFunc(RevokeHandler), c)) m.Handle("/revoke", withGitlabContext(http.HandlerFunc(RevokeHandler), c))
m.Handle("/info", withGitlabContext(http.HandlerFunc(InfoHandler), c)) m.Handle("/info", withGitlabContext(http.HandlerFunc(InfoHandler), c))

74
cmd/update.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"github.com/xanzy/go-gitlab"
)
type UpdateRequest struct {
Description string `json:"description"`
}
type UpdateResponse struct {
SuccessResponse
MergeRequest *gitlab.MergeRequest `json:"mr"`
}
func UpdateHandler(w http.ResponseWriter, r *http.Request) {
c := r.Context().Value("client").(Client)
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPut {
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
return
}
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 updateRequest UpdateRequest
err = json.Unmarshal(body, &updateRequest)
if err != nil {
c.handleError(w, err, "Could not read JSON from request", http.StatusBadRequest)
return
}
git, err := gitlab.NewClient(c.authToken)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
mr, res, err := git.MergeRequests.UpdateMergeRequest(c.projectId, c.mergeId, &gitlab.UpdateMergeRequestOptions{Description: &updateRequest.Description})
if err != nil {
c.handleError(w, err, "Could not edit merge request", http.StatusBadRequest)
return
}
if res.StatusCode != http.StatusOK {
c.handleError(w, err, "Could not edit merge request", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
response := UpdateResponse{
SuccessResponse: SuccessResponse{
Message: "Merge request updated",
Status: http.StatusOK,
},
MergeRequest: mr,
}
json.NewEncoder(w).Encode(response)
}

View File

@@ -45,6 +45,7 @@ M.setup = function(args)
if not M.setPluginConfiguration(args) then return end -- Return if not a valid gitlab project if not M.setPluginConfiguration(args) then return end -- Return if not a valid gitlab project
local command = state.BIN local command = state.BIN
.. " " .. " "
.. state.PROJECT_ID .. state.PROJECT_ID

View File

@@ -1,13 +1,14 @@
local state = require("gitlab.state") local job = require("gitlab.job")
local Popup = require("nui.popup") local state = require("gitlab.state")
local u = require("gitlab.utils") local Popup = require("nui.popup")
local keymaps = require("gitlab.keymaps") local u = require("gitlab.utils")
local summaryPopup = Popup(u.create_popup_state("Loading Summary...", "80%", "80%")) local keymaps = require("gitlab.keymaps")
local M = {} local descriptionPopup = Popup(u.create_popup_state("Loading Description...", "80%", "80%"))
local M = {}
M.summary = function() M.summary = function()
if u.base_invalid() then return end if u.base_invalid() then return end
summaryPopup:mount() descriptionPopup:mount()
local currentBuffer = vim.api.nvim_get_current_buf() local currentBuffer = vim.api.nvim_get_current_buf()
local title = state.INFO.title local title = state.INFO.title
local description = state.INFO.description local description = state.INFO.description
@@ -18,9 +19,17 @@ M.summary = function()
end end
vim.schedule(function() vim.schedule(function()
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines) vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
vim.api.nvim_buf_set_option(currentBuffer, "modifiable", false) descriptionPopup.border:set_text("top", title, "center")
summaryPopup.border:set_text("top", title, "center") keymaps.set_popup_keymaps(descriptionPopup, M.edit_description)
keymaps.set_popup_keymaps(summaryPopup) end)
end
M.edit_description = function(text)
local jsonTable = { description = text }
local json = vim.json.encode(jsonTable)
job.run_job("mr", "PUT", json, function(data)
vim.notify(data.message, vim.log.levels.INFO)
state.INFO.description = data.mr.description
end) end)
end end

View File

@@ -187,6 +187,7 @@ local create_popup_state = function(title, width, height)
buf_options = { buf_options = {
filetype = 'markdown' filetype = 'markdown'
}, },
relative = "editor",
enter = true, enter = true,
focusable = true, focusable = true,
border = { border = {