Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Gitea client add create file function #1547

Merged
merged 12 commits into from Nov 21, 2020
3 changes: 2 additions & 1 deletion internal/client/client_test.go
Expand Up @@ -19,7 +19,8 @@ func TestClientNewGitea(t *testing.T) {
ctx := &context.Context{
Config: config.Project{
GiteaURLs: config.GiteaURLs{
API: "https://git.dtluna.net/api/v1",
API: "https://git.dtluna.net/api/v1",
Download: "https://git.dtluna.net",
6543 marked this conversation as resolved.
Show resolved Hide resolved
},
},
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
52 changes: 40 additions & 12 deletions internal/client/gitea_test.go
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.GitLabURLs.Download = strings.ReplaceAll(ctx.Config.GitLabURLs.API, "/api/v1", "")
6543 marked this conversation as resolved.
Show resolved Hide resolved
}
for _, defaulter := range defaults.Defaulters {
if err := middleware.Logging(
defaulter.String(),
Expand Down
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"`
6543 marked this conversation as resolved.
Show resolved Hide resolved
SkipTLSVerify bool `yaml:"skip_tls_verify,omitempty"`
}

Expand Down