runners.go 12 KB

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