Normalize source and publication selection
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"go.maximumdirect.net/releasekit/internal/process"
|
||||
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
||||
"go.maximumdirect.net/releasekit/internal/versioncontract"
|
||||
)
|
||||
|
||||
@@ -172,14 +173,50 @@ type CheckCommand struct {
|
||||
// Settings contains the product, remote, publication expectation, shared
|
||||
// validation selections, and check commands read from a releasekit.json profile.
|
||||
type Settings struct {
|
||||
Product string `json:"product"`
|
||||
ModuleDir string `json:"module_dir,omitempty"`
|
||||
Remote string `json:"remote,omitempty"`
|
||||
ExpectedPushURL string `json:"expected_push_url,omitempty"`
|
||||
Checks []CheckCommand `json:"checks,omitempty"`
|
||||
Validation *ValidationSettings `json:"validation,omitempty"`
|
||||
Build *BuildSettings `json:"build,omitempty"`
|
||||
Delivery *DeliverySettings `json:"delivery,omitempty"`
|
||||
Product string `json:"product"`
|
||||
ModuleDir string `json:"module_dir,omitempty"`
|
||||
Source *SourceSettings `json:"source,omitempty"`
|
||||
Go *struct{} `json:"go,omitempty"`
|
||||
Publication *PublicationSettings `json:"publication,omitempty"`
|
||||
Version *VersionSettings `json:"version,omitempty"`
|
||||
Remote string `json:"remote,omitempty"`
|
||||
ExpectedPushURL string `json:"expected_push_url,omitempty"`
|
||||
Checks []CheckCommand `json:"checks,omitempty"`
|
||||
Validation *ValidationSettings `json:"validation,omitempty"`
|
||||
Build *BuildSettings `json:"build,omitempty"`
|
||||
Delivery *DeliverySettings `json:"delivery,omitempty"`
|
||||
}
|
||||
|
||||
type SourceSettings struct {
|
||||
Dir string `json:"dir,omitempty"`
|
||||
TagPrefix *string `json:"tag_prefix,omitempty"`
|
||||
}
|
||||
|
||||
type PublicationSettings struct {
|
||||
Mode string `json:"mode,omitempty"`
|
||||
}
|
||||
|
||||
type VersionSettings struct {
|
||||
Format string `json:"format,omitempty"`
|
||||
Prefix *string `json:"prefix,omitempty"`
|
||||
}
|
||||
|
||||
// SourceSpec maps the optional profile selection to the internal selection.
|
||||
func (s Settings) SourceSpec() *releaseidentity.SourceSpec {
|
||||
if s.Source == nil && s.Go == nil && s.Publication == nil && s.Version == nil {
|
||||
return nil
|
||||
}
|
||||
result := &releaseidentity.SourceSpec{GoEnabled: s.Go != nil}
|
||||
if s.Source != nil {
|
||||
result.Dir, result.TagPrefix = s.Source.Dir, s.Source.TagPrefix
|
||||
}
|
||||
if s.Publication != nil {
|
||||
result.Publication = s.Publication.Mode
|
||||
}
|
||||
if s.Version != nil {
|
||||
result.VersionFormat, result.VersionPrefix = s.Version.Format, s.Version.Prefix
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ValidationSettings selects shared candidate checks in a release profile.
|
||||
@@ -283,6 +320,12 @@ func LoadSettings(repoDir, configPath string) (Settings, error) {
|
||||
if strings.TrimSpace(settings.Product) == "" {
|
||||
return Settings{}, fmt.Errorf("profile %q: product is required", configPath)
|
||||
}
|
||||
if source := settings.SourceSpec(); source != nil {
|
||||
_, err := releaseidentity.NormalizeSource(settings.ModuleDir, source)
|
||||
if err != nil {
|
||||
return Settings{}, fmt.Errorf("profile %q: source selection: %w", configPath, err)
|
||||
}
|
||||
}
|
||||
if settings.Remote == "" {
|
||||
settings.Remote = "origin"
|
||||
}
|
||||
|
||||
51
cmd/releasekit/internal/cli/source_test.go
Normal file
51
cmd/releasekit/internal/cli/source_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProfileSourceSelection(t *testing.T) {
|
||||
tests := []struct {
|
||||
name, fields string
|
||||
explicit, goEnabled bool
|
||||
mode, format string
|
||||
emptyPrefix, invalid bool
|
||||
}{
|
||||
{name: "legacy"},
|
||||
{name: "null is omitted", fields: `,"source":null,"go":null,"publication":null,"version":null`},
|
||||
{name: "source root", fields: `,"source":{}`, explicit: true},
|
||||
{name: "explicit unprefixed tag", fields: `,"source":{"tag_prefix":""}`, explicit: true},
|
||||
{name: "Go application", fields: `,"go":{}`, explicit: true, goEnabled: true},
|
||||
{name: "Go module", fields: `,"go":{},"publication":{"mode":"go-module"}`, explicit: true, goEnabled: true, mode: "go-module"},
|
||||
{name: "calendar with explicit empty prefix", fields: `,"version":{"format":"date","prefix":""}`, explicit: true, format: "date", emptyPrefix: true},
|
||||
{name: "unknown source field", fields: `,"source":{"unknown":true}`, invalid: true},
|
||||
{name: "module publication without Go", fields: `,"publication":{"mode":"go-module"}`, invalid: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "releasekit.json"), []byte(`{"product":"test"`+tt.fields+`}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settings, err := LoadSettings(dir, "")
|
||||
if (err != nil) != tt.invalid {
|
||||
t.Fatalf("LoadSettings error = %v", err)
|
||||
}
|
||||
if tt.invalid {
|
||||
return
|
||||
}
|
||||
source := settings.SourceSpec()
|
||||
if (source != nil) != tt.explicit {
|
||||
t.Fatalf("source = %+v", source)
|
||||
}
|
||||
if source != nil && (source.GoEnabled != tt.goEnabled || string(source.Publication) != tt.mode || string(source.VersionFormat) != tt.format || (source.VersionPrefix != nil && *source.VersionPrefix == "") != tt.emptyPrefix) {
|
||||
t.Fatalf("source = %+v", source)
|
||||
}
|
||||
if tt.name == "explicit unprefixed tag" && (source.TagPrefix == nil || *source.TagPrefix != "") {
|
||||
t.Fatalf("tag prefix was not preserved: %+v", source)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -204,6 +204,7 @@ func runBuild(ctx context.Context, invocation cli.Invocation, stdout, stderr io.
|
||||
result, err := releasekit.Build(ctx, releasekit.BuildRequest{
|
||||
RepositoryDir: invocation.Repository,
|
||||
ModuleDir: settings.ModuleDir,
|
||||
Source: sourceSpec(settings),
|
||||
Version: invocation.Version,
|
||||
Spec: *configuredBuildSpec(settings.Build),
|
||||
OutputDir: invocation.OutputDir,
|
||||
@@ -252,10 +253,22 @@ func runBuild(ctx context.Context, invocation cli.Invocation, stdout, stderr io.
|
||||
return 0
|
||||
}
|
||||
|
||||
func sourceSpec(settings cli.Settings) *releasekit.SourceSpec {
|
||||
source := settings.SourceSpec()
|
||||
if source == nil {
|
||||
return nil
|
||||
}
|
||||
return &releasekit.SourceSpec{
|
||||
Dir: source.Dir, GoEnabled: source.GoEnabled, Publication: releasekit.PublicationMode(source.Publication),
|
||||
VersionFormat: releasekit.VersionFormat(source.VersionFormat), VersionPrefix: source.VersionPrefix, TagPrefix: source.TagPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
func candidateRequest(invocation cli.Invocation, settings cli.Settings, output io.Writer) releasekit.ReleaseRequest {
|
||||
request := releasekit.ReleaseRequest{
|
||||
RepositoryDir: invocation.Repository,
|
||||
ModuleDir: settings.ModuleDir,
|
||||
Source: sourceSpec(settings),
|
||||
Product: settings.Product,
|
||||
Version: invocation.Version,
|
||||
Remote: settings.Remote,
|
||||
@@ -520,6 +533,7 @@ func deliverWithDestination(ctx context.Context, invocation cli.Invocation, sett
|
||||
result, err := releasekit.Deliver(ctx, releasekit.DeliveryRequest{
|
||||
RepositoryDir: invocation.Repository,
|
||||
ModuleDir: settings.ModuleDir,
|
||||
Source: sourceSpec(settings),
|
||||
Product: settings.Product,
|
||||
Version: invocation.Version,
|
||||
ExpectedTag: invocation.ExpectedTag,
|
||||
|
||||
24
cmd/releasekit/source_test.go
Normal file
24
cmd/releasekit/source_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
)
|
||||
|
||||
func TestSourceSelectionMapsToRequest(t *testing.T) {
|
||||
empty := ""
|
||||
settings := cli.Settings{
|
||||
ModuleDir: "app",
|
||||
Source: &cli.SourceSettings{TagPrefix: &empty},
|
||||
Go: &struct{}{},
|
||||
Version: &cli.VersionSettings{Format: "date", Prefix: &empty},
|
||||
}
|
||||
request := candidateRequest(cli.Invocation{}, settings, nil)
|
||||
if request.Source == nil || !request.Source.GoEnabled || request.Source.Dir != "" || string(request.Source.Publication) != "" || string(request.Source.VersionFormat) != "date" || request.Source.VersionPrefix == nil || *request.Source.VersionPrefix != "" || request.Source.TagPrefix == nil || *request.Source.TagPrefix != "" || request.ModuleDir != "app" {
|
||||
t.Fatalf("mapped selection = %+v, module directory = %q", request.Source, request.ModuleDir)
|
||||
}
|
||||
if sourceSpec(cli.Settings{}) != nil {
|
||||
t.Fatal("legacy profile was converted to an explicit source selection")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user