Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 117 additions & 18 deletions control/packagemanagers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package control

import (
"context"
"errors"
"fmt"
"slices"
"strings"
"time"

"github.com/sirupsen/logrus"

"github.com/getplumber/plumber/configuration"
githubpkg "github.com/getplumber/plumber/github"
"github.com/getplumber/plumber/gitlab"
"github.com/getplumber/plumber/internal/ir"
"github.com/getplumber/plumber/internal/repofs"
"github.com/getplumber/plumber/packagemanager/mise"
"github.com/getplumber/plumber/utils"
)
Expand Down Expand Up @@ -52,24 +61,45 @@ func miseChecksRun(pc *configuration.PlumberConfig) bool {
return pc != nil && pc.Mise.IsEnabled() && pc.Mise.AnyCheckEnabled()
}

// scanPackageManagers reads the package manager files of the local
// checkout (conf.GitRepoRoot) into pipeline. It reads them when any
// configuration the run collects for turns a mise check on: the run's own
// configuration, or in platform mode every resolved policy
// (collectionConfigs), since each policy is then evaluated over this one
// scan. Nothing is read when --controls / --skip-controls leave
// mise-en-place out: a control the run does not report must not stop it.
// An empty root means there is no checkout: pipeline.Mise stays nil and
// markPackageManagersGap reports the control not evaluated. Reading files
// from disk works the same for every provider; remote reading comes after
// (#254).
// packageSource is where scanPackageManagers reads the repository: the
// local checkout at root, or, without one, the remote repository remote
// opens. The run's provider and mode choose it, so the mise block names
// no provider. Both empty means nothing can be read.
type packageSource struct {
root string
remote func() (repofs.Source, error)
}

// newGitHubRemoteFiles and newGitLabRemoteFiles open a remote
// repository's files; tests swap them.
var (
newGitHubRemoteFiles = func(host, owner, repo, rev string) (repofs.Source, error) {
return githubpkg.NewRemoteFiles(host, owner, repo, rev)
}
newGitLabRemoteFiles = func(conf *configuration.Configuration, ref string) (repofs.Source, error) {
return gitlab.NewRemoteFiles(conf.ProjectPath, ref, conf.GitlabToken, conf.GitlabURL, conf)
}
)

