Skip to content

Commit 13716d2

Browse files
committed
code review fixes
1 parent 0aa2d2d commit 13716d2

File tree

9 files changed

+98
-96
lines changed

9 files changed

+98
-96
lines changed

0-bootstrap/scripts/choose_build_type.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,7 @@ TARGET_BUILD="$1"
2323
build_types=("cb" "github" "gitlab" "jenkins" "terraform_cloud")
2424

2525
# Validate the build_type input
26-
valid_build_type=false
27-
for valid_type in "${build_types[@]}"; do
28-
if [ "$TARGET_BUILD" = "$valid_type" ]; then
29-
valid_build_type=true
30-
break
31-
fi
32-
done
33-
34-
if [ "$valid_build_type" = false ]; then
26+
if [[ ! " ${build_types[*]} " =~ " ${TARGET_BUILD} " ]]; then
3527
echo "Error: Invalid build type '$TARGET_BUILD'. Must be one of: ${build_types[*]}"
3628
exit 1
3729
fi

helpers/foundation-deployer/github/github.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
6348
type 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
7662
func 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\nSee:\nhttps://github.com/%s/%s/actions/runs/%d\nfor details", failureMsg, owner, repo, runID)
299291
}
300292
fmt.Println("build failed with retryable error. a new build will be triggered.")

helpers/foundation-deployer/gitlab/gitlab.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020
"io"
2121
"net/http"
22-
"regexp"
2322
"time"
2423

2524
"github.com/mitchellh/go-testing-interface"
@@ -43,10 +42,6 @@ const (
4342
StatusCanceling = "canceling"
4443
)
4544

