Capture diagnostic identities and rehearsal records
This commit is contained in:
27
cmd/releasekit/diagnostic.go
Normal file
27
cmd/releasekit/diagnostic.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
"go.maximumdirect.net/releasekit/internal/rehearsal"
|
||||
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
||||
)
|
||||
|
||||
// verifyDiagnosticTag binds dispatch to the tag's captured profile and source.
|
||||
func verifyDiagnosticTag(ctx context.Context, repository, tag, commit, selector, profilePath string) (rehearsal.VerifiedTag, cli.Settings, error) {
|
||||
var settings cli.Settings
|
||||
verified, err := rehearsal.VerifyTag(ctx, repository, tag, commit, selector, profilePath, func(ctx context.Context, capturedCommit string, profile []byte) (releaseidentity.ResolvedSource, error) {
|
||||
var err error
|
||||
settings, err = cli.DecodeSettings(profile, profilePath)
|
||||
if err != nil {
|
||||
return releaseidentity.ResolvedSource{}, err
|
||||
}
|
||||
if settings.Rehearsal == nil || settings.Rehearsal.RehearsalSelector() != selector {
|
||||
return releaseidentity.ResolvedSource{}, fmt.Errorf("captured profile does not select diagnostic selector %q", selector)
|
||||
}
|
||||
return releaseidentity.LoadCommittedSource(ctx, repository, settings.ModuleDir, capturedCommit, settings.SourceSpec())
|
||||
})
|
||||
return verified, settings, err
|
||||
}
|
||||
43
cmd/releasekit/diagnostic_test.go
Normal file
43
cmd/releasekit/diagnostic_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
"go.maximumdirect.net/releasekit/internal/rehearsal"
|
||||
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
||||
)
|
||||
|
||||
func TestVerifyDiagnosticTagUsesCapturedProfileAndSelector(t *testing.T) {
|
||||
repo := newCLIRepository(t)
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Captured","source":{},"rehearsal":{"selector":"root"}}`)
|
||||
cliGitRun(t, repo, "add", "releasekit.json")
|
||||
cliGitRun(t, repo, "commit", "-m", "capture diagnostic profile")
|
||||
captured, err := cli.CaptureCommittedProfile(t.Context(), repo, "HEAD", "releasekit.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
source, err := releaseidentity.LoadCommittedSource(t.Context(), repo, captured.Settings.ModuleDir, captured.Commit, captured.Settings.SourceSpec())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
identity, attempt, err := releaseidentity.NewDiagnostic(t.Context(), source, "root", captured.Commit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest := rehearsal.NewManifest(identity, attempt, "root", captured.Path, captured.Bytes)
|
||||
if _, err := rehearsal.CreateDiagnosticTag(t.Context(), repo, manifest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Working tree"}`)
|
||||
verified, settings, err := verifyDiagnosticTag(t.Context(), repo, identity.Tag, captured.Commit, "root", captured.Path)
|
||||
if err != nil || settings.Product != "Captured" || verified.Manifest.AttemptID != attempt {
|
||||
t.Fatalf("verified=%+v settings=%+v error=%v", verified, settings, err)
|
||||
}
|
||||
if _, _, err := verifyDiagnosticTag(t.Context(), repo, identity.Tag, captured.Commit, "other", captured.Path); err == nil {
|
||||
t.Fatal("accepted wrong selector")
|
||||
}
|
||||
if _, _, err := verifyDiagnosticTag(t.Context(), repo, identity.Tag, "0000000000000000000000000000000000000000", "root", captured.Path); err == nil {
|
||||
t.Fatal("accepted wrong event commit")
|
||||
}
|
||||
}
|
||||
62
cmd/releasekit/internal/cli/committed_profile.go
Normal file
62
cmd/releasekit/internal/cli/committed_profile.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.maximumdirect.net/releasekit/internal/git"
|
||||
)
|
||||
|
||||
// CommittedProfile captures the exact profile used by a rehearsal.
|
||||
type CommittedProfile struct {
|
||||
RepositoryDir string
|
||||
Commit string
|
||||
Path string
|
||||
SHA256 string
|
||||
Settings Settings
|
||||
Bytes []byte
|
||||
}
|
||||
|
||||
// CaptureCommittedProfile reads the chosen profile from a resolved commit,
|
||||
// independent of the working checkout and index.
|
||||
func CaptureCommittedProfile(ctx context.Context, repositoryDir, revision, configPath string) (CommittedProfile, error) {
|
||||
var result CommittedProfile
|
||||
root, err := git.RepositoryRoot(ctx, repositoryDir)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.RepositoryDir = root
|
||||
result.Commit, err = git.ResolveCommit(ctx, root, revision)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if configPath == "" {
|
||||
configPath = "releasekit.json"
|
||||
}
|
||||
if filepath.IsAbs(configPath) {
|
||||
configPath, err = filepath.Rel(root, filepath.Clean(configPath))
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("relate profile to checkout: %w", err)
|
||||
}
|
||||
}
|
||||
configPath = filepath.Clean(configPath)
|
||||
if configPath == "." || configPath == ".." || strings.HasPrefix(configPath, ".."+string(filepath.Separator)) || filepath.IsAbs(configPath) {
|
||||
return result, fmt.Errorf("rehearsal profile must be committed within the checkout")
|
||||
}
|
||||
result.Path = filepath.ToSlash(configPath)
|
||||
result.Bytes, err = git.ReadCommitFile(ctx, root, result.Commit, result.Path)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("rehearsal profile %q must exist in the selected commit: %w", result.Path, err)
|
||||
}
|
||||
result.Settings, err = DecodeSettings(result.Bytes, result.Path)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
digest := sha256.Sum256(result.Bytes)
|
||||
result.SHA256 = hex.EncodeToString(digest[:])
|
||||
return result, nil
|
||||
}
|
||||
84
cmd/releasekit/internal/cli/committed_profile_test.go
Normal file
84
cmd/releasekit/internal/cli/committed_profile_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCaptureCommittedProfileIgnoresWorkingChanges(t *testing.T) {
|
||||
repo := t.TempDir()
|
||||
run := func(args ...string) string {
|
||||
t.Helper()
|
||||
command := exec.Command("git", args...)
|
||||
command.Dir = repo
|
||||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("git %v: %v: %s", args, err, output)
|
||||
}
|
||||
return strings.TrimSpace(string(output))
|
||||
}
|
||||
run("init", "--initial-branch=main")
|
||||
run("config", "user.name", "ReleaseKit Test")
|
||||
run("config", "user.email", "releasekit@example.test")
|
||||
run("config", "commit.gpgsign", "false")
|
||||
name := filepath.Join(repo, "releasekit.json")
|
||||
first := []byte(`{"product":"Original","source":{},"rehearsal":{"woodpecker":{"base_url":"https://ci.example","repository_id":42,"workflow":"delivery","step":"publish","token_file":".releasekit/ci-token"}}}`)
|
||||
if err := os.WriteFile(name, first, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run("add", "releasekit.json")
|
||||
run("commit", "-m", "first profile")
|
||||
firstCommit := run("rev-parse", "HEAD")
|
||||
if err := os.WriteFile(name, []byte(`{"product":"Second"}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run("add", "releasekit.json")
|
||||
run("commit", "-m", "second profile")
|
||||
if err := os.WriteFile(name, []byte(`{"product":"Dirty"}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := run("status", "--porcelain")
|
||||
captured, err := CaptureCommittedProfile(t.Context(), repo, firstCommit, name)
|
||||
digest := sha256.Sum256(first)
|
||||
if err != nil || captured.Commit != firstCommit || captured.Path != "releasekit.json" || captured.Settings.Product != "Original" || captured.Settings.Rehearsal == nil || captured.Settings.Rehearsal.RehearsalSelector() != "root" || captured.Settings.Rehearsal.Woodpecker.RepositoryID != 42 || captured.SHA256 != hex.EncodeToString(digest[:]) || string(captured.Bytes) != string(first) {
|
||||
t.Fatalf("captured=%+v error=%v", captured, err)
|
||||
}
|
||||
latest, err := CaptureCommittedProfile(t.Context(), repo, "", "")
|
||||
if err != nil || latest.Settings.Product != "Second" {
|
||||
t.Fatalf("HEAD profile=%+v error=%v", latest, err)
|
||||
}
|
||||
if _, err := CaptureCommittedProfile(t.Context(), repo, "HEAD", filepath.Join(t.TempDir(), "outside.json")); err == nil || !strings.Contains(err.Error(), "within the checkout") {
|
||||
t.Fatalf("external profile error=%v", err)
|
||||
}
|
||||
if _, err := CaptureCommittedProfile(t.Context(), repo, "HEAD", "missing.json"); err == nil || !strings.Contains(err.Error(), "selected commit") {
|
||||
t.Fatalf("missing profile error=%v", err)
|
||||
}
|
||||
if after := run("status", "--porcelain"); after != before {
|
||||
t.Fatalf("capture changed checkout: %q != %q", after, before)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWoodpeckerCredentialUsesSelectedFile(t *testing.T) {
|
||||
repo := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(repo, "token"), []byte(" ci-secret \n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, source, err := ResolveWoodpeckerCredential(repo, &WoodpeckerSettings{TokenFile: "token"})
|
||||
if err != nil || token != "ci-secret" || source.Context != LocalCredentials || source.Source != filepath.Join(repo, "token") {
|
||||
t.Fatalf("token matches=%t source=%+v error=%v", token == "ci-secret", source, err)
|
||||
}
|
||||
if _, _, err := ResolveWoodpeckerCredential(repo, &WoodpeckerSettings{}); err == nil || !strings.Contains(err.Error(), "rehearsal.woodpecker.token_file") {
|
||||
t.Fatalf("missing selection error=%v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(repo, "token"), []byte(" \n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := ResolveWoodpeckerCredential(repo, &WoodpeckerSettings{TokenFile: "token"}); err == nil || strings.Contains(err.Error(), "ci-secret") {
|
||||
t.Fatalf("blank token error=%v", err)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -225,6 +226,30 @@ type Settings struct {
|
||||
Build *BuildSettings `json:"build,omitempty"`
|
||||
Delivery *DeliverySettings `json:"delivery,omitempty"`
|
||||
Readiness *ReadinessSettings `json:"readiness,omitempty"`
|
||||
Rehearsal *RehearsalSettings `json:"rehearsal,omitempty"`
|
||||
}
|
||||
|
||||
// RehearsalSettings selects a diagnostic dispatch selector and CI observer.
|
||||
type RehearsalSettings struct {
|
||||
Selector string `json:"selector,omitempty"`
|
||||
Woodpecker *WoodpeckerSettings `json:"woodpecker,omitempty"`
|
||||
}
|
||||
|
||||
// WoodpeckerSettings identifies the exact repository and delivery job.
|
||||
type WoodpeckerSettings struct {
|
||||
BaseURL string `json:"base_url"`
|
||||
RepositoryID int64 `json:"repository_id"`
|
||||
Workflow string `json:"workflow"`
|
||||
Step string `json:"step"`
|
||||
TokenFile string `json:"token_file"`
|
||||
}
|
||||
|
||||
// RehearsalSelector returns the configured selector or the default root row.
|
||||
func (s RehearsalSettings) RehearsalSelector() string {
|
||||
if s.Selector == "" {
|
||||
return "root"
|
||||
}
|
||||
return s.Selector
|
||||
}
|
||||
|
||||
// ReadinessSettings lists files deliberately required for setup diagnostics.
|
||||
@@ -361,9 +386,17 @@ func LoadSettings(repoDir, configPath string) (Settings, error) {
|
||||
return Settings{}, fmt.Errorf("open profile %q: %w", configPath, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return decodeSettings(file, configPath)
|
||||
}
|
||||
|
||||
// DecodeSettings applies the same strict profile rules to captured bytes.
|
||||
func DecodeSettings(data []byte, label string) (Settings, error) {
|
||||
return decodeSettings(bytes.NewReader(data), label)
|
||||
}
|
||||
|
||||
func decodeSettings(input io.Reader, configPath string) (Settings, error) {
|
||||
var settings Settings
|
||||
decoder := json.NewDecoder(file)
|
||||
decoder := json.NewDecoder(input)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&settings); err != nil {
|
||||
return Settings{}, fmt.Errorf("decode profile %q: %w", configPath, err)
|
||||
|
||||
@@ -31,20 +31,9 @@ func ResolveDeliveryCredential(repositoryDir string, credentials *DeliveryCreden
|
||||
if credentials == nil || credentials.Local == nil || strings.TrimSpace(credentials.Local.TokenFile) == "" {
|
||||
return "", source, errors.New("local delivery requires delivery.credentials.local.token_file")
|
||||
}
|
||||
name := credentials.Local.TokenFile
|
||||
if !filepath.IsAbs(name) {
|
||||
name = filepath.Join(repositoryDir, name)
|
||||
}
|
||||
source.Source = git.DisplayDiagnostic(name)
|
||||
contents, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
return "", source, fmt.Errorf("read local delivery token file: %s", git.DisplayDiagnostic(err.Error()))
|
||||
}
|
||||
token := strings.TrimSpace(string(contents))
|
||||
if token == "" {
|
||||
return "", source, fmt.Errorf("local delivery token file %q is empty or blank", source.Source)
|
||||
}
|
||||
return token, source, nil
|
||||
token, name, err := readTokenFile(repositoryDir, credentials.Local.TokenFile, "local delivery")
|
||||
source.Source = name
|
||||
return token, source, err
|
||||
case CICredentials:
|
||||
if credentials == nil || credentials.CI == nil {
|
||||
return "", source, errors.New("CI delivery requires delivery.credentials.ci")
|
||||
@@ -63,3 +52,30 @@ func ResolveDeliveryCredential(repositoryDir string, credentials *DeliveryCreden
|
||||
return "", source, errors.New("unknown delivery credential context")
|
||||
}
|
||||
}
|
||||
|
||||
// ResolveWoodpeckerCredential reads only the explicitly selected local CI token.
|
||||
func ResolveWoodpeckerCredential(repositoryDir string, settings *WoodpeckerSettings) (string, CredentialSource, error) {
|
||||
source := CredentialSource{Context: LocalCredentials}
|
||||
if settings == nil || strings.TrimSpace(settings.TokenFile) == "" {
|
||||
return "", source, errors.New("rehearsal requires rehearsal.woodpecker.token_file")
|
||||
}
|
||||
token, name, err := readTokenFile(repositoryDir, settings.TokenFile, "Woodpecker")
|
||||
source.Source = name
|
||||
return token, source, err
|
||||
}
|
||||
|
||||
func readTokenFile(repositoryDir, name, label string) (string, string, error) {
|
||||
if !filepath.IsAbs(name) {
|
||||
name = filepath.Join(repositoryDir, name)
|
||||
}
|
||||
safeName := git.DisplayDiagnostic(name)
|
||||
contents, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
return "", safeName, fmt.Errorf("read %s token file: %s", label, git.DisplayDiagnostic(err.Error()))
|
||||
}
|
||||
token := strings.TrimSpace(string(contents))
|
||||
if token == "" {
|
||||
return "", safeName, fmt.Errorf("%s token file %q is empty or blank", label, safeName)
|
||||
}
|
||||
return token, safeName, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user