44 lines
1.8 KiB
Go
44 lines
1.8 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) {
|
|
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 selector == "" || strings.Contains(selector, "/") {
|
|
return identity, "", fmt.Errorf("diagnostic selector must be one Git ref component")
|
|
}
|
|
if !source.GoEnabled {
|
|
identity.ModuleDir, identity.ModuleSubdir = "", ""
|
|
}
|
|
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.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 := git.CheckTagRef(ctx, source.RepositoryDir, identity.Tag); err != nil {
|
|
return identity, "", fmt.Errorf("invalid diagnostic selector %q: %w", selector, err)
|
|
}
|
|
return identity, attempt, nil
|
|
}
|