Documentation
¶
Overview ¶
Package gitenv provides auto-detection and abstraction for CI/CD environments. It detects GitHub Actions, GitLab CI, and other CI systems from environment variables and provides a unified interface for accessing repository and commit information.
Index ¶
- Constants
- func ExtractMarkers(bodies []string) map[string]bool
- func MarkerComment(key string) string
- type GitEnv
- type GitHubEnv
- func (g *GitHubEnv) BlobURL() string
- func (g *GitHubEnv) CanonicalRepoName() string
- func (g *GitHubEnv) CommitBranch() string
- func (g *GitHubEnv) CommitSha() string
- func (g *GitHubEnv) CommitTag() string
- func (g *GitHubEnv) CommitTitle() string
- func (g *GitHubEnv) CreateMRComment(option MRCommentOption) error
- func (g *GitHubEnv) DefaultBranch() string
- func (g *GitHubEnv) ExistingFindingMarkers() (map[string]bool, error)
- func (g *GitHubEnv) IsActive() bool
- func (g *GitHubEnv) JobURL() string
- func (g *GitHubEnv) MergeRequestID() string
- func (g *GitHubEnv) MergeRequestTitle() string
- func (g *GitHubEnv) ProjectID() string
- func (g *GitHubEnv) ProjectName() string
- func (g *GitHubEnv) ProjectURL() string
- func (g *GitHubEnv) Provider() string
- func (g *GitHubEnv) SetVerbose(v bool)
- func (g *GitHubEnv) SourceBranch() string
- func (g *GitHubEnv) TargetBranch() string
- func (g *GitHubEnv) TargetBranchSha() string
- func (g *GitHubEnv) UpsertSummaryComment(body string) error
- type GitLabEnv
- func (g *GitLabEnv) BlobURL() string
- func (g *GitLabEnv) CanonicalRepoName() string
- func (g *GitLabEnv) CommitBranch() string
- func (g *GitLabEnv) CommitSha() string
- func (g *GitLabEnv) CommitTag() string
- func (g *GitLabEnv) CommitTitle() string
- func (g *GitLabEnv) CreateMRComment(option MRCommentOption) error
- func (g *GitLabEnv) DefaultBranch() string
- func (g *GitLabEnv) ExistingFindingMarkers() (map[string]bool, error)
- func (g *GitLabEnv) IsActive() bool
- func (g *GitLabEnv) JobURL() string
- func (g *GitLabEnv) MergeRequestID() string
- func (g *GitLabEnv) MergeRequestTitle() string
- func (g *GitLabEnv) ProjectID() string
- func (g *GitLabEnv) ProjectName() string
- func (g *GitLabEnv) ProjectURL() string
- func (g *GitLabEnv) Provider() string
- func (g *GitLabEnv) SetVerbose(v bool)
- func (g *GitLabEnv) SourceBranch() string
- func (g *GitLabEnv) TargetBranch() string
- func (g *GitLabEnv) TargetBranchSha() string
- func (g *GitLabEnv) UpsertSummaryComment(body string) error
- type MRCommentOption
- type ManualEnv
- func (m *ManualEnv) BlobURL() string
- func (m *ManualEnv) CanonicalRepoName() string
- func (m *ManualEnv) CommitBranch() string
- func (m *ManualEnv) CommitSha() string
- func (m *ManualEnv) CommitTag() string
- func (m *ManualEnv) CommitTitle() string
- func (m *ManualEnv) CreateMRComment(_ MRCommentOption) error
- func (m *ManualEnv) DefaultBranch() string
- func (m *ManualEnv) ExistingFindingMarkers() (map[string]bool, error)
- func (m *ManualEnv) IsActive() bool
- func (m *ManualEnv) JobURL() string
- func (m *ManualEnv) MergeRequestID() string
- func (m *ManualEnv) MergeRequestTitle() string
- func (m *ManualEnv) ProjectID() string
- func (m *ManualEnv) ProjectName() string
- func (m *ManualEnv) ProjectURL() string
- func (m *ManualEnv) Provider() string
- func (m *ManualEnv) SourceBranch() string
- func (m *ManualEnv) TargetBranch() string
- func (m *ManualEnv) TargetBranchSha() string
- func (m *ManualEnv) UpsertSummaryComment(_ string) error
Constants ¶
const ( ProviderGitHub = "github" ProviderGitLab = "gitlab" ProviderBitbucket = "bitbucket" ProviderManual = "manual" )
const SummaryMarker = "<!-- openctem-summary -->"
SummaryMarker tags the single sticky PR/MR summary comment so it is updated in place each run instead of re-posted.
Variables ¶
This section is empty.
Functions ¶
func ExtractMarkers ¶ added in v0.4.0
ExtractMarkers returns the set of finding keys already present in the given comment bodies (parsed from the hidden markers).
func MarkerComment ¶ added in v0.4.0
MarkerComment returns the hidden marker to append to a comment body for the given finding key.
Types ¶
type GitEnv ¶
type GitEnv interface {
// Provider returns the provider name (github, gitlab, bitbucket, etc.)
Provider() string
// IsActive returns true if this CI environment is detected
IsActive() bool
// Repository info
ProjectID() string
ProjectName() string
ProjectURL() string
BlobURL() string
// CanonicalRepoName returns the full canonical repository name including the provider domain.
// Format: {domain}/{owner}/{repo}
// Examples:
// - github.com/openctemio/api
// - gitlab.com/myorg/myrepo
// This ensures unique asset identification across different Git providers.
CanonicalRepoName() string
// Commit info
CommitSha() string
CommitBranch() string
CommitTitle() string
CommitTag() string
DefaultBranch() string
// MR/PR info
MergeRequestID() string
MergeRequestTitle() string
SourceBranch() string
TargetBranch() string
TargetBranchSha() string
// CI info
JobURL() string
// Actions
CreateMRComment(option MRCommentOption) error
// ExistingFindingMarkers returns the set of finding keys already commented on
// the current PR/MR (parsed from hidden markers), so a re-run can skip them.
// Returns an empty map when not in a PR/MR or when listing is unsupported.
ExistingFindingMarkers() (map[string]bool, error)
// UpsertSummaryComment posts (or updates in place) the single sticky security
// summary comment on the current PR/MR. The body should NOT include the
// marker — the implementation appends SummaryMarker. No-op outside a PR/MR.
UpsertSummaryComment(body string) error
}
GitEnv provides a unified interface for CI/CD environment information. Implementations detect and read from CI-specific environment variables.
func Detect ¶
func Detect() GitEnv
Detect auto-detects the CI environment and returns the appropriate GitEnv. Returns nil if no CI environment is detected.
func DetectFromDirectory ¶
DetectFromDirectory detects git information from a local directory. Useful when running locally without CI environment.
func DetectWithVerbose ¶
DetectWithVerbose auto-detects the CI environment with optional verbose logging.
type GitHubEnv ¶
type GitHubEnv struct {
// contains filtered or unexported fields
}
GitHubEnv provides GitHub Actions CI environment information.
func (*GitHubEnv) CanonicalRepoName ¶
CanonicalRepoName returns the full canonical repository name including domain. Format: github.com/{owner}/{repo} (or custom domain for GitHub Enterprise) This ensures unique asset identification across different Git providers.
func (*GitHubEnv) CommitBranch ¶
CommitBranch returns the current branch name.
func (*GitHubEnv) CommitTitle ¶
CommitTitle returns the commit message.
func (*GitHubEnv) CreateMRComment ¶
func (g *GitHubEnv) CreateMRComment(option MRCommentOption) error
CreateMRComment creates a review comment on a pull request.
func (*GitHubEnv) DefaultBranch ¶
DefaultBranch returns the default branch name.
func (*GitHubEnv) ExistingFindingMarkers ¶ added in v0.4.0
ExistingFindingMarkers lists the PR's review comments and extracts the finding markers already posted, so a re-run can skip them (idempotency). Best-effort: any error returns an empty set so commenting degrades to "may duplicate" rather than failing the scan.
func (*GitHubEnv) MergeRequestID ¶
MergeRequestID returns the PR number.
func (*GitHubEnv) MergeRequestTitle ¶
MergeRequestTitle returns the PR title.
func (*GitHubEnv) ProjectName ¶
ProjectName returns owner/repo format.
func (*GitHubEnv) ProjectURL ¶
ProjectURL returns the repository URL.
func (*GitHubEnv) SetVerbose ¶
SetVerbose enables verbose logging.
func (*GitHubEnv) SourceBranch ¶
SourceBranch returns the source branch for PRs.
func (*GitHubEnv) TargetBranch ¶
TargetBranch returns the target branch for PRs.
func (*GitHubEnv) TargetBranchSha ¶
TargetBranchSha returns the base commit SHA for PRs.
func (*GitHubEnv) UpsertSummaryComment ¶ added in v0.4.0
UpsertSummaryComment posts or updates the single sticky security summary comment on the PR (a PR-level issue comment, not an inline review comment).
type GitLabEnv ¶
type GitLabEnv struct {
// contains filtered or unexported fields
}
GitLabEnv provides GitLab CI environment information.
func (*GitLabEnv) CanonicalRepoName ¶
CanonicalRepoName returns the full canonical repository name including domain. Format: {domain}/{namespace}/{project} Examples:
- gitlab.com/myorg/myrepo
- gitlab.mycompany.com/team/project
This ensures unique asset identification across different Git providers.
func (*GitLabEnv) CommitBranch ¶
CommitBranch returns the current branch name.
func (*GitLabEnv) CommitTitle ¶
CommitTitle returns the commit message.
func (*GitLabEnv) CreateMRComment ¶
func (g *GitLabEnv) CreateMRComment(option MRCommentOption) error
CreateMRComment creates a discussion on a merge request.
func (*GitLabEnv) DefaultBranch ¶
DefaultBranch returns the default branch name.
func (*GitLabEnv) ExistingFindingMarkers ¶ added in v0.4.0
ExistingFindingMarkers lists the MR's discussion notes and extracts the finding markers already posted, so a re-run can skip them (idempotency). Best-effort: errors return an empty set rather than failing the scan.
func (*GitLabEnv) MergeRequestID ¶
MergeRequestID returns the MR IID.
func (*GitLabEnv) MergeRequestTitle ¶
MergeRequestTitle returns the MR title.
func (*GitLabEnv) ProjectName ¶
ProjectName returns the project name.
func (*GitLabEnv) ProjectURL ¶
ProjectURL returns the project URL.
func (*GitLabEnv) SetVerbose ¶
SetVerbose enables verbose logging.
func (*GitLabEnv) SourceBranch ¶
SourceBranch returns the source branch for MRs.
func (*GitLabEnv) TargetBranch ¶
TargetBranch returns the target branch for MRs.
func (*GitLabEnv) TargetBranchSha ¶
TargetBranchSha returns the base commit SHA for MRs.
func (*GitLabEnv) UpsertSummaryComment ¶ added in v0.4.0
UpsertSummaryComment posts or updates the single sticky security summary note on the MR.
type MRCommentOption ¶
MRCommentOption configures a merge request / pull request comment.
type ManualEnv ¶
type ManualEnv struct {
// contains filtered or unexported fields
}
ManualEnv is a manual/local environment when no CI is detected.
func NewManualEnv ¶
NewManualEnv creates a manual environment with optional override values.
func (*ManualEnv) CanonicalRepoName ¶
CanonicalRepoName returns the repo URL as-is for manual environments.
func (*ManualEnv) CommitBranch ¶
func (*ManualEnv) CommitTitle ¶
func (*ManualEnv) CreateMRComment ¶
func (m *ManualEnv) CreateMRComment(_ MRCommentOption) error
func (*ManualEnv) DefaultBranch ¶
func (*ManualEnv) ExistingFindingMarkers ¶ added in v0.4.0
ExistingFindingMarkers returns an empty set — a manual/local env has no PR/MR.
func (*ManualEnv) MergeRequestID ¶
func (*ManualEnv) MergeRequestTitle ¶
func (*ManualEnv) ProjectName ¶
func (*ManualEnv) ProjectURL ¶
func (*ManualEnv) SourceBranch ¶
func (*ManualEnv) TargetBranch ¶
func (*ManualEnv) TargetBranchSha ¶
func (*ManualEnv) UpsertSummaryComment ¶ added in v0.4.0
UpsertSummaryComment is a no-op for a manual/local env (no PR/MR).