1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/client/github_test.go
Steve Azzopardi 68ff8e996f
feat: support templates for scm urls (#2465)
Background
---
When a git repository is hosted in multiple GitLab instances the
`.goreleaser.yml` needs to take in consideration both APIs endpoints. At
the moment it defaults to GitLab.com and you can override it with
`gitlab_urls` however this forces you to only support 1 GitLab instance.

We need this for
https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/14122
where we have a tool that is developed on GitLab.com but then mirrored
to an internal GitLab instance since we need it to operate GitLab.com
even when it's down.

Solution
---
Support templates like `{{ .Env.CI_SERVER_URL }}` for the
`gitlab_urls`, `github_urls`  and `gitea_urls` so it can use environment
variables and the same `.goreleaser` file can be used in multiple SCM
instances.

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2021-09-09 01:42:13 +00:00

199 lines
5.2 KiB
Go

package client
import (
"fmt"
"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 TestNewGitHubClient(t *testing.T) {
t.Run("good urls", func(t *testing.T) {
githubURL := "https://github.mycompany.com"
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: githubURL + "/api",
Upload: githubURL + "/upload",
},
})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
githubClient, ok := client.(*githubClient)
require.True(t, ok)
require.Equal(t, githubURL+"/api", githubClient.client.BaseURL.String())
require.Equal(t, githubURL+"/upload", githubClient.client.UploadURL.String())
})
t.Run("bad api url", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "://github.mycompany.com/api",
Upload: "https://github.mycompany.com/upload",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.EqualError(t, err, `parse "://github.mycompany.com/api": missing protocol scheme`)
})
t.Run("bad upload url", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "https://github.mycompany.com/api",
Upload: "not a url:4994",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.EqualError(t, err, `parse "not a url:4994": first path segment in URL cannot contain colon`)
})
t.Run("template", func(t *testing.T) {
githubURL := "https://github.mycompany.com"
ctx := context.New(config.Project{
Env: []string{
fmt.Sprintf("GORELEASER_TEST_GITHUB_URLS_API=%s/api", githubURL),
fmt.Sprintf("GORELEASER_TEST_GITHUB_URLS_UPLOAD=%s/upload", githubURL),
},
GitHubURLs: config.GitHubURLs{
API: "{{ .Env.GORELEASER_TEST_GITHUB_URLS_API }}",
Upload: "{{ .Env.GORELEASER_TEST_GITHUB_URLS_UPLOAD }}",
},
})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
githubClient, ok := client.(*githubClient)
require.True(t, ok)
require.Equal(t, githubURL+"/api", githubClient.client.BaseURL.String())
require.Equal(t, githubURL+"/upload", githubClient.client.UploadURL.String())
})
t.Run("template invalid api", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.ErrorAs(t, err, &template.ExecError{})
})
t.Run("template invalid upload", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "https://github.mycompany.com/api",
Upload: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.ErrorAs(t, err, &template.ExecError{})
})
t.Run("template invalid", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "{{.dddddddddd",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.Error(t, err)
})
}
func TestGitHubUploadReleaseIDNotInt(t *testing.T) {
ctx := context.New(config.Project{})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
require.EqualError(
t,
client.Upload(ctx, "blah", &artifact.Artifact{}, nil),
`strconv.ParseInt: parsing "blah": invalid syntax`,
)
}
func TestGitHubReleaseURLTemplate(t *testing.T) {
tests := []struct {
name string
downloadURL string
wantDownloadURL string
wantErr bool
}{
{
name: "default_download_url",
downloadURL: DefaultGitHubDownloadURL,
wantDownloadURL: "https://github.com/owner/name/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
},
{
name: "download_url_template",
downloadURL: "{{ .Env.GORELEASER_TEST_GITHUB_URLS_DOWNLOAD }}",
wantDownloadURL: "https://github.mycompany.com/owner/name/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
},
{
name: "download_url_template_invalid_value",
downloadURL: "{{ .Env.GORELEASER_NOT_EXISTS }}",
wantErr: true,
},
{
name: "download_url_template_invalid",
downloadURL: "{{.dddddddddd",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.New(config.Project{
Env: []string{
"GORELEASER_TEST_GITHUB_URLS_DOWNLOAD=https://github.mycompany.com",
},
GitHubURLs: config.GitHubURLs{
Download: tt.downloadURL,
},
Release: config.Release{
GitHub: config.Repo{
Owner: "owner",
Name: "name",
},
},
})
client, err := NewGitHub(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 TestGitHubCreateReleaseWrongNameTemplate(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
NameTemplate: "{{.dddddddddd",
},
})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
str, err := client.CreateRelease(ctx, "")
require.Empty(t, str)
require.EqualError(t, err, `template: tmpl:1: unclosed action`)
}