github.com/google/go-github/v70@v70.0.0/github/checks.go (about) 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 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // ChecksService provides access to the Checks API in the 14 // GitHub API. 15 // 16 // GitHub API docs: https://docs.github.com/rest/checks/ 17 type ChecksService service 18 19 // CheckRun represents a GitHub check run on a repository associated with a GitHub app. 20 type CheckRun struct { 21 ID *int64 `json:"id,omitempty"` 22 NodeID *string `json:"node_id,omitempty"` 23 HeadSHA *string `json:"head_sha,omitempty"` 24 ExternalID *string `json:"external_id,omitempty"` 25 URL *string `json:"url,omitempty"` 26 HTMLURL *string `json:"html_url,omitempty"` 27 DetailsURL *string `json:"details_url,omitempty"` 28 Status *string `json:"status,omitempty"` 29 Conclusion *string `json:"conclusion,omitempty"` 30 StartedAt *Timestamp `json:"started_at,omitempty"` 31 CompletedAt *Timestamp `json:"completed_at,omitempty"` 32 Output *CheckRunOutput `json:"output,omitempty"` 33 Name *string `json:"name,omitempty"` 34 CheckSuite *CheckSuite `json:"check_suite,omitempty"` 35 App *App `json:"app,omitempty"` 36 PullRequests []*PullRequest `json:"pull_requests,omitempty"` 37 } 38 39 // CheckRunOutput represents the output of a CheckRun. 40 type CheckRunOutput struct { 41 Title *string `json:"title,omitempty"` 42 Summary *string `json:"summary,omitempty"` 43 Text *string `json:"text,omitempty"` 44 AnnotationsCount *int `json:"annotations_count,omitempty"` 45 AnnotationsURL *string `json:"annotations_url,omitempty"` 46 Annotations []*CheckRunAnnotation `json:"annotations,omitempty"` 47 Images []*CheckRunImage `json:"images,omitempty"` 48 } 49 50 // CheckRunAnnotation represents an annotation object for a CheckRun output. 51 type CheckRunAnnotation struct { 52 Path *string `json:"path,omitempty"` 53 StartLine *int `json:"start_line,omitempty"` 54 EndLine *int `json:"end_line,omitempty"` 55 StartColumn *int `json:"start_column,omitempty"` 56 EndColumn *int `json:"end_column,omitempty"` 57 AnnotationLevel *string `json:"annotation_level,omitempty"` 58 Message *string `json:"message,omitempty"` 59 Title *string `json:"title,omitempty"` 60 RawDetails *string `json:"raw_details,omitempty"` 61 } 62 63 // CheckRunImage represents an image object for a CheckRun output. 64 type CheckRunImage struct { 65 Alt *string `json:"alt,omitempty"` 66 ImageURL *string `json:"image_url,omitempty"` 67 Caption *string `json:"caption,omitempty"` 68 } 69 70 // CheckSuite represents a suite of check runs. 71 type CheckSuite struct { 72 ID *int64 `json:"id,omitempty"` 73 NodeID *string `json:"node_id,omitempty"` 74 HeadBranch *string `json:"head_branch,omitempty"` 75 HeadSHA *string `json:"head_sha,omitempty"` 76 URL *string `json:"url,omitempty"` 77 BeforeSHA *string `json:"before,omitempty"` 78 AfterSHA *string `json:"after,omitempty"` 79 Status *string `json:"status,omitempty"` 80 Conclusion *string `json:"conclusion,omitempty"` 81 CreatedAt *Timestamp `json:"created_at,omitempty"` 82 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 83 App *App `json:"app,omitempty"` 84 Repository *Repository `json:"repository,omitempty"` 85 PullRequests []*PullRequest `json:"pull_requests,omitempty"` 86 87 // The following fields are only populated by Webhook events. 88 HeadCommit *Commit `json:"head_commit,omitempty"` 89 LatestCheckRunsCount *int64 `json:"latest_check_runs_count,omitempty"` 90 Rerequestable *bool `json:"rerequestable,omitempty"` 91 RunsRerequestable *bool `json:"runs_rerequestable,omitempty"` 92 } 93 94 func (c CheckRun) String() string { 95 return Stringify(c) 96 } 97 98 func (c CheckSuite) String() string { 99 return Stringify(c) 100 } 101 102 // GetCheckRun gets a check-run for a repository. 103 // 104 // GitHub API docs: https://docs.github.com/rest/checks/runs#get-a-check-run 105 // 106 //meta:operation GET /repos/{owner}/{repo}/check-runs/{check_run_id} 107 func (s *ChecksService) GetCheckRun(ctx context.Context, owner, repo string, checkRunID int64) (*CheckRun, *Response, error) { 108 u := fmt.Sprintf("repos/%v/%v/check-runs/%v", owner, repo, checkRunID) 109 req, err := s.client.NewRequest("GET", u, nil) 110 if err != nil { 111 return nil, nil, err 112 } 113 114 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 115 116 checkRun := new(CheckRun) 117 resp, err := s.client.Do(ctx, req, checkRun) 118 if err != nil { 119 return nil, resp, err 120 } 121 122 return checkRun, resp, nil 123 } 124 125 // GetCheckSuite gets a single check suite. 126 // 127 // GitHub API docs: https://docs.github.com/rest/checks/suites#get-a-check-suite 128 // 129 //meta:operation GET /repos/{owner}/{repo}/check-suites/{check_suite_id} 130 func (s *ChecksService) GetCheckSuite(ctx context.Context, owner, repo string, checkSuiteID int64) (*CheckSuite, *Response, error) { 131 u := fmt.Sprintf("repos/%v/%v/check-suites/%v", owner, repo, checkSuiteID) 132 req, err := s.client.NewRequest("GET", u, nil) 133 if err != nil { 134 return nil, nil, err 135 } 136 137 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 138 139 checkSuite := new(CheckSuite) 140 resp, err := s.client.Do(ctx, req, checkSuite) 141 if err != nil { 142 return nil, resp, err 143 } 144 145 return checkSuite, resp, nil 146 } 147 148 // CreateCheckRunOptions sets up parameters needed to create a CheckRun. 149 type CreateCheckRunOptions struct { 150 Name string `json:"name"` // The name of the check (e.g., "code-coverage"). (Required.) 151 HeadSHA string `json:"head_sha"` // The SHA of the commit. (Required.) 152 DetailsURL *string `json:"details_url,omitempty"` // The URL of the integrator's site that has the full details of the check. (Optional.) 153 ExternalID *string `json:"external_id,omitempty"` // A reference for the run on the integrator's system. (Optional.) 154 Status *string `json:"status,omitempty"` // The current status. Can be one of "queued", "in_progress", or "completed". Default: "queued". (Optional.) 155 Conclusion *string `json:"conclusion,omitempty"` // Can be one of "success", "failure", "neutral", "cancelled", "skipped", "timed_out", or "action_required". (Optional. Required if you provide a status of "completed".) 156 StartedAt *Timestamp `json:"started_at,omitempty"` // The time that the check run began. (Optional.) 157 CompletedAt *Timestamp `json:"completed_at,omitempty"` // The time the check completed. (Optional. Required if you provide conclusion.) 158 Output *CheckRunOutput `json:"output,omitempty"` // Provide descriptive details about the run. (Optional) 159 Actions []*CheckRunAction `json:"actions,omitempty"` // Possible further actions the integrator can perform, which a user may trigger. (Optional.) 160 } 161 162 // CheckRunAction exposes further actions the integrator can perform, which a user may trigger. 163 type CheckRunAction struct { 164 Label string `json:"label"` // The text to be displayed on a button in the web UI. The maximum size is 20 characters. (Required.) 165 Description string `json:"description"` // A short explanation of what this action would do. The maximum size is 40 characters. (Required.) 166 Identifier string `json:"identifier"` // A reference for the action on the integrator's system. The maximum size is 20 characters. (Required.) 167 } 168 169 // CreateCheckRun creates a check run for repository. 170 // 171 // GitHub API docs: https://docs.github.com/rest/checks/runs#create-a-check-run 172 // 173 //meta:operation POST /repos/{owner}/{repo}/check-runs 174 func (s *ChecksService) CreateCheckRun(ctx context.Context, owner, repo string, opts CreateCheckRunOptions) (*CheckRun, *Response, error) { 175 u := fmt.Sprintf("repos/%v/%v/check-runs", owner, repo) 176 req, err := s.client.NewRequest("POST", u, opts) 177 if err != nil { 178 return nil, nil, err 179 } 180 181 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 182 183 checkRun := new(CheckRun) 184 resp, err := s.client.Do(ctx, req, checkRun) 185 if err != nil { 186 return nil, resp, err 187 } 188 189 return checkRun, resp, nil 190 } 191 192 // UpdateCheckRunOptions sets up parameters needed to update a CheckRun. 193 type UpdateCheckRunOptions struct { 194 Name string `json:"name"` // The name of the check (e.g., "code-coverage"). (Required.) 195 DetailsURL *string `json:"details_url,omitempty"` // The URL of the integrator's site that has the full details of the check. (Optional.) 196 ExternalID *string `json:"external_id,omitempty"` // A reference for the run on the integrator's system. (Optional.) 197 Status *string `json:"status,omitempty"` // The current status. Can be one of "queued", "in_progress", or "completed". Default: "queued". (Optional.) 198 Conclusion *string `json:"conclusion,omitempty"` // Can be one of "success", "failure", "neutral", "cancelled", "skipped", "timed_out", or "action_required". (Optional. Required if you provide a status of "completed".) 199 CompletedAt *Timestamp `json:"completed_at,omitempty"` // The time the check completed. (Optional. Required if you provide conclusion.) 200 Output *CheckRunOutput `json:"output,omitempty"` // Provide descriptive details about the run. (Optional) 201 Actions []*CheckRunAction `json:"actions,omitempty"` // Possible further actions the integrator can perform, which a user may trigger. (Optional.) 202 } 203 204 // UpdateCheckRun updates a check run for a specific commit in a repository. 205 // 206 // GitHub API docs: https://docs.github.com/rest/checks/runs#update-a-check-run 207 // 208 //meta:operation PATCH /repos/{owner}/{repo}/check-runs/{check_run_id} 209 func (s *ChecksService) UpdateCheckRun(ctx context.Context, owner, repo string, checkRunID int64, opts UpdateCheckRunOptions) (*CheckRun, *Response, error) { 210 u := fmt.Sprintf("repos/%v/%v/check-runs/%v", owner, repo, checkRunID) 211 req, err := s.client.NewRequest("PATCH", u, opts) 212 if err != nil { 213 return nil, nil, err 214 } 215 216 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 217 218 checkRun := new(CheckRun) 219 resp, err := s.client.Do(ctx, req, checkRun) 220 if err != nil { 221 return nil, resp, err 222 } 223 224 return checkRun, resp, nil 225 } 226 227 // ListCheckRunAnnotations lists the annotations for a check run. 228 // 229 // GitHub API docs: https://docs.github.com/rest/checks/runs#list-check-run-annotations 230 // 231 //meta:operation GET /repos/{owner}/{repo}/check-runs/{check_run_id}/annotations 232 func (s *ChecksService) ListCheckRunAnnotations(ctx context.Context, owner, repo string, checkRunID int64, opts *ListOptions) ([]*CheckRunAnnotation, *Response, error) { 233 u := fmt.Sprintf("repos/%v/%v/check-runs/%v/annotations", owner, repo, checkRunID) 234 u, err := addOptions(u, opts) 235 if err != nil { 236 return nil, nil, err 237 } 238 239 req, err := s.client.NewRequest("GET", u, nil) 240 if err != nil { 241 return nil, nil, err 242 } 243 244 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 245 246 var checkRunAnnotations []*CheckRunAnnotation 247 resp, err := s.client.Do(ctx, req, &checkRunAnnotations) 248 if err != nil { 249 return nil, resp, err 250 } 251 252 return checkRunAnnotations, resp, nil 253 } 254 255 // ListCheckRunsOptions represents parameters to list check runs. 256 type ListCheckRunsOptions struct { 257 CheckName *string `url:"check_name,omitempty"` // Returns check runs with the specified name. 258 Status *string `url:"status,omitempty"` // Returns check runs with the specified status. Can be one of "queued", "in_progress", or "completed". 259 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" 260 AppID *int64 `url:"app_id,omitempty"` // Filters check runs by GitHub App ID. 261 262 ListOptions 263 } 264 265 // ListCheckRunsResults represents the result of a check run list. 266 type ListCheckRunsResults struct { 267 Total *int `json:"total_count,omitempty"` 268 CheckRuns []*CheckRun `json:"check_runs,omitempty"` 269 } 270 271 // ListCheckRunsForRef lists check runs for a specific ref. 272 // 273 // GitHub API docs: https://docs.github.com/rest/checks/runs#list-check-runs-for-a-git-reference 274 // 275 //meta:operation GET /repos/{owner}/{repo}/commits/{ref}/check-runs 276 func (s *ChecksService) ListCheckRunsForRef(ctx context.Context, owner, repo, ref string, opts *ListCheckRunsOptions) (*ListCheckRunsResults, *Response, error) { 277 u := fmt.Sprintf("repos/%v/%v/commits/%v/check-runs", owner, repo, refURLEscape(ref)) 278 u, err := addOptions(u, opts) 279 if err != nil { 280 return nil, nil, err 281 } 282 283 req, err := s.client.NewRequest("GET", u, nil) 284 if err != nil { 285 return nil, nil, err 286 } 287 288 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 289 290 var checkRunResults *ListCheckRunsResults 291 resp, err := s.client.Do(ctx, req, &checkRunResults) 292 if err != nil { 293 return nil, resp, err 294 } 295 296 return checkRunResults, resp, nil 297 } 298 299 // ListCheckRunsCheckSuite lists check runs for a check suite. 300 // 301 // GitHub API docs: https://docs.github.com/rest/checks/runs#list-check-runs-in-a-check-suite 302 // 303 //meta:operation GET /repos/{owner}/{repo}/check-suites/{check_suite_id}/check-runs 304 func (s *ChecksService) ListCheckRunsCheckSuite(ctx context.Context, owner, repo string, checkSuiteID int64, opts *ListCheckRunsOptions) (*ListCheckRunsResults, *Response, error) { 305 u := fmt.Sprintf("repos/%v/%v/check-suites/%v/check-runs", owner, repo, checkSuiteID) 306 u, err := addOptions(u, opts) 307 if err != nil { 308 return nil, nil, err 309 } 310 311 req, err := s.client.NewRequest("GET", u, nil) 312 if err != nil { 313 return nil, nil, err 314 } 315 316 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 317 318 var checkRunResults *ListCheckRunsResults 319 resp, err := s.client.Do(ctx, req, &checkRunResults) 320 if err != nil { 321 return nil, resp, err 322 } 323 324 return checkRunResults, resp, nil 325 } 326 327 // ReRequestCheckRun triggers GitHub to rerequest an existing check run. 328 // 329 // GitHub API docs: https://docs.github.com/rest/checks/runs#rerequest-a-check-run 330 // 331 //meta:operation POST /repos/{owner}/{repo}/check-runs/{check_run_id}/rerequest 332 func (s *ChecksService) ReRequestCheckRun(ctx context.Context, owner, repo string, checkRunID int64) (*Response, error) { 333 u := fmt.Sprintf("repos/%v/%v/check-runs/%v/rerequest", owner, repo, checkRunID) 334 335 req, err := s.client.NewRequest("POST", u, nil) 336 if err != nil { 337 return nil, err 338 } 339 340 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 341 342 return s.client.Do(ctx, req, nil) 343 } 344 345 // ListCheckSuiteOptions represents parameters to list check suites. 346 type ListCheckSuiteOptions struct { 347 CheckName *string `url:"check_name,omitempty"` // Filters checks suites by the name of the check run. 348 AppID *int `url:"app_id,omitempty"` // Filters check suites by GitHub App id. 349 350 ListOptions 351 } 352 353 // ListCheckSuiteResults represents the result of a check run list. 354 type ListCheckSuiteResults struct { 355 Total *int `json:"total_count,omitempty"` 356 CheckSuites []*CheckSuite `json:"check_suites,omitempty"` 357 } 358 359 // ListCheckSuitesForRef lists check suite for a specific ref. 360 // 361 // GitHub API docs: https://docs.github.com/rest/checks/suites#list-check-suites-for-a-git-reference 362 // 363 //meta:operation GET /repos/{owner}/{repo}/commits/{ref}/check-suites 364 func (s *ChecksService) ListCheckSuitesForRef(ctx context.Context, owner, repo, ref string, opts *ListCheckSuiteOptions) (*ListCheckSuiteResults, *Response, error) { 365 u := fmt.Sprintf("repos/%v/%v/commits/%v/check-suites", owner, repo, refURLEscape(ref)) 366 u, err := addOptions(u, opts) 367 if err != nil { 368 return nil, nil, err 369 } 370 371 req, err := s.client.NewRequest("GET", u, nil) 372 if err != nil { 373 return nil, nil, err 374 } 375 376 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 377 378 var checkSuiteResults *ListCheckSuiteResults 379 resp, err := s.client.Do(ctx, req, &checkSuiteResults) 380 if err != nil { 381 return nil, resp, err 382 } 383 384 return checkSuiteResults, resp, nil 385 } 386 387 // AutoTriggerCheck enables or disables automatic creation of CheckSuite events upon pushes to the repository. 388 type AutoTriggerCheck struct { 389 AppID *int64 `json:"app_id,omitempty"` // The id of the GitHub App. (Required.) 390 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.) 391 } 392 393 // CheckSuitePreferenceOptions set options for check suite preferences for a repository. 394 type CheckSuitePreferenceOptions struct { 395 AutoTriggerChecks []*AutoTriggerCheck `json:"auto_trigger_checks,omitempty"` // A slice of auto trigger checks that can be set for a check suite in a repository. 396 } 397 398 // CheckSuitePreferenceResults represents the results of the preference set operation. 399 type CheckSuitePreferenceResults struct { 400 Preferences *PreferenceList `json:"preferences,omitempty"` 401 Repository *Repository `json:"repository,omitempty"` 402 } 403 404 // PreferenceList represents a list of auto trigger checks for repository. 405 type PreferenceList struct { 406 AutoTriggerChecks []*AutoTriggerCheck `json:"auto_trigger_checks,omitempty"` // A slice of auto trigger checks that can be set for a check suite in a repository. 407 } 408 409 // SetCheckSuitePreferences changes the default automatic flow when creating check suites. 410 // 411 // GitHub API docs: https://docs.github.com/rest/checks/suites#update-repository-preferences-for-check-suites 412 // 413 //meta:operation PATCH /repos/{owner}/{repo}/check-suites/preferences 414 func (s *ChecksService) SetCheckSuitePreferences(ctx context.Context, owner, repo string, opts CheckSuitePreferenceOptions) (*CheckSuitePreferenceResults, *Response, error) { 415 u := fmt.Sprintf("repos/%v/%v/check-suites/preferences", owner, repo) 416 req, err := s.client.NewRequest("PATCH", u, opts) 417 if err != nil { 418 return nil, nil, err 419 } 420 421 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 422 423 var checkSuitePrefResults *CheckSuitePreferenceResults 424 resp, err := s.client.Do(ctx, req, &checkSuitePrefResults) 425 if err != nil { 426 return nil, resp, err 427 } 428 429 return checkSuitePrefResults, resp, nil 430 } 431 432 // CreateCheckSuiteOptions sets up parameters to manually create a check suites. 433 type CreateCheckSuiteOptions struct { 434 HeadSHA string `json:"head_sha"` // The sha of the head commit. (Required.) 435 HeadBranch *string `json:"head_branch,omitempty"` // The name of the head branch where the code changes are implemented. 436 } 437 438 // CreateCheckSuite manually creates a check suite for a repository. 439 // 440 // GitHub API docs: https://docs.github.com/rest/checks/suites#create-a-check-suite 441 // 442 //meta:operation POST /repos/{owner}/{repo}/check-suites 443 func (s *ChecksService) CreateCheckSuite(ctx context.Context, owner, repo string, opts CreateCheckSuiteOptions) (*CheckSuite, *Response, error) { 444 u := fmt.Sprintf("repos/%v/%v/check-suites", owner, repo) 445 req, err := s.client.NewRequest("POST", u, opts) 446 if err != nil { 447 return nil, nil, err 448 } 449 450 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 451 452 checkSuite := new(CheckSuite) 453 resp, err := s.client.Do(ctx, req, checkSuite) 454 if err != nil { 455 return nil, resp, err 456 } 457 458 return checkSuite, resp, nil 459 } 460 461 // ReRequestCheckSuite triggers GitHub to rerequest an existing check suite, without pushing new code to a repository. 462 // 463 // GitHub API docs: https://docs.github.com/rest/checks/suites#rerequest-a-check-suite 464 // 465 //meta:operation POST /repos/{owner}/{repo}/check-suites/{check_suite_id}/rerequest 466 func (s *ChecksService) ReRequestCheckSuite(ctx context.Context, owner, repo string, checkSuiteID int64) (*Response, error) { 467 u := fmt.Sprintf("repos/%v/%v/check-suites/%v/rerequest", owner, repo, checkSuiteID) 468 469 req, err := s.client.NewRequest("POST", u, nil) 470 if err != nil { 471 return nil, err 472 } 473 474 req.Header.Set("Accept", mediaTypeCheckRunsPreview) 475 476 resp, err := s.client.Do(ctx, req, nil) 477 return resp, err 478 }