commitstatus

package
v0.128.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextForStack added in v0.98.0

func ContextForStack(target, stack string) string

func FailureDescription added in v0.115.0

func FailureDescription(err error) string

FailureDescription normalizes err into a single-line, Unicode-safe description suitable for a commit status. Internal whitespace (including newlines) is collapsed to single spaces, and the result is truncated to at most maxDescriptionLength runes. A nil error returns "Failed".

func IsRateLimited added in v0.124.0

func IsRateLimited(err error) bool

IsRateLimited reports whether err is a provider's rate limit response: 429, or a 403 whose message mentions a rate limit (GitHub's primary and secondary limits).

func Post

func Post(ctx context.Context, provider Provider, apiBaseURL, repoURL, repoFullName, commitSHA, token string, status Status) error

Post posts a commit status to the appropriate Git provider. When provider is ProviderAuto ("") the provider is detected from repoURL. When apiBaseURL is non-empty, it overrides the inferred SCM API base URL. Returns nil (silently no-op) when token or commitSHA is empty.

func SuccessDescription added in v0.122.1

func SuccessDescription(startedAt, finishedAt time.Time) string

SuccessDescription describes a successful deployment with its duration. It omits the duration when either timestamp is missing or they are out of order.

func WithSingleAttempt added in v0.124.0

func WithSingleAttempt(ctx context.Context) context.Context

WithSingleAttempt returns a context whose commit status writes are sent once, without retries. Best-effort updates use it so they never wait on backoff or spend requests against a provider's rate limit.

Types

type Backend added in v0.124.0

type Backend string

Backend selects the provider API used to publish a reporting attempt.

const (
	// BackendStatus uses legacy commit statuses, supported by all providers.
	BackendStatus Backend = "status"
	// BackendChecks uses GitHub Checks, which support native queued, skipped, and timed-out results.
	BackendChecks Backend = "checks"
)

type Outcome added in v0.124.0

type Outcome string

Outcome distinguishes lifecycle events that legacy commit statuses conflate.

const (
	OutcomeQueued     Outcome = "queued"
	OutcomeInProgress Outcome = "in_progress"
	OutcomeDeferred   Outcome = "deferred"
	OutcomeSkipped    Outcome = "skipped"
	OutcomeTimedOut   Outcome = "timed_out"
)

func FailureOutcome added in v0.124.0

func FailureOutcome(err error) Outcome

FailureOutcome returns the Outcome corresponding to err, or "" if err is not a recognized failure type. This is used to normalize SCM API errors into commit status outcomes.

type Provider

type Provider string

Provider identifies which SCM API to use when posting a commit status. The zero value ("") means auto-detect from the repository URL.

const (
	// ProviderAuto detects the provider from the repository URL (default).
	ProviderAuto Provider = ""
	// ProviderGitHub targets the GitHub REST API.
	// For github.com the public API is used; for any other host the GitHub
	// Enterprise Server endpoint (/api/v3) is used instead.
	ProviderGitHub Provider = "github"
	// ProviderGitLab targets the GitLab API v4.
	ProviderGitLab Provider = "gitlab"
	// ProviderGitea targets the Gitea/Forgejo API v1.
	ProviderGitea Provider = "gitea"
	// ProviderAzureDevOps targets the Azure DevOps Git statuses API.
	ProviderAzureDevOps Provider = "azuredevops"
)

func ParseProvider

func ParseProvider(s string) (Provider, error)

ParseProvider converts a string (e.g. from an env var) to a Provider. Returns an error when the value is non-empty, not "auto", and not a known provider.

type Request added in v0.115.0

type Request struct {
	Provider     Provider
	APIBaseURL   string
	RepoURL      string
	RepoFullName string
	CommitSHA    string
	Token        string
	Context      string
	Target       *Target
}

Request is a fully-resolved commit status request, ready to Post or Get.

func ResolveRequest added in v0.115.0

func ResolveRequest(logger *slog.Logger, params RequestParams) (Request, bool)

ResolveRequest resolves a Request from params, applying credential precedence (a resolved scoped/GitHub App token takes priority over params.AccessToken), repository URL/full name fallback, and provider/API URL/context defaults. logger receives Debug/Warn diagnostics for skip reasons and token-resolution failures.

ok is false when the caller should skip posting/getting a commit status: commit statuses are disabled, the source is not Git, no commit SHA is available, or no credentials are configured.

func (Request) Get added in v0.115.0

func (r Request) Get(ctx context.Context) (Status, bool, error)

Get returns the latest commit status for the resolved request's context.

