64 lines
2.7 KiB
Go
64 lines
2.7 KiB
Go
package releaseidentity
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
|
)
|
|
|
|
// NewDiagnostic constructs a unique diagnostic identity for an already
|
|
// resolved source. Diagnostic versions are independent of production formats;
|
|
// Publication records the source selection, not a module publication claim.
|
|
func NewDiagnostic(ctx context.Context, source ResolvedSource, selector, commit string) (Identity, string, error) {
|
|
var random [16]byte
|
|
if _, err := rand.Read(random[:]); err != nil {
|
|
return Identity{}, "", fmt.Errorf("generate diagnostic attempt ID: %w", err)
|
|
}
|
|
attempt := hex.EncodeToString(random[:])
|
|
identity, err := DiagnosticFromAttempt(ctx, source, selector, commit, attempt)
|
|
return identity, attempt, err
|
|
}
|
|
|
|
// DiagnosticFromAttempt reconstructs an identity from verified attempt data.
|
|
func DiagnosticFromAttempt(ctx context.Context, source ResolvedSource, selector, commit, attempt string) (Identity, error) {
|
|
identity := Identity{
|
|
Selection: source.Selection, Kind: "diagnostic", SourceDir: source.ModuleDir, SourceSubdir: source.ModuleSubdir,
|
|
GoEnabled: source.GoEnabled, Publication: source.Publication, VersionFormat: source.VersionFormat,
|
|
VersionPrefix: source.VersionPrefix, ModulePath: source.ModulePath, Commit: commit,
|
|
}
|
|
if source.RepositoryDir == "" || source.ModuleDir == "" || source.ModuleSubdir == "" {
|
|
return identity, fmt.Errorf("incomplete source selection")
|
|
}
|
|
if len(attempt) != 32 || strings.ToLower(attempt) != attempt {
|
|
return identity, fmt.Errorf("invalid diagnostic attempt ID")
|
|
}
|
|
if _, err := hex.DecodeString(attempt); err != nil {
|
|
return identity, fmt.Errorf("invalid diagnostic attempt ID")
|
|
}
|
|
if !git.ValidObjectID(commit) {
|
|
return identity, fmt.Errorf("invalid diagnostic commit")
|
|
}
|
|
if !source.GoEnabled {
|
|
identity.ModuleDir, identity.ModuleSubdir = "", ""
|
|
}
|
|
identity.Tag = "releasekit-test/" + selector + "/" + attempt
|
|
identity.Version = "releasekit-test-" + attempt
|
|
identity.Note = fmt.Sprintf("# ReleaseKit diagnostic %s\n\nSelector: %s\nCommit: %s\n", attempt, selector, commit)
|
|
if err := ValidateDiagnosticSelector(ctx, source.RepositoryDir, selector); err != nil {
|
|
return identity, fmt.Errorf("invalid diagnostic selector %q: %w", selector, err)
|
|
}
|
|
return identity, nil
|
|
}
|
|
|
|
// ValidateDiagnosticSelector checks the selector using the diagnostic ref grammar.
|
|
func ValidateDiagnosticSelector(ctx context.Context, repository, selector string) error {
|
|
if selector == "" || strings.Contains(selector, "/") {
|
|
return fmt.Errorf("diagnostic selector must be one Git ref component")
|
|
}
|
|
return git.CheckTagRef(ctx, repository, "releasekit-test/"+selector+"/"+strings.Repeat("0", 32))
|
|
}
|