Carry module identity through candidate results

This commit is contained in:
2026-10-10 02:15:13 +00:00
parent f99fb59cd4
commit 3e516d0ac1
13 changed files with 229 additions and 58 deletions

View File

@@ -11,7 +11,9 @@ import (
"path/filepath"
"strings"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/git"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/process"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/releaseidentity"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/validation"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/versioncontract"
)
@@ -20,6 +22,9 @@ var ErrInvalidVersion = errors.New("invalid stable version")
type Candidate struct {
RepositoryDir string
ModuleDir string
ModulePath string
Tag string
Commit string
Version string
Note string
@@ -27,6 +32,7 @@ type Candidate struct {
type Request struct {
RepositoryDir string
ModuleDir string
Product string
Version string
Validation validation.Settings
@@ -38,18 +44,18 @@ type Request struct {
func Check(ctx context.Context, request Request) (Candidate, error) {
captured, err := Capture(ctx, request)
if err != nil {
return Candidate{}, err
return captured, err
}
if err := Validate(ctx, request, captured); err != nil {
return Candidate{}, err
return captured, err
}
if request.AfterValidate != nil {
if err := request.AfterValidate(ctx, captured); err != nil {
return Candidate{}, fmt.Errorf("after candidate validation: %w", err)
return captured, fmt.Errorf("after candidate validation: %w", err)
}
}
if err := Recheck(ctx, captured, ""); err != nil {
return Candidate{}, err
return captured, err
}
return captured, nil
}
@@ -57,43 +63,58 @@ func Check(ctx context.Context, request Request) (Candidate, error) {
// Capture validates request identity and records one clean local candidate,
// including the note bytes from the captured commit. It does not run checks.
func Capture(ctx context.Context, request Request) (Candidate, error) {
captured := Candidate{Version: request.Version}
if !validStableVersion(request.Version) {
return Candidate{}, fmt.Errorf("%w: %q", ErrInvalidVersion, request.Version)
return captured, fmt.Errorf("%w: %q", ErrInvalidVersion, request.Version)
}
if strings.TrimSpace(request.Product) == "" {
return Candidate{}, errors.New("product is required")
return captured, errors.New("product is required")
}
repositoryDir, err := filepath.Abs(request.RepositoryDir)
if err != nil {
return Candidate{}, fmt.Errorf("resolve repository directory: %w", err)
return captured, fmt.Errorf("resolve repository directory: %w", err)
}
captured.RepositoryDir = repositoryDir
state, err := inspectLocalState(ctx, repositoryDir)
if err != nil {
return Candidate{}, err
return captured, err
}
captured.Commit = state.commit
if state.branch != "main" {
return Candidate{}, fmt.Errorf("release candidate must be on main; current branch is %q", state.branch)
return captured, fmt.Errorf("release candidate must be on main; current branch is %q", state.branch)
}
if state.status != "" {
return Candidate{}, fmt.Errorf("release candidate worktree and index must be clean:\n%s", state.status)
return captured, fmt.Errorf("release candidate worktree and index must be clean:\n%s", state.status)
}
path := "docs/releases/" + request.Version + ".md"
note, err := gitOutput(ctx, repositoryDir, "show", state.commit+":"+path)
identitySelection, err := releaseidentity.ResolveSelection(repositoryDir, request.ModuleDir)
if err != nil {
return Candidate{}, fmt.Errorf("read committed release note %q: %w", path, err)
return captured, fmt.Errorf("resolve selected module: %w", err)
}
captured.RepositoryDir = identitySelection.RepositoryDir
captured.ModuleDir = identitySelection.ModuleDir
identity, err := releaseidentity.FromCommittedSelection(ctx, identitySelection, request.Version, state.commit)
if err != nil {
return captured, fmt.Errorf("resolve selected module release identity: %w", err)
}
captured.ModulePath = identity.ModulePath
captured.Tag = identity.Tag
note, err := git.ReadCommitFile(ctx, identity.RepositoryDir, state.commit, identity.NotePath)
if err != nil {
return captured, fmt.Errorf("read committed release note %q: %w", identity.NotePath, err)
}
if err := validateNote(note, request.Product, request.Version); err != nil {
return Candidate{}, fmt.Errorf("release note %q: %w", path, err)
return captured, fmt.Errorf("release note %q: %w", identity.NotePath, err)
}
if exists, err := hasLocalTag(ctx, repositoryDir, request.Version); err != nil {
return Candidate{}, fmt.Errorf("inspect local tag %q: %w", request.Version, err)
if exists, err := hasLocalTag(ctx, repositoryDir, identity.Tag); err != nil {
return captured, fmt.Errorf("inspect local tag %q: %w", identity.Tag, err)
} else if exists {
return Candidate{}, fmt.Errorf("local tag %q already exists", request.Version)
return captured, fmt.Errorf("local tag %q already exists", identity.Tag)
}
return Candidate{RepositoryDir: repositoryDir, Commit: state.commit, Version: request.Version, Note: string(note)}, nil
captured.Note = string(note)
return captured, nil
}
// Validate runs the selected offline Go suite, followed by AdditionalChecks
@@ -133,23 +154,23 @@ func Recheck(ctx context.Context, captured Candidate, expectedTagObject string)
return fmt.Errorf("worktree or index changed:\n%s", current.status)
}
if expectedTagObject == "" {
if exists, err := hasLocalTag(ctx, captured.RepositoryDir, captured.Version); err != nil {
return fmt.Errorf("recheck local tag %q: %w", captured.Version, err)
if exists, err := hasLocalTag(ctx, captured.RepositoryDir, captured.Tag); err != nil {
return fmt.Errorf("recheck local tag %q: %w", captured.Tag, err)
} else if exists {
return fmt.Errorf("local tag %q appeared during validation", captured.Version)
return fmt.Errorf("local tag %q appeared during validation", captured.Tag)
}
return nil
}
object, err := gitOutput(ctx, captured.RepositoryDir, "rev-parse", "--verify", "refs/tags/"+captured.Version)
object, err := gitOutput(ctx, captured.RepositoryDir, "rev-parse", "--verify", "refs/tags/"+captured.Tag)
if err != nil {
return fmt.Errorf("read local tag %q: %w", captured.Version, err)
return fmt.Errorf("read local tag %q: %w", captured.Tag, err)
}
tagType, err := gitOutput(ctx, captured.RepositoryDir, "cat-file", "-t", "refs/tags/"+captured.Version)
tagType, err := gitOutput(ctx, captured.RepositoryDir, "cat-file", "-t", "refs/tags/"+captured.Tag)
if err != nil {
return fmt.Errorf("inspect local tag %q: %w", captured.Version, err)
return fmt.Errorf("inspect local tag %q: %w", captured.Tag, err)
}
if strings.TrimSpace(string(object)) != expectedTagObject || strings.TrimSpace(string(tagType)) != "tag" {
return fmt.Errorf("local tag %q no longer identifies annotated tag object %s", captured.Version, expectedTagObject)
return fmt.Errorf("local tag %q no longer identifies annotated tag object %s", captured.Tag, expectedTagObject)
}
return nil
}
@@ -199,10 +220,10 @@ func gitOutput(ctx context.Context, repositoryDir string, args ...string) ([]byt
return stdout.Bytes(), nil
}
func hasLocalTag(ctx context.Context, repositoryDir, version string) (bool, error) {
func hasLocalTag(ctx context.Context, repositoryDir, tag string) (bool, error) {
var stderr bytes.Buffer
err := process.Run(ctx, process.Invocation{
Args: []string{"git", "show-ref", "--verify", "--quiet", "refs/tags/" + version},
Args: []string{"git", "show-ref", "--verify", "--quiet", "refs/tags/" + tag},
Dir: repositoryDir,
Stderr: &stderr,
})