Skip to content

Commit

Permalink
feat: upload brew tap to gitea (#1547)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
6543 committed Nov 21, 2020
1 parent 9a19dee commit 89105d6
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 21 deletions.
6 changes: 4 additions & 2 deletions internal/client/client_test.go
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
46 changes: 43 additions & 3 deletions internal/client/gitea.go
Expand Up @@ -2,6 +2,7 @@ package client

import (
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
60 changes: 44 additions & 16 deletions internal/client/gitea_test.go
Expand Up @@ -22,23 +22,23 @@ 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)
}

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)
}

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)
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions internal/pipe/defaults/defaults.go
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions internal/pipe/defaults/defaults_test.go
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -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"`
}

Expand Down
1 change: 1 addition & 0 deletions www/docs/environment.md
Expand Up @@ -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
```
Expand Down

1 comment on commit 89105d6

@vercel
Copy link

@vercel vercel bot commented on 89105d6 Nov 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.