Skip to content

Commit 0309026

Browse files
committed
Finishes DX-933, lays groundwork for DX-870
1 parent 1c4b085 commit 0309026

File tree

2 files changed

+49
-34
lines changed

2 files changed

+49
-34
lines changed

tools/flakeguard/cmd/make_pr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ func makePR(cmd *cobra.Command, args []string) error {
6565
return fmt.Errorf("failed to checkout default branch %s: %w", defaultBranch, err)
6666
}
6767

68-
fmt.Print("Fetching latest changes from default branch, tap your yubikey if it's blinking...")
68+
fmt.Printf("Fetching latest changes from default branch '%s', tap your yubikey if it's blinking...", defaultBranch)
6969
err = repo.Fetch(&git.FetchOptions{})
7070
if err != nil && err != git.NoErrAlreadyUpToDate {
7171
return fmt.Errorf("failed to fetch latest: %w", err)
7272
}
7373
fmt.Println(" ✅")
7474

75-
fmt.Print("Pulling latest changes from default branch, tap your yubikey if it's blinking...")
75+
fmt.Printf("Pulling latest changes from default branch '%s', tap your yubikey if it's blinking...", defaultBranch)
7676
err = targetRepoWorktree.Pull(&git.PullOptions{})
7777
if err != nil && err != git.NoErrAlreadyUpToDate {
7878
return fmt.Errorf("failed to pull latest changes: %w", err)

tools/flakeguard/golang/golang.go

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,50 @@ func List() (*utils.CmdOutput, error) {
3939
return utils.ExecuteCmd("go", "list", "-json", "./...")
4040
}
4141

42-
// Packages parses the output of `go list -json ./...` and returns a slice of Package structs
42+
// Packages finds all packages in the repository
4343
func Packages(repoPath string) ([]Package, error) {
44-
cmd := exec.Command("go", "list", "-json", "./...")
45-
cmd.Dir = repoPath
46-
out, err := cmd.CombinedOutput()
47-
if err != nil {
48-
return nil, fmt.Errorf("error getting packages: %w\nOutput:\n%s", err, string(out))
49-
}
50-
5144
var packages []Package
52-
scanner := bufio.NewScanner(bytes.NewReader(out))
53-
var buffer bytes.Buffer
54-
55-
for scanner.Scan() {
56-
line := scanner.Text()
57-
line = strings.TrimSpace(line)
58-
buffer.WriteString(line)
59-
60-
if line == "}" {
61-
var pkg Package
62-
if err := json.Unmarshal(buffer.Bytes(), &pkg); err != nil {
63-
return nil, err
64-
}
65-
packages = append(packages, pkg)
66-
buffer.Reset()
45+
46+
// Find all go.mod files and run go list -json ./... in the directory of each go.mod file
47+
// This is necessary because go list -json ./... only returns packages that are associated with the current go.mod file
48+
err := filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
49+
if err != nil {
50+
return err
6751
}
68-
}
52+
if info.Name() == "go.mod" {
53+
cmd := exec.Command("go", "list", "-json", "./...")
54+
cmd.Dir = filepath.Dir(path)
55+
out, err := cmd.CombinedOutput()
56+
if err != nil {
57+
return fmt.Errorf("error getting packages: %w\nOutput:\n%s", err, string(out))
58+
}
59+
scanner := bufio.NewScanner(bytes.NewReader(out))
60+
var buffer bytes.Buffer
61+
62+
for scanner.Scan() {
63+
line := scanner.Text()
64+
line = strings.TrimSpace(line)
65+
buffer.WriteString(line)
66+
67+
if line == "}" {
68+
var pkg Package
69+
if err := json.Unmarshal(buffer.Bytes(), &pkg); err != nil {
70+
return err
71+
}
72+
packages = append(packages, pkg)
73+
buffer.Reset()
74+
}
75+
}
6976

70-
if err := scanner.Err(); err != nil {
71-
return nil, err
72-
}
77+
if err := scanner.Err(); err != nil {
78+
return err
79+
}
80+
return nil
81+
}
82+
return nil
83+
})
7384

74-
return packages, nil
85+
return packages, err
7586
}
7687

7788
func GetGoDepMap(packages []Package) DepMap {
@@ -236,7 +247,7 @@ type SkipTest struct {
236247
func SkipTests(repoPath string, testsToSkip []*SkipTest) error {
237248
packages, err := Packages(repoPath)
238249
if err != nil {
239-
return fmt.Errorf("error getting packages: %w", err)
250+
return err
240251
}
241252

242253
for _, testToSkip := range testsToSkip {
@@ -251,7 +262,7 @@ func SkipTests(repoPath string, testsToSkip []*SkipTest) error {
251262
}
252263
}
253264
if packageDir == "" {
254-
return fmt.Errorf("package %s not found", packageDir)
265+
return fmt.Errorf("directory for package '%s' not found", packageImportPath)
255266
}
256267

257268
log.Debug().
@@ -309,8 +320,12 @@ func SkipTests(repoPath string, testsToSkip []*SkipTest) error {
309320
},
310321
Args: []ast.Expr{
311322
&ast.BasicLit{
312-
Kind: token.STRING,
313-
Value: "\"skipped by flakeguard\"",
323+
Kind: token.STRING,
324+
Value: fmt.Sprintf(
325+
"Skipped by flakeguard: https://%s/issues/%s",
326+
os.Getenv("JIRA_DOMAIN"),
327+
testToSkip.JiraTicket,
328+
),
314329
},
315330
},
316331
},

0 commit comments

Comments
 (0)