Skip to content

Commit

Permalink
Add run attempt to run URL
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryNguyen5 committed May 2, 2024
1 parent 9ca4f04 commit 3417c35
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 6 additions & 1 deletion tools/flakeytests/cmd/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func main() {
ghRunID := flag.String("gh_run_id", "", "run id of the gh workflow")
flag.Parse()

runAttempt := os.Getenv("GITHUB_RUN_ATTEMPT")
if runAttempt == "" {
log.Fatalf("GITHUB_RUN_ATTEMPT is required")
}

if *grafanaHost == "" {
log.Fatal("Error re-running flakey tests: `grafana_host` is required")
}
Expand Down Expand Up @@ -62,7 +67,7 @@ func main() {
readers = append(readers, r)
}

meta := flakeytests.GetGithubMetadata(*ghRepo, *ghEventName, *ghSHA, *ghEventPath, *ghRunID)
meta := flakeytests.GetGithubMetadata(*ghRepo, *ghEventName, *ghSHA, *ghEventPath, *ghRunID, runAttempt)
rep := flakeytests.NewLokiReporter(*grafanaHost, *grafanaAuth, *grafanaOrgID, *command, meta)
r := flakeytests.NewRunner(readers, rep, numReruns)
err := r.Run(ctx)
Expand Down
8 changes: 4 additions & 4 deletions tools/flakeytests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func DigString(mp map[string]interface{}, path []string) (string, error) {
return vs, nil
}

func getGithubMetadata(repo string, eventName string, sha string, e io.Reader, runID string) Context {
func getGithubMetadata(repo string, eventName string, sha string, e io.Reader, runID string, runAttempt string) Context {
d, err := io.ReadAll(e)
if err != nil {
log.Fatal("Error reading gh event into string")
Expand All @@ -42,7 +42,7 @@ func getGithubMetadata(repo string, eventName string, sha string, e io.Reader, r
log.Fatalf("Error unmarshaling gh event at path")
}

runURL := fmt.Sprintf("github.com/%s/actions/runs/%s", repo, runID)
runURL := fmt.Sprintf("github.com/%s/actions/runs/%s/attempts/%s", repo, runID, runAttempt)
basicCtx := &Context{Repository: repo, CommitSHA: sha, Type: eventName, RunURL: runURL}
switch eventName {
case "pull_request":
Expand Down Expand Up @@ -70,10 +70,10 @@ func getGithubMetadata(repo string, eventName string, sha string, e io.Reader, r
}
}

func GetGithubMetadata(repo string, eventName string, sha string, path string, runID string) Context {
func GetGithubMetadata(repo string, eventName string, sha string, path string, runID string, runAttempt string) Context {
event, err := os.Open(path)
if err != nil {
log.Fatalf("Error reading gh event at path: %s", path)
}
return getGithubMetadata(repo, eventName, sha, event, runID)
return getGithubMetadata(repo, eventName, sha, event, runID, runAttempt)
}
8 changes: 4 additions & 4 deletions tools/flakeytests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ var prEventTemplate = `
`

func TestGetGithubMetadata(t *testing.T) {
repo, eventName, sha, event, runID := "chainlink", "merge_group", "a-sha", `{}`, "1234"
expectedRunURL := fmt.Sprintf("github.com/%s/actions/runs/%s", repo, runID)
ctx := getGithubMetadata(repo, eventName, sha, strings.NewReader(event), runID)
repo, eventName, sha, event, runID, runAttempt := "chainlink", "merge_group", "a-sha", `{}`, "1234", "1"
expectedRunURL := fmt.Sprintf("github.com/%s/actions/runs/%s/attempts/%s", repo, runID, runAttempt)
ctx := getGithubMetadata(repo, eventName, sha, strings.NewReader(event), runID, runAttempt)
assert.Equal(t, Context{Repository: repo, CommitSHA: sha, Type: eventName, RunURL: expectedRunURL}, ctx)

anotherSha, eventName, url := "another-sha", "pull_request", "a-url"
event = fmt.Sprintf(prEventTemplate, anotherSha, url)
sha = "302eb05d592132309b264e316f443f1ceb81b6c3"
ctx = getGithubMetadata(repo, eventName, sha, strings.NewReader(event), runID)
ctx = getGithubMetadata(repo, eventName, sha, strings.NewReader(event), runID, runAttempt)
assert.Equal(t, Context{Repository: repo, CommitSHA: anotherSha, Type: eventName, PullRequestURL: url, RunURL: expectedRunURL}, ctx)
}

0 comments on commit 3417c35

Please sign in to comment.