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