1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-16 03:52:12 +02:00

feat: upload brew tap to gitea (#1547)

* implement for brew and scoop support for Gitea-hosted repos

* fix test

* add ReleaseURLTemplate support for gitea

* Add TestGiteaReleaseURLTemplate

* fix api to download url conversion

* switch test gitea instance to gitea.com

* fix defaults

* add test for defaults.go (GiteaURLs)

* add option into docs
This commit is contained in:
6543 2020-11-21 14:36:29 +01:00 committed by GitHub
parent 9a19dee300
commit 89105d63ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 110 additions and 21 deletions

View File

@ -19,7 +19,9 @@ func TestClientNewGitea(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://git.dtluna.net/api/v1",
// TODO: use a mocked http server to cover version api
API: "https://gitea.com/api/v1",
Download: "https://gitea.com",
},
},
TokenType: context.TokenTypeGitea,
@ -35,7 +37,7 @@ func TestClientNewGiteaInvalidURL(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "://git.dtluna.net/api/v1",
API: "://gitea.com/api/v1",
},
},
TokenType: context.TokenTypeGitea,

View File

@ -2,6 +2,7 @@ package client
import (
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"net/url"
@ -85,8 +86,42 @@ func (c *giteaClient) CreateFile(
path,
message string,
) error {
//TODO: implement for brew and scoop support for Gitea-hosted repos
return nil
// use default branch
branchName := ""
fileOptions := gitea.FileOptions{
Message: message,
BranchName: branchName,
Author: gitea.Identity{
Name: commitAuthor.Name,
Email: commitAuthor.Email,
},
Committer: gitea.Identity{
Name: commitAuthor.Name,
Email: commitAuthor.Email,
},
}
currentFile, resp, err := c.client.GetContents(repo.Owner, repo.Name, branchName, path)
// file not exist, create it
if err != nil {
if resp == nil || resp.StatusCode != http.StatusNotFound {
return err
}
_, _, err = c.client.CreateFile(repo.Owner, repo.Name, path, gitea.CreateFileOptions{
FileOptions: fileOptions,
Content: base64.StdEncoding.EncodeToString(content),
})
return err
}
// update file
_, _, err = c.client.UpdateFile(repo.Owner, repo.Name, path, gitea.UpdateFileOptions{
FileOptions: fileOptions,
SHA: currentFile.SHA,
Content: base64.StdEncoding.EncodeToString(content),
})
return err
}
func (c *giteaClient) createRelease(ctx *context.Context, title, body string) (*gitea.Release, error) {
@ -193,7 +228,12 @@ func (c *giteaClient) CreateRelease(ctx *context.Context, body string) (string,
}
func (c *giteaClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "", NotImplementedError{TokenType: context.TokenTypeGitea}
return fmt.Sprintf(
"%s/%s/%s/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
ctx.Config.GiteaURLs.Download,
ctx.Config.Release.Gitea.Owner,
ctx.Config.Release.Gitea.Name,
), nil
}
// Upload uploads a file into a release repository.

View File

@ -22,7 +22,7 @@ type GetInstanceURLSuite struct {
func (s *GetInstanceURLSuite) TestWithScheme() {
t := s.T()
rootURL := "https://git.dtluna.net"
rootURL := "https://gitea.com"
result, err := getInstanceURL(rootURL + "/api/v1")
require.NoError(t, err)
require.Equal(t, rootURL, result)
@ -30,7 +30,7 @@ func (s *GetInstanceURLSuite) TestWithScheme() {
func (s *GetInstanceURLSuite) TestParseError() {
t := s.T()
host := "://.dtluna.net"
host := "://wrong.gitea.com"
result, err := getInstanceURL(host)
require.Error(t, err)
require.Empty(t, result)
@ -38,7 +38,7 @@ func (s *GetInstanceURLSuite) TestParseError() {
func (s *GetInstanceURLSuite) TestNoScheme() {
t := s.T()
host := "git.dtluna.net"
host := "gitea.com"
result, err := getInstanceURL(host)
require.Error(t, err)
require.Empty(t, result)
@ -113,7 +113,7 @@ func (s *GiteaReleasesTestSuite) SetupTest() {
CurrentTag: s.tag,
Commit: s.commit,
ShortCommit: s.commit[0:2],
URL: "https://git.dtluna.net/goreleaser/goreleaser.git",
URL: "https://gitea.com/goreleaser/goreleaser.git",
},
PreRelease: s.isPrerelease,
}
@ -240,20 +240,25 @@ func (s *GiteaupdateReleaseSuite) TestError() {
require.Nil(t, release)
}
func TestGiteaupdateReleaseSuite(t *testing.T) {
suite.Run(t, new(GiteaupdateReleaseSuite))
func (s *GiteaupdateReleaseSuite) TestGiteaCreateFile() {
t := s.T()
fileEndpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", s.url, s.owner, s.repoName, "file.txt")
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/api/v1/version", s.url), httpmock.NewStringResponder(200, "{\"version\":\"1.12.0\"}"))
httpmock.RegisterResponder("GET", fileEndpoint, httpmock.NewStringResponder(404, ""))
httpmock.RegisterResponder("POST", fileEndpoint, httpmock.NewStringResponder(201, "{\n \"content\": {\n \"name\": \"test.file\",\n \"path\": \"test.file\",\n \"sha\": \"3b18e512dba79e4c8300dd08aeb37f8e728b8dad\",\n \"type\": \"file\",\n \"size\": 12,\n \"encoding\": \"base64\",\n \"content\": \"aGVsbG8gd29ybGQK\"\n }\n}"))
author := config.CommitAuthor{Name: s.owner}
repo := Repo{Owner: s.owner, Name: s.repoName}
content := []byte("hello world")
path := "file.txt"
message := "add hello world"
err := s.client.CreateFile(s.ctx, author, repo, content, path, message)
require.Nil(t, err)
}
func TestGiteaCreateFile(t *testing.T) {
client := giteaClient{}
ctx := context.Context{}
author := config.CommitAuthor{}
repo := Repo{}
content := []byte{}
path := ""
message := ""
file := client.CreateFile(&ctx, author, repo, content, path, message)
require.Nil(t, file)
func TestGiteaupdateReleaseSuite(t *testing.T) {
suite.Run(t, new(GiteaupdateReleaseSuite))
}
type GiteaCreateReleaseSuite struct {
@ -401,3 +406,26 @@ func (s *GiteaUploadSuite) TestSuccess() {
func TestGiteaUploadSuite(t *testing.T) {
suite.Run(t, new(GiteaUploadSuite))
}
func TestGiteaReleaseURLTemplate(t *testing.T) {
var ctx = context.New(config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
Download: "https://gitea.com",
},
Release: config.Release{
Gitea: config.Repo{
Owner: "owner",
Name: "name",
},
},
})
client, err := NewGitea(ctx, ctx.Token)
require.NoError(t, err)
urlTpl, err := client.ReleaseURLTemplate(ctx)
require.NoError(t, err)
expectedUrl := "https://gitea.com/owner/name/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
require.Equal(t, expectedUrl, urlTpl)
}

View File

@ -3,6 +3,8 @@
package defaults
import (
"strings"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/middleware"
"github.com/goreleaser/goreleaser/pkg/context"
@ -27,6 +29,9 @@ func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.GitLabURLs.Download == "" {
ctx.Config.GitLabURLs.Download = client.DefaultGitLabDownloadURL
}
if ctx.Config.GiteaURLs.Download == "" {
ctx.Config.GiteaURLs.Download = strings.ReplaceAll(ctx.Config.GiteaURLs.API, "/api/v1", "")
}
for _, defaulter := range defaults.Defaulters {
if err := middleware.Logging(
defaulter.String(),

View File

@ -104,4 +104,16 @@ func TestFillPartial(t *testing.T) {
require.Empty(t, ctx.Config.Dockers[0].Goarm)
require.Equal(t, "disttt", ctx.Config.Dist)
require.NotEqual(t, "https://github.com", ctx.Config.GitHubURLs.Download)
ctx = &context.Context{
TokenType: context.TokenTypeGitea,
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://gitea.com/api/v1",
},
},
}
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "https://gitea.com", ctx.Config.GiteaURLs.Download)
}

View File

@ -30,6 +30,7 @@ type GitLabURLs struct {
// GiteaURLs holds the URLs to be used when using gitea.
type GiteaURLs struct {
API string `yaml:"api,omitempty"`
Download string `yaml:"download,omitempty"`
SkipTLSVerify bool `yaml:"skip_tls_verify,omitempty"`
}

View File

@ -74,6 +74,7 @@ the `.goreleaser.yml` configuration file:
# .goreleaser.yml
gitea_urls:
api: https://gitea.myinstance.com/api/v1/
download: https://gitea.myinstance.com
# set to true if you use a self-signed certificate
skip_tls_verify: false
```