You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checks.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // Copyright 2018 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "net/url"
  10. )
  11. // ChecksService provides access to the Checks API in the
  12. // GitHub API.
  13. //
  14. // GitHub API docs: https://developer.github.com/v3/checks/
  15. type ChecksService service
  16. // CheckRun represents a GitHub check run on a repository associated with a GitHub app.
  17. type CheckRun struct {
  18. ID *int64 `json:"id,omitempty"`
  19. NodeID *string `json:"node_id,omitempty"`
  20. HeadSHA *string `json:"head_sha,omitempty"`
  21. ExternalID *string `json:"external_id,omitempty"`
  22. URL *string `json:"url,omitempty"`
  23. HTMLURL *string `json:"html_url,omitempty"`
  24. DetailsURL *string `json:"details_url,omitempty"`
  25. Status *string `json:"status,omitempty"`
  26. Conclusion *string `json:"conclusion,omitempty"`
  27. StartedAt *Timestamp `json:"started_at,omitempty"`
  28. CompletedAt *Timestamp `json:"completed_at,omitempty"`
  29. Output *CheckRunOutput `json:"output,omitempty"`
  30. Name *string `json:"name,omitempty"`
  31. CheckSuite *CheckSuite `json:"check_suite,omitempty"`
  32. App *App `json:"app,omitempty"`
  33. PullRequests []*PullRequest `json:"pull_requests,omitempty"`
  34. }
  35. // CheckRunOutput represents the output of a CheckRun.
  36. type CheckRunOutput struct {
  37. Title *string `json:"title,omitempty"`
  38. Summary *string `json:"summary,omitempty"`
  39. Text *string `json:"text,omitempty"`
  40. AnnotationsCount *int `json:"annotations_count,omitempty"`
  41. AnnotationsURL *string `json:"annotations_url,omitempty"`
  42. Annotations []*CheckRunAnnotation `json:"annotations,omitempty"`
  43. Images []*CheckRunImage `json:"images,omitempty"`
  44. }
  45. // CheckRunAnnotation represents an annotation object for a CheckRun output.
  46. type CheckRunAnnotation struct {
  47. Path *string `json:"path,omitempty"`
  48. BlobHRef *string `json:"blob_href,omitempty"`
  49. StartLine *int `json:"start_line,omitempty"`
  50. EndLine *int `json:"end_line,omitempty"`
  51. AnnotationLevel *string `json:"annotation_level,omitempty"`
  52. Message *string `json:"message,omitempty"`
  53. Title *string `json:"title,omitempty"`
  54. RawDetails *string `json:"raw_details,omitempty"`
  55. }
  56. // CheckRunImage represents an image object for a CheckRun output.
  57. type CheckRunImage struct {
  58. Alt *string `json:"alt,omitempty"`
  59. ImageURL *string `json:"image_url,omitempty"`
  60. Caption *string `json:"caption,omitempty"`
  61. }
  62. // CheckSuite represents a suite of check runs.
  63. type CheckSuite struct {
  64. ID *int64 `json:"id,omitempty"`
  65. NodeID *string `json:"node_id,omitempty"`
  66. HeadBranch *string `json:"head_branch,omitempty"`
  67. HeadSHA *string `json:"head_sha,omitempty"`
  68. URL *string `json:"url,omitempty"`
  69. BeforeSHA *string `json:"before,omitempty"`
  70. AfterSHA *string `json:"after,omitempty"`
  71. Status *string `json:"status,omitempty"`
  72. Conclusion *string `json:"conclusion,omitempty"`
  73. App *App `json:"app,omitempty"`
  74. Repository *Repository `json:"repository,omitempty"`
  75. PullRequests []*PullRequest `json:"pull_requests,omitempty"`
  76. }
  77. func (c CheckRun) String() string {
  78. return Stringify(c)
  79. }
  80. func (c CheckSuite) String() string {
  81. return Stringify(c)
  82. }
  83. // GetCheckRun gets a check-run for a repository.
  84. //
  85. // GitHub API docs: https://developer.github.com/v3/checks/runs/#get-a-single-check-run
  86. func (s *ChecksService) GetCheckRun(ctx context.Context, owner, repo string, checkRunID int64) (*CheckRun, *Response, error) {
  87. u := fmt.Sprintf("repos/%v/%v/check-runs/%v", owner, repo, checkRunID)
  88. req, err := s.client.NewRequest("GET", u, nil)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  93. checkRun := new(CheckRun)
  94. resp, err := s.client.Do(ctx, req, checkRun)
  95. if err != nil {
  96. return nil, resp, err
  97. }
  98. return checkRun, resp, nil
  99. }
  100. // GetCheckSuite gets a single check suite.
  101. //
  102. // GitHub API docs: https://developer.github.com/v3/checks/suites/#get-a-single-check-suite
  103. func (s *ChecksService) GetCheckSuite(ctx context.Context, owner, repo string, checkSuiteID int64) (*CheckSuite, *Response, error) {
  104. u := fmt.Sprintf("repos/%v/%v/check-suites/%v", owner, repo, checkSuiteID)
  105. req, err := s.client.NewRequest("GET", u, nil)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  110. checkSuite := new(CheckSuite)
  111. resp, err := s.client.Do(ctx, req, checkSuite)
  112. if err != nil {
  113. return nil, resp, err
  114. }
  115. return checkSuite, resp, nil
  116. }
  117. // CreateCheckRunOptions sets up parameters needed to create a CheckRun.
  118. type CreateCheckRunOptions struct {
  119. Name string `json:"name"` // The name of the check (e.g., "code-coverage"). (Required.)
  120. HeadBranch string `json:"head_branch"` // The name of the branch to perform a check against. (Required.)
  121. HeadSHA string `json:"head_sha"` // The SHA of the commit. (Required.)
  122. DetailsURL *string `json:"details_url,omitempty"` // The URL of the integrator's site that has the full details of the check. (Optional.)
  123. ExternalID *string `json:"external_id,omitempty"` // A reference for the run on the integrator's system. (Optional.)
  124. Status *string `json:"status,omitempty"` // The current status. Can be one of "queued", "in_progress", or "completed". Default: "queued". (Optional.)
  125. Conclusion *string `json:"conclusion,omitempty"` // Can be one of "success", "failure", "neutral", "cancelled", "timed_out", or "action_required". (Optional. Required if you provide a status of "completed".)
  126. StartedAt *Timestamp `json:"started_at,omitempty"` // The time that the check run began. (Optional.)
  127. CompletedAt *Timestamp `json:"completed_at,omitempty"` // The time the check completed. (Optional. Required if you provide conclusion.)
  128. Output *CheckRunOutput `json:"output,omitempty"` // Provide descriptive details about the run. (Optional)
  129. Actions []*CheckRunAction `json:"actions,omitempty"` // Possible further actions the integrator can perform, which a user may trigger. (Optional.)
  130. }
  131. // CheckRunAction exposes further actions the integrator can perform, which a user may trigger.
  132. type CheckRunAction struct {
  133. Label string `json:"label"` // The text to be displayed on a button in the web UI. The maximum size is 20 characters. (Required.)
  134. Description string `json:"description"` // A short explanation of what this action would do. The maximum size is 40 characters. (Required.)
  135. Identifier string `json:"identifier"` // A reference for the action on the integrator's system. The maximum size is 20 characters. (Required.)
  136. }
  137. // CreateCheckRun creates a check run for repository.
  138. //
  139. // GitHub API docs: https://developer.github.com/v3/checks/runs/#create-a-check-run
  140. func (s *ChecksService) CreateCheckRun(ctx context.Context, owner, repo string, opt CreateCheckRunOptions) (*CheckRun, *Response, error) {
  141. u := fmt.Sprintf("repos/%v/%v/check-runs", owner, repo)
  142. req, err := s.client.NewRequest("POST", u, opt)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  147. checkRun := new(CheckRun)
  148. resp, err := s.client.Do(ctx, req, checkRun)
  149. if err != nil {
  150. return nil, resp, err
  151. }
  152. return checkRun, resp, nil
  153. }
  154. // UpdateCheckRunOptions sets up parameters needed to update a CheckRun.
  155. type UpdateCheckRunOptions struct {
  156. Name string `json:"name"` // The name of the check (e.g., "code-coverage"). (Required.)
  157. HeadBranch *string `json:"head_branch,omitempty"` // The name of the branch to perform a check against. (Optional.)
  158. HeadSHA *string `json:"head_sha,omitempty"` // The SHA of the commit. (Optional.)
  159. DetailsURL *string `json:"details_url,omitempty"` // The URL of the integrator's site that has the full details of the check. (Optional.)
  160. ExternalID *string `json:"external_id,omitempty"` // A reference for the run on the integrator's system. (Optional.)
  161. Status *string `json:"status,omitempty"` // The current status. Can be one of "queued", "in_progress", or "completed". Default: "queued". (Optional.)
  162. Conclusion *string `json:"conclusion,omitempty"` // Can be one of "success", "failure", "neutral", "cancelled", "timed_out", or "action_required". (Optional. Required if you provide a status of "completed".)
  163. CompletedAt *Timestamp `json:"completed_at,omitempty"` // The time the check completed. (Optional. Required if you provide conclusion.)
  164. Output *CheckRunOutput `json:"output,omitempty"` // Provide descriptive details about the run. (Optional)
  165. Actions []*CheckRunAction `json:"actions,omitempty"` // Possible further actions the integrator can perform, which a user may trigger. (Optional.)
  166. }
  167. // UpdateCheckRun updates a check run for a specific commit in a repository.
  168. //
  169. // GitHub API docs: https://developer.github.com/v3/checks/runs/#update-a-check-run
  170. func (s *ChecksService) UpdateCheckRun(ctx context.Context, owner, repo string, checkRunID int64, opt UpdateCheckRunOptions) (*CheckRun, *Response, error) {
  171. u := fmt.Sprintf("repos/%v/%v/check-runs/%v", owner, repo, checkRunID)
  172. req, err := s.client.NewRequest("PATCH", u, opt)
  173. if err != nil {
  174. return nil, nil, err
  175. }
  176. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  177. checkRun := new(CheckRun)
  178. resp, err := s.client.Do(ctx, req, checkRun)
  179. if err != nil {
  180. return nil, resp, err
  181. }
  182. return checkRun, resp, nil
  183. }
  184. // ListCheckRunAnnotations lists the annotations for a check run.
  185. //
  186. // GitHub API docs: https://developer.github.com/v3/checks/runs/#list-annotations-for-a-check-run
  187. func (s *ChecksService) ListCheckRunAnnotations(ctx context.Context, owner, repo string, checkRunID int64, opt *ListOptions) ([]*CheckRunAnnotation, *Response, error) {
  188. u := fmt.Sprintf("repos/%v/%v/check-runs/%v/annotations", owner, repo, checkRunID)
  189. u, err := addOptions(u, opt)
  190. if err != nil {
  191. return nil, nil, err
  192. }
  193. req, err := s.client.NewRequest("GET", u, nil)
  194. if err != nil {
  195. return nil, nil, err
  196. }
  197. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  198. var checkRunAnnotations []*CheckRunAnnotation
  199. resp, err := s.client.Do(ctx, req, &checkRunAnnotations)
  200. if err != nil {
  201. return nil, resp, err
  202. }
  203. return checkRunAnnotations, resp, nil
  204. }
  205. // ListCheckRunsOptions represents parameters to list check runs.
  206. type ListCheckRunsOptions struct {
  207. CheckName *string `url:"check_name,omitempty"` // Returns check runs with the specified name.
  208. Status *string `url:"status,omitempty"` // Returns check runs with the specified status. Can be one of "queued", "in_progress", or "completed".
  209. Filter *string `url:"filter,omitempty"` // Filters check runs by their completed_at timestamp. Can be one of "latest" (returning the most recent check runs) or "all". Default: "latest"
  210. ListOptions
  211. }
  212. // ListCheckRunsResults represents the result of a check run list.
  213. type ListCheckRunsResults struct {
  214. Total *int `json:"total_count,omitempty"`
  215. CheckRuns []*CheckRun `json:"check_runs,omitempty"`
  216. }
  217. // ListCheckRunsForRef lists check runs for a specific ref.
  218. //
  219. // GitHub API docs: https://developer.github.com/v3/checks/runs/#list-check-runs-for-a-specific-ref
  220. func (s *ChecksService) ListCheckRunsForRef(ctx context.Context, owner, repo, ref string, opt *ListCheckRunsOptions) (*ListCheckRunsResults, *Response, error) {
  221. u := fmt.Sprintf("repos/%v/%v/commits/%v/check-runs", owner, repo, url.QueryEscape(ref))
  222. u, err := addOptions(u, opt)
  223. if err != nil {
  224. return nil, nil, err
  225. }
  226. req, err := s.client.NewRequest("GET", u, nil)
  227. if err != nil {
  228. return nil, nil, err
  229. }
  230. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  231. var checkRunResults *ListCheckRunsResults
  232. resp, err := s.client.Do(ctx, req, &checkRunResults)
  233. if err != nil {
  234. return nil, resp, err
  235. }
  236. return checkRunResults, resp, nil
  237. }
  238. // ListCheckRunsCheckSuite lists check runs for a check suite.
  239. //
  240. // GitHub API docs: https://developer.github.com/v3/checks/runs/#list-check-runs-in-a-check-suite
  241. func (s *ChecksService) ListCheckRunsCheckSuite(ctx context.Context, owner, repo string, checkSuiteID int64, opt *ListCheckRunsOptions) (*ListCheckRunsResults, *Response, error) {
  242. u := fmt.Sprintf("repos/%v/%v/check-suites/%v/check-runs", owner, repo, checkSuiteID)
  243. u, err := addOptions(u, opt)
  244. if err != nil {
  245. return nil, nil, err
  246. }
  247. req, err := s.client.NewRequest("GET", u, nil)
  248. if err != nil {
  249. return nil, nil, err
  250. }
  251. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  252. var checkRunResults *ListCheckRunsResults
  253. resp, err := s.client.Do(ctx, req, &checkRunResults)
  254. if err != nil {
  255. return nil, resp, err
  256. }
  257. return checkRunResults, resp, nil
  258. }
  259. // ListCheckSuiteOptions represents parameters to list check suites.
  260. type ListCheckSuiteOptions struct {
  261. CheckName *string `url:"check_name,omitempty"` // Filters checks suites by the name of the check run.
  262. AppID *int `url:"app_id,omitempty"` // Filters check suites by GitHub App id.
  263. ListOptions
  264. }
  265. // ListCheckSuiteResults represents the result of a check run list.
  266. type ListCheckSuiteResults struct {
  267. Total *int `json:"total_count,omitempty"`
  268. CheckSuites []*CheckSuite `json:"check_suites,omitempty"`
  269. }
  270. // ListCheckSuitesForRef lists check suite for a specific ref.
  271. //
  272. // GitHub API docs: https://developer.github.com/v3/checks/suites/#list-check-suites-for-a-specific-ref
  273. func (s *ChecksService) ListCheckSuitesForRef(ctx context.Context, owner, repo, ref string, opt *ListCheckSuiteOptions) (*ListCheckSuiteResults, *Response, error) {
  274. u := fmt.Sprintf("repos/%v/%v/commits/%v/check-suites", owner, repo, url.QueryEscape(ref))
  275. u, err := addOptions(u, opt)
  276. if err != nil {
  277. return nil, nil, err
  278. }
  279. req, err := s.client.NewRequest("GET", u, nil)
  280. if err != nil {
  281. return nil, nil, err
  282. }
  283. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  284. var checkSuiteResults *ListCheckSuiteResults
  285. resp, err := s.client.Do(ctx, req, &checkSuiteResults)
  286. if err != nil {
  287. return nil, resp, err
  288. }
  289. return checkSuiteResults, resp, nil
  290. }
  291. // AutoTriggerCheck enables or disables automatic creation of CheckSuite events upon pushes to the repository.
  292. type AutoTriggerCheck struct {
  293. AppID *int64 `json:"app_id,omitempty"` // The id of the GitHub App. (Required.)
  294. Setting *bool `json:"setting,omitempty"` // Set to "true" to enable automatic creation of CheckSuite events upon pushes to the repository, or "false" to disable them. Default: "true" (Required.)
  295. }
  296. // CheckSuitePreferenceOptions set options for check suite preferences for a repository.
  297. type CheckSuitePreferenceOptions struct {
  298. PreferenceList *PreferenceList `json:"auto_trigger_checks,omitempty"` // A list of auto trigger checks that can be set for a check suite in a repository.
  299. }
  300. // CheckSuitePreferenceResults represents the results of the preference set operation.
  301. type CheckSuitePreferenceResults struct {
  302. Preferences *PreferenceList `json:"preferences,omitempty"`
  303. Repository *Repository `json:"repository,omitempty"`
  304. }
  305. // PreferenceList represents a list of auto trigger checks for repository
  306. type PreferenceList struct {
  307. AutoTriggerChecks []*AutoTriggerCheck `json:"auto_trigger_checks,omitempty"` // A slice of auto trigger checks that can be set for a check suite in a repository.
  308. }
  309. // SetCheckSuitePreferences changes the default automatic flow when creating check suites.
  310. //
  311. // GitHub API docs: https://developer.github.com/v3/checks/suites/#set-preferences-for-check-suites-on-a-repository
  312. func (s *ChecksService) SetCheckSuitePreferences(ctx context.Context, owner, repo string, opt CheckSuitePreferenceOptions) (*CheckSuitePreferenceResults, *Response, error) {
  313. u := fmt.Sprintf("repos/%v/%v/check-suites/preferences", owner, repo)
  314. req, err := s.client.NewRequest("PATCH", u, opt)
  315. if err != nil {
  316. return nil, nil, err
  317. }
  318. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  319. var checkSuitePrefResults *CheckSuitePreferenceResults
  320. resp, err := s.client.Do(ctx, req, &checkSuitePrefResults)
  321. if err != nil {
  322. return nil, resp, err
  323. }
  324. return checkSuitePrefResults, resp, nil
  325. }
  326. // CreateCheckSuiteOptions sets up parameters to manually create a check suites
  327. type CreateCheckSuiteOptions struct {
  328. HeadSHA string `json:"head_sha"` // The sha of the head commit. (Required.)
  329. HeadBranch *string `json:"head_branch,omitempty"` // The name of the head branch where the code changes are implemented.
  330. }
  331. // CreateCheckSuite manually creates a check suite for a repository.
  332. //
  333. // GitHub API docs: https://developer.github.com/v3/checks/suites/#create-a-check-suite
  334. func (s *ChecksService) CreateCheckSuite(ctx context.Context, owner, repo string, opt CreateCheckSuiteOptions) (*CheckSuite, *Response, error) {
  335. u := fmt.Sprintf("repos/%v/%v/check-suites", owner, repo)
  336. req, err := s.client.NewRequest("POST", u, opt)
  337. if err != nil {
  338. return nil, nil, err
  339. }
  340. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  341. checkSuite := new(CheckSuite)
  342. resp, err := s.client.Do(ctx, req, checkSuite)
  343. if err != nil {
  344. return nil, resp, err
  345. }
  346. return checkSuite, resp, nil
  347. }
  348. // ReRequestCheckSuite triggers GitHub to rerequest an existing check suite, without pushing new code to a repository.
  349. //
  350. // GitHub API docs: https://developer.github.com/v3/checks/suites/#rerequest-check-suite
  351. func (s *ChecksService) ReRequestCheckSuite(ctx context.Context, owner, repo string, checkSuiteID int64) (*Response, error) {
  352. u := fmt.Sprintf("repos/%v/%v/check-suites/%v/rerequest", owner, repo, checkSuiteID)
  353. req, err := s.client.NewRequest("POST", u, nil)
  354. if err != nil {
  355. return nil, err
  356. }
  357. req.Header.Set("Accept", mediaTypeCheckRunsPreview)
  358. resp, err := s.client.Do(ctx, req, nil)
  359. return resp, err
  360. }