scm

package
v1.55.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("operation not supported by this provider")

ErrUnsupported is returned by optional Host methods that the provider cannot fulfil. Callers should gate calls on Capabilities rather than relying on this error, but implementations return it as a fallback.

Functions

func AuthConfigured

func AuthConfigured(ctx context.Context, provider Provider, workDir string) bool

func CLIAvailable

func CLIAvailable(provider Provider) bool

func ClampPRBody

func ClampPRBody(body string, max int) string

ClampPRBody truncates body to at most max PRBodyLen units, cutting on a rune boundary and appending a truncation marker (kept inside the budget) when it cuts. max <= 0 means unlimited and returns body unchanged. This is the last-resort backstop: callers that can shed whole sections to fit a budget should do so before relying on a blind clamp.

func ExtractHost

func ExtractHost(remote string) string

ExtractHost returns the lowercased host (without any port) from a git remote URL. It handles both scp-like syntax (git@host:group/project) and URL forms (https://host/group/project, ssh://git@host:22/group/project). It returns "" when no host can be determined.

func ExtractPRNumber

func ExtractPRNumber(prURL string) (string, error)

ExtractPRNumber returns the trailing numeric segment from a PR/MR URL. Supports GitHub (/pull/N), GitLab (/-/merge_requests/N), Bitbucket (/pull-requests/N), and Azure DevOps (/pullrequest/N) URLs; all of them end in a digit path segment.

func MaxPRBodyChars

func MaxPRBodyChars(p Provider) int

MaxPRBodyChars returns the hard limit a provider enforces on a PR description, measured in PRBodyLen units, or 0 when the provider imposes no practical limit. Azure DevOps rejects `az repos pr create`/`update` with "Invalid argument value. ... A description for a pull request must not be longer than 4000 characters."; GitHub and GitLab allow far larger bodies than this tool ever produces, so they report 0 (unlimited).

func PRBodyLen

func PRBodyLen(s string) int

PRBodyLen reports the length of s the way Azure DevOps (a .NET service) measures a PR description: in UTF-16 code units, so a non-BMP rune (an emoji, some CJK) counts as two. It is the strictest common denominator across providers, so a body that "fits" by this measure is genuinely safe to send.

func ResolveHost

func ResolveHost(ctx context.Context, remote string) string

ResolveHost returns the canonical host for a remote. For SSH remotes it honors HostName mappings from the user's SSH configuration while preserving the original remote URL for all Git operations.

Types

type AttestationPublicationIdentityReader

type AttestationPublicationIdentityReader interface {
	FindAttestationPublicationIdentity(ctx context.Context, headSHA, publicationNonce string) (CheckAttemptIdentity, bool, error)
}

type Capabilities

type Capabilities struct {
	MergeableState  bool
	FailedCheckLogs bool
}

Capabilities declares which optional Host methods return meaningful data. Callers must consult Capabilities before invoking optional methods.

type Check

type Check struct {
	Name   string
	Bucket CheckBucket
	// State is the provider's own outcome string for the check (GitHub
	// conclusions such as FAILURE, TIMED_OUT, CANCELLED). Buckets collapse
	// several outcomes into one value, so callers that must tell an
	// infrastructure outcome from a real job failure read this. Empty when the
	// provider reported no state.
	State       string
	CompletedAt time.Time // zero when unknown; used to detect CI re-runs between polls
	// Link is the provider's details URL for this check. It identifies the job
	// behind the check, so a rerun can target that job instead of the whole PR.
	// Empty when the provider reported no link.
	Link string
}

Check is a single CI check result on a PR.

func (Check) Failing

func (c Check) Failing() bool

Failing reports whether the check is in a failed bucket.

func (Check) Pending

func (c Check) Pending() bool

Pending reports whether the check is still running or queued.

type CheckAttemptIdentity

type CheckAttemptIdentity struct {
	RunID            int64
	RunNumber        int64
	RunAttempt       int
	Event            string
	HeadSHA          string
	PublicationNonce string
}

type CheckAttemptIdentityReader

type CheckAttemptIdentityReader interface {
	GetCheckAttemptIdentity(ctx context.Context, check Check) (CheckAttemptIdentity, error)
}

type CheckBucket

type CheckBucket string

CheckBucket is the normalized outcome of a CI check.

const (
	CheckBucketPass    CheckBucket = "pass"
	CheckBucketFail    CheckBucket = "fail"
	CheckBucketPending CheckBucket = "pending"
	CheckBucketCancel  CheckBucket = "cancel"
	CheckBucketSkip    CheckBucket = "skipping"
)

type CheckRerunner

type CheckRerunner interface {
	// RerunCheck asks the provider to run check again for the same commit. It
	// returns an error when the request could not be made, including when the
	// check names no job the provider can re-run.
	RerunCheck(ctx context.Context, pr *PR, check Check) error
}

CheckRerunner re-runs the provider-side job behind a failed check without changing the commit under test. It is deliberately a separate interface rather than a Host method: a backend whose provider exposes no rerun primitive simply does not implement it, and callers type-assert (host.(CheckRerunner)) before use, so those backends keep compiling and keep their existing behavior.

type Host

type Host interface {
	Provider() Provider
	Capabilities() Capabilities

	// Available returns nil when the host is ready to use, or a descriptive
	// error explaining why it is not (missing CLI, unauthenticated, etc).
	Available(ctx context.Context) error

	// FindPR returns the open PR for the source branch, or nil if none exists.
	FindPR(ctx context.Context, branch, base string) (*PR, error)
	CreatePR(ctx context.Context, branch, base string, content PRContent) (*PR, error)
	UpdatePR(ctx context.Context, pr *PR, content PRContent) (*PR, error)

	GetPRState(ctx context.Context, pr *PR) (PRState, error)
	GetChecks(ctx context.Context, pr *PR) ([]Check, error)

	// GetMergeableState is optional; implementations without Capabilities().MergeableState
	// must return ErrUnsupported. Callers should consult Capabilities first.
	GetMergeableState(ctx context.Context, pr *PR) (MergeableState, error)

	// FetchFailedCheckLogs is optional; returns "" when no logs can be retrieved
	// and ErrUnsupported when the provider has no log-fetching support at all.
	FetchFailedCheckLogs(ctx context.Context, pr *PR, branch, headSHA string, failingNames []string) (string, error)
}

Host is the provider-agnostic interface to a PR-hosting service. Transport (CLI vs HTTP API) is an implementation detail.

type MergeableState

type MergeableState string

MergeableState is the normalized merge-conflict status of a PR.

const (
	MergeableOK       MergeableState = "MERGEABLE"
	MergeableConflict MergeableState = "CONFLICTING"
	MergeablePending  MergeableState = "PENDING"
	MergeableUnknown  MergeableState = "UNKNOWN"
)

func (MergeableState) Conflict

func (s MergeableState) Conflict() bool

Conflict reports whether the state indicates a known merge conflict.

func (MergeableState) Resolved

func (s MergeableState) Resolved() bool

Resolved reports whether the state is final (MERGEABLE or CONFLICTING).

type PR

type PR struct {
	Number string
	URL    string
}

PR identifies a pull/merge request on a provider.

type PRContent

type PRContent struct {
	Title string
	Body  string
}

PRContent is the title + body for creating or updating a PR.

type PRState

type PRState string

PRState is the normalized lifecycle state of a PR.

const (
	PRStateOpen   PRState = "OPEN"
	PRStateMerged PRState = "MERGED"
	PRStateClosed PRState = "CLOSED"
)

type Provider

type Provider string
const (
	ProviderGitHub      Provider = "github"
	ProviderGitLab      Provider = "gitlab"
	ProviderBitbucket   Provider = "bitbucket"
	ProviderAzureDevOps Provider = "azuredevops"
	ProviderUnknown     Provider = "unknown"
)

func DetectProvider

func DetectProvider(url string) Provider

DetectProvider identifies the SCM provider for url. SSH host aliases are resolved through the user's SSH configuration before detection falls back to ProviderUnknown.

func DetectProviderContext

func DetectProviderContext(ctx context.Context, url string) Provider

DetectProviderContext is DetectProvider with caller-controlled cancellation.

func (Provider) AuthCheckCommand

func (p Provider) AuthCheckCommand() []string

func (Provider) CLIName

func (p Provider) CLIName() string

Directories

Path Synopsis
Package azuredevops implements scm.Host backed by the az CLI with the azure-devops extension.
Package azuredevops implements scm.Host backed by the az CLI with the azure-devops extension.
Package github implements scm.Host backed by the gh CLI.
Package github implements scm.Host backed by the gh CLI.
Package gitlab implements scm.Host backed by the glab CLI.
Package gitlab implements scm.Host backed by the glab CLI.

Jump to

Keyboard shortcuts

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