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