Add Branch Deletion (#317)
add TEST for branches dont return empty branch on error add DeleteBranch func Co-authored-by: lafriks <lafriks@noreply.gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/317 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
parent
7dfa25bb30
commit
50560273b9
2 changed files with 108 additions and 1 deletions
|
@ -1,4 +1,5 @@
|
||||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
||||||
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a MIT-style
|
// Use of this source code is governed by a MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
@ -64,5 +65,20 @@ func (c *Client) ListRepoBranches(user, repo string, opt ListRepoBranchesOptions
|
||||||
// GetRepoBranch get one branch's information of one repository
|
// GetRepoBranch get one branch's information of one repository
|
||||||
func (c *Client) GetRepoBranch(user, repo, branch string) (*Branch, error) {
|
func (c *Client) GetRepoBranch(user, repo, branch string) (*Branch, error) {
|
||||||
b := new(Branch)
|
b := new(Branch)
|
||||||
return b, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b)
|
if err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteRepoBranch delete a branch in a repository
|
||||||
|
func (c *Client) DeleteRepoBranch(user, repo, branch string) (bool, error) {
|
||||||
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
status, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return status == 204, nil
|
||||||
}
|
}
|
||||||
|
|
91
gitea/repo_branch_test.go
Normal file
91
gitea/repo_branch_test.go
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRepoBranches(t *testing.T) {
|
||||||
|
log.Println("== TestRepoBranches ==")
|
||||||
|
c := newTestClient()
|
||||||
|
var repoName = "branches"
|
||||||
|
|
||||||
|
repo := prepareBranchTest(t, c, repoName)
|
||||||
|
if repo == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bl, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, bl, 3)
|
||||||
|
assert.EqualValues(t, "feature", bl[0].Name)
|
||||||
|
assert.EqualValues(t, "master", bl[1].Name)
|
||||||
|
assert.EqualValues(t, "update", bl[2].Name)
|
||||||
|
|
||||||
|
b, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, bl[2].Commit.ID, b.Commit.ID)
|
||||||
|
assert.EqualValues(t, bl[2].Commit.Added, b.Commit.Added)
|
||||||
|
|
||||||
|
s, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "master")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.False(t, s)
|
||||||
|
s, err = c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, s)
|
||||||
|
|
||||||
|
bl, err = c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, bl, 2)
|
||||||
|
|
||||||
|
b, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareBranchTest(t *testing.T, c *Client, repoName string) *Repository {
|
||||||
|
origRepo, err := createTestRepo(t, repoName, c)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
masterLicence, err := c.GetContents(origRepo.Owner.UserName, origRepo.Name, "master", "README.md")
|
||||||
|
if !assert.NoError(t, err) || !assert.NotNil(t, masterLicence) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedFile, err := c.UpdateFile(origRepo.Owner.UserName, origRepo.Name, "README.md", UpdateFileOptions{
|
||||||
|
DeleteFileOptions: DeleteFileOptions{
|
||||||
|
FileOptions: FileOptions{
|
||||||
|
Message: "update it",
|
||||||
|
BranchName: "master",
|
||||||
|
NewBranchName: "update",
|
||||||
|
},
|
||||||
|
SHA: masterLicence.SHA,
|
||||||
|
},
|
||||||
|
Content: "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo=",
|
||||||
|
})
|
||||||
|
if !assert.NoError(t, err) || !assert.NotNil(t, updatedFile) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
newFile, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "WOW-file", CreateFileOptions{
|
||||||
|
Content: "QSBuZXcgRmlsZQo=",
|
||||||
|
FileOptions: FileOptions{
|
||||||
|
Message: "creat a new file",
|
||||||
|
BranchName: "master",
|
||||||
|
NewBranchName: "feature",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return origRepo
|
||||||
|
}
|
Loading…
Reference in a new issue