// scanPackageManagers reads the package manager files of the repository
// into pipeline, from the checkout or the remote repository (src). It
// reads them when any configuration the run collects for turns a mise
// check on: the run's own configuration, or in platform mode every
// resolved policy (collectionConfigs), since each policy is then
// evaluated over this one scan. Nothing is read when --controls /
// --skip-controls leave mise-en-place out: a control the run does not
// report must not stop it. With no source pipeline.Mise stays nil and
// markPackageManagersGap reports the control not evaluated.
//
// A remote read lists the tree once and fetches the mise files in
// batches (repofs.Rounds), never one request per file.
//
// A mise file that cannot be read stops the run: the error
// (*mise.ReadError) lists every such file, and no report is produced. A
// file under exemptPaths only spares the run when every configuration
// that checks mise exempts it.
func scanPackageManagers(pipeline *ir.NormalizedPipeline, conf *configuration.Configuration) error {
if pipeline == nil || conf == nil || conf.GitRepoRoot == "" {
func scanPackageManagers(pipeline *ir.NormalizedPipeline, conf *configuration.Configuration, src packageSource) error {
if pipeline == nil || conf == nil || (src.root == "" && src.remote == nil) {
return nil
}
if !shouldRunControl(controlMiseEnPlace, conf) {
Expand All @@ -91,16 +121,78 @@ func scanPackageManagers(pipeline *ir.NormalizedPipeline, conf *configuration.Co
if len(opts.ExemptPaths) == 0 {
return nil
}
root := conf.GitRepoRoot
opts.Tracked = func(paths []string) []string { return trackedFiles(root, paths) }
scan, err := mise.Scan(root, opts)
var scan *ir.MiseScan
var err error
start := time.Now()
mode := "local"
if src.root != "" {
root := src.root
opts.Tracked = func(paths []string) []string { return trackedFiles(root, paths) }
scan, err = mise.Scan(mise.DirFS(root), opts)
} else {
// Every file of a remote repository is committed: opts.Tracked
// stays nil, so a local config there is read.
mode = "remote"
scan, err = scanRemoteMise(context.Background(), src.remote, opts)
}
logrus.WithFields(logrus.Fields{"mode": mode, "duration": time.Since(start).Round(time.Millisecond)}).
Info("mise files read")
if errors.Is(err, repofs.ErrTooLarge) {
// Too large to read remotely: mise-en-place is not evaluated,
// every other control still reports (markPackageManagersGap).
logrus.WithError(err).Warn("mise files not read")
pipeline.MiseNotRead = err.Error()
return nil
}
if err != nil {
return err
}
pipeline.Mise = scan
return nil
}

// gitlabMiseRef is the ref a remote GitLab read of the mise files uses:
// the analyzed commit, so the files match the CI configuration read. When
// --branch names another branch and its head could not be fetched,
// HeadCommitSha is still the default branch's head: the branch itself is
// read then, never the default branch under the analyzed branch's name.
func gitlabMiseRef(project *gitlab.Project, result *AnalysisResult) string {
if result == nil {
return project.DefaultBranch
}
sha, branch := result.HeadCommitSha, result.AnalyzeBranch
switch {
case branch != "" && branch != project.DefaultBranch && sha == project.LatestHeadCommitSha:
return branch
case sha != "" && sha != "HEAD":
return sha
case branch != "":
return branch
}
return project.DefaultBranch
}

// scanRemoteMise lists the remote repository, fetches every file the
// scan reads by name in one batch, and scans; a file only the scan finds
// (a task include, a symlink target) is fetched in a later round.
func scanRemoteMise(ctx context.Context, open func() (repofs.Source, error), opts mise.Options) (*ir.MiseScan, error) {
src, err := open()
if err != nil {
return nil, fmt.Errorf("read the mise files: %w", err)
}
entries, err := src.List(ctx)
if err != nil {
return nil, fmt.Errorf("read the mise files: %w", err)
}
f := repofs.New(entries)
if err := f.Fetch(ctx, src, f.Paths(opts.Wanted)); err != nil {
return nil, fmt.Errorf("read the mise files: %w", err)
}
return repofs.Rounds(ctx, f, src, repofs.MaxRounds, func(f *repofs.FS) (*ir.MiseScan, error) {
return mise.Scan(f, opts)
})
}

// trackedFiles keeps the paths git tracks in the repository at root: a
// mise local config on disk is personal unless it was committed. When git
// cannot answer, every path is kept, so a committed file is never missed.
Expand Down Expand Up @@ -128,7 +220,10 @@ func trackedFiles(root string, paths []string) []string {
// markPackageManagersGap reports mise-en-place not
// evaluated, never passed, when it could not check anything:
// - the mise block is on but turns no check on (config_required),
// - there is no local checkout (a remote run).
// - the repository was too large to read remotely (collection_failed,
// the run degraded),
// - the mise files were not read for this run (a policy that turns
// mise on over a run that did not read them).
//
// A mise file that cannot be read is not a gap: it stops the run before
// this point (scanPackageManagers). A missing or disabled mise block
Expand All @@ -137,10 +232,14 @@ func markPackageManagersGap(r *AnalysisResult, pc *configuration.PlumberConfig)
if pc == nil || !pc.Mise.IsEnabled() {
return
}
m := r.evaluatedPipeline().MiseOrNil()
p := r.evaluatedPipeline()
m := p.MiseOrNil()
switch {
case !pc.Mise.AnyCheckEnabled():
r.MarkNotEvaluable(controlMiseEnPlace, ReasonConfigRequired)
case m == nil && p != nil && p.MiseNotRead != "":
r.MarkNotEvaluable(controlMiseEnPlace, ReasonCollectionFailed)
markDegraded(r, "the mise files were not read: "+p.MiseNotRead+"; mise-en-place was not evaluated")
case m == nil:
r.MarkNotEvaluable(controlMiseEnPlace, ReasonLaneNotCollected)
default:
Expand Down
Loading
Loading