github.com/daeMOn63/bitclient@v0.0.0-20190425080230-bfee94efac35/project_repository_branch_restrictions.go (about)

     1  package bitclient
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type GetRepositoryBranchRestrictionRequest struct {
     8  	PagedRequest
     9  	Type        string `url:"type,omitempty"`
    10  	MatcherType string `url:"matcherType,omitempty"`
    11  	MatcherId   string `url:"matcherId,omitempty"`
    12  	Effective   bool   `url:"effective,omitempty"`
    13  }
    14  
    15  type GetRepositoryBranchRestrictionResponse struct {
    16  	PagedResponse
    17  	Values []BranchRestriction
    18  }
    19  
    20  func (bc *BitClient) GetRepositoryBranchRestrictions(projectKey, repositorySlug string, params GetRepositoryBranchRestrictionRequest) ([]BranchRestriction, error) {
    21  	rError := new(ErrorResponse)
    22  
    23  	response := GetRepositoryBranchRestrictionResponse{}
    24  
    25  	url := fmt.Sprintf("/rest/branch-permissions/2.0/projects/%s/repos/%s/restrictions", projectKey, repositorySlug)
    26  	resp, _ := bc.sling.New().Get(url).QueryStruct(params).Receive(&response, rError)
    27  
    28  	resp, err := bc.checkReponse(resp, rError)
    29  
    30  	return response.Values, err
    31  }
    32  
    33  type SetRepositoryBranchRestrictionsRequest struct {
    34  	Id      int      `json:"id,omitempty"`
    35  	Type    string   `json:"type,omitempty"`
    36  	Matcher Matcher  `json:"matcher,omitempty"`
    37  	Users   []string `json:"users,omitempty"`
    38  	Groups  []string `json:"groups,omitempty"`
    39  }
    40  
    41  func (bc *BitClient) SetRepositoryBranchRestrictions(projectKey, repositorySlug string, params SetRepositoryBranchRestrictionsRequest) error {
    42  	rError := new(ErrorResponse)
    43  
    44  	url := fmt.Sprintf("/rest/branch-permissions/2.0/projects/%s/repos/%s/restrictions", projectKey, repositorySlug)
    45  	resp, _ := bc.sling.New().Post(url).BodyJSON(params).Receive(nil, rError)
    46  
    47  	resp, err := bc.checkReponse(resp, rError)
    48  
    49  	return err
    50  }
    51  
    52  func (bc *BitClient) CloneRepositoryMasterBranchRestrictions(sourceProjectKey, sourceRepositorySlug, targetProjectKey, targetRepositorySlug string) error {
    53  
    54  	getParams := GetRepositoryBranchRestrictionRequest{
    55  		MatcherType: "BRANCH",
    56  		MatcherId:   "refs/heads/master",
    57  	}
    58  
    59  	response, err := bc.GetRepositoryBranchRestrictions(sourceProjectKey, sourceRepositorySlug, getParams)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	for _, branchRestriction := range response {
    65  
    66  		var users []string
    67  		for _, user := range branchRestriction.Users {
    68  			if user.Active == true {
    69  				users = append(users, user.Name)
    70  			}
    71  		}
    72  
    73  		postParams := SetRepositoryBranchRestrictionsRequest{
    74  			Id:      branchRestriction.Id,
    75  			Type:    branchRestriction.Type,
    76  			Matcher: branchRestriction.Matcher,
    77  			Groups:  branchRestriction.Groups,
    78  			Users:   users,
    79  		}
    80  
    81  		err := bc.SetRepositoryBranchRestrictions(targetProjectKey, targetRepositorySlug, postParams)
    82  		if err != nil {
    83  			return err
    84  		}
    85  	}
    86  
    87  	return nil
    88  }