Validate and publish source-only candidates
This commit is contained in:
@@ -35,6 +35,7 @@ type Request struct {
|
||||
RepositoryDir string
|
||||
ModuleDir string
|
||||
Source *releaseidentity.SourceSpec
|
||||
BuildTargets bool
|
||||
Product string
|
||||
Version string
|
||||
Validation validation.Settings
|
||||
@@ -69,7 +70,7 @@ func Check(ctx context.Context, request Request) (Candidate, error) {
|
||||
// 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) {
|
||||
if request.Source == nil && !validStableVersion(request.Version) {
|
||||
return captured, fmt.Errorf("%w: %q", ErrInvalidVersion, request.Version)
|
||||
}
|
||||
if strings.TrimSpace(request.Product) == "" {
|
||||
@@ -99,10 +100,13 @@ func Capture(ctx context.Context, request Request) (Candidate, error) {
|
||||
captured.ModulePath = identity.ModulePath
|
||||
captured.Tag = identity.Tag
|
||||
if err != nil {
|
||||
if errors.Is(err, versioncontract.ErrInvalidIdentifier) {
|
||||
return captured, fmt.Errorf("%w: %q", ErrInvalidVersion, request.Version)
|
||||
}
|
||||
return captured, fmt.Errorf("resolve selected module release identity: %w", err)
|
||||
}
|
||||
if identity.Publication == "source" {
|
||||
return captured, fmt.Errorf("general source candidate validation is not yet supported")
|
||||
if !identity.GoEnabled && request.BuildTargets {
|
||||
return captured, errors.New("build targets require Go capability")
|
||||
}
|
||||
note, err := git.ReadCommitFile(ctx, identity.RepositoryDir, state.commit, identity.NotePath)
|
||||
if err != nil {
|
||||
@@ -121,6 +125,7 @@ func Capture(ctx context.Context, request Request) (Candidate, error) {
|
||||
}
|
||||
|
||||
captured.Note = string(note)
|
||||
captured.Identity.Note = captured.Note
|
||||
return captured, nil
|
||||
}
|
||||
|
||||
@@ -134,6 +139,8 @@ func Validate(ctx context.Context, request Request, captured Candidate) error {
|
||||
if err := validation.Run(ctx, validation.Request{
|
||||
RepositoryDir: captured.RepositoryDir,
|
||||
ModuleDir: captured.ModuleDir,
|
||||
SourceDir: captured.Identity.SourceDir,
|
||||
SourceOnly: !captured.Identity.GoEnabled,
|
||||
Commit: captured.Commit,
|
||||
Output: request.Output,
|
||||
Settings: request.Validation,
|
||||
|
||||
@@ -46,6 +46,7 @@ type Request struct {
|
||||
RepositoryDir string
|
||||
ModuleDir string
|
||||
Source *releaseidentity.SourceSpec
|
||||
BuildTargets bool
|
||||
Product string
|
||||
Version string
|
||||
Remote string
|
||||
@@ -58,6 +59,7 @@ type Request struct {
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Identity releaseidentity.Identity
|
||||
RepositoryDir string
|
||||
ModuleDir string
|
||||
ModulePath string
|
||||
@@ -104,6 +106,7 @@ func Publish(ctx context.Context, request Request) (result Result, resultErr err
|
||||
RepositoryDir: request.RepositoryDir,
|
||||
ModuleDir: request.ModuleDir,
|
||||
Source: request.Source,
|
||||
BuildTargets: request.BuildTargets,
|
||||
Product: request.Product,
|
||||
Version: request.Version,
|
||||
Validation: request.Validation,
|
||||
@@ -111,6 +114,7 @@ func Publish(ctx context.Context, request Request) (result Result, resultErr err
|
||||
Output: request.Output,
|
||||
}
|
||||
captured, captureErr := candidate.Capture(ctx, checks)
|
||||
result.Identity = captured.Identity
|
||||
result.RepositoryDir = captured.RepositoryDir
|
||||
result.ModuleDir = captured.ModuleDir
|
||||
result.ModulePath = captured.ModulePath
|
||||
|
||||
@@ -178,7 +178,7 @@ func deriveSelected(ctx context.Context, source ResolvedSource, version, commit
|
||||
return identity, err
|
||||
}
|
||||
if !versioncontract.ValidIdentifier(version, source.VersionFormat, source.VersionPrefix) {
|
||||
return identity, fmt.Errorf("invalid %s release identifier %q", source.VersionFormat, version)
|
||||
return identity, fmt.Errorf("%w: %q is not a valid %s identifier", versioncontract.ErrInvalidIdentifier, version, source.VersionFormat)
|
||||
}
|
||||
if source.Publication == "source" {
|
||||
prefix := source.ModuleSubdir
|
||||
|
||||
@@ -28,7 +28,7 @@ func BenchmarkSelectInputEntriesByModuleCount(b *testing.B) {
|
||||
}
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
selected, err := selectInputEntries(entries, byPath, ".", []string{})
|
||||
selected, err := selectInputEntries(entries, byPath, ".", []string{}, false)
|
||||
if err != nil || len(selected) != retainedFiles {
|
||||
b.Fatalf("selected %d files, error=%v; want %d", len(selected), err, retainedFiles)
|
||||
}
|
||||
|
||||
@@ -28,13 +28,14 @@ type CandidateTree struct {
|
||||
closeErr error
|
||||
}
|
||||
|
||||
// TreeScope selects the automatic input files for one physical module and
|
||||
// optional repository-shared files. ModuleDir is an absolute path. A nil
|
||||
// SharedFiles slice selects the optional default files; a non-nil slice
|
||||
// replaces those defaults.
|
||||
// TreeScope selects automatic input files under an absolute physical source
|
||||
// directory and optional repository-shared files. A nil SharedFiles slice
|
||||
// selects optional defaults; a non-nil slice replaces them. IncludeNestedModules
|
||||
// treats nested module declarations as ordinary files.
|
||||
type TreeScope struct {
|
||||
ModuleDir string
|
||||
SharedFiles []string
|
||||
ModuleDir string
|
||||
SharedFiles []string
|
||||
IncludeNestedModules bool
|
||||
}
|
||||
|
||||
// ReadCandidateTree enumerates the captured commit without reading from the
|
||||
@@ -74,7 +75,7 @@ func ReadCandidateTreeForScope(ctx context.Context, repositoryDir, commit string
|
||||
tree.directories[directory] = struct{}{}
|
||||
}
|
||||
}
|
||||
tree.inputEntries, err = selectInputEntries(entries, tree.byPath, modulePath, scope.SharedFiles)
|
||||
tree.inputEntries, err = selectInputEntries(entries, tree.byPath, modulePath, scope.SharedFiles, scope.IncludeNestedModules)
|
||||
if err != nil {
|
||||
_ = tree.Close()
|
||||
return nil, err
|
||||
@@ -94,17 +95,19 @@ func (tree *CandidateTree) InputEntries() []git.TreeEntry {
|
||||
return append([]git.TreeEntry(nil), tree.inputEntries...)
|
||||
}
|
||||
|
||||
func selectInputEntries(entries []git.TreeEntry, byPath map[string]int, modulePath string, sharedFiles []string) ([]git.TreeEntry, error) {
|
||||
func selectInputEntries(entries []git.TreeEntry, byPath map[string]int, modulePath string, sharedFiles []string, includeNestedModules bool) ([]git.TreeEntry, error) {
|
||||
boundaries := make(map[string]struct{})
|
||||
for _, entry := range entries {
|
||||
moduleRelative, ok := relativeToModule(entry.Path, modulePath)
|
||||
if !ok || moduleRelative == "go.mod" || path.Base(moduleRelative) != "go.mod" {
|
||||
continue
|
||||
if !includeNestedModules {
|
||||
for _, entry := range entries {
|
||||
moduleRelative, ok := relativeToModule(entry.Path, modulePath)
|
||||
if !ok || moduleRelative == "go.mod" || path.Base(moduleRelative) != "go.mod" {
|
||||
continue
|
||||
}
|
||||
if insideVendor(moduleRelative) {
|
||||
continue
|
||||
}
|
||||
boundaries[path.Dir(entry.Path)] = struct{}{}
|
||||
}
|
||||
if insideVendor(moduleRelative) {
|
||||
continue
|
||||
}
|
||||
boundaries[path.Dir(entry.Path)] = struct{}{}
|
||||
}
|
||||
selected := make(map[string]git.TreeEntry)
|
||||
for _, entry := range entries {
|
||||
|
||||
@@ -151,6 +151,26 @@ func TestReadCandidateTreeForRootScopeExcludesNestedModules(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceScopeIncludesNestedModuleFiles(t *testing.T) {
|
||||
repository := newTreeRepository(t)
|
||||
writeTreeFile(t, repository, "config/readme.md", []byte("# Configuration\n"))
|
||||
writeTreeFile(t, repository, "config/nested/go.mod", []byte("data\n"))
|
||||
writeTreeFile(t, repository, "config/nested/readme.md", []byte("# Nested\n"))
|
||||
writeTreeFile(t, repository, "outside/readme.md", []byte("# Outside\n"))
|
||||
commit := commitCandidateTree(t, repository)
|
||||
tree, err := ReadCandidateTreeForScope(context.Background(), repository, commit, TreeScope{
|
||||
ModuleDir: filepath.Join(repository, "config"), SharedFiles: []string{}, IncludeNestedModules: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cleanupCandidateTree(t, tree)
|
||||
want := "config/nested/go.mod\nconfig/nested/readme.md\nconfig/readme.md"
|
||||
if got := strings.Join(inputPaths(tree), "\n"); got != want {
|
||||
t.Fatalf("source inputs = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCandidateTreeSharedFilesReplaceDefaultsAndDeduplicate(t *testing.T) {
|
||||
repository := newTreeRepository(t)
|
||||
writeTreeFile(t, repository, "LICENSE", []byte("default\n"))
|
||||
|
||||
@@ -55,6 +55,8 @@ type Target struct {
|
||||
type Request struct {
|
||||
RepositoryDir string
|
||||
ModuleDir string
|
||||
SourceDir string
|
||||
SourceOnly bool
|
||||
Commit string
|
||||
Output io.Writer
|
||||
Settings Settings
|
||||
@@ -64,9 +66,14 @@ type Request struct {
|
||||
// ReadCandidateTree loads the committed-file inventory when a selected check
|
||||
// needs file-oriented input.
|
||||
func (request Request) ReadCandidateTree(ctx context.Context) (*CandidateTree, error) {
|
||||
directory := request.ModuleDir
|
||||
if request.SourceOnly {
|
||||
directory = request.SourceDir
|
||||
}
|
||||
return ReadCandidateTreeForScope(ctx, request.RepositoryDir, request.Commit, TreeScope{
|
||||
ModuleDir: request.ModuleDir,
|
||||
SharedFiles: request.Settings.SharedFiles,
|
||||
ModuleDir: directory,
|
||||
SharedFiles: request.Settings.SharedFiles,
|
||||
IncludeNestedModules: request.SourceOnly,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -89,8 +96,14 @@ func Run(ctx context.Context, request Request) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := runGoChecks(ctx, request); err != nil {
|
||||
return err
|
||||
if request.SourceOnly {
|
||||
if request.Settings.Race || request.Settings.FullVet || len(request.Settings.CompileTargets) != 0 {
|
||||
return fmt.Errorf("Go execution checks require Go capability")
|
||||
}
|
||||
} else {
|
||||
if err := runGoChecks(ctx, request); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return runTreeChecks(ctx, request)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package versioncontract
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrInvalidIdentifier identifies a production identifier outside its grammar.
|
||||
var ErrInvalidIdentifier = errors.New("invalid release identifier")
|
||||
|
||||
// ValidStableVersion reports whether version uses the selected vMAJOR.MINOR.PATCH form.
|
||||
func ValidStableVersion(version string) bool {
|
||||
return ValidIdentifier(version, "semver", "v")
|
||||
|
||||
Reference in New Issue
Block a user