Skip to content

Commit fa16905

Browse files
feat: wrap CLI commands, toggle draft/live mode (#280)
feat: better error checking in git commands feat: allow toggling between live mode and draft mode, adds "D" keybinding to discussion tree
1 parent 681c29e commit fa16905

File tree

15 files changed

+153
-81
lines changed

15 files changed

+153
-81
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ gitlab_url=https://my-personal-gitlab-instance.com/
9393
The plugin will look for the `.gitlab.nvim` file in the root of the current project by default. However, you may provide a custom path to the configuration file via the `config_path` option. This must be an absolute path to the directory that holds your `.gitlab.nvim` file.
9494

9595
In case even more control over the auth config is needed, there is the possibility to override the `auth_provider` settings field. It should be
96-
a function that returns the `token` as well as the `gitlab_url` value. If the `gitlab_url` is `nil`, `https://gitlab.com` is used as default.
96+
a function that returns the `token` as well as the `gitlab_url` value, and a nilable error. If the `gitlab_url` is `nil`, `https://gitlab.com` is used as default.
9797

9898
Here an example how to use a custom `auth_provider`:
9999
```lua
100100
require("gitlab").setup({
101101
auth_provider = function()
102-
return "my_token", "https://custom.gitlab.instance.url"
102+
return "my_token", "https://custom.gitlab.instance.url", nil
103103
end,
104104
}
105105
```
@@ -176,12 +176,11 @@ require("gitlab").setup({
176176
unresolved = '-', -- Symbol to show next to unresolved discussions
177177
tree_type = "simple", -- Type of discussion tree - "simple" means just list of discussions, "by_file_name" means file tree with discussions under file
178178
toggle_tree_type = "i", -- Toggle type of discussion tree - "simple", or "by_file_name"
179+
draft_mode = false, -- Whether comments are posted as drafts as part of a review
180+
toggle_draft_mode = "D" -- Toggle between draft mode (comments posted as drafts) and live mode (comments are posted immediately)
179181
winbar = nil -- Custom function to return winbar title, should return a string. Provided with WinbarTable (defined in annotations.lua)
180182
-- If using lualine, please add "gitlab" to disabled file types, otherwise you will not see the winbar.
181183
},
182-
comments = {
183-
default_to_draft = false, -- Whether to default a comment to a "draft" or not in the popup
184-
},
185184
choose_merge_request = {
186185
open_reviewer = true, -- Open the reviewer window automatically after switching merge requests
187186
},

cmd/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
)
66

77
func main() {
8+
log.SetFlags(0)
89
gitInfo, err := extractGitInfo(RefreshProjectInfo, GetProjectUrlFromNativeGitCmd, GetCurrentBranchNameFromNativeGitCmd)
910
if err != nil {
10-
log.Fatalf("Failure initializing plugin with `git` commands: %v", err)
11+
log.Fatalf("Failure initializing plugin: %v", err)
1112
}
1213

1314
err, client := initGitlabClient()

doc/gitlab.nvim.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ configure your connection to Gitlab.
124124

125125
In case even more control over the auth config is needed, there is the
126126
possibility to override the `auth_provider` settings field. It should be
127-
a function that returns the `token` as well as the `gitlab_url` value.
127+
a function that returns the `token` as well as the `gitlab_url` value and
128+
a nilable error value.
129+
128130
If the `gitlab_url` is `nil`, `https://gitlab.com` is used as default.
129131

130132
Here an example how to use a custom `auth_provider`:
131133
>lua
132134
require("gitlab").setup({
133135
auth_provider = function()
134-
return "my_token", "https://custom.gitlab.instance.url"
136+
return "my_token", "https://custom.gitlab.instance.url", nil
135137
end,
136138
}
137139
<
@@ -199,12 +201,11 @@ you call this function with no values the defaults will be used:
199201
unresolved = '-', -- Symbol to show next to unresolved discussions
200202
tree_type = "simple", -- Type of discussion tree - "simple" means just list of discussions, "by_file_name" means file tree with discussions under file
201203
toggle_tree_type = "i", -- Toggle type of discussion tree - "simple", or "by_file_name"
204+
draft_mode = false, -- Whether comments are posted as drafts as part of a review
205+
toggle_draft_mode = "D" -- Toggle between draft mode and regular mode, where comments are posted immediately
202206
winbar = nil -- Custom function to return winbar title, should return a string. Provided with WinbarTable (defined in annotations.lua)
203207
-- If using lualine, please add "gitlab" to disabled file types, otherwise you will not see the winbar.
204208
},
205-
comments = {
206-
default_to_draft = false, -- Whether to default a comment to a "draft" or not in the popup
207-
},
208209
choose_merge_request = {
209210
open_reviewer = true, -- Open the reviewer window automatically after switching merge requests
210211
},

lua/gitlab/actions/comment.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ local function create_comment_layout(opts)
140140
end, miscellaneous.attach_file, popup_opts)
141141

142142
vim.schedule(function()
143-
local default_to_draft = state.settings.comments.default_to_draft
144-
vim.api.nvim_buf_set_lines(M.draft_popup.bufnr, 0, -1, false, { u.bool_to_string(default_to_draft) })
143+
local draft_mode = state.settings.discussion_tree.draft_mode
144+
vim.api.nvim_buf_set_lines(M.draft_popup.bufnr, 0, -1, false, { u.bool_to_string(draft_mode) })
145145
end)
146146

147147
return layout
@@ -150,7 +150,10 @@ end
150150
--- This function will open a comment popup in order to create a comment on the changed/updated
151151
--- line in the current MR
152152
M.create_comment = function()
153-
local has_clean_tree = git.has_clean_tree()
153+
local has_clean_tree, err = git.has_clean_tree()
154+
if err ~= nil then
155+
return
156+
end
154157
local is_modified = vim.api.nvim_buf_get_option(0, "modified")
155158
if state.settings.reviewer_settings.diffview.imply_local and (is_modified or not has_clean_tree) then
156159
u.notify(

lua/gitlab/actions/common.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ end
3232
---@param content string
3333
---@return table
3434
M.build_content = function(content)
35-
local description_lines = {}
36-
for line in u.split_by_new_lines(content) do
37-
table.insert(description_lines, line)
38-
end
35+
local description_lines = u.lines_into_table(content)
3936
table.insert(description_lines, "")
4037
return description_lines
4138
end

lua/gitlab/actions/create_mr.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ end
4343
--- continue working on it.
4444
---@param args? Mr
4545
M.start = function(args)
46-
if not git.current_branch_up_to_date_on_remote("ERROR") then
46+
if not git.current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
4747
return
4848
end
4949

@@ -87,7 +87,10 @@ M.pick_target = function(mr)
8787
end
8888

8989
local function make_template_path(t)
90-
local base_dir = git.base_dir()
90+
local base_dir, err = git.base_dir()
91+
if err ~= nil then
92+
return
93+
end
9194
return base_dir
9295
.. state.settings.file_separator
9396
.. ".gitlab"

lua/gitlab/actions/discussions/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,10 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
528528
M.delete_comment(tree)
529529
end
530530
end, { buffer = bufnr, desc = "Delete comment" })
531+
vim.keymap.set("n", state.settings.discussion_tree.toggle_draft_mode, function()
532+
state.settings.discussion_tree.draft_mode = not state.settings.discussion_tree.draft_mode
533+
winbar.update_winbar()
534+
end, { buffer = bufnr, desc = "Toggle between draft mode and live mode" })
531535
vim.keymap.set("n", state.settings.discussion_tree.toggle_resolved, function()
532536
if M.is_current_node_note(tree) and not M.is_draft_note(tree) then
533537
M.toggle_discussion_resolved(tree)

lua/gitlab/actions/discussions/winbar.lua

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,32 @@ M.make_winbar = function(t)
125125
notes_title = "%#Text#" .. notes_title
126126
end
127127

128+
local mode = M.get_mode()
129+
128130
-- Join everything together and return it
129131
local separator = "%#Comment#|"
130-
local help = "%#Comment#%=Help: " .. t.help_keymap:gsub(" ", "<space>") .. " "
131-
return string.format(" %s %s %s %s", discussion_title, separator, notes_title, help)
132+
local end_section = "%="
133+
local help = "%#Comment#Help: " .. t.help_keymap:gsub(" ", "<space>") .. " "
134+
return string.format(
135+
" %s %s %s %s %s %s %s",
136+
discussion_title,
137+
separator,
138+
notes_title,
139+
end_section,
140+
mode,
141+
separator,
142+
help
143+
)
144+
end
145+
146+
---Returns a string for the winbar indicating the mode type, live or draft
147+
---@return string
148+
M.get_mode = function()
149+
if state.settings.discussion_tree.draft_mode then
150+
return "%#DiagnosticWarn#Draft Mode"
151+
else
152+
return "%#DiagnosticOK#Live Mode"
153+
end
132154
end
133155

134156
---Sets the current view type (if provided an argument)

lua/gitlab/actions/merge_requests.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ local M = {}
1010
---Opens up a select menu that lets you choose a different merge request.
1111
---@param opts SwitchOpts|nil
1212
M.choose_merge_request = function(opts)
13-
if not git.has_clean_tree() then
13+
local has_clean_tree, clean_tree_err = git.has_clean_tree()
14+
if clean_tree_err ~= nil then
15+
return
16+
elseif has_clean_tree ~= "" then
1417
u.notify("Your local branch has changes, please stash or commit and push", vim.log.levels.ERROR)
1518
return
1619
end
@@ -34,9 +37,8 @@ M.choose_merge_request = function(opts)
3437
end
3538

3639
vim.schedule(function()
37-
local err = git.switch_branch(choice.source_branch)
38-
if err ~= "" then
39-
u.notify(err, vim.log.levels.ERROR)
40+
local _, branch_switch_err = git.switch_branch(choice.source_branch)
41+
if branch_switch_err ~= nil then
4042
return
4143
end
4244

lua/gitlab/actions/pipeline.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ M.see_logs = function()
143143
return
144144
end
145145

146-
local lines = {}
147-
for line in u.split_by_new_lines(file) do
148-
table.insert(lines, line)
149-
end
146+
local lines = u.lines_into_table(file)
150147

151148
if #lines == 0 then
152149
u.notify("Log trace lines could not be parsed", vim.log.levels.ERROR)

0 commit comments

Comments
 (0)