go-sdk/gitea/repo_branch.go

144 lines
5.0 KiB
Go
Raw Normal View History

2016-02-03 03:24:14 +05:30
// Copyright 2016 The Gogs Authors. All rights reserved.
// Copyright 2020 The Gitea Authors. All rights reserved.
2016-02-03 03:24:14 +05:30
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
2016-01-14 20:51:47 +05:30
2016-01-15 19:05:31 +05:30
import (
"bytes"
"encoding/json"
2016-01-15 19:05:31 +05:30
"fmt"
"time"
2016-01-15 19:05:31 +05:30
)
// PayloadUser represents the author or committer of a commit
type PayloadUser struct {
// Full name of the commit author
Name string `json:"name"`
Email string `json:"email"`
UserName string `json:"username"`
}
// PayloadCommit represents a commit
type PayloadCommit struct {
// sha1 hash of the commit
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author *PayloadUser `json:"author"`
Committer *PayloadUser `json:"committer"`
Verification *PayloadCommitVerification `json:"verification"`
Timestamp time.Time `json:"timestamp"`
Added []string `json:"added"`
Removed []string `json:"removed"`
Modified []string `json:"modified"`
}
// PayloadCommitVerification represents the GPG verification of a commit
type PayloadCommitVerification struct {
Verified bool `json:"verified"`
Reason string `json:"reason"`
Signature string `json:"signature"`
Payload string `json:"payload"`
}
// Branch represents a repository branch
type Branch struct {
Name string `json:"name"`
Commit *PayloadCommit `json:"commit"`
Protected bool `json:"protected"`
RequiredApprovals int64 `json:"required_approvals"`
EnableStatusCheck bool `json:"enable_status_check"`
StatusCheckContexts []string `json:"status_check_contexts"`
UserCanPush bool `json:"user_can_push"`
UserCanMerge bool `json:"user_can_merge"`
EffectiveBranchProtectionName string `json:"effective_branch_protection_name"`
}
2016-01-14 20:51:47 +05:30
added pagination options for listing requests (#205) fix golint issue small improvements ListMyRepos now receives a value instead of a pointer reverted makefile changes added sleep in order to increase CI stability resolved conflicts Use RepositoryMeta struct on Issues (#267) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/267 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: techknowlogick <techknowlogick@gitea.io> extend SearchUsers (#248) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/248 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> Implement Repo Transfer (#264) fix test check version add TestRepoTransfer add TransferRepo Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/264 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> Add notifications (#226) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: John Olheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/226 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> Use StateType (#265) use StateType Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/265 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> opt setDefaults on issue_comment trigger CI Trigger notification resolved conflicts Fix AddEmail (#260) Merge branch 'master' into fix-AddEMail Add ListIssueCommentOptions for optional param (#243) Merge branch 'master' into impruve-ListXIssueComments Remove ListUserIssues() ... (#262) Merge branch 'master' into rm-ListUserIssues correct version (#257) Merge branch 'master' into targed-version Extend Issue Struct (#258) fix extend IssueTest add Repository to Issue struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/258 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Merge branch 'master' into targed-version format code (#245) fix if test is to fast remove lgtm bot config (gitea handle this now) format code Add GetIssueComment (#216) Merge branch 'master' into update-comment-api Fix & Refactor UserApp Functions (#247) Merge branch 'master' into refactor-user-app Add SearchRepo API Call (#254) Merge branch 'master' into search_repo IssueSubscribtion: Check http Status responce (#242) Merge branch 'master' into impruve-issueWatch Refactor RepoWatch (#241) Merge branch 'master' into refactor-repoWatch Add ListMilestoneOption to ListRepoMilestones (#244) use StateType use PageSize adjut test since gitea bug got fixed (#gitea/10047) add TestMilestones add optional ListMilestoneOption format Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net> Merge branch 'master' into refactor-repoWatch Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> add TEST fix GetMyWatchedRepos [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Ol... Co-authored-by: Hilário Coelho <hilario.coelho@securityside.com> Co-authored-by: 6543 <6543@noreply.gitea.io> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/205 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net>
2020-02-05 13:29:55 +05:30
// ListRepoBranchesOptions options for listing a repository's branches
type ListRepoBranchesOptions struct {
ListOptions
}
2016-11-10 15:14:00 +05:30
// ListRepoBranches list all the branches of one repository
func (c *Client) ListRepoBranches(user, repo string, opt ListRepoBranchesOptions) ([]*Branch, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo); err != nil {
return nil, nil, err
}
added pagination options for listing requests (#205) fix golint issue small improvements ListMyRepos now receives a value instead of a pointer reverted makefile changes added sleep in order to increase CI stability resolved conflicts Use RepositoryMeta struct on Issues (#267) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/267 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: techknowlogick <techknowlogick@gitea.io> extend SearchUsers (#248) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/248 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> Implement Repo Transfer (#264) fix test check version add TestRepoTransfer add TransferRepo Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/264 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> Add notifications (#226) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: John Olheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/226 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> Use StateType (#265) use StateType Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/265 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: lafriks <lafriks@noreply.gitea.io> opt setDefaults on issue_comment trigger CI Trigger notification resolved conflicts Fix AddEmail (#260) Merge branch 'master' into fix-AddEMail Add ListIssueCommentOptions for optional param (#243) Merge branch 'master' into impruve-ListXIssueComments Remove ListUserIssues() ... (#262) Merge branch 'master' into rm-ListUserIssues correct version (#257) Merge branch 'master' into targed-version Extend Issue Struct (#258) fix extend IssueTest add Repository to Issue struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/258 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Merge branch 'master' into targed-version format code (#245) fix if test is to fast remove lgtm bot config (gitea handle this now) format code Add GetIssueComment (#216) Merge branch 'master' into update-comment-api Fix & Refactor UserApp Functions (#247) Merge branch 'master' into refactor-user-app Add SearchRepo API Call (#254) Merge branch 'master' into search_repo IssueSubscribtion: Check http Status responce (#242) Merge branch 'master' into impruve-issueWatch Refactor RepoWatch (#241) Merge branch 'master' into refactor-repoWatch Add ListMilestoneOption to ListRepoMilestones (#244) use StateType use PageSize adjut test since gitea bug got fixed (#gitea/10047) add TestMilestones add optional ListMilestoneOption format Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net> Merge branch 'master' into refactor-repoWatch Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> add TEST fix GetMyWatchedRepos [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Ol... Co-authored-by: Hilário Coelho <hilario.coelho@securityside.com> Co-authored-by: 6543 <6543@noreply.gitea.io> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/205 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net>
2020-02-05 13:29:55 +05:30
opt.setDefaults()
branches := make([]*Branch, 0, opt.PageSize)
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &branches)
return branches, resp, err
2016-01-14 20:51:47 +05:30
}
2016-11-10 15:14:00 +05:30
// GetRepoBranch get one branch's information of one repository
func (c *Client) GetRepoBranch(user, repo, branch string) (*Branch, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo, &branch); err != nil {
return nil, nil, err
}
2016-01-14 20:51:47 +05:30
b := new(Branch)
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b)
if err != nil {
return nil, resp, err
}
return b, resp, nil
}
// DeleteRepoBranch delete a branch in a repository
func (c *Client) DeleteRepoBranch(user, repo, branch string) (bool, *Response, error) {
if err := escapeValidatePathSegments(&user, &repo, &branch); err != nil {
return false, nil, err
}
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
return false, nil, err
}
status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil)
if err != nil {
return false, resp, err
}
return status == 204, resp, nil
2016-01-14 20:51:47 +05:30
}
// 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, *Response, error) {
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
return nil, nil, err
}
if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil {
return nil, nil, err
}
if err := opt.Validate(); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, nil, err
}
branch := new(Branch)
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/branches", owner, repo), jsonHeader, bytes.NewReader(body), branch)
return branch, resp, err
}