69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package readiness
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
|
|
"go.maximumdirect.net/releasekit/internal/artifact"
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
|
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
|
)
|
|
|
|
func committedFile(ctx context.Context, repo, commit, name string) ([]byte, error) {
|
|
resolver := git.CommittedPaths(repo, commit)
|
|
destination, err := resolver.ResolveDestination(ctx, "root", name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if destination.Kind != git.DestinationEntry {
|
|
return nil, fmt.Errorf("committed input %q is %s", git.DisplayDiagnostic(name), destination.Kind)
|
|
}
|
|
entry, found, err := resolver.Lookup(ctx, destination.Path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !found || (entry.Mode != "100644" && entry.Mode != "100755") {
|
|
return nil, errors.New("committed input is not a regular file")
|
|
}
|
|
return git.ReadBlob(ctx, repo, entry.ObjectID)
|
|
}
|
|
|
|
func inspectCommittedBuild(ctx context.Context, source releaseidentity.ResolvedSource, commit string, build artifact.Settings) error {
|
|
if err := validateBuildSettings(build); err != nil {
|
|
return err
|
|
}
|
|
name := build.MainPackage
|
|
if name == source.ModulePath {
|
|
name = "."
|
|
} else if strings.HasPrefix(name, source.ModulePath+"/") {
|
|
name = "./" + strings.TrimPrefix(name, source.ModulePath+"/")
|
|
}
|
|
if name != "." && !strings.HasPrefix(name, "./") {
|
|
return nil
|
|
}
|
|
resolver := git.CommittedPaths(source.RepositoryDir, commit)
|
|
destination, err := resolver.ResolveDestination(ctx, path.Join(source.ModuleSubdir, "placeholder"), name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if destination.Kind != git.DestinationDirectory {
|
|
return fmt.Errorf("committed main package is %s", destination.Kind)
|
|
}
|
|
entries, err := git.ReadTree(ctx, source.RepositoryDir, commit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, entry := range entries {
|
|
if path.Dir(entry.Path) != destination.Path || !strings.HasSuffix(entry.Path, ".go") || strings.HasSuffix(entry.Path, "_test.go") {
|
|
continue
|
|
}
|
|
if _, err := committedFile(ctx, source.RepositoryDir, commit, entry.Path); err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("committed main package contains no non-test Go source")
|
|
}
|