Skip to content

Commit 9cec7a5

Browse files
committed
Support multiple jobs in a table
1 parent b1f7d99 commit 9cec7a5

File tree

2 files changed

+65
-49
lines changed

2 files changed

+65
-49
lines changed

tools/amplify-preview/amplify.go

+54-39
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"strconv"
2324
"strings"
2425
"sync"
2526
"time"
@@ -168,31 +169,36 @@ func (amp *AmplifyPreview) StartJob(ctx context.Context, branch *types.Branch) (
168169

169170
}
170171

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) {
172173
appID, err := appIDFromBranchARN(*branch.BranchArn)
173174
if err != nil {
174-
return nil, err
175+
return nil, nil, err
175176
}
176177

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
179188
}
180189

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+
}
192198
}
193199
}
194200

195-
return nil, errNoJobForBranch
201+
return latestJob, activeJob, nil
196202
}
197203

198204
func appIDFromBranchARN(branchArn string) (string, error) {
@@ -226,7 +232,7 @@ func (err aggregatedError) Error() error {
226232
return fmt.Errorf("%s for apps:\n\t%s", err.message, msg.String())
227233
}
228234

229-
func amplifyJobToMarkdown(job *types.JobSummary, branch *types.Branch) string {
235+
func amplifyJobsToMarkdown(branch *types.Branch, jobs ...*types.JobSummary) string {
230236
var mdTableHeader = [...]string{"Branch", "Commit", "Job ID", "Status", "Preview", "Updated (UTC)"}
231237
var commentBody strings.Builder
232238
var jobStatusToEmoji = map[types.JobStatus]rune{
@@ -239,35 +245,44 @@ func amplifyJobToMarkdown(job *types.JobSummary, branch *types.Branch) string {
239245

240246
appID, _ := appIDFromBranchARN(*branch.BranchArn)
241247

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
250249
commentBody.WriteString(amplifyMarkdownHeader)
251250
commentBody.WriteByte('\n')
252-
253-
// Markdown table header
254251
commentBody.WriteString(strings.Join(mdTableHeader[:], " | "))
255252
commentBody.WriteByte('\n')
256253
commentBody.WriteString(strings.TrimSuffix(strings.Repeat("---------|", len(mdTableHeader)), "|"))
257254
commentBody.WriteByte('\n')
255+
256+
var previousJobStatus types.JobStatus = "unknown"
258257
// 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+
}
271286

272287
return commentBody.String()
273288
}

tools/amplify-preview/main.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var (
4040
createBranches = kingpin.Flag("crate-branches",
4141
"Defines whether Amplify branches should be created if missing, or just lookup existing ones").Envar("CREATE_BRANCHES").Default("false").Bool()
4242
wait = kingpin.Flag("wait",
43-
"Wait for pending/running job to complete").Envar("wait").Default("false").Bool()
43+
"Wait for pending/running job to complete").Envar("WAIT").Default("false").Bool()
4444
)
4545

4646
const (
@@ -89,47 +89,48 @@ func main() {
8989
}
9090

9191
// check if existing branch was/being deployed already
92-
job, err := amp.GetLatestJob(ctx, branch, nil)
92+
latestJob, activeJob, err := amp.GetLatestAndActiveJobs(ctx, branch)
9393
if err != nil {
9494
if !*createBranches && !errors.Is(err, errNoJobForBranch) {
9595
logger.Error("failed to get amplify job", logKeyBranchName, *gitBranchName, "error", err)
9696
os.Exit(1)
9797
}
9898

9999
// if job not found and branch was just created - start new job
100-
job, err = amp.StartJob(ctx, branch)
100+
latestJob, err = amp.StartJob(ctx, branch)
101101
if err != nil {
102102
logger.Error("failed to start amplify job", logKeyBranchName, *gitBranchName, "error", err)
103103
os.Exit(1)
104104
}
105105
}
106106

107-
if err := postPreviewURL(ctx, amplifyJobToMarkdown(job, branch)); err != nil {
107+
if err := postPreviewURL(ctx, amplifyJobsToMarkdown(branch, latestJob, activeJob)); err != nil {
108108
logger.Error("failed to post preview URL", "error", err)
109109
os.Exit(1)
110110
}
111111
logger.Info("Successfully posted PR comment")
112112

113113
if *wait {
114-
for i := 0; !isAmplifyJobCompleted(job) && i < jobWaitTimeAttempts; i++ {
115-
job, err := amp.GetLatestJob(ctx, branch, nil)
114+
for i := 0; !isAmplifyJobCompleted(latestJob) && i < jobWaitTimeAttempts; i++ {
115+
latestJob, activeJob, err = amp.GetLatestAndActiveJobs(ctx, branch)
116116
if err != nil {
117117
logger.Error("failed to get amplify job", logKeyBranchName, *gitBranchName, "error", err)
118118
os.Exit(1)
119119
}
120120

121-
logger.Info("Job is not in a completed state yet. Sleeping...", logKeyBranchName, *gitBranchName, "job_status", job.Status, "job_id", job.JobId)
121+
logger.Info("Job is not in a completed state yet. Sleeping...",
122+
logKeyBranchName, *gitBranchName, "job_status", latestJob.Status, "job_id", *latestJob.JobId, "attempts_left", jobWaitTimeAttempts-i)
122123
time.Sleep(jobWaitSleepTime)
123124
}
124125

125-
if err := postPreviewURL(ctx, amplifyJobToMarkdown(job, branch)); err != nil {
126+
if err := postPreviewURL(ctx, amplifyJobsToMarkdown(branch, latestJob, activeJob)); err != nil {
126127
logger.Error("failed to post preview URL", "error", err)
127128
os.Exit(1)
128129
}
129130
}
130131

131-
if job.Status == types.JobStatusFailed {
132-
logger.Error("amplify job is in failed state", logKeyBranchName, *gitBranchName, "job_status", job.Status, "job_id", job.JobId)
132+
if latestJob.Status == types.JobStatusFailed {
133+
logger.Error("amplify job is in failed state", logKeyBranchName, *gitBranchName, "job_status", latestJob.Status, "job_id", *latestJob.JobId)
133134
os.Exit(1)
134135
}
135136
}

0 commit comments

Comments
 (0)