@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "strconv"
23
24
"strings"
24
25
"sync"
25
26
"time"
@@ -168,31 +169,36 @@ func (amp *AmplifyPreview) StartJob(ctx context.Context, branch *types.Branch) (
168
169
169
170
}
170
171
171
- func (amp * AmplifyPreview ) GetLatestJob (ctx context.Context , branch * types.Branch , jobID * string ) ( * types.JobSummary , error ) {
172
+ func (amp * AmplifyPreview ) GetLatestAndActiveJobs (ctx context.Context , branch * types.Branch ) ( latestJob , activeJob * types.JobSummary , err error ) {
172
173
appID , err := appIDFromBranchARN (* branch .BranchArn )
173
174
if err != nil {
174
- return nil , err
175
+ return nil , nil , err
175
176
}
176
177
177
- if jobID == nil {
178
- jobID = branch .ActiveJobId
178
+ resp , err := amp .client .ListJobs (ctx , & amplify.ListJobsInput {
179
+ AppId : aws .String (appID ),
180
+ BranchName : branch .BranchName ,
181
+ MaxResults : 50 ,
182
+ })
183
+ if err != nil {
184
+ return nil , nil , err
185
+ }
186
+ if len (resp .JobSummaries ) == 0 {
187
+ return nil , nil , errNoJobForBranch
179
188
}
180
189
181
- if jobID != nil {
182
- resp , err := amp .client .ListJobs (ctx , & amplify.ListJobsInput {
183
- AppId : aws .String (appID ),
184
- BranchName : branch .BranchName ,
185
- MaxResults : 1 ,
186
- })
187
- if err != nil {
188
- return nil , err
189
- }
190
- if len (resp .JobSummaries ) > 0 {
191
- return & resp .JobSummaries [0 ], nil
190
+ latestJob = & resp .JobSummaries [0 ]
191
+ if branch .ActiveJobId != nil {
192
+ for _ , j := range resp .JobSummaries {
193
+ jobID , _ := strconv .Atoi (* j .JobId )
194
+ activeJobID , _ := strconv .Atoi (* branch .ActiveJobId )
195
+ if jobID == activeJobID {
196
+ activeJob = & j
197
+ }
192
198
}
193
199
}
194
200
195
- return nil , errNoJobForBranch
201
+ return latestJob , activeJob , nil
196
202
}
197
203
198
204
func appIDFromBranchARN (branchArn string ) (string , error ) {
@@ -226,7 +232,7 @@ func (err aggregatedError) Error() error {
226
232
return fmt .Errorf ("%s for apps:\n \t %s" , err .message , msg .String ())
227
233
}
228
234
229
- func amplifyJobToMarkdown ( job * types.JobSummary , branch * types.Branch ) string {
235
+ func amplifyJobsToMarkdown ( branch * types.Branch , jobs ... * types.JobSummary ) string {
230
236
var mdTableHeader = [... ]string {"Branch" , "Commit" , "Job ID" , "Status" , "Preview" , "Updated (UTC)" }
231
237
var commentBody strings.Builder
232
238
var jobStatusToEmoji = map [types.JobStatus ]rune {
@@ -239,35 +245,44 @@ func amplifyJobToMarkdown(job *types.JobSummary, branch *types.Branch) string {
239
245
240
246
appID , _ := appIDFromBranchARN (* branch .BranchArn )
241
247
242
- updateTime := job .StartTime
243
- if job .EndTime != nil {
244
- updateTime = job .EndTime
245
- }
246
- if updateTime == nil {
247
- updateTime = branch .CreateTime
248
- }
249
-
248
+ // Markdown table header
250
249
commentBody .WriteString (amplifyMarkdownHeader )
251
250
commentBody .WriteByte ('\n' )
252
-
253
- // Markdown table header
254
251
commentBody .WriteString (strings .Join (mdTableHeader [:], " | " ))
255
252
commentBody .WriteByte ('\n' )
256
253
commentBody .WriteString (strings .TrimSuffix (strings .Repeat ("---------|" , len (mdTableHeader )), "|" ))
257
254
commentBody .WriteByte ('\n' )
255
+
256
+ var previousJobStatus types.JobStatus = "unknown"
258
257
// Markdown table content
259
- commentBody .WriteString (* branch .BranchName )
260
- commentBody .WriteString (" | " )
261
- commentBody .WriteString (* job .CommitId )
262
- commentBody .WriteString (" | " )
263
- commentBody .WriteString (* job .JobId )
264
- commentBody .WriteString (" | " )
265
- commentBody .WriteString (fmt .Sprintf ("%c%s" , jobStatusToEmoji [job .Status ], job .Status ))
266
- commentBody .WriteString (" | " )
267
- commentBody .WriteString (fmt .Sprintf ("[%[1]s](https://%[1]s.%s.%s)" , * branch .DisplayName , appID , amplifyDefaultDomain ))
268
- commentBody .WriteString (" | " )
269
- commentBody .WriteString (updateTime .Format (time .DateTime ))
270
- commentBody .WriteByte ('\n' )
258
+ for _ , job := range jobs {
259
+ if job == nil || job .Status == previousJobStatus {
260
+ continue
261
+ }
262
+
263
+ updateTime := job .StartTime
264
+ if job .EndTime != nil {
265
+ updateTime = job .EndTime
266
+ }
267
+ if updateTime == nil {
268
+ updateTime = branch .CreateTime
269
+ }
270
+
271
+ commentBody .WriteString (* branch .BranchName )
272
+ commentBody .WriteString (" | " )
273
+ commentBody .WriteString (* job .CommitId )
274
+ commentBody .WriteString (" | " )
275
+ commentBody .WriteString (* job .JobId )
276
+ commentBody .WriteString (" | " )
277
+ commentBody .WriteString (fmt .Sprintf ("%c%s" , jobStatusToEmoji [job .Status ], job .Status ))
278
+ commentBody .WriteString (" | " )
279
+ commentBody .WriteString (fmt .Sprintf ("[%[1]s](https://%[1]s.%s.%s)" , * branch .DisplayName , appID , amplifyDefaultDomain ))
280
+ commentBody .WriteString (" | " )
281
+ commentBody .WriteString (updateTime .Format (time .DateTime ))
282
+ commentBody .WriteByte ('\n' )
283
+
284
+ previousJobStatus = job .Status
285
+ }
271
286
272
287
return commentBody .String ()
273
288
}
0 commit comments