129 lines
5.2 KiB
Go
129 lines
5.2 KiB
Go
package releasekit
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"path/filepath"
|
|
|
|
internaldelivery "go.maximumdirect.net/releasekit/internal/delivery"
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
|
"go.maximumdirect.net/releasekit/internal/rehearsal"
|
|
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
|
)
|
|
|
|
// RehearsalDeliveryRequest supplies a prepared diagnostic checkout and explicit
|
|
// credentials. Source, Product, and Build correspond to the committed profile;
|
|
// the CLI decodes that profile before calling this operation.
|
|
type RehearsalDeliveryRequest struct {
|
|
RepositoryDir string
|
|
ModuleDir string
|
|
Source *SourceSpec
|
|
Product string
|
|
Tag string
|
|
Commit string
|
|
// ProfilePath selects a committed in-checkout profile; empty uses releasekit.json.
|
|
ProfilePath string
|
|
Selector string
|
|
Destination DeliveryDestination
|
|
Build *BuildSpec
|
|
Output io.Writer
|
|
}
|
|
|
|
// RehearsalDeliveryResult reports in-job execution and hosted read-back evidence.
|
|
// Diagnostics contain safe phase descriptions; the returned error retains causes.
|
|
type RehearsalDeliveryResult struct {
|
|
Delivery DeliveryResult
|
|
Scope string
|
|
Build string
|
|
Verification string
|
|
PageID int64
|
|
Artifacts []RehearsalArtifact
|
|
Diagnostics []string
|
|
}
|
|
|
|
// DeliverRehearsal verifies a diagnostic tag, checks hosted credentials, prepares
|
|
// selected build dependencies, and uses shared delivery with in-job read-back.
|
|
// It runs in the prepared CI checkout and leaves result framing to its caller.
|
|
func DeliverRehearsal(ctx context.Context, request RehearsalDeliveryRequest) (result RehearsalDeliveryResult, resultErr error) {
|
|
result = RehearsalDeliveryResult{Delivery: DeliveryResult{State: DeliveryNotAttempted}, Scope: "page", Build: "not_requested", Verification: "not_attempted"}
|
|
phase := "diagnostic identity failed"
|
|
defer func() {
|
|
if resultErr != nil {
|
|
result.Diagnostics = append(result.Diagnostics, phase)
|
|
}
|
|
}()
|
|
root, err := git.RepositoryRoot(ctx, request.RepositoryDir)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if err := PrepareTaggedCheckout(ctx, TaggedCheckoutRequest{RepositoryDir: root, Tag: request.Tag, ExpectedCommit: request.Commit, VerifyOnly: true}); err != nil {
|
|
return result, err
|
|
}
|
|
profile := request.ProfilePath
|
|
if profile == "" {
|
|
profile = "releasekit.json"
|
|
}
|
|
if filepath.IsAbs(profile) {
|
|
profile, err = filepath.Rel(root, filepath.Clean(profile))
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
profile = filepath.ToSlash(filepath.Clean(profile))
|
|
selector := request.Selector
|
|
if selector == "" {
|
|
selector = "root"
|
|
}
|
|
verified, err := rehearsal.VerifyTag(ctx, root, request.Tag, request.Commit, selector, profile, func(ctx context.Context, commit string, _ []byte) (releaseidentity.ResolvedSource, error) {
|
|
return releaseidentity.LoadCommittedSource(ctx, root, request.ModuleDir, commit, internalSourceSpec(request.Source))
|
|
})
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
identity, err := releaseidentity.DiagnosticFromAttempt(ctx, verified.Source, selector, request.Commit, verified.Manifest.AttemptID)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
result.Delivery = publicDeliveryResult(internaldelivery.Result{Identity: identity, RepositoryDir: root, ModuleDir: identity.ModuleDir, ModulePath: identity.ModulePath, Tag: identity.Tag, CandidateCommit: identity.Commit, State: internaldelivery.NotAttempted}, identity.Version)
|
|
if request.Build != nil && len(request.Build.Targets) != 0 {
|
|
phase = "build configuration failed"
|
|
result.Scope, result.Build = "page-and-assets", "not_attempted"
|
|
if !identity.GoEnabled {
|
|
return result, errors.New("build targets require Go capability")
|
|
}
|
|
}
|
|
phase = "delivery preflight failed"
|
|
backend, err := deliveryBackend(request.Destination)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if request.Build != nil && len(request.Build.Targets) != 0 {
|
|
if err := backend.Preflight(ctx); err != nil {
|
|
return result, publicPreflightError(err)
|
|
}
|
|
phase = "prepare build dependencies"
|
|
if err := PrepareBuildDependencies(ctx, DependencyPreparationRequest{RepositoryDir: root, ModuleDir: identity.ModuleSubdir}); err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
phase = "diagnostic delivery failed"
|
|
delivered, err := internaldelivery.Deliver(ctx, internaldelivery.Request{RepositoryDir: root, ModuleDir: request.ModuleDir, Source: internalSourceSpec(request.Source), Product: request.Product, Version: identity.Version, ExpectedTag: identity.Tag, Backend: backend, Build: internalBuildSpec(request.Build), Output: request.Output, Diagnostic: &internaldelivery.Diagnostic{Identity: identity, Note: rehearsal.DiagnosticNote(request.Product, verified.Manifest)}})
|
|
result.Delivery = publicDeliveryResult(delivered, identity.Version)
|
|
result.PageID = delivered.PageID
|
|
if delivered.BuildComplete {
|
|
result.Build = "complete"
|
|
} else if delivered.BuildStarted {
|
|
result.Build = "failed"
|
|
}
|
|
if delivered.Verified {
|
|
result.Verification = "complete"
|
|
} else if delivered.State != internaldelivery.NotAttempted {
|
|
result.Verification = "failed"
|
|
}
|
|
for _, item := range delivered.VerifiedAssets {
|
|
result.Artifacts = append(result.Artifacts, RehearsalArtifact{Name: item.Name, Size: item.Size, SHA256: item.SHA256})
|
|
}
|
|
return result, publicPreflightError(err)
|
|
}
|