Skip to content

Commit 30daecf

Browse files
harrisoncramerjakubbortlikOscarCreator
authored
Release (#415)
fix: parse dates without timezone offset (#404) fix: enable replying if tree is in a different tab (#407) fix: wrong get url (#413) fix: Restore cursor when updating from outside of tree (#406) --------- Co-authored-by: Jakub F. Bortlík <jakub.bortlik@proton.me> Co-authored-by: Oscar <oscar.creator13@gmail.com>
1 parent 341d56a commit 30daecf

File tree

10 files changed

+145
-23
lines changed

10 files changed

+145
-23
lines changed

cmd/app/git/git.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Extracts information about the current repository and returns
3636
it to the client for initialization. The current directory must be a valid
3737
Gitlab project and the branch must be a feature branch
3838
*/
39-
func NewGitData(remote string, g GitManager) (GitData, error) {
39+
func NewGitData(remote string, gitlabUrl string, g GitManager) (GitData, error) {
4040
err := g.RefreshProjectInfo(remote)
4141
if err != nil {
4242
return GitData{}, fmt.Errorf("could not get latest information from remote: %v", err)
@@ -65,7 +65,14 @@ func NewGitData(remote string, g GitManager) (GitData, error) {
6565
return GitData{}, fmt.Errorf("invalid git URL format: %s", url)
6666
}
6767

68-
namespace := matches[1]
68+
// remove part of the hostname from the parsed namespace
69+
url_re := regexp.MustCompile(`[^\/]\/([^\/].*)$`)
70+
url_matches := url_re.FindStringSubmatch(gitlabUrl)
71+
var namespace string = matches[1]
72+
if len(url_matches) == 2 {
73+
namespace = strings.TrimLeft(strings.TrimPrefix(namespace, url_matches[1]), "/")
74+
}
75+
6976
projectName := matches[2]
7077

7178
branchName, err := g.GetCurrentBranchNameFromNativeGitCmd()

cmd/app/git/git_test.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func (f FakeGitManager) GetProjectUrlFromNativeGitCmd(string) (url string, err e
3030

3131
type TestCase struct {
3232
desc string
33+
url string
3334
branch string
3435
projectName string
3536
namespace string
@@ -40,116 +41,156 @@ func TestExtractGitInfo_Success(t *testing.T) {
4041
testCases := []TestCase{
4142
{
4243
desc: "Project configured in SSH under a single folder",
44+
url: "git@custom-gitlab.com",
4345
remote: "git@custom-gitlab.com:namespace-1/project-name.git",
4446
branch: "feature/abc",
4547
projectName: "project-name",
4648
namespace: "namespace-1",
4749
},
4850
{
4951
desc: "Project configured in SSH under a single folder without .git extension",
52+
url: "git@custom-gitlab.com",
5053
remote: "git@custom-gitlab.com:namespace-1/project-name",
5154
branch: "feature/abc",
5255
projectName: "project-name",
5356
namespace: "namespace-1",
5457
},
5558
{
5659
desc: "Project configured in SSH under one nested folder",
60+
url: "git@custom-gitlab.com",
5761
remote: "git@custom-gitlab.com:namespace-1/namespace-2/project-name.git",
5862
branch: "feature/abc",
5963
projectName: "project-name",
6064
namespace: "namespace-1/namespace-2",
6165
},
6266
{
6367
desc: "Project configured in SSH under two nested folders",
68+
url: "git@custom-gitlab.com",
6469
remote: "git@custom-gitlab.com:namespace-1/namespace-2/namespace-3/project-name.git",
6570
branch: "feature/abc",
6671
projectName: "project-name",
6772
namespace: "namespace-1/namespace-2/namespace-3",
6873
},
6974
{
7075
desc: "Project configured in SSH:// under a single folder",
76+
url: "ssh://custom-gitlab.com",
7177
remote: "ssh://custom-gitlab.com/namespace-1/project-name.git",
7278
branch: "feature/abc",
7379
projectName: "project-name",
7480
namespace: "namespace-1",
7581
},
7682
{
7783
desc: "Project configured in SSH:// under a single folder without .git extension",
84+
url: "ssh://custom-gitlab.com",
7885
remote: "ssh://custom-gitlab.com/namespace-1/project-name",
7986
branch: "feature/abc",
8087
projectName: "project-name",
8188
namespace: "namespace-1",
8289
},
8390
{
8491
desc: "Project configured in SSH:// under two nested folders",
92+
url: "ssh://custom-gitlab.com",
8593
remote: "ssh://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
8694
branch: "feature/abc",
8795
projectName: "project-name",
8896
namespace: "namespace-1/namespace-2/namespace-3",
8997
},
9098
{
9199
desc: "Project configured in SSH:// and have a custom port",
100+
url: "ssh://custom-gitlab.com",
92101
remote: "ssh://custom-gitlab.com:2222/namespace-1/project-name",
93102
branch: "feature/abc",
94103
projectName: "project-name",
95104
namespace: "namespace-1",
96105
},
106+
{
107+
desc: "Project configured in SSH:// and have a custom port (with gitlab url namespace)",
108+
url: "ssh://custom-gitlab.com/a",
109+
remote: "ssh://custom-gitlab.com:2222/a/namespace-1/project-name",
110+
branch: "feature/abc",
111+
projectName: "project-name",
112+
namespace: "namespace-1",
113+
},
97114
{
98115
desc: "Project configured in HTTP and under a single folder without .git extension",
116+
url: "http://custom-gitlab.com",
99117
remote: "http://custom-gitlab.com/namespace-1/project-name",
100118
branch: "feature/abc",
101119
projectName: "project-name",
102120
namespace: "namespace-1",
103121
},
104122
{
105123
desc: "Project configured in HTTP and under a single folder without .git extension (with embedded credentials)",
124+
url: "http://custom-gitlab.com",
106125
remote: "http://username:password@custom-gitlab.com/namespace-1/project-name",
107126
branch: "feature/abc",
108127
projectName: "project-name",
109128
namespace: "namespace-1",
110129
},
111130
{
112131
desc: "Project configured in HTTPS and under a single folder",
132+
url: "https://custom-gitlab.com",
113133
remote: "https://custom-gitlab.com/namespace-1/project-name.git",
114134
branch: "feature/abc",
115135
projectName: "project-name",
116136
namespace: "namespace-1",
117137
},
118138
{
119139
desc: "Project configured in HTTPS and under a single folder (with embedded credentials)",
140+
url: "https://custom-gitlab.com",
120141
remote: "https://username:password@custom-gitlab.com/namespace-1/project-name.git",
121142
branch: "feature/abc",
122143
projectName: "project-name",
123144
namespace: "namespace-1",
124145
},
125146
{
126147
desc: "Project configured in HTTPS and under a nested folder",
148+
url: "https://custom-gitlab.com",
127149
remote: "https://custom-gitlab.com/namespace-1/namespace-2/project-name.git",
128150
branch: "feature/abc",
129151
projectName: "project-name",
130152
namespace: "namespace-1/namespace-2",
131153
},
132154
{
133155
desc: "Project configured in HTTPS and under a nested folder (with embedded credentials)",
156+
url: "https://custom-gitlab.com",
134157
remote: "https://username:password@custom-gitlab.com/namespace-1/namespace-2/project-name.git",
135158
branch: "feature/abc",
136159
projectName: "project-name",
137160
namespace: "namespace-1/namespace-2",
138161
},
139162
{
140163
desc: "Project configured in HTTPS and under two nested folders",
164+
url: "https://custom-gitlab.com",
141165
remote: "https://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
142166
branch: "feature/abc",
143167
projectName: "project-name",
144168
namespace: "namespace-1/namespace-2/namespace-3",
145169
},
146170
{
147171
desc: "Project configured in HTTPS and under two nested folders (with embedded credentials)",
172+
url: "https://custom-gitlab.com",
148173
remote: "https://username:password@custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
149174
branch: "feature/abc",
150175
projectName: "project-name",
151176
namespace: "namespace-1/namespace-2/namespace-3",
152177
},
178+
{
179+
desc: "Project configured in HTTPS and under one nested folders (with gitlab url namespace)",
180+
url: "https://custom-gitlab.com/gitlab",
181+
remote: "https://username:password@custom-gitlab.com/gitlab/namespace-2/namespace-3/project-name.git",
182+
branch: "feature/abc",
183+
projectName: "project-name",
184+
namespace: "namespace-2/namespace-3",
185+
},
186+
{
187+
desc: "Project configured in HTTPS and under one nested folders (with gitlab url namespace + extra slash)",
188+
url: "https://custom-gitlab.com/gitlab/",
189+
remote: "https://username:password@custom-gitlab.com/gitlab/namespace-2/namespace-3/project-name.git",
190+
branch: "feature/abc",
191+
projectName: "project-name",
192+
namespace: "namespace-2/namespace-3",
193+
},
153194
}
154195
for _, tC := range testCases {
155196
t.Run(tC.desc, func(t *testing.T) {
@@ -159,7 +200,7 @@ func TestExtractGitInfo_Success(t *testing.T) {
159200
BranchName: tC.branch,
160201
RemoteUrl: tC.remote,
161202
}
162-
data, err := NewGitData(tC.remote, g)
203+
data, err := NewGitData(tC.remote, tC.url, g)
163204
if err != nil {
164205
t.Errorf("No error was expected, got %s", err)
165206
}
@@ -204,7 +245,7 @@ func TestExtractGitInfo_FailToGetProjectRemoteUrl(t *testing.T) {
204245
g := failingUrlManager{
205246
errMsg: tC.errMsg,
206247
}
207-
_, err := NewGitData("", g)
248+
_, err := NewGitData("", "", g)
208249
if err == nil {
209250
t.Errorf("Expected an error, got none")
210251
}
@@ -236,7 +277,7 @@ func TestExtractGitInfo_FailToGetCurrentBranchName(t *testing.T) {
236277
},
237278
errMsg: tC.errMsg,
238279
}
239-
_, err := NewGitData("", g)
280+
_, err := NewGitData("", "", g)
240281
if err == nil {
241282
t.Errorf("Expected an error, got none")
242283
}

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
}
2727

2828
gitManager := git.Git{}
29-
gitData, err := git.NewGitData(pluginOptions.ConnectionSettings.Remote, gitManager)
29+
gitData, err := git.NewGitData(pluginOptions.ConnectionSettings.Remote, pluginOptions.GitlabUrl, gitManager)
3030

3131
if err != nil {
3232
log.Fatalf("Failure initializing plugin: %v", err)

config/emojis.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9661,6 +9661,25 @@
96619661
],
96629662
"moji": "😅"
96639663
},
9664+
"tada": {
9665+
"unicode": "1F389",
9666+
"unicode_alternates": [],
9667+
"name": "party popper as a 'tada' celebration",
9668+
"shortname": ":tada:",
9669+
"category": "people",
9670+
"aliases": [
9671+
":party_popper:"
9672+
],
9673+
"aliases_ascii": [],
9674+
"keywords": [
9675+
"celebrate",
9676+
"celebration",
9677+
"hooray",
9678+
"hurrah",
9679+
"hurray"
9680+
],
9681+
"moji": "🎉"
9682+
},
96649683
"thermometer_face": {
96659684
"unicode": "1F912",
96669685
"unicode_alternates": [],

lua/gitlab/actions/comment.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
4141
local body = { discussion_id = discussion_id, reply = text, draft = is_draft }
4242
job.run_job("/mr/reply", "POST", body, function()
4343
u.notify("Sent reply!", vim.log.levels.INFO)
44-
if is_draft then
45-
draft_notes.load_draft_notes(function()
46-
discussions.rebuild_view(unlinked)
47-
end)
48-
else
49-
discussions.rebuild_view(unlinked)
50-
end
44+
discussions.rebuild_view(unlinked)
5145
end)
5246
return
5347
end
@@ -69,8 +63,6 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
6963
return
7064
end
7165

72-
vim.print("Here: ", unlinked, discussion_id)
73-
7466
local reviewer_data = reviewer.get_reviewer_data()
7567
if reviewer_data == nil then
7668
u.notify("Error getting reviewer data", vim.log.levels.ERROR)
@@ -102,7 +94,7 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
10294
job.run_job("/mr/draft_notes/", "POST", body, function()
10395
u.notify("Draft reply created!", vim.log.levels.INFO)
10496
draft_notes.load_draft_notes(function()
105-
discussions.rebuild_view(false, true)
97+
discussions.rebuild_view(unlinked)
10698
end)
10799
end)
108100
return
@@ -166,7 +158,7 @@ end
166158
---@param opts LayoutOpts
167159
---@return NuiLayout|nil
168160
M.create_comment_layout = function(opts)
169-
if opts.unlinked ~= true then
161+
if opts.unlinked ~= true and opts.discussion_id == nil then
170162
-- Check that diffview is initialized
171163
if reviewer.tabnr == nil then
172164
u.notify("Reviewer must be initialized first", vim.log.levels.ERROR)

lua/gitlab/actions/discussions/init.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ M.open = function(callback)
139139
local current_window = vim.api.nvim_get_current_win() -- Save user's current window in case they switched while content was loading
140140
vim.api.nvim_set_current_win(M.split.winid)
141141

142-
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unliked_bufnr)
142+
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
143143
M.rebuild_discussion_tree()
144144
M.rebuild_unlinked_discussion_tree()
145145

@@ -432,6 +432,9 @@ M.rebuild_discussion_tree = function()
432432
if M.linked_bufnr == nil then
433433
return
434434
end
435+
436+
local current_node = discussions_tree.get_node_at_cursor(M.discussion_tree, M.last_node_at_cursor)
437+
435438
local expanded_node_ids = M.gather_expanded_node_ids(M.discussion_tree)
436439
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
437440

@@ -447,12 +450,14 @@ M.rebuild_discussion_tree = function()
447450
bufnr = M.linked_bufnr,
448451
prepare_node = tree_utils.nui_tree_prepare_node,
449452
})
453+
450454
-- Re-expand already expanded nodes
451455
for _, id in ipairs(expanded_node_ids) do
452456
tree_utils.open_node_by_id(discussion_tree, id)
453457
end
454-
455458
discussion_tree:render()
459+
discussions_tree.restore_cursor_position(M.split.winid, discussion_tree, current_node)
460+
456461
M.set_tree_keymaps(discussion_tree, M.linked_bufnr, false)
457462
M.discussion_tree = discussion_tree
458463
common.switch_can_edit_bufs(false, M.linked_bufnr, M.unlinked_bufnr)
@@ -466,6 +471,9 @@ M.rebuild_unlinked_discussion_tree = function()
466471
if M.unlinked_bufnr == nil then
467472
return
468473
end
474+
475+
local current_node = discussions_tree.get_node_at_cursor(M.unlinked_discussion_tree, M.last_node_at_cursor)
476+
469477
local expanded_node_ids = M.gather_expanded_node_ids(M.unlinked_discussion_tree)
470478
common.switch_can_edit_bufs(true, M.linked_bufnr, M.unlinked_bufnr)
471479
vim.api.nvim_buf_set_lines(M.unlinked_bufnr, 0, -1, false, {})
@@ -487,6 +495,7 @@ M.rebuild_unlinked_discussion_tree = function()
487495
tree_utils.open_node_by_id(unlinked_discussion_tree, id)
488496
end
489497
unlinked_discussion_tree:render()
498+
discussions_tree.restore_cursor_position(M.split.winid, unlinked_discussion_tree, current_node)
490499

491500
M.set_tree_keymaps(unlinked_discussion_tree, M.unlinked_bufnr, true)
492501
M.unlinked_discussion_tree = unlinked_discussion_tree
@@ -535,6 +544,14 @@ M.create_split_and_bufs = function()
535544
buffer = linked_bufnr,
536545
callback = function()
537546
M.last_row, M.last_column = unpack(vim.api.nvim_win_get_cursor(0))
547+
M.last_node_at_cursor = M.discussion_tree and M.discussion_tree:get_node() or nil
548+
end,
549+
})
550+
551+
vim.api.nvim_create_autocmd("WinLeave", {
552+
buffer = unlinked_bufnr,
553+
callback = function()
554+
M.last_node_at_cursor = M.unlinked_discussion_tree and M.unlinked_discussion_tree:get_node() or nil
538555
end,
539556
})
540557

0 commit comments

Comments
 (0)