@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "log/slog"
23
24
"strconv"
24
25
"strings"
25
26
"sync"
@@ -32,13 +33,9 @@ import (
32
33
)
33
34
34
35
var (
35
- errBranchNotFound = errors .New ("branch not found" )
36
- errNoJobForBranch = errors .New ("current branch has no jobs" )
37
- amplifyJobCompletedStatuses = map [types.JobStatus ]struct {}{
38
- types .JobStatusFailed : {},
39
- types .JobStatusCancelled : {},
40
- types .JobStatusSucceed : {},
41
- }
36
+ errBranchNotFound = errors .New ("branch not found" )
37
+ errNoJobForBranch = errors .New ("current branch has no jobs" )
38
+ errNilBranch = errors .New ("branch is nil" )
42
39
)
43
40
44
41
const (
@@ -50,6 +47,10 @@ const (
50
47
amplifyDefaultDomain = "amplifyapp.com"
51
48
)
52
49
50
+ // AmplifyPreview is used to get/create amplify preview
51
+ // deployments on AWS Amplify across multiple apps
52
+ // to work around hard limit 50 branches per app
53
+ // https://docs.aws.amazon.com/amplify/latest/userguide/quotas-chapter.html
53
54
type AmplifyPreview struct {
54
55
appIDs []string
55
56
branchName string
@@ -97,7 +98,7 @@ func (amp *AmplifyPreview) FindExistingBranch(ctx context.Context) (*types.Branc
97
98
for resp := range resultCh {
98
99
var errNotFound * types.NotFoundException
99
100
if errors .As (resp .err , & errNotFound ) {
100
- logger .Debug ("Branch not found" , logKeyAppID , resp .appID , logKeyBranchName , amp .branchName )
101
+ slog .Debug ("Branch not found" , logKeyAppID , resp .appID , logKeyBranchName , amp .branchName )
101
102
continue
102
103
} else if resp .err != nil {
103
104
failedResp .perAppErr [resp .appID ] = resp .err
@@ -126,20 +127,20 @@ func (amp *AmplifyPreview) CreateBranch(ctx context.Context) (*types.Branch, err
126
127
resp , err := amp .client .CreateBranch (ctx , & amplify.CreateBranchInput {
127
128
AppId : aws .String (appID ),
128
129
BranchName : aws .String (amp .branchName ),
129
- Description : aws .String ("Branch generated for PR TODO " ),
130
+ Description : aws .String ("Branch created from 'amplify-preview' GHA action " ),
130
131
Stage : types .StagePullRequest ,
131
132
EnableAutoBuild : aws .Bool (true ),
132
133
})
133
134
134
135
var errLimitExceeded * types.LimitExceededException
135
136
if errors .As (err , & errLimitExceeded ) {
136
- logger .Debug ("Reached branches limit" , logKeyAppID , appID )
137
+ slog .Debug ("Reached branches limit" , logKeyAppID , appID )
137
138
} else if err != nil {
138
139
failedResp .perAppErr [appID ] = err
139
140
}
140
141
141
142
if resp != nil {
142
- logger .Info ("Successfully created branch" , logKeyAppID , appID , logKeyBranchName , * resp .Branch .BranchName , logKeyJobID , resp .Branch .ActiveJobId )
143
+ slog .Info ("Successfully created branch" , logKeyAppID , appID , logKeyBranchName , * resp .Branch .BranchName , logKeyJobID , resp .Branch .ActiveJobId )
143
144
return resp .Branch , nil
144
145
}
145
146
}
@@ -148,6 +149,9 @@ func (amp *AmplifyPreview) CreateBranch(ctx context.Context) (*types.Branch, err
148
149
}
149
150
150
151
func (amp * AmplifyPreview ) StartJob (ctx context.Context , branch * types.Branch ) (* types.JobSummary , error ) {
152
+ if branch == nil {
153
+ return nil , errNilBranch
154
+ }
151
155
appID , err := appIDFromBranchARN (* branch .BranchArn )
152
156
if err != nil {
153
157
return nil , err
@@ -164,13 +168,16 @@ func (amp *AmplifyPreview) StartJob(ctx context.Context, branch *types.Branch) (
164
168
return nil , err
165
169
}
166
170
167
- logger .Info ("Successfully started job" , logKeyAppID , appID , logKeyBranchName , * branch .BranchName , logKeyJobID , * resp .JobSummary .JobId )
171
+ slog .Info ("Successfully started job" , logKeyAppID , appID , logKeyBranchName , * branch .BranchName , logKeyJobID , * resp .JobSummary .JobId )
168
172
169
173
return resp .JobSummary , nil
170
174
171
175
}
172
176
173
177
func (amp * AmplifyPreview ) findJobsByID (ctx context.Context , branch * types.Branch , includeLatest bool , ids ... string ) (jobSummaries []types.JobSummary , err error ) {
178
+ if branch == nil {
179
+ return nil , errNilBranch
180
+ }
174
181
appID , err := appIDFromBranchARN (* branch .BranchArn )
175
182
if err != nil {
176
183
return nil , err
@@ -193,9 +200,15 @@ func (amp *AmplifyPreview) findJobsByID(ctx context.Context, branch *types.Branc
193
200
}
194
201
195
202
for _ , id := range ids {
196
- wantJobID , _ := strconv .Atoi (id )
203
+ wantJobID , err := strconv .Atoi (id )
204
+ if err != nil {
205
+ slog .Debug ("unexpected Job ID value" , logKeyJobID , wantJobID )
206
+ }
197
207
for _ , j := range resp .JobSummaries {
198
- jobID , _ := strconv .Atoi (* j .JobId )
208
+ jobID , err := strconv .Atoi (* j .JobId )
209
+ if err != nil {
210
+ slog .Debug ("unexpected Job ID value" , logKeyJobID , jobID )
211
+ }
199
212
if jobID == wantJobID {
200
213
jobSummaries = append (jobSummaries , j )
201
214
break
@@ -208,6 +221,10 @@ func (amp *AmplifyPreview) findJobsByID(ctx context.Context, branch *types.Branc
208
221
}
209
222
210
223
func (amp * AmplifyPreview ) GetLatestAndActiveJobs (ctx context.Context , branch * types.Branch ) (latestJob , activeJob * types.JobSummary , err error ) {
224
+ if branch == nil {
225
+ return nil , nil , errNilBranch
226
+ }
227
+
211
228
var jobIDs []string
212
229
if branch .ActiveJobId != nil {
213
230
jobIDs = append (jobIDs , * branch .ActiveJobId )
@@ -228,7 +245,7 @@ func (amp *AmplifyPreview) GetLatestAndActiveJobs(ctx context.Context, branch *t
228
245
}
229
246
230
247
func (amp * AmplifyPreview ) WaitForJobCompletion (ctx context.Context , branch * types.Branch , job * types.JobSummary ) (currentJob , activeJob * types.JobSummary , err error ) {
231
- for i := 0 ; i < jobWaitTimeAttempts ; i ++ {
248
+ for i := range jobWaitTimeAttempts {
232
249
jobSummaries , err := amp .findJobsByID (ctx , branch , true , * job .JobId )
233
250
if err != nil {
234
251
return nil , nil , err
@@ -243,7 +260,7 @@ func (amp *AmplifyPreview) WaitForJobCompletion(ctx context.Context, branch *typ
243
260
break
244
261
}
245
262
246
- logger .Info ("Job is not in a completed state yet. Sleeping..." ,
263
+ slog .Info (fmt . Sprintf ( "Job is not in a completed state yet. Sleeping for %s" , jobWaitSleepTime . String ()) ,
247
264
logKeyBranchName , amp .branchName , "job_status" , currentJob .Status , "job_id" , * currentJob .JobId , "attempts_left" , jobWaitTimeAttempts - i )
248
265
time .Sleep (jobWaitSleepTime )
249
266
}
@@ -265,8 +282,12 @@ func appIDFromBranchARN(branchArn string) (string, error) {
265
282
}
266
283
267
284
func isAmplifyJobCompleted (job * types.JobSummary ) bool {
268
- _ , ok := amplifyJobCompletedStatuses [job .Status ]
269
- return ok
285
+ switch job .Status {
286
+ case types .JobStatusFailed , types .JobStatusCancelled , types .JobStatusSucceed :
287
+ return true
288
+ default :
289
+ return false
290
+ }
270
291
}
271
292
272
293
func (err aggregatedError ) Error () error {
0 commit comments