deploy_keys.go 5.5 KB

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