|
| 1 | +package main |
| 2 | + |
| 3 | +// Copyright 2025 The Cockroach Authors. |
| 4 | +// |
| 5 | +// Use of this software is governed by the CockroachDB Software License |
| 6 | +// included in the /LICENSE file. |
| 7 | +// |
| 8 | +// testowner is a helper binary to get the owner of a given test. |
| 9 | +// Before invoking this binary, you should first generate Go code: `dev gen go` |
| 10 | +// This is because tests can be found in generated files that need to exist in |
| 11 | +// the workspace. Further, this should be run from the root of the workspace. |
| 12 | +// |
| 13 | +// The binary can be invoked in one of two ways: |
| 14 | +// * single test lookup: `testowner PKG TEST`. The owner(s) for the test will be |
| 15 | +// printed to stdout, separated by a space. |
| 16 | +// * multi-test lookup: `testowner` (with no arguments). In this case, stdin |
| 17 | +// will be a list of test lookups, one on each line, of the form `PKG TEST`. |
| 18 | +// the output will be one owner per line. |
| 19 | + |
| 20 | +import ( |
| 21 | + "bufio" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "os" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/cockroachdb/cockroach/pkg/internal/codeowners" |
| 28 | + "github.com/cockroachdb/cockroach/pkg/internal/team" |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + co, err := codeowners.DefaultLoadCodeOwners() |
| 33 | + if err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | + stdout := bufio.NewWriter(os.Stdout) |
| 37 | + stderr := bufio.NewWriter(os.Stderr) |
| 38 | + if len(os.Args) == 1 { |
| 39 | + stdin := bufio.NewScanner(os.Stdin) |
| 40 | + for stdin.Scan() { |
| 41 | + line := stdin.Text() |
| 42 | + pkg, test := parseQuery(line) |
| 43 | + teams, logs := co.GetTestOwner(pkg, test) |
| 44 | + writeOwners(stdout, teams) |
| 45 | + for _, log := range logs { |
| 46 | + fmt.Fprintln(stderr, log) |
| 47 | + } |
| 48 | + } |
| 49 | + if err := stdin.Err(); err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + } else if len(os.Args) == 2 { |
| 53 | + pkg, test := parseQuery(os.Args[1]) |
| 54 | + teams, logs := co.GetTestOwner(pkg, test) |
| 55 | + writeOwners(stdout, teams) |
| 56 | + for _, log := range logs { |
| 57 | + fmt.Fprintln(stderr, log) |
| 58 | + } |
| 59 | + } else { |
| 60 | + fmt.Fprintf(stderr, "usage: `testowner PKG.TEST` or `testowner` (with queries in stdin); got %d arguments, expected 1-2", len(os.Args)) |
| 61 | + } |
| 62 | + if err := stdout.Flush(); err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + if err := stderr.Flush(); err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func parseQuery(pkgAndTest string) (pkg string, test string) { |
| 71 | + const githubPrefix = "github.com/cockroachdb/cockroach/" |
| 72 | + var dotIdx int |
| 73 | + if strings.HasPrefix(pkgAndTest, githubPrefix) { |
| 74 | + dotIdx = strings.IndexByte(pkgAndTest[len(githubPrefix):], '.') |
| 75 | + if dotIdx <= 0 { |
| 76 | + panic(fmt.Sprintf("could not parse query (expected PKG.TEST): %s", pkgAndTest)) |
| 77 | + } |
| 78 | + dotIdx += len(githubPrefix) |
| 79 | + } else { |
| 80 | + dotIdx = strings.IndexByte(pkgAndTest, '.') |
| 81 | + if dotIdx <= 0 { |
| 82 | + panic(fmt.Sprintf("could not parse query (expected PKG.TEST): %s", pkgAndTest)) |
| 83 | + } |
| 84 | + } |
| 85 | + pkg, test = pkgAndTest[:dotIdx], pkgAndTest[dotIdx+1:] |
| 86 | + return |
| 87 | +} |
| 88 | + |
| 89 | +func writeOwners(w io.Writer, teams []team.Team) { |
| 90 | + var prev bool |
| 91 | + if len(teams) == 0 { |
| 92 | + panic("empty team slice") |
| 93 | + } |
| 94 | + for _, team := range teams { |
| 95 | + if prev { |
| 96 | + _, err := w.Write([]byte{' '}) |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + } |
| 101 | + fmt.Fprintf(w, "%s", team.TeamName) |
| 102 | + prev = true |
| 103 | + } |
| 104 | + _, err := w.Write([]byte{'\n'}) |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | +} |
0 commit comments