Files
gitlab.nvim/README.md
Harrison (Harry) Cramer 4f0d4b49ef BREAKING CHANGE: Setup refactor and code cleanup
This MR makes several major tweaks to the codebase. Primarily it adjusts
the setup steps for the application so that rather than providing just
the project ID in the `.gitlab.nvim` file, users can also provide a
vareity of other settings, such as auth_token, base_branch, and so
forth. This is to make the project more extensible in the future.

This MR also fixes a variety of issues with error handling in the code,
primarily in the request/response model between the Lua jobs and the
Golang server.

BREAKING CHANGE: Modifies `.gitlab.nvim` and setup steps
2023-08-06 11:23:36 -04:00

209 lines
6.7 KiB
Markdown

# gitlab.nvim
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
- Reply to exisiting comments
- Read MR summaries
- Approve an MR
- Revoke approval for an MR
https://user-images.githubusercontent.com/32515581/233739969-216dad6e-fa77-417f-9d2d-5e875ab2fb40.mp4
## Requirements
- <a href="https://go.dev/">Go</a>
- <a href="https://www.gnu.org/software/make/manual/make.html">make (for install)</a>
- <a href="https://github.com/MunifTanjim/nui.nvim">nui.nvim</a>
- <a href="https://github.com/nvim-lua/plenary.nvim">plenary.nvim</a>
## Installation
With <a href="https://github.com/folke/lazy.nvim">Lazy</a>:
```lua
return {
"harrisoncramer/gitlab.nvim",
dependencies = {
"MunifTanjim/nui.nvim",
"nvim-lua/plenary.nvim"
},
build = function () require("gitlab").build() end, -- Builds the Go binary
config = function()
require("gitlab").setup()
end,
}
```
And with Packer:
```lua
use {
'harrisoncramer/gitlab.nvim',
requires = {
"MunifTanjim/nui.nvim",
"nvim-lua/plenary.nvim"
},
run = function() require("gitlab").build() end,
config = function()
require("gitlab").setup()
end,
}
```
## Configuration
This plugin requires a `.gitlab.nvim` file in the root of the local Gitlab directory. Provide this file with values required to connect to your gitlab instance (gitlab_url is optional, used for self-hosted instances):
```
project_id=112415
auth_token=your_gitlab_token
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:
```bash
export AUTH_TOKEN="your_gitlab_token"
```
By default, the plugin will interact with MRs against a "main" branch. You can configure this by passing in the `base_branch` option to the `.gitlab.nvim` configuration file for your project.
```
project_id=112415
auth_token=your_gitlab_token
gitlab_url=https://my-personal-gitlab-instance.com
base_branch=master
```
## Configuring the Plugin
Here is the default setup function:
```lua
require("gitlab").setup({
port = 20136, -- The port of the Go server, which runs in the background
log_path = vim.fn.stdpath("cache"), -- Log path for the Go server
keymaps = {
popup = { -- The popup for comment creation, editing, and replying
exit = "<Esc>",
perform_action = "<leader>s", -- Once in normal mode, does action
},
discussion_tree = { -- The discussion tree that holds all comments
jump_to_location = "o",
edit_comment = "e",
delete_comment = "dd",
reply_to_comment = "r",
toggle_node = "t",
},
dialogue = { -- The confirmation dialogue for deleting comments
focus_next = { "j", "<Down>", "<Tab>" },
focus_prev = { "k", "<Up>", "<S-Tab>" },
close = { "<Esc>", "<C-c>" },
submit = { "<CR>", "<Space>" },
}
}
})
```
## Usage
First, check out the branch that you want to review locally.
```
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:
```lua
require("gitlab").summary()
```
The `approve` command will approve the merge request for the current branch:
```lua
require("gitlab").approve()
```
The `revoke` command will revoke approval for the merge request for the current branch:
```lua
require("gitlab").revoke()
```
The `comment` command will open up a NUI popover that will allow you to create a Gitlab comment on the current line. To send the comment, use `<leader>s` while the comment popup is open:
```lua
require("gitlab").comment()
```
### Discussions
Gitlab groups threads of notes together into "disucssions." To get a list of all the discussions for the current MR, use the `list_discussions` command. This command will open up a split view of all the comments on the current merge request. You can jump to the comment location by using the `o` key in the tree buffer, and you can reply to a thread by using the `r` keybinding in the tree buffer:
```lua
require("gitlab").list_discussions()
```
Within the discussion tree, there are several functions that you can call, however, it's better to use the keybindings provided in the setup function. If you want to call them manually, they are:
```lua
require("gitlab").delete_comment()
require("gitlab").edit_comment()
require("gitlab").reply()
```
## Keybindings
The plugin does not set up any keybindings outside of these buffers, you need to set them up yourself. Here's what I'm using:
```lua
local gitlab = require("gitlab")
vim.keymap.set("n", "<leader>gls", gitlab.summary)
vim.keymap.set("n", "<leader>glA", gitlab.approve)
vim.keymap.set("n", "<leader>glR", gitlab.revoke)
vim.keymap.set("n", "<leader>glc", gitlab.create_comment)
vim.keymap.set("n", "<leader>gld", gitlab.list_discussions)
```
## Diff Views
This plugin does not provide you with a diff view out of the box for viewing changes. That is already handled by other plugins. I highly recommend using Diffview to see which files have changed in an MR. This is the function that I'm using to accomplish this:
```lua
-- Review changes against develop (will break if no develop branch present)
vim.keymap.set("n", "<leader>gR", function()
local isDiff = vim.fn.getwinvar(nil, "&diff")
local bufName = vim.api.nvim_buf_get_name(0)
if isDiff ~= 0 or u.string_starts(bufName, "diff") then
vim.cmd.tabclose()
vim.cmd.tabprev()
else
vim.cmd.DiffviewOpen("main")
end
end)
```
Which looks like this in my editor:
<img width="1727" alt="Screenshot 2023-04-21 at 6 37 39 PM" src="https://user-images.githubusercontent.com/32515581/233744560-0d718c92-f810-4fde-b40d-8b6f42eb6f0e.png">
This is useful if you plan to leave comments on the diff, because this plugin currently only supports leaving comments on lines that have been added or modified. I'm currenly working on adding functionality to allow users to leave comments on any lines, including those that have been deleted or untouched.
## Debugging
This plugin is built on top of a Golang server. If you want to debug that server, you can run it independently of Neovim. For instance, to start it up in a certain project, navigate to your plugin directory, and build the binary:
```bash
$ cd ~/.local/share/nvim/lazy/gitlab.nvim
$ cd cmd
$ go build -gcflags=all="-N -l" -o bin && cp ./bin ~/path-to-your-project
$ cd ~/path-to-your-project
$ dlv exec ./bin -- 41057709 https://www.gitlab.com 21036 your-gitlab-token
```
You can send JSON to it like you would any other REST server.