Documentation
¶
Index ¶
- Constants
- Variables
- func APIURL() string
- func DefaultCorrelator() string
- func DownloadAsset(ctx context.Context, token string, a ReleaseAsset, destDir string) (string, error)
- func EncodeSARIF(sarifJSON []byte) (string, error)
- func IsPermission(err error) bool
- func IsStatus(err error, statuses ...int) bool
- func RepoFromRemote(remote string) string
- func ResolveToken() (token, source string, err error)
- type APIError
- type Artifact
- type ArtifactCollector
- func (c *ArtifactCollector) DownloadArtifact(ctx context.Context, artifact Artifact) (string, error)
- func (c *ArtifactCollector) GetRepository(ctx context.Context) (*Repository, error)
- func (c *ArtifactCollector) GetWorkflowRun(ctx context.Context) (*WorkflowRun, error)
- func (c *ArtifactCollector) ListArtifacts(ctx context.Context) ([]Artifact, error)
- func (c *ArtifactCollector) ListPullsForCommit(ctx context.Context, sha string) ([]PullRequestRef, error)
- type ArtifactMetadata
- type ArtifactsResponse
- type Client
- func (c *Client) Do(ctx context.Context, method, path string, body, out any) (http.Header, error)
- func (c *Client) GetAll(ctx context.Context, path string, out any) error
- func (c *Client) SubmitSnapshot(ctx context.Context, repo string, s *Snapshot) (*SnapshotResult, error)
- func (c *Client) UploadSARIF(ctx context.Context, t Target, sarifJSON []byte, checkoutURI string) (*SARIFUpload, error)
- func (c *Client) WaitSARIF(ctx context.Context, repo, id string, timeout, interval time.Duration) (*SARIFUpload, error)
- type Manifest
- type ManifestFile
- type PullRequestRef
- type Release
- type ReleaseAsset
- type Repository
- type ResolvedPackage
- type SARIFUpload
- type Snapshot
- type SnapshotDetector
- type SnapshotJob
- type SnapshotOptions
- type SnapshotResult
- type Target
- type User
- type WorkflowRun
Constants ¶
const DefaultAPIURL = "https://api.github.com"
DefaultAPIURL is github.com's REST endpoint.
const MaxSARIFUploadBytes = 10 << 20
MaxSARIFUploadBytes is code scanning's limit on the gzipped document.
Variables ¶
var ErrNoToken = errors.New("no GitHub token found: set GITHUB_TOKEN or run 'gh auth login'")
ErrNoToken is returned when no GitHub token can be found.
var ErrRateLimited = errors.New("GitHub API rate limit exhausted")
ErrRateLimited is returned when the rate limit outlasts MaxWait.
Functions ¶
func DefaultCorrelator ¶ added in v3.161.0
func DefaultCorrelator() string
DefaultCorrelator is vulnetix/<workflow>/<job> from the Actions environment.
func DownloadAsset ¶ added in v3.84.0
func DownloadAsset(ctx context.Context, token string, a ReleaseAsset, destDir string) (string, error)
DownloadAsset writes an asset into destDir and returns its path.
func EncodeSARIF ¶ added in v3.161.0
EncodeSARIF gzips and base64-encodes a SARIF document as the API wants.
func IsPermission ¶ added in v3.161.0
IsPermission reports a refusal that more permissions would fix: 401, 403 (not a rate limit) or 404, which GitHub answers for a resource the token cannot see. Publishing treats these as warnings.
func IsStatus ¶ added in v3.161.0
IsStatus reports whether err is an APIError with one of the statuses.
func RepoFromRemote ¶ added in v3.161.0
RepoFromRemote reads owner/name from a git remote URL (https or ssh), or returns "".
func ResolveToken ¶ added in v3.161.0
ResolveToken finds a GitHub token: GITHUB_TOKEN, then GH_TOKEN (the gh CLI's convention), then `gh auth token`. source names where it came from ("env:GITHUB_TOKEN", "env:GH_TOKEN", "gh-cli") for status output; the token itself is never printed.
Types ¶
type Artifact ¶
type Artifact struct {
ID int64 `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
SizeInBytes int64 `json:"size_in_bytes"`
URL string `json:"url"`
ArchiveDownloadURL string `json:"archive_download_url"`
Expired bool `json:"expired"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ExpiresAt time.Time `json:"expires_at"`
}
Artifact represents a GitHub Actions artifact
type ArtifactCollector ¶
type ArtifactCollector struct {
// contains filtered or unexported fields
}
ArtifactCollector handles collection of GitHub Actions artifacts
func NewArtifactCollector ¶
func NewArtifactCollector(token, apiURL, repository, runID string) *ArtifactCollector
NewArtifactCollector creates a new artifact collector
func (*ArtifactCollector) DownloadArtifact ¶
func (c *ArtifactCollector) DownloadArtifact(ctx context.Context, artifact Artifact) (string, error)
DownloadArtifact downloads an artifact and extracts it to a temporary directory
func (*ArtifactCollector) GetRepository ¶ added in v3.73.0
func (c *ArtifactCollector) GetRepository(ctx context.Context) (*Repository, error)
GetRepository fetches the repository's metadata.
func (*ArtifactCollector) GetWorkflowRun ¶ added in v3.73.0
func (c *ArtifactCollector) GetWorkflowRun(ctx context.Context) (*WorkflowRun, error)
GetWorkflowRun fetches this run's metadata.
func (*ArtifactCollector) ListArtifacts ¶
func (c *ArtifactCollector) ListArtifacts(ctx context.Context) ([]Artifact, error)
ListArtifacts lists all artifacts for the current workflow run
func (*ArtifactCollector) ListPullsForCommit ¶ added in v3.73.0
func (c *ArtifactCollector) ListPullsForCommit(ctx context.Context, sha string) ([]PullRequestRef, error)
ListPullsForCommit finds the pull requests a commit belongs to. Used on a push to a branch that has an open PR, where the event payload carries no PR number.
type ArtifactMetadata ¶
type ArtifactMetadata struct {
Repository string `json:"repository"`
RepositoryOwner string `json:"repository_owner"`
RunID string `json:"run_id"`
RunNumber string `json:"run_number"`
WorkflowName string `json:"workflow_name"`
JobName string `json:"job"`
SHA string `json:"sha"`
RefName string `json:"ref_name"`
RefType string `json:"ref_type"`
EventName string `json:"event_name"`
Actor string `json:"actor"`
ServerURL string `json:"server_url"`
APIURL string `json:"api_url"`
Artifacts []string `json:"artifacts"`
ExtraEnvVars map[string]string `json:"extra_env_vars,omitempty"`
}
ArtifactMetadata contains metadata about the workflow and artifacts
func CollectMetadata ¶
func CollectMetadata(artifactNames []string) *ArtifactMetadata
CollectMetadata collects metadata from GitHub Actions environment
type ArtifactsResponse ¶
type ArtifactsResponse struct {
TotalCount int `json:"total_count"`
Artifacts []Artifact `json:"artifacts"`
}
ArtifactsResponse represents the GitHub API response for artifacts
type Client ¶ added in v3.161.0
type Client struct {
BaseURL string
Token string
HTTP *http.Client
// MaxWait bounds the time one request spends waiting out rate limits.
MaxWait time.Duration
// Calls counts requests made.
Calls int
}
Client makes authenticated GitHub REST calls.
func (*Client) Do ¶ added in v3.161.0
Do sends body (JSON-encoded unless it is []byte or nil) and decodes a JSON answer into out (when non-nil). It returns the response headers.
func (*Client) GetAll ¶ added in v3.161.0
GetAll fetches every page of a list endpoint and decodes the concatenated items into out (a pointer to a slice).
func (*Client) SubmitSnapshot ¶ added in v3.161.0
func (c *Client) SubmitSnapshot(ctx context.Context, repo string, s *Snapshot) (*SnapshotResult, error)
SubmitSnapshot posts a snapshot.
func (*Client) UploadSARIF ¶ added in v3.161.0
func (c *Client) UploadSARIF(ctx context.Context, t Target, sarifJSON []byte, checkoutURI string) (*SARIFUpload, error)
UploadSARIF posts one SARIF document for the target commit and ref.
func (*Client) WaitSARIF ¶ added in v3.161.0
func (c *Client) WaitSARIF(ctx context.Context, repo, id string, timeout, interval time.Duration) (*SARIFUpload, error)
WaitSARIF polls an upload until it is complete or failed, or until timeout. A timeout is not an error: GitHub keeps processing, and the upload stands.
type Manifest ¶ added in v3.161.0
type Manifest struct {
Name string `json:"name"`
File *ManifestFile `json:"file,omitempty"`
Resolved map[string]ResolvedPackage `json:"resolved"`
}
Manifest is one manifest's resolved packages.
type ManifestFile ¶ added in v3.161.0
type ManifestFile struct {
SourceLocation string `json:"source_location"`
}
ManifestFile is the manifest's path in the repository.
type PullRequestRef ¶ added in v3.73.0
type PullRequestRef struct {
Number int `json:"number"`
State string `json:"state"`
Base struct {
Ref string `json:"ref"`
} `json:"base"`
Head struct {
Ref string `json:"ref"`
} `json:"head"`
}
PullRequestRef is one PR a commit belongs to.
type Release ¶ added in v3.84.0
type Release struct {
TagName string `json:"tag_name"`
Assets []ReleaseAsset `json:"assets"`
}
func FetchReleaseByTag ¶ added in v3.84.0
func FetchReleaseByTag(ctx context.Context, token, apiURL, repository, tag string) (*Release, error)
FetchReleaseByTag reads one release by its tag.
apiURL is a parameter rather than a constant because GitHub Enterprise serves a different host, and guessing github.com there would read the wrong instance.
func (*Release) AssetMatching ¶ added in v3.84.0
func (r *Release) AssetMatching(substr string) *ReleaseAsset
AssetMatching finds the first asset whose name contains substr, ignoring case.
Substring rather than exact match because projects spell the same file checksums.txt, SHA256SUMS and tool_checksums.txt, and requiring each repository to declare which would be a per-repository input for a question the release itself already answers.
type ReleaseAsset ¶ added in v3.84.0
type Repository ¶ added in v3.73.0
type Repository struct {
ID int64 `json:"id"`
FullName string `json:"full_name"`
Private bool `json:"private"`
Visibility string `json:"visibility"`
DefaultBranch string `json:"default_branch"`
Owner *User `json:"owner"`
License *struct {
SpdxID string `json:"spdx_id"`
} `json:"license"`
}
Repository is the subset of GET /repos/{repo} worth keeping.
type ResolvedPackage ¶ added in v3.161.0
type ResolvedPackage struct {
PackageURL string `json:"package_url"`
Relationship string `json:"relationship,omitempty"`
Scope string `json:"scope,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
}
ResolvedPackage is one package in a manifest.
type SARIFUpload ¶ added in v3.161.0
type SARIFUpload struct {
ID string `json:"id"`
URL string `json:"url"`
ProcessingStatus string `json:"processing_status,omitempty"`
AnalysesURL string `json:"analyses_url,omitempty"`
Errors []string `json:"errors,omitempty"`
}
SARIFUpload is one upload's id and processing state.
type Snapshot ¶ added in v3.161.0
type Snapshot struct {
Version int `json:"version"`
SHA string `json:"sha"`
Ref string `json:"ref"`
Job SnapshotJob `json:"job"`
Detector SnapshotDetector `json:"detector"`
Scanned string `json:"scanned"`
Manifests map[string]Manifest `json:"manifests"`
}
Snapshot is the dependency submission API's request body.
func BuildSnapshot ¶ added in v3.161.0
func BuildSnapshot(cdxJSON []byte, o SnapshotOptions) (*Snapshot, error)
BuildSnapshot turns a CycloneDX JSON document into a snapshot.
func (*Snapshot) ManifestCount ¶ added in v3.161.0
ManifestCount and PackageCount summarise a snapshot.
func (*Snapshot) PackageCount ¶ added in v3.161.0
type SnapshotDetector ¶ added in v3.161.0
type SnapshotDetector struct {
Name string `json:"name"`
Version string `json:"version"`
URL string `json:"url"`
}
SnapshotDetector names the tool.
type SnapshotJob ¶ added in v3.161.0
type SnapshotJob struct {
Correlator string `json:"correlator"`
ID string `json:"id"`
HTMLURL string `json:"html_url,omitempty"`
}
SnapshotJob identifies the run; the correlator is what makes a later snapshot replace this one rather than add to it.
type SnapshotOptions ¶ added in v3.161.0
type SnapshotOptions struct {
Target Target
Correlator string
JobID string
JobURL string
DetectorVersion string
// DefaultManifest names the manifest for components with no
// vulnetix:source-file (an SBOM this CLI did not write).
DefaultManifest string
Now time.Time
}
SnapshotOptions are the run details a snapshot carries.
type SnapshotResult ¶ added in v3.161.0
type SnapshotResult struct {
ID int64 `json:"id"`
CreatedAt string `json:"created_at"`
Result string `json:"result"`
Message string `json:"message"`
}
SnapshotResult is the API's answer.
type Target ¶ added in v3.161.0
type Target struct {
// Repo is owner/name.
Repo string
// SHA is the full commit SHA.
SHA string
// Ref is the full ref (refs/heads/main, refs/pull/7/merge).
Ref string
}
Target is the repository, commit and ref a publish applies to.
func ResolveTarget ¶ added in v3.161.0
ResolveTarget works out the repository, commit and ref, explicit values first, then the GitHub Actions environment (GITHUB_REPOSITORY, GITHUB_SHA, GITHUB_REF; on pull_request the merge ref and merge commit, which is what code scanning expects), then the git checkout at dir. It errors when any of the three is still unknown.
type WorkflowRun ¶ added in v3.73.0
type WorkflowRun struct {
ID int64 `json:"id"`
Name string `json:"name"`
RunNumber int `json:"run_number"`
RunAttempt int `json:"run_attempt"`
Event string `json:"event"`
Status string `json:"status"`
Conclusion string `json:"conclusion"`
HeadBranch string `json:"head_branch"`
HeadSHA string `json:"head_sha"`
HTMLURL string `json:"html_url"`
Path string `json:"path"`
Actor *User `json:"actor"`
TriggeringActor *User `json:"triggering_actor"`
PullRequests []struct {
Number int `json:"number"`
} `json:"pull_requests"`
}
WorkflowRun is the subset of GET /repos/{repo}/actions/runs/{id} worth keeping.