vault/tools/pipeline/internal/pkg/changed/checkers.go
Ryan Cragun c37b3c46b4
VAULT-34822: Add pipeline github list changed-files (#30100)
* VAULT-34822: Add `pipeline github list changed-files`

Add a new `github list changed-files` sub-command to `pipeline` command and
integrate it into the pipeline. This replaces our previous
`changed-files.sh` script.

This command works quite a bit differently than the full checkout and
diff based solution we used before. Instead of checking out the base ref
and head ref and comparing a diff, we now provide either a pull request
number or git commit SHA and use the Github REST API to determine the
changed files.

This approach has several benefits:
  - Not requiring a local checkout of the repo to get the list of
    changed files. This yields a significant perfomance improvement in
    `setup` jobs where we typically determine the changed files list.
  - The CLI supports both PRs and commit SHAs.
  - The implementation is portable and doesn't require any system tools
    like `git` or `bash` to be installed.
  - A much more advanced system for adding group metadata to the changed
    files. These groupings are going to be used heavily in future
    pipeline automation work and will be used to make required jobs
    smarter.

The theoretical drawbacks:
   - It requires a GITHUB_TOKEN and only works for remote branches or
     commits in Github. We could eventually add a local diff sub-command
     or option to work locally, but that was not required for what we're
     trying to achieve here.

While the groupings that I added in this change are quite rudimentary,
the system will allow us to add additional groups with very little
overhead. I tried to make this change more or less a port of the old
system to enable future work. I did include one small change of
behavior, which is that we now build all extended targets if the
`go.mod` or `go.sum` files change. We do this to ensure that dependency
changes don't subtly result in some extended platform breakage.

Signed-off-by: Ryan Cragun <me@ryan.ec>
2025-03-28 15:18:52 -06:00

104 lines
2.8 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package changed
import (
"context"
"os"
"path/filepath"
"strings"
)
// FileGroupCheck is a function that takes a reference to a changed file and returns groups that
// the file belongs to
type FileGroupCheck func(context.Context, *File) FileGroups
// DefaultFileGroupCheckers are the default file group checkers
var DefaultFileGroupCheckers = []FileGroupCheck{
FileGroupCheckerDir,
FileGroupCheckerFileName,
FileGroupCheckerFileGo,
FileGroupCheckerFileProto,
}
// FileGroupCheckerDir is a file group checker that groups based on the files directory
func FileGroupCheckerDir(ctx context.Context, file *File) FileGroups {
name := file.Name()
groups := FileGroups{}
for dir, groups := range map[string]FileGroups{
".github": groups.Add(FileGroupPipeline),
"changelog": groups.Add(FileGroupChangelog),
"enos": groups.Add(FileGroupEnos),
"tools": groups.Add(FileGroupTools),
"ui": groups.Add(FileGroupWebUI),
"website": groups.Add(FileGroupDocs),
} {
if strings.HasPrefix(name, dir+string(os.PathSeparator)) {
return groups
}
}
return nil
}
// FileGroupCheckerFileName is a file group checker that groups based on the files name
func FileGroupCheckerFileName(ctx context.Context, file *File) FileGroups {
name := file.Name()
groups := FileGroups{}
switch {
case strings.HasPrefix(name, "buf."):
return groups.Add(FileGroupProto)
case strings.HasPrefix(name, "CHANGELOG"):
return groups.Add(FileGroupChangelog)
case strings.HasPrefix(name, "CODEOWNERS"):
return groups.Add(FileGroupPipeline)
case strings.HasSuffix(name, "go.mod") || strings.HasSuffix(name, "go.sum"):
return groups.Add(FileGroupGoModules, FileGroupGoApp)
}
return nil
}
// FileGroupCheckerFileGo is a file group checker that groups based on the files extension being .go
func FileGroupCheckerFileGo(ctx context.Context, file *File) FileGroups {
name := file.Name()
ext := filepath.Ext(name)
if ext != ".go" {
return nil
}
groups := FileGroups{}
groups = groups.Add(FileGroupGoApp)
if strings.Contains(name, "raft_autopilot") {
groups = groups.Add(FileGroupAutopilot)
}
if strings.HasSuffix(name, "_ent.go") {
groups = groups.Add(FileGroupEnterprise)
} else if strings.HasSuffix(name, "_oss.go") || strings.HasSuffix(name, "_ce.go") {
groups = groups.Add(FileGroupCommunity)
}
if strings.HasPrefix(name, "tools/pipeline") {
groups = groups.Add(FileGroupPipeline)
}
return groups
}
// FileGroupCheckerFileProto is a file group checker that groups based on the files extension being .proto
func FileGroupCheckerFileProto(ctx context.Context, file *File) FileGroups {
name := file.Name()
ext := filepath.Ext(name)
if ext != ".proto" {
return nil
}
return FileGroups{FileGroupProto}
}