kubernetes-sigs/promo-tools

Add flag to promote only last git diff

saschagrunert opened this issue · 0 comments

We can filter the manifest parse results based on which files or entries have changed in the PR and on the k8s.io main branch:

// ParseThinManifestsFromDir parses all thin Manifest files within a directory.
// We effectively have to create a map of manifests, keyed by the source
// registry (there can only be 1 source registry).
func ParseThinManifestsFromDir(
dir string,
) ([]Manifest, error) {
mfests := make([]Manifest, 0)
// Check that the thin manifests dir follows a regular, predefined format.
// This is to ensure that there isn't any funny business going on around
// paths.
if err := ValidateThinManifestDirectoryStructure(dir); err != nil {
return mfests, err
}
var parseAsManifest filepath.WalkFunc = func(
path string,
info os.FileInfo,
err error,
) error {
if err != nil {
// Prevent panic in case of incoming errors accessing this path.
logrus.Errorf("failure accessing a path %q: %v\n", path, err)
}
// Skip directories (because they are not YAML files).
if info.IsDir() {
return nil
}
// First try to parse the path as a manifest file, which must be named
// "promoter-manifest.yaml". This restriction is in place to limit the
// scope of what is read in as a promoter manifest.
if filepath.Base(path) != "promoter-manifest.yaml" {
return nil
}
// If there are any files named "promoter-manifest.yaml", they must be
// inside a subfolder within "manifests/<dir>" --- any other paths are
// forbidden.
shortened := strings.TrimPrefix(path, dir)
shortenedList := strings.Split(shortened, "/")
if len(shortenedList) != ThinManifestDepth {
return fmt.Errorf("unexpected manifest path %q",
path)
}
mfest, errParse := ParseThinManifestFromFile(path)
if errParse != nil {
logrus.Errorf("could not parse manifest file '%s'\n", path)
return errParse
}
// Save successful parse result.
mfests = append(mfests, mfest)
return nil
}
// Only look at manifests starting with the "manifests" subfolder (no need
// to walk any other toplevel subfolder).
if err := filepath.Walk(filepath.Join(dir, "manifests"), parseAsManifest); err != nil {
return mfests, err
}
if len(mfests) == 0 {
return nil, fmt.Errorf("no manifests found in dir: %s", dir)
}
return mfests, nil
}

The tricky part is to find the right diff for PRs as well as main. A new flag can be introduced to test the option later on.

Refers to #637