From bed1af57dc777b151f8f370b1e63da0f2b6574ef Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 22 May 2020 00:08:52 +0200 Subject: [PATCH] implement for brew and scoop support for Gitea-hosted repos --- internal/client/gitea.go | 41 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/internal/client/gitea.go b/internal/client/gitea.go index c46f25cb068d..51b64a4f22ca 100644 --- a/internal/client/gitea.go +++ b/internal/client/gitea.go @@ -2,6 +2,7 @@ package client import ( "crypto/tls" + "encoding/base64" "fmt" "net/http" "net/url" @@ -61,8 +62,44 @@ func (c *giteaClient) CreateFile( path, message string, ) error { - //TODO: implement for brew and scoop support for Gitea-hosted repos - return nil + owner := repo.Owner + repoName := repo.Name + + branchName := "master" + + 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, err := c.client.GetContents(owner, repoName, branchName, path) + // file not exist, create it + if err != nil { + if err.Error() != "404 Not Found" { + return err + } + _, err = c.client.CreateFile(owner, repoName, path, gitea.CreateFileOptions{ + FileOptions: fileOptions, + Content: base64.StdEncoding.EncodeToString(content), + }) + return err + } + + // update file + _, err = c.client.UpdateFile(owner, repoName, 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) {