Commit 3f9374bd authored by Michael Lihs's avatar Michael Lihs Committed by Sander van Harmelen

Add support for protected branches (#288)

* Add support for labels: subscribeTo / unsubscribeFrom

* Add support for protected branches: ListProtectedBranches

* Add support for ProtectedBranches: Get

* Add support for protected branches: protect repository branch

* Add support for protected branches: unprotect repository branch

* Update README
parent a11605e2
......@@ -34,6 +34,7 @@ includes all calls to the following services:
- [x] Pipelines
- [x] Project Snippets
- [x] Projects (including setting Webhooks)
- [x] Protected Branches
- [x] Repositories
- [x] Repository Files
- [x] Services
......
......@@ -62,6 +62,7 @@ type AccessLevelValue int
//
// GitLab API docs: https://docs.gitlab.com/ce/permissions/permissions.html
const (
NoPermissions AccessLevelValue = 0
GuestPermissions AccessLevelValue = 10
ReporterPermissions AccessLevelValue = 20
DeveloperPermissions AccessLevelValue = 30
......@@ -216,6 +217,7 @@ type Client struct {
Projects *ProjectsService
ProjectMembers *ProjectMembersService
ProjectSnippets *ProjectSnippetsService
ProtectedBranches *ProtectedBranchesService
Pipelines *PipelinesService
PipelineTriggers *PipelineTriggersService
Repositories *RepositoriesService
......@@ -292,6 +294,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
c.ProjectSnippets = &ProjectSnippetsService{client: c}
c.Pipelines = &PipelinesService{client: c}
c.PipelineTriggers = &PipelineTriggersService{client: c}
c.ProtectedBranches = &ProtectedBranchesService{client: c}
c.Repositories = &RepositoriesService{client: c}
c.RepositoryFiles = &RepositoryFilesService{client: c}
c.Services = &ServicesService{client: c}
......
......@@ -188,7 +188,7 @@ func (s *LabelsService) SubscribeToLabel(pid interface{}, labelID interface{}, o
l := new(Label)
resp, err := s.client.Do(req, l)
if err != nil {
return nil, resp, err
return nil, resp, err
}
return l, resp, err
......
//
// Copyright 2017, Sander van Harmelen, Michael Lihs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"fmt"
"net/url"
)
// ProtectedBranchesService handles communication with the
// protected branch related methods of the GitLab API.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#protected-branches-api
type ProtectedBranchesService struct {
client *Client
}
// BranchAccessDescription represents the access description for a
// protected branch
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#protected-branches-api
type BranchAccessDescription struct {
AccessLevel AccessLevelValue `json:"access_level"`
AccessLevelDescription string `json:"access_level_description"`
}
// ProtectedBranch represents a protected branch
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
type ProtectedBranch struct {
Name string `json:"name"`
PushAccessLevels []*BranchAccessDescription `json:"push_access_levels"`
MergeAccessLevels []*BranchAccessDescription `json:"merge_access_levels"`
}
// ListProtectedBranches gets a list of protected branches from a project
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
func (s *ProtectedBranchesService) ListProtectedBranches(pid interface{}, options ...OptionFunc) ([]*ProtectedBranch, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/protected_branches", url.QueryEscape(project))
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
var p []*ProtectedBranch
resp, err := s.client.Do(req, &p)
if err != nil {
return nil, resp, err
}
return p, resp, err
}
// GetProtectedBranch gets a single protected branch
// or wildcard protected branch
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#get-a-single-protected-branch-or-wildcard-protected-branch
func (s *ProtectedBranchesService) GetProtectedBranch(pid interface{}, name string, options ...OptionFunc) (*ProtectedBranch, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/protected_branches/%s", url.QueryEscape(project), name)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
p := new(ProtectedBranch)
resp, err := s.client.Do(req, p)
if err != nil {
return nil, resp, err
}
return p, resp, err
}
// ProtectRepositoryBranchesOptions represents the available
// ProtectRepositoryBranches() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#protect-repository-branches
type ProtectRepositoryBranchesOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
PushAccessLevel *AccessLevelValue `url:"push_access_level,omitempty" json:"push_access_level,omitempty"`
MergeAccessLevel *AccessLevelValue `url:"merge_access_level,omitempty" json:"merge_access_level,omitempty"`
}
// ProtectRepositoryBranches protects a single repository branch or several
// project repository branches using a wildcard protected branch.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#protect-repository-branches
func (s *ProtectedBranchesService) ProtectRepositoryBranches(pid interface{}, opt *ProtectRepositoryBranchesOptions, options ...OptionFunc) (*ProtectedBranch, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/protected_branches", url.QueryEscape(project))
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}
p := new(ProtectedBranch)
resp, err := s.client.Do(req, p)
if err != nil {
return nil, resp, err
}
return p, resp, err
}
// UnprotectRepositoryBranches unprotects the given protected branch
// or wildcard protected branch
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/protected_branches.html#unprotect-repository-branches
func (s *ProtectedBranchesService) UnprotectRepositoryBranches(pid interface{}, name string, options ...OptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/protected_branches/%s", url.QueryEscape(project), name)
req, err := s.client.NewRequest("DELETE", u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment