1//
2// Copyright 2017, Sander van Harmelen
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 "time"
22)
23
24// DeployKeysService handles communication with the keys related methods
25// of the GitLab API.
26//
27// GitLab API docs: https://docs.gitlab.com/ce/api/deploy_keys.html
28type DeployKeysService struct {
29 client *Client
30}
31
32// DeployKey represents a GitLab deploy key.
33type DeployKey struct {
34 ID int `json:"id"`
35 Title string `json:"title"`
36 Key string `json:"key"`
37 CanPush *bool `json:"can_push"`
38 CreatedAt *time.Time `json:"created_at"`
39}
40
41func (k DeployKey) String() string {
42 return Stringify(k)
43}
44
45// ListAllDeployKeys gets a list of all deploy keys
46//
47// GitLab API docs:
48// https://docs.gitlab.com/ce/api/deploy_keys.html#list-all-deploy-keys
49func (s *DeployKeysService) ListAllDeployKeys(options ...OptionFunc) ([]*DeployKey, *Response, error) {
50 req, err := s.client.NewRequest("GET", "deploy_keys", nil, options)
51 if err != nil {
52 return nil, nil, err
53 }
54
55 var ks []*DeployKey
56 resp, err := s.client.Do(req, &ks)
57 if err != nil {
58 return nil, resp, err
59 }
60
61 return ks, resp, err
62}
63
64// ListProjectDeployKeysOptions represents the available ListProjectDeployKeys()
65// options.
66//
67// GitLab API docs:
68// https://docs.gitlab.com/ce/api/deploy_keys.html#list-project-deploy-keys
69type ListProjectDeployKeysOptions ListOptions
70
71// ListProjectDeployKeys gets a list of a project's deploy keys
72//
73// GitLab API docs:
74// https://docs.gitlab.com/ce/api/deploy_keys.html#list-project-deploy-keys
75func (s *DeployKeysService) ListProjectDeployKeys(pid interface{}, opt *ListProjectDeployKeysOptions, options ...OptionFunc) ([]*DeployKey, *Response, error) {
76 project, err := parseID(pid)
77 if err != nil {
78 return nil, nil, err
79 }
80 u := fmt.Sprintf("projects/%s/deploy_keys", pathEscape(project))
81
82 req, err := s.client.NewRequest("GET", u, opt, options)
83 if err != nil {
84 return nil, nil, err
85 }
86
87 var ks []*DeployKey
88 resp, err := s.client.Do(req, &ks)
89 if err != nil {
90 return nil, resp, err
91 }
92
93 return ks, resp, err
94}
95
96// GetDeployKey gets a single deploy key.
97//
98// GitLab API docs:
99// https://docs.gitlab.com/ce/api/deploy_keys.html#single-deploy-key
100func (s *DeployKeysService) GetDeployKey(pid interface{}, deployKey int, options ...OptionFunc) (*DeployKey, *Response, error) {
101 project, err := parseID(pid)
102 if err != nil {
103 return nil, nil, err
104 }
105 u := fmt.Sprintf("projects/%s/deploy_keys/%d", pathEscape(project), deployKey)
106
107 req, err := s.client.NewRequest("GET", u, nil, options)
108 if err != nil {
109 return nil, nil, err
110 }
111
112 k := new(DeployKey)
113 resp, err := s.client.Do(req, k)
114 if err != nil {
115 return nil, resp, err
116 }
117
118 return k, resp, err
119}
120
121// AddDeployKeyOptions represents the available ADDDeployKey() options.
122//
123// GitLab API docs:
124// https://docs.gitlab.com/ce/api/deploy_keys.html#add-deploy-key
125type AddDeployKeyOptions struct {
126 Title *string `url:"title,omitempty" json:"title,omitempty"`
127 Key *string `url:"key,omitempty" json:"key,omitempty"`
128 CanPush *bool `url:"can_push,omitempty" json:"can_push,omitempty"`
129}
130
131// AddDeployKey creates a new deploy key for a project. If deploy key already
132// exists in another project - it will be joined to project but only if
133// original one was is accessible by same user.
134//
135// GitLab API docs:
136// https://docs.gitlab.com/ce/api/deploy_keys.html#add-deploy-key
137func (s *DeployKeysService) AddDeployKey(pid interface{}, opt *AddDeployKeyOptions, options ...OptionFunc) (*DeployKey, *Response, error) {
138 project, err := parseID(pid)
139 if err != nil {
140 return nil, nil, err
141 }
142 u := fmt.Sprintf("projects/%s/deploy_keys", pathEscape(project))
143
144 req, err := s.client.NewRequest("POST", u, opt, options)
145 if err != nil {
146 return nil, nil, err
147 }
148
149 k := new(DeployKey)
150 resp, err := s.client.Do(req, k)
151 if err != nil {
152 return nil, resp, err
153 }
154
155 return k, resp, err
156}
157
158// DeleteDeployKey deletes a deploy key from a project.
159//
160// GitLab API docs:
161// https://docs.gitlab.com/ce/api/deploy_keys.html#delete-deploy-key
162func (s *DeployKeysService) DeleteDeployKey(pid interface{}, deployKey int, options ...OptionFunc) (*Response, error) {
163 project, err := parseID(pid)
164 if err != nil {
165 return nil, err
166 }
167 u := fmt.Sprintf("projects/%s/deploy_keys/%d", pathEscape(project), deployKey)
168
169 req, err := s.client.NewRequest("DELETE", u, nil, options)
170 if err != nil {
171 return nil, err
172 }
173
174 return s.client.Do(req, nil)
175}
176
177// EnableDeployKey enables a deploy key.
178//
179// GitLab API docs:
180// https://docs.gitlab.com/ce/api/deploy_keys.html#enable-deploy-key
181func (s *DeployKeysService) EnableDeployKey(pid interface{}, deployKey int, options ...OptionFunc) (*DeployKey, *Response, error) {
182 project, err := parseID(pid)
183 if err != nil {
184 return nil, nil, err
185 }
186 u := fmt.Sprintf("projects/%s/deploy_keys/%d/enable", pathEscape(project), deployKey)
187
188 req, err := s.client.NewRequest("POST", u, nil, options)
189 if err != nil {
190 return nil, nil, err
191 }
192
193 k := new(DeployKey)
194 resp, err := s.client.Do(req, k)
195 if err != nil {
196 return nil, resp, err
197 }
198
199 return k, resp, err
200}