protected_branches.go

  1//
  2// Copyright 2017, 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
 17package gitlab
 18
 19import (
 20	"fmt"
 21)
 22
 23// ProtectedBranchesService handles communication with the protected branch
 24// related methods of the GitLab API.
 25//
 26// GitLab API docs:
 27// https://docs.gitlab.com/ce/api/protected_branches.html#protected-branches-api
 28type ProtectedBranchesService struct {
 29	client *Client
 30}
 31
 32// BranchAccessDescription represents the access description for a protected
 33// branch.
 34//
 35// GitLab API docs:
 36// https://docs.gitlab.com/ce/api/protected_branches.html#protected-branches-api
 37type BranchAccessDescription struct {
 38	AccessLevel            AccessLevelValue `json:"access_level"`
 39	AccessLevelDescription string           `json:"access_level_description"`
 40}
 41
 42// ProtectedBranch represents a protected branch.
 43//
 44// GitLab API docs:
 45// https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
 46type ProtectedBranch struct {
 47	Name              string                     `json:"name"`
 48	PushAccessLevels  []*BranchAccessDescription `json:"push_access_levels"`
 49	MergeAccessLevels []*BranchAccessDescription `json:"merge_access_levels"`
 50}
 51
 52// ListProtectedBranchesOptions represents the available ListProtectedBranches()
 53// options.
 54//
 55// GitLab API docs:
 56// https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
 57type ListProtectedBranchesOptions ListOptions
 58
 59// ListProtectedBranches gets a list of protected branches from a project.
 60//
 61// GitLab API docs:
 62// https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
 63func (s *ProtectedBranchesService) ListProtectedBranches(pid interface{}, opt *ListProtectedBranchesOptions, options ...OptionFunc) ([]*ProtectedBranch, *Response, error) {
 64	project, err := parseID(pid)
 65	if err != nil {
 66		return nil, nil, err
 67	}
 68	u := fmt.Sprintf("projects/%s/protected_branches", pathEscape(project))
 69
 70	req, err := s.client.NewRequest("GET", u, opt, options)
 71	if err != nil {
 72		return nil, nil, err
 73	}
 74
 75	var p []*ProtectedBranch
 76	resp, err := s.client.Do(req, &p)
 77	if err != nil {
 78		return nil, resp, err
 79	}
 80
 81	return p, resp, err
 82}
 83
 84// GetProtectedBranch gets a single protected branch or wildcard protected branch.
 85//
 86// GitLab API docs:
 87// https://docs.gitlab.com/ce/api/protected_branches.html#get-a-single-protected-branch-or-wildcard-protected-branch
 88func (s *ProtectedBranchesService) GetProtectedBranch(pid interface{}, branch string, options ...OptionFunc) (*ProtectedBranch, *Response, error) {
 89	project, err := parseID(pid)
 90	if err != nil {
 91		return nil, nil, err
 92	}
 93	u := fmt.Sprintf("projects/%s/protected_branches/%s", pathEscape(project), branch)
 94
 95	req, err := s.client.NewRequest("GET", u, nil, options)
 96	if err != nil {
 97		return nil, nil, err
 98	}
 99
100	p := new(ProtectedBranch)
101	resp, err := s.client.Do(req, p)
102	if err != nil {
103		return nil, resp, err
104	}
105
106	return p, resp, err
107}
108
109// ProtectRepositoryBranchesOptions represents the available
110// ProtectRepositoryBranches() options.
111//
112// GitLab API docs:
113// https://docs.gitlab.com/ce/api/protected_branches.html#protect-repository-branches
114type ProtectRepositoryBranchesOptions struct {
115	Name             *string           `url:"name,omitempty" json:"name,omitempty"`
116	PushAccessLevel  *AccessLevelValue `url:"push_access_level,omitempty" json:"push_access_level,omitempty"`
117	MergeAccessLevel *AccessLevelValue `url:"merge_access_level,omitempty" json:"merge_access_level,omitempty"`
118}
119
120// ProtectRepositoryBranches protects a single repository branch or several
121// project repository branches using a wildcard protected branch.
122//
123// GitLab API docs:
124// https://docs.gitlab.com/ce/api/protected_branches.html#protect-repository-branches
125func (s *ProtectedBranchesService) ProtectRepositoryBranches(pid interface{}, opt *ProtectRepositoryBranchesOptions, options ...OptionFunc) (*ProtectedBranch, *Response, error) {
126	project, err := parseID(pid)
127	if err != nil {
128		return nil, nil, err
129	}
130	u := fmt.Sprintf("projects/%s/protected_branches", pathEscape(project))
131
132	req, err := s.client.NewRequest("POST", u, opt, options)
133	if err != nil {
134		return nil, nil, err
135	}
136
137	p := new(ProtectedBranch)
138	resp, err := s.client.Do(req, p)
139	if err != nil {
140		return nil, resp, err
141	}
142
143	return p, resp, err
144}
145
146// UnprotectRepositoryBranches unprotects the given protected branch or wildcard
147// protected branch.
148//
149// GitLab API docs:
150// https://docs.gitlab.com/ce/api/protected_branches.html#unprotect-repository-branches
151func (s *ProtectedBranchesService) UnprotectRepositoryBranches(pid interface{}, branch string, options ...OptionFunc) (*Response, error) {
152	project, err := parseID(pid)
153	if err != nil {
154		return nil, err
155	}
156	u := fmt.Sprintf("projects/%s/protected_branches/%s", pathEscape(project), branch)
157
158	req, err := s.client.NewRequest("DELETE", u, nil, options)
159	if err != nil {
160		return nil, err
161	}
162
163	return s.client.Do(req, nil)
164}