Skip to content

Commit e4c9dbe

Browse files
authored
fix: wrong get url (#413)
fix: fixes wrong GET to gitlab when the URL includes namespacing. Fixes #413.
1 parent c8c0395 commit e4c9dbe

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
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+
4345
remote: "[email protected]: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+
5053
remote: "[email protected]: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+
5761
remote: "[email protected]: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+
6469
remote: "[email protected]: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:[email protected]/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:[email protected]/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:[email protected]/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:[email protected]/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:[email protected]/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:[email protected]/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
@@ -6364,6 +6364,25 @@
63646364
],
63656365
"moji": "📦"
63666366
},
6367+
"party": {
6368+
"unicode": "1F389",
6369+
"unicode_alternates": [],
6370+
"name": "party popper as a 'tada' celebration",
6371+
"shortname": ":tada:",
6372+
"category": "people",
6373+
"aliases": [
6374+
":party_popper:"
6375+
],
6376+
"aliases_ascii": [],
6377+
"keywords": [
6378+
"celebrate",
6379+
"celebration",
6380+
"hooray",
6381+
"hurrah",
6382+
"hurray"
6383+
],
6384+
"moji": "🎉"
6385+
},
63676386
"pensive": {
63686387
"unicode": "1F614",
63696388
"unicode_alternates": [],

0 commit comments

Comments
 (0)