@@ -19,14 +19,13 @@ import (
1919 "fmt"
2020 "io"
2121 "net/http"
22- "regexp "
22+ "os "
2323 "time"
2424
2525 "github.com/google/go-github/v58/github"
2626 "github.com/mitchellh/go-testing-interface"
27- "github.com/terraform-google-modules/terraform-example-foundation/test/integration/testutils"
2827
29- localutil "github.com/terraform-google-modules/terraform-example-foundation/helpers/foundation-deployer/utils"
28+ "github.com/terraform-google-modules/terraform-example-foundation/helpers/foundation-deployer/utils"
3029
3130 "golang.org/x/oauth2"
3231)
@@ -46,20 +45,6 @@ const (
4645 StatusActionRequired = "action_required"
4746)
4847
49- var (
50- retryRegexp = map [* regexp.Regexp ]string {}
51- )
52-
53- func init () {
54- for e , m := range testutils .RetryableTransientErrors {
55- r , err := regexp .Compile (fmt .Sprintf ("(?s)%s" , e )) //(?s) enables dot (.) to match newline.
56- if err != nil {
57- panic (fmt .Sprintf ("failed to compile regex %s: %s" , e , err .Error ()))
58- }
59- retryRegexp [r ] = m
60- }
61- }
62-
6348type GH struct {
6449 TriggerNewBuild func (t testing.TB , ctx context.Context , owner , repo , token string , runID int64 ) (int64 , string , string , error )
6550 sleepTime time.Duration
@@ -72,6 +57,7 @@ func NewGH() GH {
7257 sleepTime : 20 ,
7358 }
7459}
60+
7561// triggerNewBuild triggers a new action execution
7662func triggerNewBuild (t testing.TB , ctx context.Context , owner , repo , token string , runID int64 ) (int64 , string , string , error ) {
7763 ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : token })
@@ -80,14 +66,14 @@ func triggerNewBuild(t testing.TB, ctx context.Context, owner, repo, token strin
8066
8167 resp , err := client .Actions .RerunWorkflowByID (ctx , owner , repo , runID )
8268 if err != nil {
83- return 0 , "" , "" , fmt .Errorf ("Error re-running workflow: %v" , err )
69+ return 0 , "" , "" , fmt .Errorf ("error re-running workflow: %v" , err )
8470 }
8571 if resp .StatusCode != http .StatusCreated {
8672 bodyBytes , err := io .ReadAll (resp .Body )
8773 if err != nil {
88- return 0 , "" , "" , fmt .Errorf ("Error re-running workflow status: %d body parsing error: %v" , resp .StatusCode , err )
74+ return 0 , "" , "" , fmt .Errorf ("error re-running workflow status: %d body parsing error: %v" , resp .StatusCode , err )
8975 }
90- return 0 , "" , "" , fmt .Errorf ("Error re-running workflow status: %d body: %s" , resp .StatusCode , string (bodyBytes ))
76+ return 0 , "" , "" , fmt .Errorf ("error re-running workflow status: %d body: %s" , resp .StatusCode , string (bodyBytes ))
9177 }
9278 opts := & github.ListWorkflowRunsOptions {
9379
@@ -103,11 +89,11 @@ func triggerNewBuild(t testing.TB, ctx context.Context, owner, repo, token strin
10389 runs , _ , err := client .Actions .ListRepositoryWorkflowRuns (ctx , owner , repo , opts )
10490
10591 if err != nil {
106- return 0 , "" , "" , fmt .Errorf ("Error listing workflow runs: %v" , err )
92+ return 0 , "" , "" , fmt .Errorf ("error listing workflow runs: %v" , err )
10793 }
10894
10995 if len (runs .WorkflowRuns ) == 0 {
110- return 0 , "" , "" , fmt .Errorf ("No workflow runs found for repo %s/%s " , owner , repo )
96+ return 0 , "" , "" , fmt .Errorf ("no workflow runs found for repo %s/%s" , owner , repo )
11197 }
11298
11399 var newRunID int64
@@ -143,7 +129,7 @@ func (g GH) GetLastActionState(t testing.TB, ctx context.Context, owner, repo, t
143129 runs , _ , err := client .Actions .ListRepositoryWorkflowRuns (ctx , owner , repo , opts )
144130
145131 if err != nil {
146- return 0 , "" , "" , fmt .Errorf ("Error listing workflow runs: %v" , err )
132+ return 0 , "" , "" , fmt .Errorf ("error listing workflow runs: %v" , err )
147133 }
148134
149135 if len (runs .WorkflowRuns ) == 0 {
@@ -175,7 +161,7 @@ func (g GH) GetActionState(t testing.TB, ctx context.Context, owner, repo, token
175161
176162 run , _ , err := client .Actions .GetWorkflowRunByID (ctx , owner , repo , runID )
177163 if err != nil {
178- return "" , "" , fmt .Errorf ("Error getting workflow run: %v" , err )
164+ return "" , "" , fmt .Errorf ("error getting workflow run: %v" , err )
179165 }
180166
181167 var status string
@@ -201,7 +187,7 @@ func (g GH) GetBuildLogs(t testing.TB, ctx context.Context, owner, repo, token s
201187 }
202188 jobs , _ , err := client .Actions .ListWorkflowJobs (ctx , owner , repo , runID , listJobsOpts )
203189 if err != nil {
204- return "" , fmt .Errorf ("Error listing jobs for run %d: %v" , runID , err )
190+ return "" , fmt .Errorf ("error listing jobs for run %d: %v" , runID , err )
205191 }
206192
207193 for _ , job := range jobs .Jobs {
@@ -214,25 +200,31 @@ func (g GH) GetBuildLogs(t testing.TB, ctx context.Context, owner, repo, token s
214200
215201 logURL , resp , err := client .Actions .GetWorkflowJobLogs (ctx , owner , repo , jobID , 3 )
216202 if err != nil {
217- return "" , fmt .Errorf (" ERROR : Could not get log URL for job %d: %v\n " , jobID , err )
203+ return "" , fmt .Errorf ("error : Could not get log URL for job %d: %v" , jobID , err )
218204 }
219205 if resp .StatusCode != http .StatusFound {
220- return "" , fmt .Errorf (" ERROR : Expected a 302 redirect for job logs, but got %s\n " , resp .Status )
206+ return "" , fmt .Errorf ("error : Expected a 302 redirect for job logs, but got %s" , resp .Status )
221207 }
222208
223209 logContentResp , err := http .Get (logURL .String ())
224210 if err != nil {
225- return "" , fmt .Errorf (" ERROR : Could not download logs from %s: %v\n " , logURL , err )
211+ return "" , fmt .Errorf ("error : Could not download logs from %s: %v" , logURL , err )
226212 }
227- defer logContentResp .Body .Close ()
213+
214+ defer func () {
215+ err := logContentResp .Body .Close ()
216+ if err != nil {
217+ fmt .Fprintf (os .Stderr , "error closing execution log file: %s" , err )
218+ }
219+ }()
228220
229221 if logContentResp .StatusCode != http .StatusOK {
230- return "" , fmt .Errorf (" ERROR : Expected status 200 OK from log URL, but got %s\n " , logContentResp .Status )
222+ return "" , fmt .Errorf ("error : Expected status 200 OK from log URL, but got %s" , logContentResp .Status )
231223 }
232224
233225 bodyBytes , err := io .ReadAll (logContentResp .Body )
234226 if err != nil {
235- return "" , fmt .Errorf ("Error reading response body: %v" , err )
227+ return "" , fmt .Errorf ("error reading response body: %v" , err )
236228 }
237229 return string (bodyBytes ), nil
238230 }
@@ -283,7 +275,7 @@ func (g GH) WaitBuildSuccess(t testing.TB, owner, repo, token string, failureMsg
283275 }
284276 for i := 0 ; i < maxErrorRetries ; i ++ {
285277 if status != statusCompleted {
286- status , conclusion , err = g .GetFinalActionState (t , ctx , owner , repo , token , runID , maxBuildRetry )
278+ _ , conclusion , err = g .GetFinalActionState (t , ctx , owner , repo , token , runID , maxBuildRetry )
287279 if err != nil {
288280 return err
289281 }
@@ -294,7 +286,7 @@ func (g GH) WaitBuildSuccess(t testing.TB, owner, repo, token string, failureMsg
294286 if err != nil {
295287 return err
296288 }
297- if localutil .IsRetryableError (t , logs ) {
289+ if utils .IsRetryableError (t , logs ) {
298290 return fmt .Errorf ("%s\n See:\n https://github.com/%s/%s/actions/runs/%d\n for details" , failureMsg , owner , repo , runID )
299291 }
300292 fmt .Println ("build failed with retryable error. a new build will be triggered." )
0 commit comments