Help us shape the future of Go! Take the 2026 Go Developer Survey

forge

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: AGPL-3.0, AGPL-3.0-only Imports: 15 Imported by: 0

Documentation

Overview

Package forge provides a platform-agnostic abstraction over git forges (GitLab, GitHub, Gitea/Forgejo). Every write operation (release creation, badge update, file commit, MR/PR creation) goes through this interface so StageFreight works identically regardless of where the repo is hosted.

Index

Constants

This section is empty.

Variables

View Source
var ErrBranchMoved = errors.New("target branch moved during commit")

ErrBranchMoved is returned when the target branch has moved since the expected SHA.

View Source
var ErrNotSupported = errors.New("operation not supported by this forge")

ErrNotSupported is returned when a forge does not support a particular operation.

Functions

func BaseURL

func BaseURL(remoteURL string) string

BaseURL extracts the forge base URL from a git remote URL. Handles SSH (git@host:path) and HTTPS (https://host/path) formats.

Types

type APIError

type APIError struct {
	Method     string
	URL        string
	StatusCode int
	Body       string
}

APIError is returned by doJSON when the API returns a non-2xx status.

func (*APIError) Error

func (e *APIError) Error() string

type Asset

type Asset struct {
	Name     string // display name
	FilePath string // local file to upload
	MIMEType string // e.g., "application/json"
}

Asset is a file to attach to a release.

type CommitFileOptions

type CommitFileOptions struct {
	Branch  string
	Path    string // file path in repo
	Content []byte
	Message string
}

CommitFileOptions configures a file commit via forge API.

type CommitFilesOptions

type CommitFilesOptions struct {
	Branch      string
	Message     string
	Files       []FileAction
	ExpectedSHA string // optional: fail if branch head != this SHA
}

CommitFilesOptions configures a multi-file atomic commit via forge API.

type CommitResult

type CommitResult struct {
	SHA string
}

CommitResult holds the result of a forge commit operation.

type FileAction

type FileAction struct {
	Path    string
	Content []byte // nil for deletes
	Delete  bool   // true = delete this file
}

FileAction describes a single file operation within a multi-file commit.

type Forge

type Forge interface {
	// Provider returns which platform this forge represents.
	Provider() Provider

	// CreateRelease creates a release/tag on the forge.
	CreateRelease(ctx context.Context, opts ReleaseOptions) (*Release, error)

	// UploadAsset attaches a file to an existing release.
	UploadAsset(ctx context.Context, releaseID string, asset Asset) error

	// AddReleaseLink adds a URL link to a release (e.g., registry image links).
	AddReleaseLink(ctx context.Context, releaseID string, link ReleaseLink) error

	// CommitFile creates or updates a file in the repo via the forge API.
	// Used for badge SVG updates without a local clone.
	CommitFile(ctx context.Context, opts CommitFileOptions) error

	// CommitFiles creates or updates multiple files in a single atomic commit
	// via the forge API. Used by the commit subsystem for CI push.
	CommitFiles(ctx context.Context, opts CommitFilesOptions) (*CommitResult, error)

	// BranchHeadSHA returns the current HEAD SHA of a branch via the forge API.
	BranchHeadSHA(ctx context.Context, branch string) (string, error)

	// CreateMR opens a merge/pull request.
	CreateMR(ctx context.Context, opts MROptions) (*MR, error)

	// CancelPipeline cancels the currently running pipeline (best-effort cleanup
	// after deps pushes a repaired commit). Returns nil if the provider doesn't
	// support pipeline cancellation.
	CancelPipeline(ctx context.Context, pipelineID string) error

	// ListReleases returns all releases, newest first.
	ListReleases(ctx context.Context) ([]ReleaseInfo, error)

	// DeleteRelease removes a release by its tag name.
	DeleteRelease(ctx context.Context, tagName string) error

	// DownloadJobArtifact fetches a single file from the latest successful job's
	// artifacts for the given ref. Returns the raw file bytes.
	// Returns os.ErrNotExist (or equivalent) if no artifacts found.
	// Implementations may return ErrNotSupported if the forge doesn't support this.
	DownloadJobArtifact(ctx context.Context, ref, jobName, artifactPath string) ([]byte, error)
}

Forge is the interface every platform implements.

type GitHubForge

type GitHubForge struct {
	BaseURL string // "https://api.github.com" or "https://ghes.example.com/api/v3"
	Token   string
	Owner   string
	Repo    string
}

GitHubForge implements the Forge interface for GitHub and GitHub Enterprise.

func NewGitHub

func NewGitHub(baseURL string) *GitHubForge

NewGitHub creates a GitHub forge client. Token is resolved from env: GITHUB_TOKEN, GH_TOKEN. Owner/Repo is resolved from env: GITHUB_REPOSITORY (owner/repo).

func (g *GitHubForge) AddReleaseLink(ctx context.Context, releaseID string, link ReleaseLink) error

func (*GitHubForge) BranchHeadSHA

func (g *GitHubForge) BranchHeadSHA(ctx context.Context, branch string) (string, error)

func (*GitHubForge) CancelPipeline

func (g *GitHubForge) CancelPipeline(ctx context.Context, pipelineID string) error

func (*GitHubForge) CommitFile

func (g *GitHubForge) CommitFile(ctx context.Context, opts CommitFileOptions) error

func (*GitHubForge) CommitFiles

func (g *GitHubForge) CommitFiles(ctx context.Context, opts CommitFilesOptions) (*CommitResult, error)

func (*GitHubForge) CreateMR

func (g *GitHubForge) CreateMR(ctx context.Context, opts MROptions) (*MR, error)

func (*GitHubForge) CreateRelease

func (g *GitHubForge) CreateRelease(ctx context.Context, opts ReleaseOptions) (*Release, error)

func (*GitHubForge) DeleteRelease

func (g *GitHubForge) DeleteRelease(ctx context.Context, tagName string) error

func (*GitHubForge) DownloadJobArtifact

func (g *GitHubForge) DownloadJobArtifact(ctx context.Context, ref, jobName, artifactPath string) ([]byte, error)

func (*GitHubForge) ListReleases

func (g *GitHubForge) ListReleases(ctx context.Context) ([]ReleaseInfo, error)

func (*GitHubForge) Provider

func (g *GitHubForge) Provider() Provider

func (*GitHubForge) UploadAsset

func (g *GitHubForge) UploadAsset(ctx context.Context, releaseID string, asset Asset) error

type GitLabForge

type GitLabForge struct {
	BaseURL   string // e.g., "https://gitlab.prplanit.com"
	Token     string // private token or job token
	ProjectID string // numeric ID or "group/project" URL-encoded path
	// contains filtered or unexported fields
}

GitLabForge implements the Forge interface for GitLab instances.

func NewGitLab

func NewGitLab(baseURL string) *GitLabForge

NewGitLab creates a GitLab forge client. Token is resolved from env: GITLAB_TOKEN, CI_JOB_TOKEN. ProjectID is resolved from env: CI_PROJECT_ID, CI_PROJECT_PATH.

func (g *GitLabForge) AddReleaseLink(ctx context.Context, releaseID string, link ReleaseLink) error

func (*GitLabForge) BranchHeadSHA

func (g *GitLabForge) BranchHeadSHA(ctx context.Context, branch string) (string, error)

func (*GitLabForge) CancelPipeline

func (g *GitLabForge) CancelPipeline(ctx context.Context, pipelineID string) error

func (*GitLabForge) CommitFile

func (g *GitLabForge) CommitFile(ctx context.Context, opts CommitFileOptions) error

func (*GitLabForge) CommitFiles

func (g *GitLabForge) CommitFiles(ctx context.Context, opts CommitFilesOptions) (*CommitResult, error)

func (*GitLabForge) CreateMR

func (g *GitLabForge) CreateMR(ctx context.Context, opts MROptions) (*MR, error)

func (*GitLabForge) CreateRelease

func (g *GitLabForge) CreateRelease(ctx context.Context, opts ReleaseOptions) (*Release, error)

func (*GitLabForge) DeleteRelease

func (g *GitLabForge) DeleteRelease(ctx context.Context, tagName string) error

func (*GitLabForge) DownloadJobArtifact

func (g *GitLabForge) DownloadJobArtifact(ctx context.Context, ref, jobName, artifactPath string) ([]byte, error)

func (*GitLabForge) ListReleases

func (g *GitLabForge) ListReleases(ctx context.Context) ([]ReleaseInfo, error)

func (*GitLabForge) Provider

func (g *GitLabForge) Provider() Provider

func (*GitLabForge) UploadAsset

func (g *GitLabForge) UploadAsset(ctx context.Context, releaseID string, asset Asset) error

type GiteaForge

type GiteaForge struct {
	BaseURL string // e.g., "https://codeberg.org"
	Token   string
	Owner   string
	Repo    string
}

GiteaForge implements the Forge interface for Gitea and Forgejo instances.

func NewGitea

func NewGitea(baseURL string) *GiteaForge

NewGitea creates a Gitea/Forgejo forge client. Token is resolved from env: GITEA_TOKEN, FORGEJO_TOKEN. Owner/Repo is resolved from env: CI_REPO (Woodpecker CI) or GITHUB_REPOSITORY (Gitea Actions, which uses GitHub-compatible vars).

func (g *GiteaForge) AddReleaseLink(ctx context.Context, releaseID string, link ReleaseLink) error

func (*GiteaForge) BranchHeadSHA

func (g *GiteaForge) BranchHeadSHA(ctx context.Context, branch string) (string, error)

func (*GiteaForge) CancelPipeline

func (g *GiteaForge) CancelPipeline(ctx context.Context, pipelineID string) error

func (*GiteaForge) CommitFile

func (g *GiteaForge) CommitFile(ctx context.Context, opts CommitFileOptions) error

func (*GiteaForge) CommitFiles

func (g *GiteaForge) CommitFiles(ctx context.Context, opts CommitFilesOptions) (*CommitResult, error)

func (*GiteaForge) CreateMR

func (g *GiteaForge) CreateMR(ctx context.Context, opts MROptions) (*MR, error)

func (*GiteaForge) CreateRelease

func (g *GiteaForge) CreateRelease(ctx context.Context, opts ReleaseOptions) (*Release, error)

func (*GiteaForge) DeleteRelease

func (g *GiteaForge) DeleteRelease(ctx context.Context, tagName string) error

func (*GiteaForge) DownloadJobArtifact

func (g *GiteaForge) DownloadJobArtifact(ctx context.Context, ref, jobName, artifactPath string) ([]byte, error)

func (*GiteaForge) ListReleases

func (g *GiteaForge) ListReleases(ctx context.Context) ([]ReleaseInfo, error)

func (*GiteaForge) Provider

func (g *GiteaForge) Provider() Provider

func (*GiteaForge) UploadAsset

func (g *GiteaForge) UploadAsset(ctx context.Context, releaseID string, asset Asset) error

type MR

type MR struct {
	ID  string
	URL string
}

MR is a created merge/pull request.

type MROptions

type MROptions struct {
	Title        string
	Description  string
	SourceBranch string
	TargetBranch string
	Draft        bool
}

MROptions configures a merge/pull request.

type Provider

type Provider string

Provider identifies a git forge platform.

const (
	GitLab  Provider = "gitlab"
	GitHub  Provider = "github"
	Gitea   Provider = "gitea"
	Unknown Provider = "unknown"
)

func DetectProvider

func DetectProvider(remoteURL string) Provider

DetectProvider determines the forge platform from a git remote URL.

type Release

type Release struct {
	ID  string // platform-specific ID
	URL string // web URL to the release page
}

Release is a created release on a forge.

type ReleaseInfo

type ReleaseInfo struct {
	ID        string // platform-specific ID (numeric for GitHub/Gitea, tag_name for GitLab)
	TagName   string
	CreatedAt time.Time
}

ReleaseInfo describes an existing release on a forge.

type ReleaseLink struct {
	Name     string // display name (e.g., "Docker Hub 1.3.0")
	URL      string // target URL
	LinkType string // "image", "package", "other"
}

ReleaseLink is a URL to embed in a release (e.g., registry image link).

type ReleaseOptions

type ReleaseOptions struct {
	TagName     string
	Ref         string // commit SHA, branch, or tag to create the release from (required by GitLab when tag doesn't exist)
	Name        string
	Description string // markdown body (release notes)
	Draft       bool
	Prerelease  bool
}

ReleaseOptions configures a new release.

Jump to

Keyboard shortcuts

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