Add CreateBranch (#407)
Add CreateBranch Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/407 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
fb7de7ecb8
commit
5b418aa8c6
2 changed files with 47 additions and 0 deletions
|
@ -6,6 +6,8 @@
|
||||||
package gitea
|
package gitea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -89,3 +91,41 @@ func (c *Client) DeleteRepoBranch(user, repo, branch string) (bool, error) {
|
||||||
}
|
}
|
||||||
return status == 204, nil
|
return status == 204, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateBranchOption options when creating a branch in a repository
|
||||||
|
type CreateBranchOption struct {
|
||||||
|
// Name of the branch to create
|
||||||
|
BranchName string `json:"new_branch_name"`
|
||||||
|
// Name of the old branch to create from (optional)
|
||||||
|
OldBranchName string `json:"old_branch_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the CreateBranchOption struct
|
||||||
|
func (opt CreateBranchOption) Validate() error {
|
||||||
|
if len(opt.BranchName) == 0 {
|
||||||
|
return fmt.Errorf("BranchName is empty")
|
||||||
|
}
|
||||||
|
if len(opt.BranchName) > 100 {
|
||||||
|
return fmt.Errorf("BranchName to long")
|
||||||
|
}
|
||||||
|
if len(opt.OldBranchName) > 100 {
|
||||||
|
return fmt.Errorf("OldBranchName to long")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateBranch creates a branch for a user's repository
|
||||||
|
func (c *Client) CreateBranch(owner, repo string, opt CreateBranchOption) (*Branch, error) {
|
||||||
|
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := opt.Validate(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body, err := json.Marshal(&opt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
branch := new(Branch)
|
||||||
|
return branch, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/branches", owner, repo), jsonHeader, bytes.NewReader(body), branch)
|
||||||
|
}
|
||||||
|
|
|
@ -47,6 +47,13 @@ func TestRepoBranches(t *testing.T) {
|
||||||
b, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
b, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, "feature")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Nil(t, b)
|
assert.Nil(t, b)
|
||||||
|
|
||||||
|
bNew, err := c.CreateBranch(repo.Owner.UserName, repo.Name, CreateBranchOption{BranchName: "NewBranch"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
b, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, bNew.Name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, bNew, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepoBranchProtection(t *testing.T) {
|
func TestRepoBranchProtection(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue