/
gitlab_test.go
604 lines (535 loc) · 15.3 KB
/
gitlab_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestGitLabReleaseURLTemplate(t *testing.T) {
repo := config.Repo{
Owner: "owner",
Name: "name",
}
tests := []struct {
name string
repo config.Repo
downloadURL string
wantDownloadURL string
wantErr bool
}{
{
name: "default_download_url",
downloadURL: DefaultGitLabDownloadURL,
repo: repo,
wantDownloadURL: "https://gitlab.com/owner/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
},
{
name: "default_download_url_no_owner",
downloadURL: DefaultGitLabDownloadURL,
repo: config.Repo{Name: "name"},
wantDownloadURL: "https://gitlab.com/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
},
{
name: "download_url_template",
repo: repo,
downloadURL: "{{ .Env.GORELEASER_TEST_GITLAB_URLS_DOWNLOAD }}",
wantDownloadURL: "https://gitlab.mycompany.com/owner/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
},
{
name: "download_url_template_invalid_value",
downloadURL: "{{ .Eenv.GORELEASER_NOT_EXISTS }}",
wantErr: true,
},
{
name: "download_url_template_invalid",
downloadURL: "{{.dddddddddd",
wantErr: true,
},
{
name: "download_url_string",
downloadURL: "https://gitlab.mycompany.com",
wantDownloadURL: "https://gitlab.mycompany.com/",
},
}
for _, tt := range tests {
ctx := context.New(config.Project{
Env: []string{
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
},
GitLabURLs: config.GitLabURLs{
Download: tt.downloadURL,
},
Release: config.Release{
GitLab: tt.repo,
},
})
client, err := NewGitLab(ctx, ctx.Token)
require.NoError(t, err)
urlTpl, err := client.ReleaseURLTemplate(ctx)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantDownloadURL, urlTpl)
}
}
func TestGitLabURLsAPITemplate(t *testing.T) {
tests := []struct {
name string
apiURL string
wantHost string
}{
{
name: "default_values",
wantHost: "gitlab.com",
},
{
name: "speicifed_api_env_key",
apiURL: "https://gitlab.mycompany.com",
wantHost: "gitlab.mycompany.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
envs := []string{}
gitlabURLs := config.GitLabURLs{}
if tt.apiURL != "" {
envs = append(envs, fmt.Sprintf("GORELEASER_TEST_GITLAB_URLS_API=%s", tt.apiURL))
gitlabURLs.API = "{{ .Env.GORELEASER_TEST_GITLAB_URLS_API }}"
}
ctx := context.New(config.Project{
Env: envs,
GitLabURLs: gitlabURLs,
})
client, err := NewGitLab(ctx, ctx.Token)
require.NoError(t, err)
gitlabClient, ok := client.(*gitlabClient)
require.True(t, ok)
require.Equal(t, tt.wantHost, gitlabClient.client.BaseURL().Host)
})
}
t.Run("no_env_specified", func(t *testing.T) {
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
})
_, err := NewGitLab(ctx, ctx.Token)
require.ErrorAs(t, err, &template.ExecError{})
})
t.Run("invalid_template", func(t *testing.T) {
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: "{{.dddddddddd",
},
})
_, err := NewGitLab(ctx, ctx.Token)
require.Error(t, err)
})
}
func TestGitLabURLsDownloadTemplate(t *testing.T) {
tests := []struct {
name string
usePackageRegistry bool
downloadURL string
wantURL string
wantErr bool
}{
{
name: "empty_download_url",
wantURL: "/",
},
{
name: "download_url_template",
downloadURL: "{{ .Env.GORELEASER_TEST_GITLAB_URLS_DOWNLOAD }}",
wantURL: "https://gitlab.mycompany.com/",
},
{
name: "download_url_template_invalid_value",
downloadURL: "{{ .Eenv.GORELEASER_NOT_EXISTS }}",
wantErr: true,
},
{
name: "download_url_template_invalid",
downloadURL: "{{.dddddddddd",
wantErr: true,
},
{
name: "download_url_string",
downloadURL: "https://gitlab.mycompany.com",
wantURL: "https://gitlab.mycompany.com/",
},
{
name: "url_registry",
wantURL: "/api/v4/projects/test%2Ftest/packages/generic/projectname/v1%2E0%2E0/test",
usePackageRegistry: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer fmt.Fprint(w, "{}")
defer w.WriteHeader(http.StatusOK)
defer r.Body.Close()
if !strings.Contains(r.URL.Path, "assets/links") {
_, _ = io.Copy(io.Discard, r.Body)
return
}
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
reqBody := map[string]interface{}{}
err = json.Unmarshal(b, &reqBody)
require.NoError(t, err)
url := reqBody["url"].(string)
require.Truef(t, strings.HasSuffix(url, tt.wantURL), "expected %q to end with %q", url, tt.wantURL)
}))
defer srv.Close()
ctx := context.New(config.Project{
ProjectName: "projectname",
Env: []string{
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
},
Release: config.Release{
GitLab: config.Repo{
Owner: "test",
Name: "test",
},
},
GitLabURLs: config.GitLabURLs{
API: srv.URL,
Download: tt.downloadURL,
UsePackageRegistry: tt.usePackageRegistry,
},
})
ctx.Version = "v1.0.0"
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)
client, err := NewGitLab(ctx, ctx.Token)
require.NoError(t, err)
err = client.Upload(ctx, "1234", &artifact.Artifact{Name: "test", Path: "some-path"}, tmpFile)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestGitLabCreateReleaseUknownHost(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
GitLab: config.Repo{
Owner: "owner",
Name: "name",
},
},
GitLabURLs: config.GitLabURLs{
API: "http://goreleaser-notexists",
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
_, err = client.CreateRelease(ctx, "body")
require.Error(t, err)
}
func TestGitLabCreateReleaseReleaseNotExists(t *testing.T) {
notExistsStatusCodes := []int{http.StatusNotFound, http.StatusForbidden}
for _, tt := range notExistsStatusCodes {
t.Run(strconv.Itoa(tt), func(t *testing.T) {
totalRequests := 0
createdRelease := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
totalRequests++
if !strings.Contains(r.URL.Path, "releases") {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
return
}
// Check if release exists
if r.Method == http.MethodGet {
w.WriteHeader(tt)
fmt.Fprint(w, "{}")
return
}
// Create release if it doens't exists
if r.Method == http.MethodPost {
createdRelease = true
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
return
}
require.FailNow(t, "should not reach here")
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
_, err = client.CreateRelease(ctx, "body")
require.NoError(t, err)
require.True(t, createdRelease)
require.Equal(t, 3, totalRequests)
})
}
}
func TestGitLabCreateReleaseUnkownHTTPError(t *testing.T) {
totalRequests := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
totalRequests++
defer r.Body.Close()
w.WriteHeader(http.StatusUnprocessableEntity)
fmt.Fprint(w, "{}")
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
_, err = client.CreateRelease(ctx, "body")
require.Error(t, err)
require.Equal(t, 2, totalRequests)
}
func TestGitlabGetDefaultBranch(t *testing.T) {
totalRequests := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
totalRequests++
defer r.Body.Close()
// Assume the request to create a branch was good
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
repo := Repo{
Owner: "someone",
Name: "something",
Branch: "somebranch",
}
_, err = client.GetDefaultBranch(ctx, repo)
require.NoError(t, err)
require.Equal(t, 2, totalRequests)
}
func TestGitlabGetDefaultBranchErr(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// Assume the request to create a branch was good
w.WriteHeader(http.StatusNotImplemented)
fmt.Fprint(w, "{}")
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
repo := Repo{
Owner: "someone",
Name: "something",
Branch: "somebranch",
}
_, err = client.GetDefaultBranch(ctx, repo)
require.Error(t, err)
}
func TestGitlabChangelog(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/compare") {
r, err := os.Open("testdata/gitlab/compare.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
require.NoError(t, err)
return
}
defer r.Body.Close()
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
repo := Repo{
Owner: "someone",
Name: "something",
Branch: "somebranch",
}
log, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
require.NoError(t, err)
require.Equal(t, "6dcb09b5: Fix all the bugs (Joey User <joey@user.edu>)", log)
}
func TestGitlabCreateFile(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Handle the test where we know the branch
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/newfile.txt") {
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "newfile.txt", "branch": "somebranch" }`))
require.NoError(t, err)
return
}
// Handle the test where we detect the branch
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/newfile-in-default.txt") {
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "newfile.txt", "branch": "main" }`))
require.NoError(t, err)
return
}
// File of doooom...gets created, but 404s when getting fetched
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/doomed-file-404.txt") {
if r.Method == "PUT" {
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "doomed-file-404.txt", "branch": "main" }`))
require.NoError(t, err)
} else {
w.WriteHeader(http.StatusNotFound)
}
return
}
defer r.Body.Close()
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
// Test using an arbitrary branch
repo := Repo{
Owner: "someone",
Name: "something",
Branch: "somebranch",
}
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "newfile.txt", "test: test commit")
require.NoError(t, err)
// Test detecting the default branch
repo = Repo{
Owner: "someone",
Name: "something",
// Note there is no branch here, gonna try and guess it!
}
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "newfile-in-default.txt", "test: test commit")
require.NoError(t, err)
// Test a doomed file. This is a file that is 'successfully' created, but returns a 404 when trying to fetch
repo = Repo{
Owner: "someone",
Name: "something",
Branch: "doomed",
}
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "doomed-file-404.txt", "test: test commit")
require.Error(t, err)
}
func TestCloseMileston(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "projects/someone/something/milestones") {
r, err := os.Open("testdata/gitlab/milestones.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
require.NoError(t, err)
return
} else if strings.HasSuffix(r.URL.Path, "projects/someone/something/milestones/12") {
r, err := os.Open("testdata/gitlab/milestone.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
require.NoError(t, err)
return
}
defer r.Body.Close()
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
repo := Repo{
Owner: "someone",
Name: "something",
}
err = client.CloseMilestone(ctx, repo, "10.0")
require.NoError(t, err)
// Be sure to error on missing milestones
err = client.CloseMilestone(ctx, repo, "never-will-exist")
require.Error(t, err)
}
func TestCheckUseJobToken(t *testing.T) {
tests := []struct {
useJobToken bool
token string
ciToken string
want bool
desc string
name string
}{
{
useJobToken: true,
token: "real-ci-token",
ciToken: "real-ci-token",
desc: "token and CI_JOB_TOKEN match so should return true",
want: true,
name: "UseJobToken-tokens-equal",
},
{
useJobToken: true,
token: "some-random-token",
ciToken: "real-ci-token",
desc: "token and CI_JOB_TOKEN do NOT match so should return false",
want: false,
name: "UseJobToken-tokens-diff",
},
{
useJobToken: false,
token: "real-ci-token",
ciToken: "real-ci-token",
desc: "token and CI_JOB_TOKEN match, however UseJobToken is set to false, so return false",
want: false,
name: "NoUseJobToken-tokens-equal",
},
{
useJobToken: false,
token: "real-ci-token",
ciToken: "real-ci-token",
desc: "token and CI_JOB_TOKEN do not match, and UseJobToken is set to false, should return false",
want: false,
name: "NoUseJobToken-tokens-diff",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("CI_JOB_TOKEN", tt.ciToken)
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
UseJobToken: tt.useJobToken,
},
})
got := checkUseJobToken(ctx, tt.token)
require.Equal(t, tt.want, got, tt.desc)
})
}
}