diff --git a/.github/labeler-test-only.yml b/.github/labeler-test-only.yml new file mode 100644 index 00000000000..432c4da8943 --- /dev/null +++ b/.github/labeler-test-only.yml @@ -0,0 +1,21 @@ +# Configuration for the test-only-fix step of the "PR Labeler" workflow +# (.github/workflows/labeler.yml). Same format as .github/labeler.yml. It is a +# separate file because this step runs with sync-labels, so the label is +# removed again once a later push adds files outside this list. +# +# any-glob-to-all-files needs a single glob that matches every changed file, +# so all test-only paths are alternatives of one brace expansion. +# +# The bundle/, internal/ and libs/ entries are packages the CLI binary does not +# import. TestLabelerTestOnlyDirsNotInBinary in internal/build checks this. + +# The list-files API returns at most 3000 files, sorted by path. acceptance/ +# sorts before bundle/, cmd/ and libs/, so a truncated list could hide CLI +# changes. Larger PRs skip this step and keep their current labels. +# https://docs.github.com/en/rest/pulls/pulls#list-pull-requests-files +max-files-changed: 2999 + +test-only-fix: + - changed-files: + - any-glob-to-all-files: + - "{**/*_test.go,**/testdata/**,acceptance/**,integration/**,bundle/tests/**,bundle/internal/bundletest/**,internal/mocks/**,internal/testarchive/**,internal/testcli/**,internal/testutil/**,libs/appproxy/testutil/**,libs/cmdio/cmdiotest/**,libs/dyn/dynassert/**,libs/jsonschema/test_types/**,libs/testdiff/**,libs/testfile/**,libs/testproxy/**,libs/testserver/**,python/databricks_tests/**,python/codegen/codegen_tests/**}" diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index cba79c172bd..356e9d9dcb7 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -1,7 +1,7 @@ name: PR Labeler # Applies labels to PRs based on the files they touch, using the glob->label -# mapping in .github/labeler.yml. +# mappings in .github/labeler.yml and .github/labeler-test-only.yml. # # WARNING: # This workflow uses pull_request_target so it can label PRs from forks (the @@ -32,3 +32,11 @@ jobs: repo-token: ${{ secrets.GITHUB_TOKEN }} # Only add labels; never remove labels that were applied manually or by other automation. sync-labels: false + + - uses: actions/labeler@bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13 # v7.0.0 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + configuration-path: .github/labeler-test-only.yml + # test-only-fix claims the PR changes no CLI code, so this step owns it: a push that + # changes any file outside the config removes it, even when it was added by hand. + sync-labels: true diff --git a/Taskfile.yml b/Taskfile.yml index 37b0608050f..d47a72d61ca 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -447,6 +447,7 @@ tasks: # internal/build reads these at test time. - NOTICE - .codegen/_openapi_sha + - .github/labeler-test-only.yml # bundle/internal/schema TestRequiredAnnotationsForNewFields reads these. - bundle/internal/schema/annotations*.yml generates: diff --git a/internal/build/labeler_test.go b/internal/build/labeler_test.go new file mode 100644 index 00000000000..44ef2c98d60 --- /dev/null +++ b/internal/build/labeler_test.go @@ -0,0 +1,67 @@ +package build + +import ( + "os" + "os/exec" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.yaml.in/yaml/v3" +) + +// TestLabelerTestOnlyDirsNotInBinary fails when the CLI binary imports a package +// under a directory that .github/labeler-test-only.yml treats as test-only, since +// a PR changing that directory would then be labeled test-only-fix. +func TestLabelerTestOnlyDirsNotInBinary(t *testing.T) { + raw, err := os.ReadFile("../../.github/labeler-test-only.yml") + require.NoError(t, err) + + var config struct { + TestOnlyFix []struct { + ChangedFiles []struct { + AnyGlobToAllFiles []string `yaml:"any-glob-to-all-files"` + } `yaml:"changed-files"` + } `yaml:"test-only-fix"` + } + require.NoError(t, yaml.Unmarshal(raw, &config)) + glob := config.TestOnlyFix[0].ChangedFiles[0].AnyGlobToAllFiles[0] + + // Directory entries look like "libs/testserver/**". Entries such as + // "**/*_test.go" match by file name and are skipped. + var dirs []string + for alt := range strings.SplitSeq(strings.Trim(glob, "{}"), ",") { + dir, ok := strings.CutSuffix(alt, "/**") + if ok && !strings.Contains(dir, "*") { + dirs = append(dirs, dir) + } + } + require.NotEmpty(t, dirs) + + // Release targets from .goreleaser.yaml; some files build only on some of them. + for _, target := range []string{"linux/amd64", "linux/arm64", "darwin/amd64", "darwin/arm64", "windows/amd64", "windows/arm64"} { + t.Run(target, func(t *testing.T) { + t.Parallel() + goos, goarch, _ := strings.Cut(target, "/") + // go test does not see the sources go list reads, so a cached local pass can be + // stale. CI reruns it because a fresh checkout changes the config file's mtime. + cmd := exec.CommandContext(t.Context(), "go", "list", "-deps", "-f", "{{.ImportPath}}", ".") + cmd.Dir = "../.." + cmd.Env = append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch, "CGO_ENABLED=0") + out, err := cmd.Output() + require.NoError(t, err) + + for pkg := range strings.FieldsSeq(string(out)) { + rel, ok := strings.CutPrefix(pkg, "github.com/databricks/cli/") + if !ok { + continue + } + for _, dir := range dirs { + assert.False(t, rel == dir || strings.HasPrefix(rel, dir+"/"), + "CLI binary imports %s, but .github/labeler-test-only.yml lists %s/** as test-only", pkg, dir) + } + } + }) + } +}