171 lines
8.5 KiB
Go
171 lines
8.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/releasekit"
|
|
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
|
"go.maximumdirect.net/releasekit/internal/artifact"
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
|
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
|
)
|
|
|
|
type readinessCheckDocument struct {
|
|
ID string `json:"id"`
|
|
Operation releasekit.ReadinessOperation `json:"operation"`
|
|
Status releasekit.ReadinessStatus `json:"status"`
|
|
Reason string `json:"reason,omitempty"`
|
|
Message string `json:"message"`
|
|
}
|
|
type readinessCredentialDocument struct {
|
|
Context string `json:"context"`
|
|
Source string `json:"source,omitempty"`
|
|
Declared bool `json:"declared"`
|
|
}
|
|
type readinessDocument struct {
|
|
SchemaVersion int `json:"schema_version"`
|
|
Command string `json:"command"`
|
|
InputSource string `json:"input_source"`
|
|
RepositoryDir string `json:"repository_dir,omitempty"`
|
|
Operations []releasekit.ReadinessOperation `json:"operations"`
|
|
Context string `json:"context"`
|
|
Offline bool `json:"offline"`
|
|
GitDestination string `json:"git_destination,omitempty"`
|
|
DeliveryDestination string `json:"delivery_destination,omitempty"`
|
|
Credentials []readinessCredentialDocument `json:"credentials,omitempty"`
|
|
Checks []readinessCheckDocument `json:"checks"`
|
|
}
|
|
|
|
func runValidate(ctx context.Context, invocation cli.Invocation, output io.Writer) int {
|
|
report := releasekit.ValidationReport{InputSource: "working", Context: invocation.Context, Offline: invocation.Offline}
|
|
for _, operation := range invocation.Operations {
|
|
report.Operations = append(report.Operations, releasekit.ReadinessOperation(operation))
|
|
}
|
|
var reportErr error
|
|
root, err := git.RepositoryRoot(ctx, invocation.Repository)
|
|
if err != nil {
|
|
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "repository", Operation: "configuration", Status: releasekit.ReadinessFailed, Reason: "required_input", Message: git.DisplayDiagnostic(err.Error()), Cause: err})
|
|
reportErr = err
|
|
} else {
|
|
invocation.Repository = root
|
|
settings, loadErr := cli.LoadSettings(root, invocation.ConfigPath)
|
|
if loadErr != nil {
|
|
report.RepositoryDir = root
|
|
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "profile", Operation: "configuration", Status: releasekit.ReadinessFailed, Reason: "configuration", Message: git.DisplayDiagnostic(loadErr.Error()), Cause: loadErr})
|
|
reportErr = loadErr
|
|
} else {
|
|
request := releasekit.ValidateRequest{RepositoryDir: root, ModuleDir: settings.ModuleDir, Source: sourceSpec(settings), Product: settings.Product, Remote: settings.Remote, ExpectedPushURL: settings.ExpectedPushURL, Context: invocation.Context, Offline: invocation.Offline, Build: configuredBuildSpec(settings.Build), Validation: validationSpec(settings.Validation)}
|
|
for _, operation := range invocation.Operations {
|
|
request.Operations = append(request.Operations, releasekit.ReadinessOperation(operation))
|
|
}
|
|
for _, operation := range invocation.Operations {
|
|
if operation == "rehearsal" && settings.Rehearsal != nil && settings.Rehearsal.Woodpecker != nil {
|
|
request.RehearsalSelector = settings.Rehearsal.RehearsalSelector()
|
|
wood := settings.Rehearsal.Woodpecker
|
|
token, _, credentialErr := cli.ResolveWoodpeckerCredential(root, wood)
|
|
request.Woodpecker = &releasekit.WoodpeckerObservation{BaseURL: wood.BaseURL, RepositoryID: wood.RepositoryID, Workflow: wood.Workflow, Step: wood.Step, Token: token, CredentialError: credentialErr}
|
|
}
|
|
}
|
|
if settings.Readiness != nil {
|
|
request.RequiredFiles = append([]string(nil), settings.Readiness.RequiredFiles...)
|
|
}
|
|
wantsDelivery := len(invocation.Operations) == 0 && settings.Delivery != nil
|
|
for _, operation := range invocation.Operations {
|
|
if operation == "delivery" || operation == "rehearsal" {
|
|
wantsDelivery = true
|
|
}
|
|
}
|
|
if delivery := settings.Delivery; delivery != nil {
|
|
request.Destination = &releasekit.DeliveryDestination{Forge: releasekit.Forge(delivery.Forge), BaseURL: delivery.BaseURL, Owner: delivery.Owner, Repository: delivery.Repository}
|
|
if credentials := delivery.Credentials; credentials != nil {
|
|
if credentials.Local != nil {
|
|
request.Credentials = append(request.Credentials, releasekit.ReadinessCredential{Context: "local", Source: git.DisplayDiagnostic(credentials.Local.TokenFile), Declared: true})
|
|
}
|
|
if credentials.CI != nil {
|
|
name := credentials.CI.TokenEnvironmentVariable
|
|
if strings.TrimSpace(name) == "" {
|
|
name = "RELEASEKIT_DELIVERY_TOKEN"
|
|
}
|
|
request.Credentials = append(request.Credentials, releasekit.ReadinessCredential{Context: "ci", Source: git.DisplayDiagnostic(name), Declared: true})
|
|
}
|
|
}
|
|
if wantsDelivery {
|
|
token, _, tokenErr := cli.ResolveDeliveryCredential(root, delivery.Credentials, cli.CredentialContext(invocation.Context))
|
|
request.Destination.Token = token
|
|
request.CredentialError = tokenErr
|
|
}
|
|
}
|
|
var assetErr error
|
|
assetSkipped := false
|
|
if invocation.AssetsDir != "" {
|
|
if !wantsDelivery {
|
|
assetErr = fmt.Errorf("--assets requires delivery selection")
|
|
} else {
|
|
selection, selectionErr := releaseidentity.ResolveWorkingSource(root, settings.ModuleDir, settings.SourceSpec())
|
|
if selectionErr != nil {
|
|
assetSkipped = true
|
|
} else {
|
|
directory := artifact.OutputDirectory(selection.ModuleDir, invocation.AssetsDir)
|
|
receipt, receiptErr := cli.LoadBuildReceipt(filepath.Join(directory, cli.BuildReceiptName))
|
|
if receiptErr != nil {
|
|
assetErr = receiptErr
|
|
} else {
|
|
request.Prebuilt = buildResultFromReceipt(receipt)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
report, reportErr = releasekit.Validate(ctx, request)
|
|
if assetSkipped {
|
|
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "prebuilt", Operation: releasekit.ReadinessDelivery, Status: releasekit.ReadinessSkipped, Reason: "dependency", Message: "selected source unavailable"})
|
|
}
|
|
if assetErr != nil {
|
|
report.Checks = append(report.Checks, releasekit.ReadinessResult{ID: "prebuilt", Operation: releasekit.ReadinessDelivery, Status: releasekit.ReadinessFailed, Reason: "configuration", Message: git.DisplayDiagnostic(assetErr.Error()), Cause: assetErr})
|
|
reportErr = assetErr
|
|
}
|
|
}
|
|
}
|
|
if err := writeReadinessReport(output, invocation.JSON, report); err != nil {
|
|
return 1
|
|
}
|
|
if reportErr == nil {
|
|
return 0
|
|
}
|
|
if ctx.Err() != nil {
|
|
return 1
|
|
}
|
|
for _, check := range report.Checks {
|
|
if check.Status == releasekit.ReadinessFailed && (check.Reason == "configuration" || check.Reason == "required_input") {
|
|
return 2
|
|
}
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func writeReadinessReport(output io.Writer, structured bool, report releasekit.ValidationReport) error {
|
|
document := readinessDocument{SchemaVersion: 1, Command: "validate", InputSource: report.InputSource, RepositoryDir: git.DisplayDiagnostic(report.RepositoryDir), Operations: append([]releasekit.ReadinessOperation{}, report.Operations...), Context: report.Context, Offline: report.Offline, GitDestination: git.DisplayURL(report.GitDestination), DeliveryDestination: git.DisplayDiagnostic(report.DeliveryDestination), Checks: make([]readinessCheckDocument, 0, len(report.Checks))}
|
|
for _, credential := range report.Credentials {
|
|
document.Credentials = append(document.Credentials, readinessCredentialDocument{Context: credential.Context, Source: git.DisplayDiagnostic(credential.Source), Declared: credential.Declared})
|
|
}
|
|
for _, check := range report.Checks {
|
|
document.Checks = append(document.Checks, readinessCheckDocument{ID: check.ID, Operation: check.Operation, Status: check.Status, Reason: check.Reason, Message: git.DisplayDiagnostic(check.Message)})
|
|
}
|
|
if structured {
|
|
return json.NewEncoder(output).Encode(document)
|
|
}
|
|
if _, err := fmt.Fprintf(output, "readiness source=%s context=%s offline=%t operations=%v\n", document.InputSource, document.Context, document.Offline, document.Operations); err != nil {
|
|
return err
|
|
}
|
|
for _, check := range document.Checks {
|
|
if _, err := fmt.Fprintf(output, "%s %s [%s]: %s\n", check.Status, check.ID, check.Operation, check.Message); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|