46-
var (
47-
retryRegexp = map[*regexp.Regexp]string{}
48-
)
49-
5045
type GL struct {
5146
TriggerNewBuild func(t testing.TB, ctx context.Context, owner, project, token string, jobID int) (int, error)
5247
sleepTime time.Duration
@@ -65,12 +60,12 @@ func triggerNewBuild(t testing.TB, ctx context.Context, owner, project, token st
6560

6661
git, err := gitlab.NewClient(token)
6762
if err != nil {
68-
return 0, fmt.Errorf("Failed to create client: %v", err)
63+
return 0, fmt.Errorf("failed to create client: %v", err)
6964
}
7065

7166
job, resp, err := git.Jobs.RetryJob(fmt.Sprintf("%s/%s", owner, project), jobID, gitlab.WithContext(ctx))
7267
if err != nil {
73-
return 0, fmt.Errorf("Error retrying job: %v", err)
68+
return 0, fmt.Errorf("error retrying job: %v", err)
7469
}
7570
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode <= http.StatusNetworkAuthenticationRequired {
7671
bodyBytes, err := io.ReadAll(resp.Body)
@@ -86,7 +81,7 @@ func triggerNewBuild(t testing.TB, ctx context.Context, owner, project, token st
8681
func (g GL) GetLastJobStatus(t testing.TB, ctx context.Context, owner, project, token string) (string, int, error) {
8782
git, err := gitlab.NewClient(token)
8883
if err != nil {
89-
return "", 0, fmt.Errorf("Failed to create client: %v", err)
84+
return "", 0, fmt.Errorf("failed to create client: %v", err)
9085
}
9186

9287
includeRetried := true
@@ -102,11 +97,11 @@ func (g GL) GetLastJobStatus(t testing.TB, ctx context.Context, owner, project,
10297

10398
jobs, _, err := git.Jobs.ListProjectJobs(fmt.Sprintf("%s/%s", owner, project), opts, gitlab.WithContext(ctx))
10499
if err != nil {
105-
return "", 0, fmt.Errorf("Error listing project jobs: %v", err)
100+
return "", 0, fmt.Errorf("error listing project jobs: %v", err)
106101
}
107102

108103
if len(jobs) == 0 {
109-
return "", 0, fmt.Errorf("No jobs found for project: %s/%s", owner, project)
104+
return "", 0, fmt.Errorf("no jobs found for project: %s/%s", owner, project)
110105
}
111106

112107
return jobs[0].Status, jobs[0].ID, nil
@@ -116,21 +111,21 @@ func (g GL) GetLastJobStatus(t testing.TB, ctx context.Context, owner, project,
116111
func (g GL) GetJobLogs(t testing.TB, ctx context.Context, owner, project, token string, jobID int) (string, error) {
117112
git, err := gitlab.NewClient(token)
118113
if err != nil {
119-
return "", fmt.Errorf("Failed to create client: %v", err)
114+
return "", fmt.Errorf("failed to create client: %v", err)
120115
}
121116

122117
reader, resp, err := git.Jobs.GetTraceFile(fmt.Sprintf("%s/%s", owner, project), jobID)
123118
if err != nil {
124-
return "", fmt.Errorf("Error getting job trace file: %v", err)
119+
return "", fmt.Errorf("error getting job trace file: %v", err)
125120
}
126121

127122
if resp.StatusCode != http.StatusOK {
128-
return "", fmt.Errorf("Expected status 200 OK, but got %s for job %d trace", resp.Status, jobID)
123+
return "", fmt.Errorf("expected status 200 OK, but got %s for job %d trace", resp.Status, jobID)
129124
}
130125

131126
logBytes, err := io.ReadAll(reader)
132127
if err != nil {
133-
return "", fmt.Errorf("Failed to read job %d trace from bytes.Reader: %v", jobID, err)
128+
return "", fmt.Errorf("failed to read job %d trace from bytes.Reader: %v", jobID, err)
134129
}
135130

136131
return string(logBytes), nil
@@ -140,12 +135,12 @@ func (g GL) GetJobLogs(t testing.TB, ctx context.Context, owner, project, token
140135
func (g GL) GetJobStatus(t testing.TB, ctx context.Context, owner, project, token string, jobID int) (string, error) {
141136
git, err := gitlab.NewClient(token)
142137
if err != nil {
143-
return "", fmt.Errorf("Failed to create client: %v", err)
138+
return "", fmt.Errorf("failed to create client: %v", err)
144139
}
145140

146141
job, _, err := git.Jobs.GetJob(fmt.Sprintf("%s/%s", owner, project), jobID, gitlab.WithContext(ctx))
147142
if err != nil {
148-
return "", fmt.Errorf("Error retrying job: %v", err)
143+
return "", fmt.Errorf("error retrying job: %v", err)
149144
}
150145

151146
return job.Status, nil
@@ -232,7 +227,7 @@ func (g GL) AddProjectsToJobTokenScope(t testing.TB, owner, cicdProject, token s
232227
ctx := context.Background()
233228
git, err := gitlab.NewClient(token)
234229
if err != nil {
235-
return fmt.Errorf("Failed to create client: %v", err)
230+
return fmt.Errorf("failed to create client: %v", err)
236231
}
237232
runnerProjectPath := fmt.Sprintf("%s/%s", owner, cicdProject)
238233

@@ -243,13 +238,13 @@ func (g GL) AddProjectsToJobTokenScope(t testing.TB, owner, cicdProject, token s
243238

244239
runnerProjectID, err := getProjectIDByPath(git, ctx, runnerProjectPath)
245240
if err != nil {
246-
return fmt.Errorf("Could not get the project ID for the runner repository. Aborting. Error: %v", err)
241+
return fmt.Errorf("could not get the project ID for the runner repository. Aborting. Error: %v", err)
247242
}
248243
for _, targetProjectPath := range projectsToAdd {
249244

250245
targetProjectID, err := getProjectIDByPath(git, ctx, targetProjectPath)
251246
if err != nil {
252-
return fmt.Errorf("Could not find project ID. Error: %v", err)
247+
return fmt.Errorf("could not find project ID. Error: %v", err)
253248
}
254249

255250
opts := &gitlab.JobTokenInboundAllowOptions{
@@ -266,7 +261,7 @@ func (g GL) AddProjectsToJobTokenScope(t testing.TB, owner, cicdProject, token s
266261
}
267262

268263
if resp.StatusCode != http.StatusCreated {
269-
return fmt.Errorf("DONE (Status: %s)\n", resp.Status)
264+
return fmt.Errorf("done (Status: %s)", resp.Status)
270265
}
271266
}
272267

helpers/foundation-deployer/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func main() {
188188
"TF_VAR_gh_token": conf.GitToken,
189189
}
190190
}
191-
if conf.BuildType != stages.BuildTypeGitLab {
191+
if conf.BuildType == stages.BuildTypeGitLab {
192192
envVars = map[string]string{
193193
"TF_VAR_gitlab_token": conf.GitToken,
194194
}

0 commit comments

Comments
 (0)