2019-08-13 20:28:03 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-09-09 03:42:13 +02:00
|
|
|
"encoding/json"
|
2024-10-11 17:03:13 +02:00
|
|
|
"errors"
|
2021-09-09 03:42:13 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2021-09-10 21:35:42 +02:00
|
|
|
"strconv"
|
2021-09-09 03:42:13 +02:00
|
|
|
"strings"
|
2019-08-13 20:28:03 +02:00
|
|
|
"testing"
|
2021-09-09 03:42:13 +02:00
|
|
|
"text/template"
|
2019-08-13 20:28:03 +02:00
|
|
|
|
2024-05-26 20:02:57 +02:00
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/artifact"
|
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/testctx"
|
|
|
|
"github.com/goreleaser/goreleaser/v2/pkg/config"
|
2024-08-30 22:03:10 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-04-24 03:49:18 +02:00
|
|
|
"github.com/xanzy/go-gitlab"
|
2019-08-13 20:28:03 +02:00
|
|
|
)
|
|
|
|
|
2020-07-06 15:48:17 +02:00
|
|
|
func TestGitLabReleaseURLTemplate(t *testing.T) {
|
2021-10-06 03:06:20 +02:00
|
|
|
repo := config.Repo{
|
|
|
|
Owner: "owner",
|
|
|
|
Name: "name",
|
|
|
|
}
|
2021-09-09 03:42:13 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-10-06 03:06:20 +02:00
|
|
|
repo config.Repo
|
2021-09-09 03:42:13 +02:00
|
|
|
downloadURL string
|
|
|
|
wantDownloadURL string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default_download_url",
|
|
|
|
downloadURL: DefaultGitLabDownloadURL,
|
2021-10-06 03:06:20 +02:00
|
|
|
repo: repo,
|
2021-09-09 03:42:13 +02:00
|
|
|
wantDownloadURL: "https://gitlab.com/owner/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
|
|
|
|
},
|
2021-10-06 03:06:20 +02:00
|
|
|
{
|
|
|
|
name: "default_download_url_no_owner",
|
|
|
|
downloadURL: DefaultGitLabDownloadURL,
|
|
|
|
repo: config.Repo{Name: "name"},
|
|
|
|
wantDownloadURL: "https://gitlab.com/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
|
|
|
|
},
|
2021-09-09 03:42:13 +02:00
|
|
|
{
|
|
|
|
name: "download_url_template",
|
2021-10-06 03:06:20 +02:00
|
|
|
repo: repo,
|
2021-09-09 03:42:13 +02:00
|
|
|
downloadURL: "{{ .Env.GORELEASER_TEST_GITLAB_URLS_DOWNLOAD }}",
|
|
|
|
wantDownloadURL: "https://gitlab.mycompany.com/owner/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "download_url_template_invalid_value",
|
2021-10-06 03:06:20 +02:00
|
|
|
downloadURL: "{{ .Eenv.GORELEASER_NOT_EXISTS }}",
|
2021-09-09 03:42:13 +02:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "download_url_template_invalid",
|
|
|
|
downloadURL: "{{.dddddddddd",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-10-06 03:06:20 +02:00
|
|
|
{
|
|
|
|
name: "download_url_string",
|
|
|
|
downloadURL: "https://gitlab.mycompany.com",
|
|
|
|
wantDownloadURL: "https://gitlab.mycompany.com/",
|
|
|
|
},
|
2021-09-09 03:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-09 03:42:13 +02:00
|
|
|
Env: []string{
|
|
|
|
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
|
2020-07-06 15:48:17 +02:00
|
|
|
},
|
2021-09-09 03:42:13 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
Download: tt.downloadURL,
|
|
|
|
},
|
|
|
|
Release: config.Release{
|
2021-10-06 03:06:20 +02:00
|
|
|
GitLab: tt.repo,
|
2021-09-09 03:42:13 +02:00
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, ctx.Token)
|
2021-09-09 03:42:13 +02:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2024-11-06 01:28:16 +02:00
|
|
|
name: "specified_api_env_key",
|
2021-09-09 03:42:13 +02:00
|
|
|
apiURL: "https://gitlab.mycompany.com",
|
|
|
|
wantHost: "gitlab.mycompany.com",
|
2020-07-06 15:48:17 +02:00
|
|
|
},
|
2021-09-09 03:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 }}"
|
|
|
|
}
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-09 03:42:13 +02:00
|
|
|
Env: envs,
|
|
|
|
GitLabURLs: gitlabURLs,
|
|
|
|
})
|
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, ctx.Token)
|
2021-09-09 03:42:13 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-07 03:58:06 +02:00
|
|
|
require.Equal(t, tt.wantHost, client.client.BaseURL().Host)
|
2021-09-09 03:42:13 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no_env_specified", func(t *testing.T) {
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-09 03:42:13 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
_, err := newGitLab(ctx, ctx.Token)
|
2021-09-09 03:42:13 +02:00
|
|
|
require.ErrorAs(t, err, &template.ExecError{})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid_template", func(t *testing.T) {
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-09 03:42:13 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: "{{.dddddddddd",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
_, err := newGitLab(ctx, ctx.Token)
|
2021-09-09 03:42:13 +02:00
|
|
|
require.Error(t, err)
|
2020-07-06 15:48:17 +02:00
|
|
|
})
|
2021-09-09 03:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGitLabURLsDownloadTemplate(t *testing.T) {
|
|
|
|
tests := []struct {
|
2022-01-10 15:58:05 +02:00
|
|
|
name string
|
|
|
|
usePackageRegistry bool
|
|
|
|
downloadURL string
|
|
|
|
wantURL string
|
|
|
|
wantErr bool
|
2021-09-09 03:42:13 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
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/",
|
|
|
|
},
|
2022-01-10 15:58:05 +02:00
|
|
|
{
|
|
|
|
name: "url_registry",
|
2023-03-02 05:01:11 +02:00
|
|
|
wantURL: "/api/v4/projects/test%2Ftest/packages/generic/projectname/1%2E0%2E0/test",
|
2022-01-10 15:58:05 +02:00
|
|
|
usePackageRegistry: true,
|
|
|
|
},
|
2021-09-09 03:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-10-11 17:03:13 +02:00
|
|
|
first := true
|
2021-09-09 03:42:13 +02:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if !strings.Contains(r.URL.Path, "assets/links") {
|
|
|
|
_, _ = io.Copy(io.Discard, r.Body)
|
2024-10-11 17:03:13 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if first {
|
|
|
|
http.Error(w, `{"message":{"name":["has already been taken"]}}`, http.StatusBadRequest)
|
|
|
|
first = false
|
2021-09-09 03:42:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-11 17:03:13 +02:00
|
|
|
defer w.WriteHeader(http.StatusOK)
|
|
|
|
defer fmt.Fprint(w, "{}")
|
2021-09-09 03:42:13 +02:00
|
|
|
b, err := io.ReadAll(r.Body)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2021-09-09 03:42:13 +02:00
|
|
|
|
|
|
|
reqBody := map[string]interface{}{}
|
|
|
|
err = json.Unmarshal(b, &reqBody)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2021-09-09 03:42:13 +02:00
|
|
|
|
2022-01-10 15:58:05 +02:00
|
|
|
url := reqBody["url"].(string)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.Truef(t, strings.HasSuffix(url, tt.wantURL), "expected %q to end with %q", url, tt.wantURL)
|
2021-09-09 03:42:13 +02:00
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2022-01-10 15:58:05 +02:00
|
|
|
ProjectName: "projectname",
|
2021-09-09 03:42:13 +02:00
|
|
|
Env: []string{
|
|
|
|
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
|
|
|
|
},
|
|
|
|
Release: config.Release{
|
|
|
|
GitLab: config.Repo{
|
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
},
|
2024-10-11 17:03:13 +02:00
|
|
|
ReplaceExistingArtifacts: true,
|
2021-09-09 03:42:13 +02:00
|
|
|
},
|
|
|
|
GitLabURLs: config.GitLabURLs{
|
2022-01-10 15:58:05 +02:00
|
|
|
API: srv.URL,
|
|
|
|
Download: tt.downloadURL,
|
|
|
|
UsePackageRegistry: tt.usePackageRegistry,
|
2021-09-09 03:42:13 +02:00
|
|
|
},
|
2023-03-02 05:01:11 +02:00
|
|
|
}, testctx.WithVersion("1.0.0"))
|
2022-01-10 15:58:05 +02:00
|
|
|
|
2021-09-09 03:42:13 +02:00
|
|
|
tmpFile, err := os.CreateTemp(t.TempDir(), "")
|
|
|
|
require.NoError(t, err)
|
2020-07-06 15:48:17 +02:00
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, ctx.Token)
|
2021-09-09 03:42:13 +02:00
|
|
|
require.NoError(t, err)
|
2020-07-06 15:48:17 +02:00
|
|
|
|
2021-09-09 03:42:13 +02:00
|
|
|
err = client.Upload(ctx, "1234", &artifact.Artifact{Name: "test", Path: "some-path"}, tmpFile)
|
2024-10-11 17:03:13 +02:00
|
|
|
if errors.As(err, &RetriableError{}) {
|
|
|
|
err = client.Upload(ctx, "1234", &artifact.Artifact{Name: "test", Path: "some-path"}, tmpFile)
|
|
|
|
}
|
2021-09-09 03:42:13 +02:00
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
2024-10-11 17:03:13 +02:00
|
|
|
retriable := errors.As(err, &RetriableError{})
|
|
|
|
require.False(t, retriable, "should be a final error")
|
2021-09-09 03:42:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
2020-07-06 15:48:17 +02:00
|
|
|
}
|
2021-09-10 21:35:42 +02:00
|
|
|
|
2023-06-05 18:08:57 +02:00
|
|
|
func TestGitLabCreateReleaseUnknownHost(t *testing.T) {
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-10 21:35:42 +02:00
|
|
|
Release: config.Release{
|
|
|
|
GitLab: config.Repo{
|
|
|
|
Owner: "owner",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: "http://goreleaser-notexists",
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-09-10 21:35:42 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:08:57 +02:00
|
|
|
// Create release if it doesn't exist
|
2021-09-10 21:35:42 +02:00
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
createdRelease = true
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.Empty(t, "should not reach here")
|
2021-09-10 21:35:42 +02:00
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-10 21:35:42 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-09-10 21:35:42 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.CreateRelease(ctx, "body")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, createdRelease)
|
2023-03-20 14:28:52 +02:00
|
|
|
require.Equal(t, 2, totalRequests)
|
2021-09-10 21:35:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:19:19 +02:00
|
|
|
func TestGitLabCreateReleaseReleaseExists(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(200)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.NewEncoder(w).Encode(map[string]string{
|
2023-02-07 16:19:19 +02:00
|
|
|
"description": "original description",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update release
|
|
|
|
if r.Method == http.MethodPut {
|
|
|
|
createdRelease = true
|
|
|
|
var resBody map[string]string
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.NewDecoder(r.Body).Decode(&resBody))
|
|
|
|
assert.Equal(t, "original description", resBody["description"])
|
2023-02-07 16:19:19 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.Empty(t, "should not reach here")
|
2023-02-07 16:19:19 +02:00
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2023-02-07 16:19:19 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
Release: config.Release{
|
|
|
|
ReleaseNotesMode: config.ReleaseNotesModeKeepExisting,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2023-02-07 16:19:19 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.CreateRelease(ctx, "body")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, createdRelease)
|
2023-03-20 14:28:52 +02:00
|
|
|
require.Equal(t, 2, totalRequests)
|
2023-02-07 16:19:19 +02:00
|
|
|
}
|
|
|
|
|
2023-06-05 18:08:57 +02:00
|
|
|
func TestGitLabCreateReleaseUnknownHTTPError(t *testing.T) {
|
2021-09-10 21:35:42 +02:00
|
|
|
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()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-10 21:35:42 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-09-10 21:35:42 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.CreateRelease(ctx, "body")
|
|
|
|
require.Error(t, err)
|
2023-03-20 14:28:52 +02:00
|
|
|
require.Equal(t, 1, totalRequests)
|
2021-09-10 21:35:42 +02:00
|
|
|
}
|
2021-10-03 16:22:26 +02:00
|
|
|
|
2023-07-14 03:42:00 +02:00
|
|
|
func TestGitLabGetDefaultBranch(t *testing.T) {
|
2021-10-03 16:22:26 +02:00
|
|
|
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()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-10-03 16:22:26 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-10-03 16:22:26 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
repo := Repo{
|
|
|
|
Owner: "someone",
|
|
|
|
Name: "something",
|
|
|
|
Branch: "somebranch",
|
|
|
|
}
|
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
_, err = client.getDefaultBranch(ctx, repo)
|
2021-10-03 16:22:26 +02:00
|
|
|
require.NoError(t, err)
|
2023-03-20 14:28:52 +02:00
|
|
|
require.Equal(t, 1, totalRequests)
|
2021-10-03 16:22:26 +02:00
|
|
|
}
|
|
|
|
|
2024-06-15 18:47:12 +02:00
|
|
|
func TestGitLabGetDefaultBranchEnv(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
|
|
|
t.Error("shouldn't have made any calls to the API")
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx := testctx.NewWithCfg(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",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Setenv("CI_DEFAULT_BRANCH", "foo")
|
|
|
|
b, err := client.getDefaultBranch(ctx, repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "foo", b)
|
|
|
|
}
|
|
|
|
|
2023-07-14 03:42:00 +02:00
|
|
|
func TestGitLabGetDefaultBranchErr(t *testing.T) {
|
2021-10-03 16:22:26 +02:00
|
|
|
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()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-10-03 16:22:26 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-10-03 16:22:26 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
repo := Repo{
|
|
|
|
Owner: "someone",
|
|
|
|
Name: "something",
|
|
|
|
Branch: "somebranch",
|
|
|
|
}
|
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
_, err = client.getDefaultBranch(ctx, repo)
|
2021-10-03 16:22:26 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2021-10-04 14:32:30 +02:00
|
|
|
|
2023-07-14 03:42:00 +02:00
|
|
|
func TestGitLabChangelog(t *testing.T) {
|
2021-10-04 14:32:30 +02:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-10-10 15:52:46 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/compare") {
|
|
|
|
r, err := os.Open("testdata/gitlab/compare.json")
|
2024-10-03 19:37:29 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
defer r.Close()
|
|
|
|
_, err = io.Copy(w, r)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-10-10 15:52:46 +02:00
|
|
|
return
|
|
|
|
}
|
2021-10-04 14:32:30 +02:00
|
|
|
defer r.Body.Close()
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-10-04 14:32:30 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-10-04 14:32:30 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
repo := Repo{
|
|
|
|
Owner: "someone",
|
|
|
|
Name: "something",
|
|
|
|
Branch: "somebranch",
|
|
|
|
}
|
|
|
|
|
2021-10-10 15:52:46 +02:00
|
|
|
log, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
|
|
|
|
require.NoError(t, err)
|
2024-04-24 14:08:20 +02:00
|
|
|
require.Equal(t, []ChangelogItem{
|
|
|
|
{
|
2024-06-15 20:40:08 +02:00
|
|
|
SHA: "6dcb09b5b57875f334f61aebed695e2e4193db5e",
|
2024-04-24 14:08:20 +02:00
|
|
|
Message: "Fix all the bugs",
|
|
|
|
AuthorName: "Joey User",
|
|
|
|
AuthorEmail: "joey@user.edu",
|
|
|
|
AuthorUsername: "",
|
|
|
|
},
|
|
|
|
}, log)
|
2021-10-04 14:32:30 +02:00
|
|
|
}
|
2021-10-25 15:29:19 +02:00
|
|
|
|
2023-07-14 03:42:00 +02:00
|
|
|
func TestGitLabCreateFile(t *testing.T) {
|
2021-10-25 15:29:19 +02:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-04-23 14:01:23 +02:00
|
|
|
// Handle the test where we know the branch and it exists
|
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/branches/somebranch") {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
2021-10-25 15:29:19 +02:00
|
|
|
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" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2021-10-25 15:29:19 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-23 14:01:23 +02:00
|
|
|
|
2021-10-25 15:29:19 +02:00
|
|
|
// Handle the test where we detect the branch
|
2024-04-23 14:01:23 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/someone/something") {
|
|
|
|
_, err := io.Copy(w, strings.NewReader(`{ "default_branch": "main" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-23 14:01:23 +02:00
|
|
|
return
|
|
|
|
}
|
2021-10-25 15:29:19 +02:00
|
|
|
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" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2021-10-25 15:29:19 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-23 14:01:23 +02:00
|
|
|
|
|
|
|
// Handle the test where the branch doesn't exist already
|
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/branches/non-existing-branch") {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/newfile-on-new-branch.txt") {
|
|
|
|
if r.Method == "POST" {
|
|
|
|
var resBody map[string]string
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.NewDecoder(r.Body).Decode(&resBody))
|
|
|
|
assert.Equal(t, "master", resBody["start_branch"])
|
2024-04-23 14:01:23 +02:00
|
|
|
}
|
|
|
|
_, err := io.Copy(w, strings.NewReader(`{"file_path":"newfile-on-new-branch.txt","branch":"non-existing-branch"}`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-23 14:01:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:26:04 +02:00
|
|
|
// Handle the case with a projectID
|
2024-04-23 14:01:23 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/123456789/repository/branches/main") {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
2024-03-17 20:26:04 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "projects/123456789/repository/files/newfile-projectID.txt") {
|
|
|
|
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "newfile-projectID.txt", "branch": "main" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-03-17 20:26:04 +02:00
|
|
|
return
|
|
|
|
}
|
2021-10-25 15:29:19 +02:00
|
|
|
// 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" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2021-10-25 15:29:19 +02:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-10-25 15:29:19 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-10-25 15:29:19 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-04-23 14:01:23 +02:00
|
|
|
// Test using an arbitrary existing branch
|
2021-10-25 15:29:19 +02:00
|
|
|
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)
|
|
|
|
|
2024-04-23 14:01:23 +02:00
|
|
|
// Test creating a new branch
|
|
|
|
repo = Repo{
|
|
|
|
Owner: "someone",
|
|
|
|
Name: "something",
|
|
|
|
Branch: "non-existing-branch",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "newfile-on-new-branch.txt", "test: test commit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-03-17 20:26:04 +02:00
|
|
|
// Test using projectID
|
|
|
|
repo = Repo{
|
|
|
|
Name: "123456789",
|
|
|
|
Branch: "main",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "newfile-projectID.txt", "test: test commit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-25 15:29:19 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:26:04 +02:00
|
|
|
func TestGitLabCloseMilestone(t *testing.T) {
|
2021-10-25 15:29:19 +02:00
|
|
|
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")
|
2024-10-03 19:37:29 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
defer r.Close()
|
|
|
|
_, err = io.Copy(w, r)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-10-25 15:29:19 +02:00
|
|
|
return
|
|
|
|
} else if strings.HasSuffix(r.URL.Path, "projects/someone/something/milestones/12") {
|
|
|
|
r, err := os.Open("testdata/gitlab/milestone.json")
|
2024-10-03 19:37:29 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
defer r.Close()
|
|
|
|
_, err = io.Copy(w, r)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-10-25 15:29:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-10-25 15:29:19 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
2023-04-07 03:58:06 +02:00
|
|
|
client, err := newGitLab(ctx, "test-token")
|
2021-10-25 15:29:19 +02:00
|
|
|
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)
|
|
|
|
}
|
2022-09-29 02:58:15 +02:00
|
|
|
|
2023-07-14 03:42:00 +02:00
|
|
|
func TestGitLabCheckUseJobToken(t *testing.T) {
|
2022-09-29 02:58:15 +02:00
|
|
|
tests := []struct {
|
2022-10-01 01:20:33 +02:00
|
|
|
useJobToken bool
|
|
|
|
token string
|
|
|
|
ciToken string
|
|
|
|
want bool
|
|
|
|
desc string
|
|
|
|
name string
|
2022-09-29 02:58:15 +02:00
|
|
|
}{
|
|
|
|
{
|
2022-10-01 01:20:33 +02:00
|
|
|
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",
|
2022-09-29 02:58:15 +02:00
|
|
|
},
|
|
|
|
{
|
2022-10-01 01:20:33 +02:00
|
|
|
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",
|
2022-09-29 02:58:15 +02:00
|
|
|
},
|
|
|
|
{
|
2022-10-01 01:20:33 +02:00
|
|
|
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",
|
2022-09-29 02:58:15 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2022-10-01 01:20:33 +02:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Setenv("CI_JOB_TOKEN", tt.ciToken)
|
2023-03-02 05:01:11 +02:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2022-10-01 01:20:33 +02:00
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
UseJobToken: tt.useJobToken,
|
|
|
|
},
|
|
|
|
})
|
2022-10-01 05:05:11 +02:00
|
|
|
got := checkUseJobToken(*ctx, tt.token)
|
2022-10-01 01:20:33 +02:00
|
|
|
require.Equal(t, tt.want, got, tt.desc)
|
|
|
|
})
|
2022-09-29 02:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-24 03:49:18 +02:00
|
|
|
|
|
|
|
func TestGitLabOpenPullRequestCrossRepo(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someone/something" {
|
|
|
|
_, err := io.Copy(w, strings.NewReader(`{ "id": 32156 }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someoneelse/something/merge_requests" {
|
|
|
|
got, err := io.ReadAll(r.Body)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
var pr gitlab.MergeRequest
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.Unmarshal(got, &pr))
|
|
|
|
assert.Equal(t, "main", pr.TargetBranch)
|
|
|
|
assert.Equal(t, "foo", pr.SourceBranch)
|
|
|
|
assert.Equal(t, "some title", pr.Title)
|
|
|
|
assert.Equal(t, 32156, pr.TargetProjectID)
|
2024-04-24 03:49:18 +02:00
|
|
|
|
|
|
|
_, err = io.Copy(w, strings.NewReader(`{"web_url": "https://gitlab.com/someoneelse/something/merge_requests/1"}`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error("unhandled request: " + r.URL.Path)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
|
|
|
GitLabURLs: config.GitLabURLs{
|
|
|
|
API: srv.URL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
client, err := newGitLab(ctx, "test-token")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
base := Repo{
|
|
|
|
Owner: "someone",
|
|
|
|
Name: "something",
|
|
|
|
Branch: "main",
|
|
|
|
}
|
|
|
|
head := Repo{
|
|
|
|
Owner: "someoneelse",
|
|
|
|
Name: "something",
|
|
|
|
Branch: "foo",
|
|
|
|
}
|
|
|
|
require.NoError(t, client.OpenPullRequest(ctx, base, head, "some title", false))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGitLabOpenPullRequestBaseEmpty(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someone/something" {
|
|
|
|
_, err := io.Copy(w, strings.NewReader(`{ "default_branch": "main" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someone/something/merge_requests" {
|
|
|
|
got, err := io.ReadAll(r.Body)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
var pr gitlab.MergeRequest
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.Unmarshal(got, &pr))
|
|
|
|
assert.Equal(t, "main", pr.TargetBranch)
|
|
|
|
assert.Equal(t, "foo", pr.SourceBranch)
|
|
|
|
assert.Equal(t, "some title", pr.Title)
|
|
|
|
assert.Equal(t, 0, pr.TargetProjectID)
|
2024-04-24 03:49:18 +02:00
|
|
|
|
|
|
|
_, err = io.Copy(w, strings.NewReader(`{"web_url": "https://gitlab.com/someoneelse/something/merge_requests/1"}`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error("unhandled request: " + r.URL.Path)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx := testctx.NewWithCfg(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: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, client.OpenPullRequest(ctx, Repo{}, repo, "some title", false))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGitLabOpenPullRequestDraft(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someone/something" {
|
|
|
|
_, err := io.Copy(w, strings.NewReader(`{ "default_branch": "main" }`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someone/something/merge_requests" {
|
|
|
|
got, err := io.ReadAll(r.Body)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
var pr gitlab.MergeRequest
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.Unmarshal(got, &pr))
|
|
|
|
assert.Equal(t, "main", pr.TargetBranch)
|
|
|
|
assert.Equal(t, "main", pr.SourceBranch)
|
|
|
|
assert.Equal(t, "Draft: some title", pr.Title)
|
|
|
|
assert.Equal(t, 0, pr.TargetProjectID)
|
2024-04-24 03:49:18 +02:00
|
|
|
|
|
|
|
_, err = io.Copy(w, strings.NewReader(`{"web_url": "https://gitlab.com/someoneelse/something/merge_requests/1"}`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error("unhandled request: " + r.URL.Path)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx := testctx.NewWithCfg(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: "main",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, client.OpenPullRequest(ctx, Repo{}, repo, "some title", true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGitLabOpenPullBaseBranchGiven(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v4/projects/someone/something/merge_requests" {
|
|
|
|
got, err := io.ReadAll(r.Body)
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
var pr gitlab.MergeRequest
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, json.Unmarshal(got, &pr))
|
|
|
|
assert.Equal(t, "main", pr.TargetBranch)
|
|
|
|
assert.Equal(t, "foo", pr.SourceBranch)
|
|
|
|
assert.Equal(t, "some title", pr.Title)
|
|
|
|
assert.Equal(t, 0, pr.TargetProjectID)
|
2024-04-24 03:49:18 +02:00
|
|
|
|
|
|
|
_, err = io.Copy(w, strings.NewReader(`{"web_url": "https://gitlab.com/someoneelse/something/merge_requests/1"}`))
|
2024-08-30 22:03:10 +02:00
|
|
|
assert.NoError(t, err)
|
2024-04-24 03:49:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error("unhandled request: " + r.URL.Path)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx := testctx.NewWithCfg(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: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, client.OpenPullRequest(ctx, Repo{Branch: "main"}, repo, "some title", false))
|
|
|
|
}
|