...

Source file src/github.com/xanzy/go-gitlab/protected_branches.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen, Michael Lihs
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"net/url"
    23  )
    24  
    25  // ProtectedBranchesService handles communication with the protected branch
    26  // related methods of the GitLab API.
    27  //
    28  // GitLab API docs:
    29  // https://docs.gitlab.com/ee/api/protected_branches.html
    30  type ProtectedBranchesService struct {
    31  	client *Client
    32  }
    33  
    34  // ProtectedBranch represents a protected branch.
    35  //
    36  // GitLab API docs:
    37  // https://docs.gitlab.com/ee/api/protected_branches.html#list-protected-branches
    38  type ProtectedBranch struct {
    39  	ID                        int                        `json:"id"`
    40  	Name                      string                     `json:"name"`
    41  	PushAccessLevels          []*BranchAccessDescription `json:"push_access_levels"`
    42  	MergeAccessLevels         []*BranchAccessDescription `json:"merge_access_levels"`
    43  	UnprotectAccessLevels     []*BranchAccessDescription `json:"unprotect_access_levels"`
    44  	AllowForcePush            bool                       `json:"allow_force_push"`
    45  	CodeOwnerApprovalRequired bool                       `json:"code_owner_approval_required"`
    46  }
    47  
    48  // BranchAccessDescription represents the access description for a protected
    49  // branch.
    50  //
    51  // GitLab API docs:
    52  // https://docs.gitlab.com/ee/api/protected_branches.html#list-protected-branches
    53  type BranchAccessDescription struct {
    54  	ID                     int              `json:"id"`
    55  	AccessLevel            AccessLevelValue `json:"access_level"`
    56  	AccessLevelDescription string           `json:"access_level_description"`
    57  	UserID                 int              `json:"user_id"`
    58  	GroupID                int              `json:"group_id"`
    59  }
    60  
    61  // ListProtectedBranchesOptions represents the available ListProtectedBranches()
    62  // options.
    63  //
    64  // GitLab API docs:
    65  // https://docs.gitlab.com/ee/api/protected_branches.html#list-protected-branches
    66  type ListProtectedBranchesOptions struct {
    67  	ListOptions
    68  	Search *string `url:"search,omitempty" json:"search,omitempty"`
    69  }
    70  
    71  // ListProtectedBranches gets a list of protected branches from a project.
    72  //
    73  // GitLab API docs:
    74  // https://docs.gitlab.com/ee/api/protected_branches.html#list-protected-branches
    75  func (s *ProtectedBranchesService) ListProtectedBranches(pid interface{}, opt *ListProtectedBranchesOptions, options ...RequestOptionFunc) ([]*ProtectedBranch, *Response, error) {
    76  	project, err := parseID(pid)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  	u := fmt.Sprintf("projects/%s/protected_branches", PathEscape(project))
    81  
    82  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  
    87  	var p []*ProtectedBranch
    88  	resp, err := s.client.Do(req, &p)
    89  	if err != nil {
    90  		return nil, resp, err
    91  	}
    92  
    93  	return p, resp, nil
    94  }
    95  
    96  // GetProtectedBranch gets a single protected branch or wildcard protected branch.
    97  //
    98  // GitLab API docs:
    99  // https://docs.gitlab.com/ee/api/protected_branches.html#get-a-single-protected-branch-or-wildcard-protected-branch
   100  func (s *ProtectedBranchesService) GetProtectedBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*ProtectedBranch, *Response, error) {
   101  	project, err := parseID(pid)
   102  	if err != nil {
   103  		return nil, nil, err
   104  	}
   105  	u := fmt.Sprintf("projects/%s/protected_branches/%s", PathEscape(project), url.PathEscape(branch))
   106  
   107  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   108  	if err != nil {
   109  		return nil, nil, err
   110  	}
   111  
   112  	p := new(ProtectedBranch)
   113  	resp, err := s.client.Do(req, p)
   114  	if err != nil {
   115  		return nil, resp, err
   116  	}
   117  
   118  	return p, resp, nil
   119  }
   120  
   121  // ProtectRepositoryBranchesOptions represents the available
   122  // ProtectRepositoryBranches() options.
   123  //
   124  // GitLab API docs:
   125  // https://docs.gitlab.com/ee/api/protected_branches.html#protect-repository-branches
   126  type ProtectRepositoryBranchesOptions struct {
   127  	Name                      *string                     `url:"name,omitempty" json:"name,omitempty"`
   128  	PushAccessLevel           *AccessLevelValue           `url:"push_access_level,omitempty" json:"push_access_level,omitempty"`
   129  	MergeAccessLevel          *AccessLevelValue           `url:"merge_access_level,omitempty" json:"merge_access_level,omitempty"`
   130  	UnprotectAccessLevel      *AccessLevelValue           `url:"unprotect_access_level,omitempty" json:"unprotect_access_level,omitempty"`
   131  	AllowForcePush            *bool                       `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
   132  	AllowedToPush             *[]*BranchPermissionOptions `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
   133  	AllowedToMerge            *[]*BranchPermissionOptions `url:"allowed_to_merge,omitempty" json:"allowed_to_merge,omitempty"`
   134  	AllowedToUnprotect        *[]*BranchPermissionOptions `url:"allowed_to_unprotect,omitempty" json:"allowed_to_unprotect,omitempty"`
   135  	CodeOwnerApprovalRequired *bool                       `url:"code_owner_approval_required,omitempty" json:"code_owner_approval_required,omitempty"`
   136  }
   137  
   138  // BranchPermissionOptions represents a branch permission option.
   139  //
   140  // GitLab API docs:
   141  // https://docs.gitlab.com/ee/api/protected_branches.html#protect-repository-branches
   142  type BranchPermissionOptions struct {
   143  	ID          *int              `url:"id,omitempty" json:"id,omitempty"`
   144  	UserID      *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
   145  	GroupID     *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
   146  	DeployKeyID *int              `url:"deploy_key_id,omitempty" json:"deploy_key_id,omitempty"`
   147  	AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   148  	Destroy     *bool             `url:"_destroy,omitempty" json:"_destroy,omitempty"`
   149  }
   150  
   151  // ProtectRepositoryBranches protects a single repository branch or several
   152  // project repository branches using a wildcard protected branch.
   153  //
   154  // GitLab API docs:
   155  // https://docs.gitlab.com/ee/api/protected_branches.html#protect-repository-branches
   156  func (s *ProtectedBranchesService) ProtectRepositoryBranches(pid interface{}, opt *ProtectRepositoryBranchesOptions, options ...RequestOptionFunc) (*ProtectedBranch, *Response, error) {
   157  	project, err := parseID(pid)
   158  	if err != nil {
   159  		return nil, nil, err
   160  	}
   161  	u := fmt.Sprintf("projects/%s/protected_branches", PathEscape(project))
   162  
   163  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   164  	if err != nil {
   165  		return nil, nil, err
   166  	}
   167  
   168  	p := new(ProtectedBranch)
   169  	resp, err := s.client.Do(req, p)
   170  	if err != nil {
   171  		return nil, resp, err
   172  	}
   173  
   174  	return p, resp, nil
   175  }
   176  
   177  // UnprotectRepositoryBranches unprotects the given protected branch or wildcard
   178  // protected branch.
   179  //
   180  // GitLab API docs:
   181  // https://docs.gitlab.com/ee/api/protected_branches.html#unprotect-repository-branches
   182  func (s *ProtectedBranchesService) UnprotectRepositoryBranches(pid interface{}, branch string, options ...RequestOptionFunc) (*Response, error) {
   183  	project, err := parseID(pid)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	u := fmt.Sprintf("projects/%s/protected_branches/%s", PathEscape(project), url.PathEscape(branch))
   188  
   189  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return s.client.Do(req, nil)
   195  }
   196  
   197  // UpdateProtectedBranchOptions represents the available
   198  // UpdateProtectedBranch() options.
   199  //
   200  // GitLab API docs:
   201  // https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
   202  type UpdateProtectedBranchOptions struct {
   203  	Name                      *string                     `url:"name,omitempty" json:"name,omitempty"`
   204  	AllowForcePush            *bool                       `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
   205  	CodeOwnerApprovalRequired *bool                       `url:"code_owner_approval_required,omitempty" json:"code_owner_approval_required,omitempty"`
   206  	AllowedToPush             *[]*BranchPermissionOptions `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
   207  	AllowedToMerge            *[]*BranchPermissionOptions `url:"allowed_to_merge,omitempty" json:"allowed_to_merge,omitempty"`
   208  	AllowedToUnprotect        *[]*BranchPermissionOptions `url:"allowed_to_unprotect,omitempty" json:"allowed_to_unprotect,omitempty"`
   209  }
   210  
   211  // UpdateProtectedBranch updates a protected branch.
   212  //
   213  // Gitlab API docs:
   214  // https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
   215  func (s *ProtectedBranchesService) UpdateProtectedBranch(pid interface{}, branch string, opt *UpdateProtectedBranchOptions, options ...RequestOptionFunc) (*ProtectedBranch, *Response, error) {
   216  	project, err := parseID(pid)
   217  	if err != nil {
   218  		return nil, nil, err
   219  	}
   220  	u := fmt.Sprintf("projects/%s/protected_branches/%s", PathEscape(project), url.PathEscape(branch))
   221  
   222  	req, err := s.client.NewRequest(http.MethodPatch, u, opt, options)
   223  	if err != nil {
   224  		return nil, nil, err
   225  	}
   226  
   227  	p := new(ProtectedBranch)
   228  	resp, err := s.client.Do(req, p)
   229  	if err != nil {
   230  		return nil, resp, err
   231  	}
   232  
   233  	return p, resp, nil
   234  }
   235  
   236  // RequireCodeOwnerApprovalsOptions represents the available
   237  // RequireCodeOwnerApprovals() options.
   238  //
   239  // GitLab API docs:
   240  // https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
   241  type RequireCodeOwnerApprovalsOptions struct {
   242  	CodeOwnerApprovalRequired *bool `url:"code_owner_approval_required,omitempty" json:"code_owner_approval_required,omitempty"`
   243  }
   244  
   245  // RequireCodeOwnerApprovals updates the code owner approval option.
   246  //
   247  // Deprecated: Use UpdateProtectedBranch() instead.
   248  //
   249  // Gitlab API docs:
   250  // https://docs.gitlab.com/ee/api/protected_branches.html#update-a-protected-branch
   251  func (s *ProtectedBranchesService) RequireCodeOwnerApprovals(pid interface{}, branch string, opt *RequireCodeOwnerApprovalsOptions, options ...RequestOptionFunc) (*Response, error) {
   252  	updateOptions := &UpdateProtectedBranchOptions{
   253  		CodeOwnerApprovalRequired: opt.CodeOwnerApprovalRequired,
   254  	}
   255  	_, req, err := s.UpdateProtectedBranch(pid, branch, updateOptions, options...)
   256  	return req, err
   257  }
   258  

View as plain text