Skip to content

Commit

Permalink
refactor & use DefaultBranch for file Creation
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed May 23, 2020
1 parent 2e910fc commit 35609d8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions internal/client/gitea.go
Expand Up @@ -62,10 +62,16 @@ func (c *giteaClient) CreateFile(
path,
message string,
) error {
owner := repo.Owner
repoName := repo.Name

branchName := "master"
// Gitea >= 1.12.0 Use DefaultBranch on ""
branchName := ""
if err := c.client.CheckServerVersionConstraint(">=1.12.0"); err != nil {
r, err := c.client.GetRepo(repo.Owner, repo.Name)
if err != nil {
return err
}
branchName = r.DefaultBranch
}

fileOptions := gitea.FileOptions{
Message: message,
Expand All @@ -80,21 +86,22 @@ func (c *giteaClient) CreateFile(
},
}

currentFile, err := c.client.GetContents(owner, repoName, branchName, path)
currentFile, err := c.client.GetContents(repo.Owner, repo.Name, branchName, path)
// file not exist, create it
if err != nil {
// ToDo: https://gitea.com/gitea/go-sdk/issues/303
if err.Error() != "404 Not Found" {
return err
}
_, err = c.client.CreateFile(owner, repoName, path, gitea.CreateFileOptions{
_, 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(owner, repoName, path, gitea.UpdateFileOptions{
_, err = c.client.UpdateFile(repo.Owner, repo.Name, path, gitea.UpdateFileOptions{
FileOptions: fileOptions,
SHA: currentFile.SHA,
Content: base64.StdEncoding.EncodeToString(content),
Expand Down

0 comments on commit 35609d8

Please sign in to comment.