runners.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // RunnersService handles communication with the runner related methods of the
  23. // GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/runners.html
  26. type RunnersService struct {
  27. client *Client
  28. }
  29. // Runner represents a GitLab CI Runner.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/runners.html
  32. type Runner struct {
  33. ID int `json:"id"`
  34. Description string `json:"description"`
  35. Active bool `json:"active"`
  36. IsShared bool `json:"is_shared"`
  37. Name string `json:"name"`
  38. Online bool `json:"online"`
  39. Status string `json:"status"`
  40. Token string `json:"token"`
  41. }
  42. // RunnerDetails represents the GitLab CI runner details.
  43. //
  44. // GitLab API docs: https://docs.gitlab.com/ce/api/runners.html
  45. type RunnerDetails struct {
  46. Active bool `json:"active"`
  47. Architecture string `json:"architecture"`
  48. Description string `json:"description"`
  49. ID int `json:"id"`
  50. IsShared bool `json:"is_shared"`
  51. ContactedAt *time.Time `json:"contacted_at"`
  52. Name string `json:"name"`
  53. Online bool `json:"online"`
  54. Status string `json:"status"`
  55. Platform string `json:"platform"`
  56. Projects []struct {
  57. ID int `json:"id"`
  58. Name string `json:"name"`
  59. NameWithNamespace string `json:"name_with_namespace"`
  60. Path string `json:"path"`
  61. PathWithNamespace string `json:"path_with_namespace"`
  62. } `json:"projects"`
  63. Token string `json:"token"`
  64. Revision string `json:"revision"`
  65. TagList []string `json:"tag_list"`
  66. Version string `json:"version"`
  67. AccessLevel string `json:"access_level"`
  68. MaximumTimeout int `json:"maximum_timeout"`
  69. }
  70. // ListRunnersOptions represents the available ListRunners() options.
  71. //
  72. // GitLab API docs:
  73. // https://docs.gitlab.com/ce/api/runners.html#list-owned-runners
  74. type ListRunnersOptions struct {
  75. ListOptions
  76. Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
  77. }
  78. // ListRunners gets a list of runners accessible by the authenticated user.
  79. //
  80. // GitLab API docs:
  81. // https://docs.gitlab.com/ce/api/runners.html#list-owned-runners
  82. func (s *RunnersService) ListRunners(opt *ListRunnersOptions, options ...OptionFunc) ([]*Runner, *Response, error) {
  83. req, err := s.client.NewRequest("GET", "runners", opt, options)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. var rs []*Runner
  88. resp, err := s.client.Do(req, &rs)
  89. if err != nil {
  90. return nil, resp, err
  91. }
  92. return rs, resp, err
  93. }
  94. // ListAllRunners gets a list of all runners in the GitLab instance. Access is
  95. // restricted to users with admin privileges.
  96. //
  97. // GitLab API docs:
  98. // https://docs.gitlab.com/ce/api/runners.html#list-all-runners
  99. func (s *RunnersService) ListAllRunners(opt *ListRunnersOptions, options ...OptionFunc) ([]*Runner, *Response, error) {
  100. req, err := s.client.NewRequest("GET", "runners/all", opt, options)
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. var rs []*Runner
  105. resp, err := s.client.Do(req, &rs)
  106. if err != nil {
  107. return nil, resp, err
  108. }
  109. return rs, resp, err
  110. }
  111. // GetRunnerDetails returns details for given runner.
  112. //
  113. // GitLab API docs:
  114. // https://docs.gitlab.com/ce/api/runners.html#get-runner-39-s-details
  115. func (s *RunnersService) GetRunnerDetails(rid interface{}, options ...OptionFunc) (*RunnerDetails, *Response, error) {
  116. runner, err := parseID(rid)
  117. if err != nil {
  118. return nil, nil, err
  119. }
  120. u := fmt.Sprintf("runners/%s", runner)
  121. req, err := s.client.NewRequest("GET", u, nil, options)
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. var rs *RunnerDetails
  126. resp, err := s.client.Do(req, &rs)
  127. if err != nil {
  128. return nil, resp, err
  129. }
  130. return rs, resp, err
  131. }
  132. // UpdateRunnerDetailsOptions represents the available UpdateRunnerDetails() options.
  133. //
  134. // GitLab API docs:
  135. // https://docs.gitlab.com/ce/api/runners.html#update-runner-39-s-details
  136. type UpdateRunnerDetailsOptions struct {
  137. Description *string `url:"description,omitempty" json:"description,omitempty"`
  138. Active *bool `url:"active,omitempty" json:"active,omitempty"`
  139. TagList []string `url:"tag_list[],omitempty" json:"tag_list,omitempty"`
  140. RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
  141. Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
  142. AccessLevel *string `url:"access_level,omitempty" json:"access_level,omitempty"`
  143. MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
  144. }
  145. // UpdateRunnerDetails updates details for a given runner.
  146. //
  147. // GitLab API docs:
  148. // https://docs.gitlab.com/ce/api/runners.html#update-runner-39-s-details
  149. func (s *RunnersService) UpdateRunnerDetails(rid interface{}, opt *UpdateRunnerDetailsOptions, options ...OptionFunc) (*RunnerDetails, *Response, error) {
  150. runner, err := parseID(rid)
  151. if err != nil {
  152. return nil, nil, err
  153. }
  154. u := fmt.Sprintf("runners/%s", runner)
  155. req, err := s.client.NewRequest("PUT", u, opt, options)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. var rs *RunnerDetails
  160. resp, err := s.client.Do(req, &rs)
  161. if err != nil {
  162. return nil, resp, err
  163. }
  164. return rs, resp, err
  165. }
  166. // RemoveRunner removes a runner.
  167. //
  168. // GitLab API docs:
  169. // https://docs.gitlab.com/ce/api/runners.html#remove-a-runner
  170. func (s *RunnersService) RemoveRunner(rid interface{}, options ...OptionFunc) (*Response, error) {
  171. runner, err := parseID(rid)
  172. if err != nil {
  173. return nil, err
  174. }
  175. u := fmt.Sprintf("runners/%s", runner)
  176. req, err := s.client.NewRequest("DELETE", u, nil, options)
  177. if err != nil {
  178. return nil, err
  179. }
  180. return s.client.Do(req, nil)
  181. }
  182. // ListRunnerJobsOptions represents the available ListRunnerJobs()
  183. // options. Status can be one of: running, success, failed, canceled.
  184. //
  185. // GitLab API docs:
  186. // https://docs.gitlab.com/ce/api/runners.html#list-runner-39-s-jobs
  187. type ListRunnerJobsOptions struct {
  188. ListOptions
  189. Status *string `url:"status,omitempty" json:"status,omitempty"`
  190. }
  191. // ListRunnerJobs gets a list of jobs that are being processed or were processed by specified Runner.
  192. //
  193. // GitLab API docs:
  194. // https://docs.gitlab.com/ce/api/runners.html#list-runner-39-s-jobs
  195. func (s *RunnersService) ListRunnerJobs(rid interface{}, opt *ListRunnerJobsOptions, options ...OptionFunc) ([]*Job, *Response, error) {
  196. runner, err := parseID(rid)
  197. if err != nil {
  198. return nil, nil, err
  199. }
  200. u := fmt.Sprintf("runners/%s/jobs", runner)
  201. req, err := s.client.NewRequest("GET", u, opt, options)
  202. if err != nil {
  203. return nil, nil, err
  204. }
  205. var rs []*Job
  206. resp, err := s.client.Do(req, &rs)
  207. if err != nil {
  208. return nil, resp, err
  209. }
  210. return rs, resp, err
  211. }
  212. // ListProjectRunnersOptions represents the available ListProjectRunners()
  213. // options.
  214. //
  215. // GitLab API docs:
  216. // https://docs.gitlab.com/ce/api/runners.html#list-project-s-runners
  217. type ListProjectRunnersOptions ListRunnersOptions
  218. // ListProjectRunners gets a list of runners accessible by the authenticated user.
  219. //
  220. // GitLab API docs:
  221. // https://docs.gitlab.com/ce/api/runners.html#list-project-s-runners
  222. func (s *RunnersService) ListProjectRunners(pid interface{}, opt *ListProjectRunnersOptions, options ...OptionFunc) ([]*Runner, *Response, error) {
  223. project, err := parseID(pid)
  224. if err != nil {
  225. return nil, nil, err
  226. }
  227. u := fmt.Sprintf("projects/%s/runners", url.QueryEscape(project))
  228. req, err := s.client.NewRequest("GET", u, opt, options)
  229. if err != nil {
  230. return nil, nil, err
  231. }
  232. var rs []*Runner
  233. resp, err := s.client.Do(req, &rs)
  234. if err != nil {
  235. return nil, resp, err
  236. }
  237. return rs, resp, err
  238. }
  239. // EnableProjectRunnerOptions represents the available EnableProjectRunner()
  240. // options.
  241. //
  242. // GitLab API docs:
  243. // https://docs.gitlab.com/ce/api/runners.html#enable-a-runner-in-project
  244. type EnableProjectRunnerOptions struct {
  245. RunnerID int `json:"runner_id"`
  246. }
  247. // EnableProjectRunner enables an available specific runner in the project.
  248. //
  249. // GitLab API docs:
  250. // https://docs.gitlab.com/ce/api/runners.html#enable-a-runner-in-project
  251. func (s *RunnersService) EnableProjectRunner(pid interface{}, opt *EnableProjectRunnerOptions, options ...OptionFunc) (*Runner, *Response, error) {
  252. project, err := parseID(pid)
  253. if err != nil {
  254. return nil, nil, err
  255. }
  256. u := fmt.Sprintf("projects/%s/runners", url.QueryEscape(project))
  257. req, err := s.client.NewRequest("POST", u, opt, options)
  258. if err != nil {
  259. return nil, nil, err
  260. }
  261. var r *Runner
  262. resp, err := s.client.Do(req, &r)
  263. if err != nil {
  264. return nil, resp, err
  265. }
  266. return r, resp, err
  267. }
  268. // DisableProjectRunner disables a specific runner from project.
  269. //
  270. // GitLab API docs:
  271. // https://docs.gitlab.com/ce/api/runners.html#disable-a-runner-from-project
  272. func (s *RunnersService) DisableProjectRunner(pid interface{}, rid interface{}, options ...OptionFunc) (*Response, error) {
  273. project, err := parseID(pid)
  274. if err != nil {
  275. return nil, err
  276. }
  277. runner, err := parseID(rid)
  278. if err != nil {
  279. return nil, err
  280. }
  281. u := fmt.Sprintf("projects/%s/runners/%s", url.QueryEscape(project), url.QueryEscape(runner))
  282. req, err := s.client.NewRequest("DELETE", u, nil, options)
  283. if err != nil {
  284. return nil, err
  285. }
  286. return s.client.Do(req, nil)
  287. }
  288. // RegisterNewRunnerOptions represents the available RegisterNewRunner()
  289. // options.
  290. //
  291. // GitLab API docs:
  292. // https://docs.gitlab.com/ce/api/runners.html#register-a-new-runner
  293. type RegisterNewRunnerOptions struct {
  294. Token *string `url:"token" json:"token"`
  295. Description *string `url:"description,omitempty" json:"description,omitempty"`
  296. Info *string `url:"info,omitempty" json:"info,omitempty"`
  297. Active *bool `url:"active,omitempty" json:"active,omitempty"`
  298. Locked *bool `url:"locked,omitempty" json:"locked,omitempty"`
  299. RunUntagged *bool `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
  300. TagList []string `url:"tag_list[],omitempty" json:"tag_list,omitempty"`
  301. MaximumTimeout *int `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
  302. }
  303. // RegisterNewRunner registers a new Runner for the instance.
  304. //
  305. // GitLab API docs:
  306. // https://docs.gitlab.com/ce/api/runners.html#register-a-new-runner
  307. func (s *RunnersService) RegisterNewRunner(opt *RegisterNewRunnerOptions, options ...OptionFunc) (*Runner, *Response, error) {
  308. req, err := s.client.NewRequest("POST", "runners", opt, options)
  309. if err != nil {
  310. return nil, nil, err
  311. }
  312. var r *Runner
  313. resp, err := s.client.Do(req, &r)
  314. if err != nil {
  315. return nil, resp, err
  316. }
  317. return r, resp, err
  318. }
  319. // DeleteRegisteredRunnerOptions represents the available
  320. // DeleteRegisteredRunner() options.
  321. //
  322. // GitLab API docs:
  323. // https://docs.gitlab.com/ce/api/runners.html#delete-a-registered-runner
  324. type DeleteRegisteredRunnerOptions struct {
  325. Token *string `url:"token" json:"token"`
  326. }
  327. // DeleteRegisteredRunner registers a new Runner for the instance.
  328. //
  329. // GitLab API docs:
  330. // https://docs.gitlab.com/ce/api/runners.html#delete-a-registered-runner
  331. func (s *RunnersService) DeleteRegisteredRunner(opt *DeleteRegisteredRunnerOptions, options ...OptionFunc) (*Response, error) {
  332. req, err := s.client.NewRequest("DELETE", "runners", opt, options)
  333. if err != nil {
  334. return nil, err
  335. }
  336. return s.client.Do(req, nil)
  337. }
  338. // VerifyRegisteredRunnerOptions represents the available
  339. // VerifyRegisteredRunner() options.
  340. //
  341. // GitLab API docs:
  342. // https://docs.gitlab.com/ce/api/runners.html#verify-authentication-for-a-registered-runner
  343. type VerifyRegisteredRunnerOptions struct {
  344. Token *string `url:"token" json:"token"`
  345. }
  346. // VerifyRegisteredRunner registers a new Runner for the instance.
  347. //
  348. // GitLab API docs:
  349. // https://docs.gitlab.com/ce/api/runners.html#verify-authentication-for-a-registered-runner
  350. func (s *RunnersService) VerifyRegisteredRunner(opt *VerifyRegisteredRunnerOptions, options ...OptionFunc) (*Response, error) {
  351. req, err := s.client.NewRequest("POST", "runners/verify", opt, options)
  352. if err != nil {
  353. return nil, err
  354. }
  355. return s.client.Do(req, nil)
  356. }