vault/tools/pipeline/internal/pkg/changed/file.go
Ryan Cragun 10c4371135
VAULT-34834: pipeline: add better heuristics for changed files (#30284)
* VAULT-34834: pipeline: add better heuristics for changed files

To fully support automated Enterprise to Community backports we need to
have better changed file detection for community and enterprise only
files. Armed with this metadata, future changes will be able to inspect
changed files and automatically remove enterprise only files when
creating the CE backports.

For this change we now have the following changed file groups:
  - autopilot
  - changelog
  - community
  - docs
  - enos
  - enterprise
  - app
  - gotoolchain
  - pipeline
  - proto
  - tools
  - ui

Not included in the change, but something I did while updating out
checkers was generate a list of files that included only in
vault-enterprise and run every path the enterprise detection rules
to ensure that they are categorized appropriately post changes in
VAULT-35431. While it's possible that they'll drift, our changed
file categorization is best effort anyway and changes will always
happen in vault-enterprise and require a developer to approve the
changes.

We've also included a few new files into the various groups and updated
the various workflows to use the new categories. I've also included a
small change to the pipeline composite action whereby we do not handle
Go module caching. This will greatly reduce work on doc-only branches
that need only ensure that the pipeline binary is compiled.

Signed-off-by: Ryan Cragun <me@ryan.ec>
2025-04-18 10:54:41 -06:00

93 lines
2.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package changed
import (
"slices"
"strings"
gh "github.com/google/go-github/v68/github"
)
type (
// File is a changed file in a PR or commit
File struct {
File *gh.CommitFile `json:"file,omitempty"`
Groups FileGroups `json:"groups,omitempty"`
}
// Files is a slice of changed files in a PR or commit
Files []*File
// FileGroup is group name describing a class of file the changed file belongs to
FileGroup string
// FileGroup is a set of groups a changed file belongs to. Use FileGroups.Add() instead of append()
// to ensure uniqueness and ordering
FileGroups []FileGroup
)
const (
FileGroupAutopilot FileGroup = "autopilot"
FileGroupChangelog FileGroup = "changelog"
FileGroupCommunity FileGroup = "community"
FileGroupDocs FileGroup = "docs"
FileGroupEnos FileGroup = "enos"
FileGroupEnterprise FileGroup = "enterprise"
FileGroupGoApp FileGroup = "app"
FileGroupGoToolchain FileGroup = "gotoolchain"
FileGroupPipeline FileGroup = "pipeline"
FileGroupProto FileGroup = "proto"
FileGroupTools FileGroup = "tools"
FileGroupWebUI FileGroup = "ui"
)
// Name is the file name of the changed file
func (f *File) Name() string {
if f == nil || f.File == nil {
return ""
}
return f.File.GetFilename()
}
// Add takes a variadic set of groups and adds them to the ordered set of groups
func (g FileGroups) Add(groups ...FileGroup) FileGroups {
for _, group := range groups {
idx, in := g.In(group)
if in {
continue
}
g = slices.Insert(g, idx, group)
}
return g
}
// In takes a group and determines the index and presence of the group in the group set
func (g FileGroups) In(group FileGroup) (int, bool) {
return slices.BinarySearch(g, group)
}
// String is a string representation of all groups a file is in
func (g FileGroups) String() string {
groups := []string{}
for _, g := range g {
groups = append(groups, string(g))
}
return strings.Join(groups, ", ")
}
// Names returns a list of file names
func (f Files) Names() []string {
if len(f) < 1 {
return nil
}
files := []string{}
for _, file := range f {
files = append(files, file.Name())
}
return files
}