func (Request) Post added in v0.115.0

func (r Request) Post(ctx context.Context, status Status) error

Post posts status using the resolved request's provider/repository/credentials. status.Context is overridden with the request's resolved Context.

func (Request) SupportsProgress added in v0.124.0

func (r Request) SupportsProgress() bool

SupportsProgress reports whether the request's provider can show the phase of a running deployment. GitLab cannot: it rejects a running status for a pipeline that is already running, so its description cannot change.

type RequestParams added in v0.115.0

type RequestParams struct {
	// Enabled mirrors the application-level "post commit statuses" toggle
	// (e.g. app.Config.GitCommitStatus). When false, ResolveRequest always skips.
	Enabled bool
	// SourceIsGit must be true for commit statuses to be posted/read; OCI (or
	// any other non-Git) sources are always skipped.
	SourceIsGit bool
	// SourceURL is the repository URL used both for credential resolution and,
	// when PayloadWebURL/PayloadFullName are empty, as the repository identity.
	SourceURL string
	// CommitSHA is the resolved commit the status applies to. Empty skips.
	CommitSHA string
	// PayloadWebURL optionally overrides SourceURL as the repository URL (e.g.
	// the browsable web URL from a webhook payload).
	PayloadWebURL string
	// PayloadFullName optionally overrides the "owner/repo" full name derived
	// from the repository URL (e.g. from a webhook payload).
	PayloadFullName string
	// ProviderOverride is the raw configured SCM provider (e.g. app.Config.GitScmProvider).
	// Empty/"auto" auto-detects the provider from the repository URL.
	ProviderOverride string
	// APIBaseURL optionally overrides the inferred SCM API base URL (e.g. app.Config.GitScmApiUrl).
	APIBaseURL string
	// AccessToken is the configured fallback access token (e.g. app.Config.GitAccessToken),
	// used when no scoped/GitHub App credential is resolved for SourceURL.
	AccessToken string
	// ContextName is the commit status context label (e.g. "doco-cd/demo").
	// Empty defaults to DeployContext.
	ContextName string
	// Target pins an already-started reporting attempt, including handovers.
	Target *Target
	// Scope separates deployments with the same check name on different Docker contexts.
	Scope string
}

RequestParams holds the raw inputs needed to resolve a commit status Request. Callers (e.g. the source and stages packages) supply the relevant subset of their own application configuration and per-deployment context; RequestParams intentionally avoids depending on any higher-level config type to keep this package free of import cycles.

type State

type State string

State represents the commit status state sent to the Git provider.

const (
	StatePending State = "pending"
	StateSuccess State = "success"
	StateFailure State = "failure"
	StateError   State = "error"

	BaseContext   = "doco-cd"
	DeployContext = BaseContext + "/deploy"
)

type Status

type Status struct {
	State       State
	Outcome     Outcome
	Description string
	// Summary optionally explains the status in Markdown. Only GitHub check
	// runs show it, other providers only show the description.
	Summary string
	// Context is the label shown in the Git UI (e.g. "doco-cd/demo").
	// Defaults to BaseContext when empty.
	Context   string
	TargetURL string // optional link to deployment logs
	// Phase is the current phase of a running deployment, e.g. "pulling
	// images". GitHub check runs show it in their title.
	Phase string
}

Status holds the information to post as a commit status.

func FailureStatus added in v0.128.0

func FailureStatus(state State, err error) Status

FailureStatus keeps a short description for commit statuses and the full error as literal text for GitHub check summaries.

func Get

func Get(ctx context.Context, provider Provider, apiBaseURL, repoURL, repoFullName, commitSHA, token, contextName string) (Status, bool, error)

Get returns the latest commit status for the requested context. When provider is ProviderAuto ("") the provider is detected from repoURL. When apiBaseURL is non-empty, it overrides the inferred SCM API base URL. Returns found=false when token, commitSHA, or matching status is missing.

func ProgressStatus added in v0.124.0

func ProgressStatus(phase string) Status

ProgressStatus returns the status of a running deployment in phase, for example "In Progress: pulling images".

type Target added in v0.124.0

type Target struct {
	Backend    Backend   `json:"backend"`
	ExternalID string    `json:"external_id,omitempty"`
	CheckRunID int64     `json:"check_run_id,omitempty"`
	AppID      string    `json:"app_id,omitempty"`
	Scope      string    `json:"scope,omitempty"`
	StartedAt  time.Time `json:"started_at,omitzero"`
}

Target identifies a reporting attempt without storing its credentials.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL