435 lines
17 KiB
Go
435 lines
17 KiB
Go
// Package readiness inspects working inputs and read-only access evidence.
|
|
package readiness
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"go/version"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/releasekit/internal/artifact"
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
|
"go.maximumdirect.net/releasekit/internal/gotool"
|
|
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
|
"go.maximumdirect.net/releasekit/internal/validation"
|
|
"golang.org/x/mod/modfile"
|
|
"golang.org/x/mod/module"
|
|
)
|
|
|
|
type Request struct {
|
|
RepositoryDir, ModuleDir, Product, Remote, ExpectedPushURL, Context string
|
|
Validation validation.Settings
|
|
Selector string
|
|
CIConfigured bool
|
|
Commit string
|
|
Source *releaseidentity.SourceSpec
|
|
Operations []string
|
|
Offline bool
|
|
Build *artifact.Settings
|
|
HasDelivery, HasRehearsal bool
|
|
RequiredFiles []string
|
|
DeliveryConfig func() error
|
|
Credential func() error
|
|
Delivery func(context.Context) error
|
|
RehearsalConfig func() error
|
|
Rehearsal func(context.Context) error
|
|
Prebuilt func(context.Context, releaseidentity.ResolvedSource) error
|
|
}
|
|
|
|
type Check struct {
|
|
ID, Operation, Status, Reason, Message string
|
|
Cause error
|
|
}
|
|
|
|
type Report struct {
|
|
RepositoryDir, GitDestination string
|
|
Operations []string
|
|
Checks []Check
|
|
}
|
|
|
|
func (r *Report) record(id, operation, reason string, err error) bool {
|
|
check := Check{ID: id, Operation: operation, Status: "passed", Reason: reason, Message: "available"}
|
|
if err != nil {
|
|
check.Status = "failed"
|
|
check.Message = git.DisplayDiagnostic(err.Error())
|
|
check.Cause = err
|
|
}
|
|
r.Checks = append(r.Checks, check)
|
|
return err == nil
|
|
}
|
|
func (r *Report) note(id, operation, status, reason, message string) {
|
|
r.Checks = append(r.Checks, Check{ID: id, Operation: operation, Status: status, Reason: reason, Message: message})
|
|
}
|
|
|
|
// Run collects independent working-input and read-only observations. Network
|
|
// effects are limited to selected publication reads and the supplied preflight.
|
|
func Run(ctx context.Context, request Request) Report {
|
|
report := Report{Operations: append([]string(nil), request.Operations...)}
|
|
selected := map[string]bool{}
|
|
for _, op := range report.Operations {
|
|
selected[op] = true
|
|
}
|
|
has := func(op string) bool {
|
|
return selected[op] || selected["rehearsal"] && (op == "publication" || op == "delivery")
|
|
}
|
|
if strings.TrimSpace(request.Product) == "" {
|
|
report.record("product", "configuration", "configuration", errors.New("product is required"))
|
|
}
|
|
if request.Context != "local" && request.Context != "ci" {
|
|
report.record("context", "configuration", "configuration", errors.New("context must be local or ci"))
|
|
}
|
|
for _, op := range report.Operations {
|
|
switch op {
|
|
case "publication", "go", "build", "delivery", "rehearsal":
|
|
default:
|
|
report.record("operation", op, "configuration", fmt.Errorf("unknown operation %q", op))
|
|
}
|
|
}
|
|
repository, repoErr := git.RepositoryRoot(ctx, request.RepositoryDir)
|
|
report.record("repository", "configuration", "configuration", repoErr)
|
|
report.RepositoryDir = repository
|
|
var source releaseidentity.ResolvedSource
|
|
var sourceErr error
|
|
if repoErr == nil {
|
|
if request.Commit == "" {
|
|
source, sourceErr = releaseidentity.LoadWorkingSource(ctx, repository, request.ModuleDir, request.Source)
|
|
} else {
|
|
source, sourceErr = releaseidentity.LoadCommittedSource(ctx, repository, request.ModuleDir, request.Commit, request.Source)
|
|
}
|
|
if sourceErr == nil {
|
|
sourceErr = inspectSourceConfiguration(ctx, request, source)
|
|
}
|
|
report.record("source", "configuration", "configuration", sourceErr)
|
|
} else {
|
|
report.note("source", "configuration", "skipped", "dependency", "repository unavailable")
|
|
}
|
|
if repoErr == nil && sourceErr == nil && (selected["publication"] || selected["go"]) {
|
|
report.record("candidate-configuration", "publication", "configuration", validation.ValidateConfiguration(request.Validation, source.GoEnabled))
|
|
}
|
|
if selected["rehearsal"] {
|
|
report.record("ci-credential-declaration", "rehearsal", "configuration", CheckCIConfigured(request.CIConfigured))
|
|
if repoErr == nil {
|
|
selector := request.Selector
|
|
if selector == "" {
|
|
selector = "root"
|
|
}
|
|
report.record("rehearsal-selector", "rehearsal", "configuration", releaseidentity.ValidateDiagnosticSelector(ctx, repository, selector))
|
|
}
|
|
}
|
|
|
|
if selected["go"] && sourceErr == nil && repoErr == nil && !source.GoEnabled {
|
|
report.record("go-selection", "go", "configuration", errors.New("Go capability is not declared"))
|
|
}
|
|
if selected["build"] && (request.Build == nil || len(request.Build.Targets) == 0) {
|
|
report.record("build-selection", "build", "configuration", errors.New("build targets are not declared"))
|
|
}
|
|
if selected["build"] && repoErr == nil && sourceErr == nil && !source.GoEnabled {
|
|
report.record("build-capability", "build", "configuration", errors.New("build targets require Go capability"))
|
|
}
|
|
if has("delivery") && !request.HasDelivery {
|
|
report.record("delivery-selection", "delivery", "configuration", errors.New("delivery is not declared"))
|
|
}
|
|
if selected["rehearsal"] && !request.HasRehearsal {
|
|
report.record("rehearsal-selection", "rehearsal", "configuration", errors.New("rehearsal is not declared"))
|
|
}
|
|
for _, name := range request.RequiredFiles {
|
|
path := name
|
|
committedName := name
|
|
committedInput := request.Commit != "" && !filepath.IsAbs(name)
|
|
if request.Commit != "" && filepath.IsAbs(name) && repoErr == nil {
|
|
if relative, relErr := filepath.Rel(repository, filepath.Clean(name)); relErr == nil && relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
|
committedName, committedInput = relative, true
|
|
}
|
|
}
|
|
if !filepath.IsAbs(path) {
|
|
path = filepath.Join(repository, path)
|
|
}
|
|
var err error
|
|
if strings.TrimSpace(name) == "" {
|
|
err = errors.New("required file path is blank")
|
|
} else if !filepath.IsAbs(name) && (filepath.Clean(name) == ".." || strings.HasPrefix(filepath.Clean(name), ".."+string(filepath.Separator))) {
|
|
err = errors.New("relative required file path escapes the repository")
|
|
} else if repoErr != nil && !filepath.IsAbs(name) {
|
|
err = errors.New("repository unavailable for relative required file")
|
|
} else if committedInput {
|
|
_, err = committedFile(ctx, repository, request.Commit, filepath.ToSlash(committedName))
|
|
} else {
|
|
var info os.FileInfo
|
|
info, err = os.Stat(path)
|
|
if err == nil && !info.Mode().IsRegular() {
|
|
err = errors.New("required input is not a regular file")
|
|
}
|
|
if err == nil {
|
|
var file *os.File
|
|
file, err = os.Open(path)
|
|
if err == nil {
|
|
err = file.Close()
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
err = fmt.Errorf("required file %q: %w", git.DisplayDiagnostic(name), err)
|
|
}
|
|
report.record("required-file", "configuration", "required_input", err)
|
|
}
|
|
if has("publication") {
|
|
if repoErr != nil {
|
|
report.note("git-identity", "publication", "skipped", "dependency", "repository unavailable")
|
|
report.note("git-main", "publication", "skipped", "dependency", "repository unavailable")
|
|
} else {
|
|
if request.Context == "local" {
|
|
report.record("git-identity", "publication", "configuration", git.CheckTaggerIdentity(ctx, repository))
|
|
}
|
|
remote := request.Remote
|
|
if remote == "" {
|
|
remote = "origin"
|
|
}
|
|
endpoint, err := git.Resolve(ctx, repository, remote)
|
|
if err == nil {
|
|
report.GitDestination = git.DisplayURL(endpoint.PushURL)
|
|
if request.ExpectedPushURL != "" && request.ExpectedPushURL != endpoint.PushURL {
|
|
err = fmt.Errorf("effective push endpoint %q differs from expected %q", git.DisplayURL(endpoint.PushURL), git.DisplayURL(request.ExpectedPushURL))
|
|
}
|
|
}
|
|
if report.record("git-endpoint", "publication", "configuration", err) {
|
|
if request.Offline {
|
|
report.note("git-main", "publication", "unchecked", "offline", "authoritative main not read in offline mode")
|
|
} else {
|
|
main, readErr := endpoint.QueryMain(ctx, repository)
|
|
if readErr == nil && main.State != git.RefPresent {
|
|
readErr = errors.New("authoritative main is absent or ambiguous")
|
|
}
|
|
report.record("git-main", "publication", "remote", readErr)
|
|
}
|
|
} else {
|
|
report.note("git-main", "publication", "skipped", "dependency", "publication endpoint unavailable")
|
|
}
|
|
}
|
|
}
|
|
buildNeeded := selected["build"] || selected["rehearsal"] && request.Build != nil && len(request.Build.Targets) > 0
|
|
goNeeded := selected["go"] || buildNeeded || selected["rehearsal"] && source.GoEnabled
|
|
if goNeeded && repoErr == nil && sourceErr == nil && source.GoEnabled {
|
|
var goErr error
|
|
if request.Commit == "" {
|
|
goErr = inspectGo(ctx, source.ModuleDir)
|
|
} else {
|
|
var declaration []byte
|
|
declaration, goErr = committedFile(ctx, repository, request.Commit, filepath.ToSlash(filepath.Join(source.ModuleSubdir, "go.mod")))
|
|
if goErr == nil {
|
|
goErr = inspectGoDeclaration(ctx, repository, declaration)
|
|
}
|
|
}
|
|
report.record("go-toolchain", "go", "required_input", goErr)
|
|
report.note("go-dependencies", "go", "unchecked", "not_executed", "dependency cache and build execution were not checked")
|
|
} else if goNeeded {
|
|
report.note("go-toolchain", "go", "skipped", "dependency", "selected Go source unavailable")
|
|
}
|
|
if buildNeeded && request.Build != nil && len(request.Build.Targets) > 0 {
|
|
if repoErr != nil || sourceErr != nil || !source.GoEnabled {
|
|
report.note("build-inputs", "build", "skipped", "dependency", "selected Go source unavailable")
|
|
} else {
|
|
var buildErr error
|
|
if request.Commit == "" {
|
|
buildErr = inspectBuild(source, *request.Build)
|
|
} else {
|
|
buildErr = inspectCommittedBuild(ctx, source, request.Commit, *request.Build)
|
|
}
|
|
if report.record("build-inputs", "build", "configuration", buildErr) && !localMainPackage(source, request.Build.MainPackage) {
|
|
report.note("main-package", "build", "unchecked", "not_executed", "external package inputs require build execution")
|
|
}
|
|
}
|
|
}
|
|
if has("delivery") && request.HasDelivery {
|
|
var configErr, credentialErr error
|
|
if request.DeliveryConfig == nil {
|
|
configErr = errors.New("delivery settings unavailable")
|
|
} else {
|
|
configErr = request.DeliveryConfig()
|
|
}
|
|
if request.Credential == nil {
|
|
credentialErr = errors.New("delivery credential unavailable")
|
|
} else {
|
|
credentialErr = request.Credential()
|
|
}
|
|
configOK := report.record("delivery-inputs", "delivery", "configuration", configErr)
|
|
credentialOK := report.record("delivery-credential", "delivery", "configuration", credentialErr)
|
|
if !configOK || !credentialOK {
|
|
report.note("delivery-access", "delivery", "skipped", "dependency", "delivery settings or credential unavailable")
|
|
} else if request.Offline {
|
|
report.note("delivery-access", "delivery", "unchecked", "offline", "hosted access not checked in offline mode")
|
|
} else if request.Delivery == nil {
|
|
report.record("delivery-access", "delivery", "remote", errors.New("delivery observation unavailable"))
|
|
} else {
|
|
report.record("delivery-access", "delivery", "remote", request.Delivery(ctx))
|
|
}
|
|
}
|
|
if request.Prebuilt != nil {
|
|
if !has("delivery") {
|
|
report.record("prebuilt", "delivery", "configuration", errors.New("prebuilt assets require delivery selection"))
|
|
} else if sourceErr != nil || repoErr != nil {
|
|
report.note("prebuilt", "delivery", "skipped", "dependency", "selected source unavailable")
|
|
} else {
|
|
report.record("prebuilt", "delivery", "required_input", request.Prebuilt(ctx, source))
|
|
}
|
|
}
|
|
if selected["rehearsal"] {
|
|
report.note("ci-secret-injection", "rehearsal", "unchecked", "not_executed", "CI job secret injection requires job evidence")
|
|
configOK := false
|
|
if request.HasRehearsal && request.RehearsalConfig != nil {
|
|
configOK = report.record("woodpecker-settings", "rehearsal", "configuration", request.RehearsalConfig())
|
|
}
|
|
if !configOK {
|
|
report.note("woodpecker-access", "rehearsal", "skipped", "dependency", "rehearsal settings unavailable")
|
|
} else if request.Offline {
|
|
report.note("woodpecker-access", "rehearsal", "unchecked", "offline", "Woodpecker access not checked in offline mode")
|
|
} else if request.Rehearsal == nil {
|
|
report.note("woodpecker-access", "rehearsal", "unchecked", "unavailable", "Woodpecker observation is unavailable")
|
|
} else {
|
|
report.record("woodpecker-access", "rehearsal", "remote", request.Rehearsal(ctx))
|
|
}
|
|
}
|
|
return report
|
|
}
|
|
|
|
// inspectSourceConfiguration exercises the selected tag rules with a valid
|
|
// representative identifier. It neither selects nor requires a release version.
|
|
func inspectSourceConfiguration(ctx context.Context, request Request, source releaseidentity.ResolvedSource) error {
|
|
version := source.VersionPrefix + "1.0.0"
|
|
if source.Publication == "go-module" {
|
|
if source.VersionFormat != "semver" || source.VersionPrefix != "v" {
|
|
return errors.New("Go module publication requires prefixed semantic versions")
|
|
}
|
|
_, major, ok := module.SplitPathVersion(source.ModulePath)
|
|
if !ok {
|
|
return errors.New("invalid Go module path major")
|
|
}
|
|
if major != "" {
|
|
version = strings.TrimLeft(major, "/.") + ".0.0"
|
|
}
|
|
} else {
|
|
switch source.VersionFormat {
|
|
case "date":
|
|
version = source.VersionPrefix + "2000-01-01"
|
|
case "year-month":
|
|
version = source.VersionPrefix + "2000.01.0"
|
|
}
|
|
}
|
|
var err error
|
|
if request.Commit == "" {
|
|
_, err = releaseidentity.FromWorkingSource(ctx, source.RepositoryDir, request.ModuleDir, version, request.Source)
|
|
} else {
|
|
_, err = releaseidentity.FromCommittedSource(ctx, source.RepositoryDir, request.ModuleDir, version, request.Commit, request.Source)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func inspectGo(ctx context.Context, dir string) error {
|
|
data, err := os.ReadFile(filepath.Join(dir, "go.mod"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return inspectGoDeclaration(ctx, dir, data)
|
|
}
|
|
|
|
func inspectGoDeclaration(ctx context.Context, dir string, data []byte) error {
|
|
declaration, err := modfile.Parse("go.mod", data, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
output, err := gotool.LocalVersion(ctx, dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fields := strings.Fields(output)
|
|
if len(fields) < 3 || !version.IsValid(fields[2]) {
|
|
return errors.New("Go returned an unrecognized version")
|
|
}
|
|
if declaration.Go != nil && version.Compare(fields[2], "go"+declaration.Go.Version) < 0 {
|
|
return fmt.Errorf("local %s is older than declared Go %s", fields[2], declaration.Go.Version)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func inspectBuild(source releaseidentity.ResolvedSource, build artifact.Settings) error {
|
|
if err := validateBuildSettings(build); err != nil {
|
|
return err
|
|
}
|
|
name := build.MainPackage
|
|
if name == source.ModulePath {
|
|
name = "."
|
|
} else if strings.HasPrefix(name, source.ModulePath+"/") {
|
|
name = "./" + strings.TrimPrefix(name, source.ModulePath+"/")
|
|
}
|
|
if name == "." || strings.HasPrefix(name, "./") {
|
|
path := filepath.Join(source.ModuleDir, filepath.FromSlash(name))
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return fmt.Errorf("inspect main package: %w", err)
|
|
}
|
|
if !info.IsDir() {
|
|
return errors.New("main package input is not a directory")
|
|
}
|
|
entries, err := os.ReadDir(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var inputErr error
|
|
for _, entry := range entries {
|
|
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go") && !strings.HasSuffix(entry.Name(), "_test.go") {
|
|
filename := filepath.Join(path, entry.Name())
|
|
info, err := os.Stat(filename)
|
|
if err != nil {
|
|
inputErr = err
|
|
continue
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
continue
|
|
}
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
inputErr = err
|
|
continue
|
|
}
|
|
closeErr := file.Close()
|
|
if closeErr != nil {
|
|
inputErr = closeErr
|
|
continue
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
if inputErr != nil {
|
|
return fmt.Errorf("read main package input: %w", inputErr)
|
|
}
|
|
return errors.New("main package directory contains no non-test Go source")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateBuildSettings(build artifact.Settings) error {
|
|
if strings.TrimSpace(build.MainPackage) == "" || !artifact.ValidComponent(build.BinaryName) {
|
|
return errors.New("build requires a main package and valid binary name")
|
|
}
|
|
for _, target := range build.Targets {
|
|
if !artifact.ValidComponent(target.OS) || !artifact.ValidComponent(target.Arch) {
|
|
return fmt.Errorf("invalid build target %q/%q", target.OS, target.Arch)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func localMainPackage(source releaseidentity.ResolvedSource, name string) bool {
|
|
return name == "." || strings.HasPrefix(name, "./") || name == source.ModulePath || strings.HasPrefix(name, source.ModulePath+"/")
|
|
}
|
|
|
|
// CheckCIConfigured validates the declaration independently of job secret injection.
|
|
func CheckCIConfigured(configured bool) error {
|
|
if !configured {
|
|
return errors.New("committed profile must declare CI delivery credentials")
|
|
}
|
|
return nil
|
|
}
|