...

Source file src/github.com/google/go-github/v55/github/code-scanning.go

Documentation: github.com/google/go-github/v55/github

     1  // Copyright 2020 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  	"strconv"
    12  	"strings"
    13  )
    14  
    15  // CodeScanningService handles communication with the code scanning related
    16  // methods of the GitHub API.
    17  //
    18  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
    19  type CodeScanningService service
    20  
    21  // Rule represents the complete details of GitHub Code Scanning alert type.
    22  type Rule struct {
    23  	ID                    *string  `json:"id,omitempty"`
    24  	Severity              *string  `json:"severity,omitempty"`
    25  	Description           *string  `json:"description,omitempty"`
    26  	Name                  *string  `json:"name,omitempty"`
    27  	SecuritySeverityLevel *string  `json:"security_severity_level,omitempty"`
    28  	FullDescription       *string  `json:"full_description,omitempty"`
    29  	Tags                  []string `json:"tags,omitempty"`
    30  	Help                  *string  `json:"help,omitempty"`
    31  }
    32  
    33  // Location represents the exact location of the GitHub Code Scanning Alert in the scanned project.
    34  type Location struct {
    35  	Path        *string `json:"path,omitempty"`
    36  	StartLine   *int    `json:"start_line,omitempty"`
    37  	EndLine     *int    `json:"end_line,omitempty"`
    38  	StartColumn *int    `json:"start_column,omitempty"`
    39  	EndColumn   *int    `json:"end_column,omitempty"`
    40  }
    41  
    42  // Message is a part of MostRecentInstance struct which provides the appropriate message when any action is performed on the analysis object.
    43  type Message struct {
    44  	Text *string `json:"text,omitempty"`
    45  }
    46  
    47  // MostRecentInstance provides details of the most recent instance of this alert for the default branch or for the specified Git reference.
    48  type MostRecentInstance struct {
    49  	Ref             *string   `json:"ref,omitempty"`
    50  	AnalysisKey     *string   `json:"analysis_key,omitempty"`
    51  	Category        *string   `json:"category,omitempty"`
    52  	Environment     *string   `json:"environment,omitempty"`
    53  	State           *string   `json:"state,omitempty"`
    54  	CommitSHA       *string   `json:"commit_sha,omitempty"`
    55  	Message         *Message  `json:"message,omitempty"`
    56  	Location        *Location `json:"location,omitempty"`
    57  	HTMLURL         *string   `json:"html_url,omitempty"`
    58  	Classifications []string  `json:"classifications,omitempty"`
    59  }
    60  
    61  // Tool represents the tool used to generate a GitHub Code Scanning Alert.
    62  type Tool struct {
    63  	Name    *string `json:"name,omitempty"`
    64  	GUID    *string `json:"guid,omitempty"`
    65  	Version *string `json:"version,omitempty"`
    66  }
    67  
    68  // Alert represents an individual GitHub Code Scanning Alert on a single repository.
    69  //
    70  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
    71  type Alert struct {
    72  	Number             *int                  `json:"number,omitempty"`
    73  	Repository         *Repository           `json:"repository,omitempty"`
    74  	RuleID             *string               `json:"rule_id,omitempty"`
    75  	RuleSeverity       *string               `json:"rule_severity,omitempty"`
    76  	RuleDescription    *string               `json:"rule_description,omitempty"`
    77  	Rule               *Rule                 `json:"rule,omitempty"`
    78  	Tool               *Tool                 `json:"tool,omitempty"`
    79  	CreatedAt          *Timestamp            `json:"created_at,omitempty"`
    80  	UpdatedAt          *Timestamp            `json:"updated_at,omitempty"`
    81  	FixedAt            *Timestamp            `json:"fixed_at,omitempty"`
    82  	State              *string               `json:"state,omitempty"`
    83  	ClosedBy           *User                 `json:"closed_by,omitempty"`
    84  	ClosedAt           *Timestamp            `json:"closed_at,omitempty"`
    85  	URL                *string               `json:"url,omitempty"`
    86  	HTMLURL            *string               `json:"html_url,omitempty"`
    87  	MostRecentInstance *MostRecentInstance   `json:"most_recent_instance,omitempty"`
    88  	Instances          []*MostRecentInstance `json:"instances,omitempty"`
    89  	DismissedBy        *User                 `json:"dismissed_by,omitempty"`
    90  	DismissedAt        *Timestamp            `json:"dismissed_at,omitempty"`
    91  	DismissedReason    *string               `json:"dismissed_reason,omitempty"`
    92  	DismissedComment   *string               `json:"dismissed_comment,omitempty"`
    93  	InstancesURL       *string               `json:"instances_url,omitempty"`
    94  }
    95  
    96  // ID returns the ID associated with an alert. It is the number at the end of the security alert's URL.
    97  func (a *Alert) ID() int64 {
    98  	if a == nil {
    99  		return 0
   100  	}
   101  
   102  	s := a.GetHTMLURL()
   103  
   104  	// Check for an ID to parse at the end of the url
   105  	if i := strings.LastIndex(s, "/"); i >= 0 {
   106  		s = s[i+1:]
   107  	}
   108  
   109  	// Return the alert ID as a 64-bit integer. Unable to convert or out of range returns 0.
   110  	id, err := strconv.ParseInt(s, 10, 64)
   111  	if err != nil {
   112  		return 0
   113  	}
   114  
   115  	return id
   116  }
   117  
   118  // AlertInstancesListOptions specifies optional parameters to the CodeScanningService.ListAlertInstances method.
   119  type AlertInstancesListOptions struct {
   120  	// Return code scanning alert instances for a specific branch reference.
   121  	// The ref can be formatted as refs/heads/<branch name> or simply <branch name>. To reference a pull request use refs/pull/<number>/merge
   122  	Ref string `url:"ref,omitempty"`
   123  
   124  	ListOptions
   125  }
   126  
   127  // AlertListOptions specifies optional parameters to the CodeScanningService.ListAlerts method.
   128  type AlertListOptions struct {
   129  	// State of the code scanning alerts to list. Set to closed to list only closed code scanning alerts. Default: open
   130  	State string `url:"state,omitempty"`
   131  
   132  	// Return code scanning alerts for a specific branch reference.
   133  	// The ref can be formatted as refs/heads/<branch name> or simply <branch name>. To reference a pull request use refs/pull/<number>/merge
   134  	Ref string `url:"ref,omitempty"`
   135  
   136  	// If specified, only code scanning alerts with this severity will be returned. Possible values are: critical, high, medium, low, warning, note, error.
   137  	Severity string `url:"severity,omitempty"`
   138  
   139  	// The name of a code scanning tool. Only results by this tool will be listed.
   140  	ToolName string `url:"tool_name,omitempty"`
   141  
   142  	ListCursorOptions
   143  
   144  	// Add ListOptions so offset pagination with integer type "page" query parameter is accepted
   145  	// since ListCursorOptions accepts "page" as string only.
   146  	ListOptions
   147  }
   148  
   149  // AnalysesListOptions specifies optional parameters to the CodeScanningService.ListAnalysesForRepo method.
   150  type AnalysesListOptions struct {
   151  	// Return code scanning analyses belonging to the same SARIF upload.
   152  	SarifID *string `url:"sarif_id,omitempty"`
   153  
   154  	// Return code scanning analyses for a specific branch reference.
   155  	// The ref can be formatted as refs/heads/<branch name> or simply <branch name>. To reference a pull request use refs/pull/<number>/merge
   156  	Ref *string `url:"ref,omitempty"`
   157  
   158  	ListOptions
   159  }
   160  
   161  // CodeQLDatabase represents a metadata about the CodeQL database.
   162  //
   163  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
   164  type CodeQLDatabase struct {
   165  	ID          *int64     `json:"id,omitempty"`
   166  	Name        *string    `json:"name,omitempty"`
   167  	Language    *string    `json:"language,omitempty"`
   168  	Uploader    *User      `json:"uploader,omitempty"`
   169  	ContentType *string    `json:"content_type,omitempty"`
   170  	Size        *int64     `json:"size,omitempty"`
   171  	CreatedAt   *Timestamp `json:"created_at,omitempty"`
   172  	UpdatedAt   *Timestamp `json:"updated_at,omitempty"`
   173  	URL         *string    `json:"url,omitempty"`
   174  }
   175  
   176  // ScanningAnalysis represents an individual GitHub Code Scanning ScanningAnalysis on a single repository.
   177  //
   178  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
   179  type ScanningAnalysis struct {
   180  	ID           *int64     `json:"id,omitempty"`
   181  	Ref          *string    `json:"ref,omitempty"`
   182  	CommitSHA    *string    `json:"commit_sha,omitempty"`
   183  	AnalysisKey  *string    `json:"analysis_key,omitempty"`
   184  	Environment  *string    `json:"environment,omitempty"`
   185  	Error        *string    `json:"error,omitempty"`
   186  	Category     *string    `json:"category,omitempty"`
   187  	CreatedAt    *Timestamp `json:"created_at,omitempty"`
   188  	ResultsCount *int       `json:"results_count,omitempty"`
   189  	RulesCount   *int       `json:"rules_count,omitempty"`
   190  	URL          *string    `json:"url,omitempty"`
   191  	SarifID      *string    `json:"sarif_id,omitempty"`
   192  	Tool         *Tool      `json:"tool,omitempty"`
   193  	Deletable    *bool      `json:"deletable,omitempty"`
   194  	Warning      *string    `json:"warning,omitempty"`
   195  }
   196  
   197  // SarifAnalysis specifies the results of a code scanning job.
   198  //
   199  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
   200  type SarifAnalysis struct {
   201  	CommitSHA   *string    `json:"commit_sha,omitempty"`
   202  	Ref         *string    `json:"ref,omitempty"`
   203  	Sarif       *string    `json:"sarif,omitempty"`
   204  	CheckoutURI *string    `json:"checkout_uri,omitempty"`
   205  	StartedAt   *Timestamp `json:"started_at,omitempty"`
   206  	ToolName    *string    `json:"tool_name,omitempty"`
   207  }
   208  
   209  // CodeScanningAlertState specifies the state of a code scanning alert.
   210  //
   211  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
   212  type CodeScanningAlertState struct {
   213  	// State sets the state of the code scanning alert and is a required field.
   214  	// You must also provide DismissedReason when you set the state to "dismissed".
   215  	// State can be one of: "open", "dismissed".
   216  	State string `json:"state"`
   217  	// DismissedReason represents the reason for dismissing or closing the alert.
   218  	// It is required when the state is "dismissed".
   219  	// It can be one of: "false positive", "won't fix", "used in tests".
   220  	DismissedReason *string `json:"dismissed_reason,omitempty"`
   221  	// DismissedComment is associated with the dismissal of the alert.
   222  	DismissedComment *string `json:"dismissed_comment,omitempty"`
   223  }
   224  
   225  // SarifID identifies a sarif analysis upload.
   226  //
   227  // GitHub API docs: https://docs.github.com/en/rest/code-scanning
   228  type SarifID struct {
   229  	ID  *string `json:"id,omitempty"`
   230  	URL *string `json:"url,omitempty"`
   231  }
   232  
   233  // ListAlertsForOrg lists code scanning alerts for an org.
   234  //
   235  // You must use an access token with the security_events scope to use this endpoint. GitHub Apps must have the security_events
   236  // read permission to use this endpoint.
   237  //
   238  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#list-code-scanning-alerts-for-an-organization
   239  func (s *CodeScanningService) ListAlertsForOrg(ctx context.Context, org string, opts *AlertListOptions) ([]*Alert, *Response, error) {
   240  	u := fmt.Sprintf("orgs/%v/code-scanning/alerts", org)
   241  	u, err := addOptions(u, opts)
   242  	if err != nil {
   243  		return nil, nil, err
   244  	}
   245  
   246  	req, err := s.client.NewRequest("GET", u, nil)
   247  	if err != nil {
   248  		return nil, nil, err
   249  	}
   250  
   251  	var alerts []*Alert
   252  	resp, err := s.client.Do(ctx, req, &alerts)
   253  	if err != nil {
   254  		return nil, resp, err
   255  	}
   256  
   257  	return alerts, resp, nil
   258  }
   259  
   260  // ListAlertsForRepo lists code scanning alerts for a repository.
   261  //
   262  // Lists all open code scanning alerts for the default branch (usually master) and protected branches in a repository.
   263  // You must use an access token with the security_events scope to use this endpoint. GitHub Apps must have the security_events
   264  // read permission to use this endpoint.
   265  //
   266  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#list-code-scanning-alerts-for-a-repository
   267  func (s *CodeScanningService) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *AlertListOptions) ([]*Alert, *Response, error) {
   268  	u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts", owner, repo)
   269  	u, err := addOptions(u, opts)
   270  	if err != nil {
   271  		return nil, nil, err
   272  	}
   273  
   274  	req, err := s.client.NewRequest("GET", u, nil)
   275  	if err != nil {
   276  		return nil, nil, err
   277  	}
   278  
   279  	var alerts []*Alert
   280  	resp, err := s.client.Do(ctx, req, &alerts)
   281  	if err != nil {
   282  		return nil, resp, err
   283  	}
   284  
   285  	return alerts, resp, nil
   286  }
   287  
   288  // GetAlert gets a single code scanning alert for a repository.
   289  //
   290  // You must use an access token with the security_events scope to use this endpoint.
   291  // GitHub Apps must have the security_events read permission to use this endpoint.
   292  //
   293  // The security alert_id is the number at the end of the security alert's URL.
   294  //
   295  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#get-a-code-scanning-alert
   296  func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, id int64) (*Alert, *Response, error) {
   297  	u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v", owner, repo, id)
   298  
   299  	req, err := s.client.NewRequest("GET", u, nil)
   300  	if err != nil {
   301  		return nil, nil, err
   302  	}
   303  
   304  	a := new(Alert)
   305  	resp, err := s.client.Do(ctx, req, a)
   306  	if err != nil {
   307  		return nil, resp, err
   308  	}
   309  
   310  	return a, resp, nil
   311  }
   312  
   313  // UpdateAlert updates the state of a single code scanning alert for a repository.
   314  //
   315  // You must use an access token with the security_events scope to use this endpoint.
   316  // GitHub Apps must have the security_events read permission to use this endpoint.
   317  //
   318  // The security alert_id is the number at the end of the security alert's URL.
   319  //
   320  // GitHub API docs: https://docs.github.com/en/rest/code-scanning?apiVersion=2022-11-28#update-a-code-scanning-alert
   321  func (s *CodeScanningService) UpdateAlert(ctx context.Context, owner, repo string, id int64, stateInfo *CodeScanningAlertState) (*Alert, *Response, error) {
   322  	u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v", owner, repo, id)
   323  
   324  	req, err := s.client.NewRequest("PATCH", u, stateInfo)
   325  	if err != nil {
   326  		return nil, nil, err
   327  	}
   328  
   329  	a := new(Alert)
   330  	resp, err := s.client.Do(ctx, req, a)
   331  	if err != nil {
   332  		return nil, resp, err
   333  	}
   334  
   335  	return a, resp, nil
   336  }
   337  
   338  // ListAlertInstances lists instances of a code scanning alert.
   339  //
   340  // You must use an access token with the security_events scope to use this endpoint.
   341  // GitHub Apps must have the security_events read permission to use this endpoint.
   342  //
   343  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#list-instances-of-a-code-scanning-alert
   344  func (s *CodeScanningService) ListAlertInstances(ctx context.Context, owner, repo string, id int64, opts *AlertInstancesListOptions) ([]*MostRecentInstance, *Response, error) {
   345  	u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v/instances", owner, repo, id)
   346  	u, err := addOptions(u, opts)
   347  	if err != nil {
   348  		return nil, nil, err
   349  	}
   350  
   351  	req, err := s.client.NewRequest("GET", u, nil)
   352  	if err != nil {
   353  		return nil, nil, err
   354  	}
   355  
   356  	var alertInstances []*MostRecentInstance
   357  	resp, err := s.client.Do(ctx, req, &alertInstances)
   358  	if err != nil {
   359  		return nil, resp, err
   360  	}
   361  
   362  	return alertInstances, resp, nil
   363  }
   364  
   365  // UploadSarif uploads the result of code scanning job to GitHub.
   366  //
   367  // For the parameter sarif, you must first compress your SARIF file using gzip and then translate the contents of the file into a Base64 encoding string.
   368  // You must use an access token with the security_events scope to use this endpoint. GitHub Apps must have the security_events
   369  // write permission to use this endpoint.
   370  //
   371  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#upload-an-analysis-as-sarif-data
   372  func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif *SarifAnalysis) (*SarifID, *Response, error) {
   373  	u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo)
   374  
   375  	req, err := s.client.NewRequest("POST", u, sarif)
   376  	if err != nil {
   377  		return nil, nil, err
   378  	}
   379  
   380  	sarifID := new(SarifID)
   381  	resp, err := s.client.Do(ctx, req, sarifID)
   382  	if err != nil {
   383  		return nil, resp, err
   384  	}
   385  
   386  	return sarifID, resp, nil
   387  }
   388  
   389  // SARIFUpload represents information about a SARIF upload.
   390  type SARIFUpload struct {
   391  	// `pending` files have not yet been processed, while `complete` means results from the SARIF have been stored.
   392  	// `failed` files have either not been processed at all, or could only be partially processed.
   393  	ProcessingStatus *string `json:"processing_status,omitempty"`
   394  	// The REST API URL for getting the analyses associated with the upload.
   395  	AnalysesURL *string `json:"analyses_url,omitempty"`
   396  }
   397  
   398  // GetSARIF gets information about a SARIF upload.
   399  //
   400  // You must use an access token with the security_events scope to use this endpoint.
   401  // GitHub Apps must have the security_events read permission to use this endpoint.
   402  //
   403  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#get-information-about-a-sarif-upload
   404  func (s *CodeScanningService) GetSARIF(ctx context.Context, owner, repo, sarifID string) (*SARIFUpload, *Response, error) {
   405  	u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs/%v", owner, repo, sarifID)
   406  
   407  	req, err := s.client.NewRequest("GET", u, nil)
   408  	if err != nil {
   409  		return nil, nil, err
   410  	}
   411  
   412  	sarifUpload := new(SARIFUpload)
   413  	resp, err := s.client.Do(ctx, req, sarifUpload)
   414  	if err != nil {
   415  		return nil, resp, err
   416  	}
   417  
   418  	return sarifUpload, resp, nil
   419  }
   420  
   421  // ListAnalysesForRepo lists code scanning analyses for a repository.
   422  //
   423  // Lists the details of all code scanning analyses for a repository, starting with the most recent.
   424  // You must use an access token with the security_events scope to use this endpoint.
   425  // GitHub Apps must have the security_events read permission to use this endpoint.
   426  //
   427  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#list-code-scanning-analyses-for-a-repository
   428  func (s *CodeScanningService) ListAnalysesForRepo(ctx context.Context, owner, repo string, opts *AnalysesListOptions) ([]*ScanningAnalysis, *Response, error) {
   429  	u := fmt.Sprintf("repos/%v/%v/code-scanning/analyses", owner, repo)
   430  	u, err := addOptions(u, opts)
   431  	if err != nil {
   432  		return nil, nil, err
   433  	}
   434  
   435  	req, err := s.client.NewRequest("GET", u, nil)
   436  	if err != nil {
   437  		return nil, nil, err
   438  	}
   439  
   440  	var analyses []*ScanningAnalysis
   441  	resp, err := s.client.Do(ctx, req, &analyses)
   442  	if err != nil {
   443  		return nil, resp, err
   444  	}
   445  
   446  	return analyses, resp, nil
   447  }
   448  
   449  // GetAnalysis gets a single code scanning analysis for a repository.
   450  //
   451  // You must use an access token with the security_events scope to use this endpoint.
   452  // GitHub Apps must have the security_events read permission to use this endpoint.
   453  //
   454  // The security analysis_id is the ID of the analysis, as returned from the ListAnalysesForRepo operation.
   455  //
   456  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#get-a-code-scanning-analysis-for-a-repository
   457  func (s *CodeScanningService) GetAnalysis(ctx context.Context, owner, repo string, id int64) (*ScanningAnalysis, *Response, error) {
   458  	u := fmt.Sprintf("repos/%v/%v/code-scanning/analyses/%v", owner, repo, id)
   459  
   460  	req, err := s.client.NewRequest("GET", u, nil)
   461  	if err != nil {
   462  		return nil, nil, err
   463  	}
   464  
   465  	analysis := new(ScanningAnalysis)
   466  	resp, err := s.client.Do(ctx, req, analysis)
   467  	if err != nil {
   468  		return nil, resp, err
   469  	}
   470  
   471  	return analysis, resp, nil
   472  }
   473  
   474  // DeleteAnalysis represents a successful deletion of a code scanning analysis.
   475  type DeleteAnalysis struct {
   476  	// Next deletable analysis in chain, without last analysis deletion confirmation
   477  	NextAnalysisURL *string `json:"next_analysis_url,omitempty"`
   478  	// Next deletable analysis in chain, with last analysis deletion confirmation
   479  	ConfirmDeleteURL *string `json:"confirm_delete_url,omitempty"`
   480  }
   481  
   482  // DeleteAnalysis deletes a single code scanning analysis from a repository.
   483  //
   484  // You must use an access token with the repo scope to use this endpoint.
   485  // GitHub Apps must have the security_events read permission to use this endpoint.
   486  //
   487  // The security analysis_id is the ID of the analysis, as returned from the ListAnalysesForRepo operation.
   488  //
   489  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#delete-a-code-scanning-analysis-from-a-repository
   490  func (s *CodeScanningService) DeleteAnalysis(ctx context.Context, owner, repo string, id int64) (*DeleteAnalysis, *Response, error) {
   491  	u := fmt.Sprintf("repos/%v/%v/code-scanning/analyses/%v", owner, repo, id)
   492  
   493  	req, err := s.client.NewRequest("DELETE", u, nil)
   494  	if err != nil {
   495  		return nil, nil, err
   496  	}
   497  
   498  	deleteAnalysis := new(DeleteAnalysis)
   499  	resp, err := s.client.Do(ctx, req, deleteAnalysis)
   500  	if err != nil {
   501  		return nil, resp, err
   502  	}
   503  
   504  	return deleteAnalysis, resp, nil
   505  }
   506  
   507  // ListCodeQLDatabases lists the CodeQL databases that are available in a repository.
   508  //
   509  // You must use an access token with the security_events scope to use this endpoint.
   510  // GitHub Apps must have the contents read permission to use this endpoint.
   511  //
   512  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#list-codeql-databases-for-a-repository
   513  func (s *CodeScanningService) ListCodeQLDatabases(ctx context.Context, owner, repo string) ([]*CodeQLDatabase, *Response, error) {
   514  	u := fmt.Sprintf("repos/%v/%v/code-scanning/codeql/databases", owner, repo)
   515  
   516  	req, err := s.client.NewRequest("GET", u, nil)
   517  	if err != nil {
   518  		return nil, nil, err
   519  	}
   520  
   521  	var codeqlDatabases []*CodeQLDatabase
   522  	resp, err := s.client.Do(ctx, req, &codeqlDatabases)
   523  	if err != nil {
   524  		return nil, resp, err
   525  	}
   526  
   527  	return codeqlDatabases, resp, nil
   528  }
   529  
   530  // GetCodeQLDatabase gets a CodeQL database for a language in a repository.
   531  //
   532  // You must use an access token with the security_events scope to use this endpoint.
   533  // GitHub Apps must have the contents read permission to use this endpoint.
   534  //
   535  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#get-a-codeql-database-for-a-repository
   536  func (s *CodeScanningService) GetCodeQLDatabase(ctx context.Context, owner, repo, language string) (*CodeQLDatabase, *Response, error) {
   537  	u := fmt.Sprintf("repos/%v/%v/code-scanning/codeql/databases/%v", owner, repo, language)
   538  
   539  	req, err := s.client.NewRequest("GET", u, nil)
   540  	if err != nil {
   541  		return nil, nil, err
   542  	}
   543  
   544  	codeqlDatabase := new(CodeQLDatabase)
   545  	resp, err := s.client.Do(ctx, req, codeqlDatabase)
   546  	if err != nil {
   547  		return nil, resp, err
   548  	}
   549  
   550  	return codeqlDatabase, resp, nil
   551  }
   552  
   553  // DefaultSetupConfiguration represents a code scanning default setup configuration.
   554  type DefaultSetupConfiguration struct {
   555  	State      *string    `json:"state,omitempty"`
   556  	Languages  []string   `json:"languages,omitempty"`
   557  	QuerySuite *string    `json:"query_suite,omitempty"`
   558  	UpdatedAt  *Timestamp `json:"updated_at,omitempty"`
   559  }
   560  
   561  // GetDefaultSetupConfiguration gets a code scanning default setup configuration.
   562  //
   563  // You must use an access token with the repo scope to use this
   564  // endpoint with private repos or the public_repo scope for public repos. GitHub Apps must have the repo write
   565  // permission to use this endpoint.
   566  //
   567  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#get-a-code-scanning-default-setup-configuration
   568  func (s *CodeScanningService) GetDefaultSetupConfiguration(ctx context.Context, owner, repo string) (*DefaultSetupConfiguration, *Response, error) {
   569  	u := fmt.Sprintf("repos/%s/%s/code-scanning/default-setup", owner, repo)
   570  
   571  	req, err := s.client.NewRequest("GET", u, nil)
   572  	if err != nil {
   573  		return nil, nil, err
   574  	}
   575  
   576  	cfg := new(DefaultSetupConfiguration)
   577  	resp, err := s.client.Do(ctx, req, cfg)
   578  	if err != nil {
   579  		return nil, resp, err
   580  	}
   581  
   582  	return cfg, resp, nil
   583  }
   584  
   585  // UpdateDefaultSetupConfigurationOptions specifies parameters to the CodeScanningService.UpdateDefaultSetupConfiguration
   586  // method.
   587  type UpdateDefaultSetupConfigurationOptions struct {
   588  	State      string   `json:"state"`
   589  	QuerySuite *string  `json:"query_suite,omitempty"`
   590  	Languages  []string `json:"languages,omitempty"`
   591  }
   592  
   593  // UpdateDefaultSetupConfigurationResponse represents a response from updating a code scanning default setup configuration.
   594  type UpdateDefaultSetupConfigurationResponse struct {
   595  	RunID  *int64  `json:"run_id,omitempty"`
   596  	RunURL *string `json:"run_url,omitempty"`
   597  }
   598  
   599  // UpdateDefaultSetupConfiguration updates a code scanning default setup configuration.
   600  //
   601  // You must use an access token with the repo scope to use this
   602  // endpoint with private repos or the public_repo scope for public repos. GitHub Apps must have the repo write
   603  // permission to use this endpoint.
   604  //
   605  // This method might return an AcceptedError and a status code of 202. This is because this is the status that GitHub
   606  // returns to signify that it has now scheduled the update of the pull request branch in a background task.
   607  //
   608  // GitHub API docs: https://docs.github.com/en/rest/code-scanning/code-scanning#update-a-code-scanning-default-setup-configuration
   609  func (s *CodeScanningService) UpdateDefaultSetupConfiguration(ctx context.Context, owner, repo string, options *UpdateDefaultSetupConfigurationOptions) (*UpdateDefaultSetupConfigurationResponse, *Response, error) {
   610  	u := fmt.Sprintf("repos/%s/%s/code-scanning/default-setup", owner, repo)
   611  
   612  	req, err := s.client.NewRequest("PATCH", u, options)
   613  	if err != nil {
   614  		return nil, nil, err
   615  	}
   616  
   617  	a := new(UpdateDefaultSetupConfigurationResponse)
   618  	resp, err := s.client.Do(ctx, req, a)
   619  	if err != nil {
   620  		return nil, resp, err
   621  	}
   622  
   623  	return a, resp, nil
   624  }
   625  

View as plain text