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