11package cmd
22
33import (
4- "context"
54 "encoding/json"
65 "fmt"
76 "os"
87 "path/filepath"
9- "strings"
108 "time"
119
1210 "github.com/briandowns/spinner"
13- "github.com/google/go-github/v67/github"
1411 "github.com/rs/zerolog/log"
1512 "github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
1613 "github.com/spf13/cobra"
17- "golang.org/x/oauth2"
1814)
1915
20- const exampleGitHubToken = "EXAMPLE_GITHUB_TOKEN" //nolint:gosec
21-
2216var GenerateReportCmd = & cobra.Command {
2317 Use : "generate-report" ,
2418 Short : "Generate test reports from aggregated results that can be posted to GitHub" ,
@@ -30,23 +24,16 @@ var GenerateReportCmd = &cobra.Command{
3024 outputDir , _ := cmd .Flags ().GetString ("output-path" )
3125 maxPassRatio , _ := cmd .Flags ().GetFloat64 ("max-pass-ratio" )
3226 generatePRComment , _ := cmd .Flags ().GetBool ("generate-pr-comment" )
33- githubRepo , _ := cmd .Flags ().GetString ("github-repository " )
34- githubRunID , _ := cmd . Flags (). GetInt64 ( "github-run-id" )
35- artifactName , _ := cmd . Flags (). GetString ( "failed-tests-artifact-name" )
27+ failedLogsURL , _ := cmd .Flags ().GetString ("failed-logs-url " )
28+
29+ failedLogsArtifactName := "failed-test-results-with-logs.json"
3630
3731 initialDirSize , err := getDirSize (outputDir )
3832 if err != nil {
3933 log .Error ().Err (err ).Str ("path" , outputDir ).Msg ("Error getting initial directory size" )
4034 // intentionally don't exit here, as we can still proceed with the generation
4135 }
4236
43- // Get the GitHub token from environment variable
44- githubToken := os .Getenv ("GITHUB_TOKEN" )
45- if githubToken == "" {
46- log .Error ().Msg ("GITHUB_TOKEN environment variable is not set" )
47- os .Exit (ErrorExitCode )
48- }
49-
5037 // Load the aggregated report
5138 s := spinner .New (spinner .CharSets [11 ], 100 * time .Millisecond )
5239 s .Suffix = " Loading aggregated test report..."
@@ -72,19 +59,6 @@ var GenerateReportCmd = &cobra.Command{
7259 fmt .Println ()
7360 log .Info ().Msg ("Successfully loaded aggregated test report" )
7461
75- // Check if there are failed tests
76- hasFailedTests := aggregatedReport .SummaryData .FailedRuns > 0
77-
78- var artifactLink string
79- if hasFailedTests && githubRepo != "" && githubRunID != 0 && artifactName != "" {
80- // Fetch artifact link from GitHub API
81- artifactLink , err = fetchArtifactLinkWithRetry (githubToken , githubRepo , githubRunID , artifactName , 5 , 5 * time .Second )
82- if err != nil {
83- log .Error ().Err (err ).Msg ("Error fetching artifact link" )
84- os .Exit (ErrorExitCode )
85- }
86- }
87-
8862 // Create output directory if it doesn't exist
8963 if err := fs .MkdirAll (outputDir , 0755 ); err != nil {
9064 log .Error ().Err (err ).Msg ("Error creating output directory" )
@@ -96,7 +70,7 @@ var GenerateReportCmd = &cobra.Command{
9670 s .Suffix = " Generating GitHub summary markdown..."
9771 s .Start ()
9872
99- err = generateGitHubSummaryMarkdown (aggregatedReport , filepath .Join (outputDir , "all-test" ), artifactLink , artifactName )
73+ err = generateGitHubSummaryMarkdown (aggregatedReport , filepath .Join (outputDir , "all-test" ), failedLogsURL , failedLogsArtifactName )
10074 if err != nil {
10175 s .Stop ()
10276 fmt .Println ()
@@ -147,8 +121,8 @@ var GenerateReportCmd = &cobra.Command{
147121 currentCommitSHA ,
148122 repoURL ,
149123 actionRunID ,
150- artifactName ,
151- artifactLink ,
124+ failedLogsArtifactName ,
125+ failedLogsURL ,
152126 maxPassRatio ,
153127 )
154128 if err != nil {
@@ -192,82 +166,6 @@ func init() {
192166 }
193167}
194168
195- func fetchArtifactLink (githubToken , githubRepo string , githubRunID int64 , artifactName string ) (string , error ) {
196- if githubToken == exampleGitHubToken {
197- return "https://example-artifact-link.com" , nil
198- }
199- ctx := context .Background ()
200- ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : githubToken })
201- tc := oauth2 .NewClient (ctx , ts )
202- client := github .NewClient (tc )
203-
204- // Split owner/repo
205- repoParts := strings .SplitN (githubRepo , "/" , 2 )
206- if len (repoParts ) != 2 {
207- return "" , fmt .Errorf ("invalid format for --github-repository, expected owner/repo" )
208- }
209- owner , repo := repoParts [0 ], repoParts [1 ]
210-
211- opts := & github.ListOptions {PerPage : 100 } // The max GitHub allows is 100 per page
212- var allArtifacts []* github.Artifact
213-
214- // Paginate through all artifacts
215- for {
216- artifacts , resp , err := client .Actions .ListWorkflowRunArtifacts (ctx , owner , repo , githubRunID , opts )
217- if err != nil {
218- return "" , fmt .Errorf ("error listing artifacts: %w" , err )
219- }
220-
221- allArtifacts = append (allArtifacts , artifacts .Artifacts ... )
222-
223- if resp .NextPage == 0 {
224- // No more pages
225- break
226- }
227- // Move to the next page
228- opts .Page = resp .NextPage
229- }
230-
231- // Find the artifact
232- for _ , artifact := range allArtifacts {
233- if artifact .GetName () == artifactName {
234- artifactID := artifact .GetID ()
235- artifactURL := fmt .Sprintf ("https://github.com/%s/%s/actions/runs/%d/artifacts/%d" ,
236- owner , repo , githubRunID , artifactID )
237- return artifactURL , nil
238- }
239- }
240-
241- return "" , fmt .Errorf ("artifact '%s' not found in the workflow run" , artifactName )
242- }
243-
244- func fetchArtifactLinkWithRetry (
245- githubToken , githubRepo string ,
246- githubRunID int64 , artifactName string ,
247- maxRetries int , delay time.Duration ,
248- ) (string , error ) {
249- var lastErr error
250- for attempt := 1 ; attempt <= maxRetries ; attempt ++ {
251- link , err := fetchArtifactLink (githubToken , githubRepo , githubRunID , artifactName )
252- if err == nil {
253- // Found the artifact link successfully
254- return link , nil
255- }
256-
257- // If this was our last attempt, return the error
258- lastErr = err
259- if attempt == maxRetries {
260- break
261- }
262-
263- // Otherwise wait and retry
264- log .Printf ("[Attempt %d/%d] Artifact not yet available. Retrying in %s..." , attempt , maxRetries , delay )
265- time .Sleep (delay )
266- }
267-
268- return "" , fmt .Errorf ("failed to fetch artifact link after %d retries: %w" , maxRetries , lastErr )
269- }
270-
271169func generateGitHubSummaryMarkdown (report * reports.TestReport , outputPath , artifactLink , artifactName string ) error {
272170 fs := reports.OSFileSystem {}
273171 mdFileName := outputPath + "-summary.md"
0 commit comments