From 5e7aaf5917da95e936ee6b90d24159c23bb50345 Mon Sep 17 00:00:00 2001 From: Taras Postument Date: Mon, 16 Dec 2024 12:57:15 +0000 Subject: [PATCH] Support multiple jobs in a table --- tools/amplify-preview/amplify.go | 90 ++++++++++++++++++-------------- tools/amplify-preview/main.go | 20 +++---- 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/tools/amplify-preview/amplify.go b/tools/amplify-preview/amplify.go index 6f85f3e..db7ddd2 100644 --- a/tools/amplify-preview/amplify.go +++ b/tools/amplify-preview/amplify.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "strconv" "strings" "sync" "time" @@ -168,31 +169,36 @@ func (amp *AmplifyPreview) StartJob(ctx context.Context, branch *types.Branch) ( } -func (amp *AmplifyPreview) GetLatestJob(ctx context.Context, branch *types.Branch, jobID *string) (*types.JobSummary, error) { +func (amp *AmplifyPreview) GetLatestAndActiveJobs(ctx context.Context, branch *types.Branch) (latestJob, activeJob *types.JobSummary, err error) { appID, err := appIDFromBranchARN(*branch.BranchArn) if err != nil { - return nil, err + return nil, nil, err } - if jobID == nil { - jobID = branch.ActiveJobId + resp, err := amp.client.ListJobs(ctx, &lify.ListJobsInput{ + AppId: aws.String(appID), + BranchName: branch.BranchName, + MaxResults: 50, + }) + if err != nil { + return nil, nil, err + } + if len(resp.JobSummaries) == 0 { + return nil, nil, errNoJobForBranch } - if jobID != nil { - resp, err := amp.client.ListJobs(ctx, &lify.ListJobsInput{ - AppId: aws.String(appID), - BranchName: branch.BranchName, - MaxResults: 1, - }) - if err != nil { - return nil, err - } - if len(resp.JobSummaries) > 0 { - return &resp.JobSummaries[0], nil + latestJob = &resp.JobSummaries[0] + if branch.ActiveJobId != nil { + for _, j := range resp.JobSummaries { + jobID, _ := strconv.Atoi(*j.JobId) + activeJobID, _ := strconv.Atoi(*branch.ActiveJobId) + if jobID == activeJobID { + activeJob = &j + } } } - return nil, errNoJobForBranch + return latestJob, activeJob, nil } func appIDFromBranchARN(branchArn string) (string, error) { @@ -226,7 +232,7 @@ func (err aggregatedError) Error() error { return fmt.Errorf("%s for apps:\n\t%s", err.message, msg.String()) } -func amplifyJobToMarkdown(job *types.JobSummary, branch *types.Branch) string { +func amplifyJobsToMarkdown(branch *types.Branch, jobs ...*types.JobSummary) string { var mdTableHeader = [...]string{"Branch", "Commit", "Job ID", "Status", "Preview", "Updated (UTC)"} var commentBody strings.Builder var jobStatusToEmoji = map[types.JobStatus]rune{ @@ -239,35 +245,41 @@ func amplifyJobToMarkdown(job *types.JobSummary, branch *types.Branch) string { appID, _ := appIDFromBranchARN(*branch.BranchArn) - updateTime := job.StartTime - if job.EndTime != nil { - updateTime = job.EndTime - } - if updateTime == nil { - updateTime = branch.CreateTime - } - + // Markdown table header commentBody.WriteString(amplifyMarkdownHeader) commentBody.WriteByte('\n') - - // Markdown table header commentBody.WriteString(strings.Join(mdTableHeader[:], " | ")) commentBody.WriteByte('\n') commentBody.WriteString(strings.TrimSuffix(strings.Repeat("---------|", len(mdTableHeader)), "|")) commentBody.WriteByte('\n') + // Markdown table content - commentBody.WriteString(*branch.BranchName) - commentBody.WriteString(" | ") - commentBody.WriteString(*job.CommitId) - commentBody.WriteString(" | ") - commentBody.WriteString(*job.JobId) - commentBody.WriteString(" | ") - commentBody.WriteString(fmt.Sprintf("%c%s", jobStatusToEmoji[job.Status], job.Status)) - commentBody.WriteString(" | ") - commentBody.WriteString(fmt.Sprintf("[%[1]s](https://%[1]s.%s.%s)", *branch.DisplayName, appID, amplifyDefaultDomain)) - commentBody.WriteString(" | ") - commentBody.WriteString(updateTime.Format(time.DateTime)) - commentBody.WriteByte('\n') + for _, job := range jobs { + if job == nil { + continue + } + + updateTime := job.StartTime + if job.EndTime != nil { + updateTime = job.EndTime + } + if updateTime == nil { + updateTime = branch.CreateTime + } + + commentBody.WriteString(*branch.BranchName) + commentBody.WriteString(" | ") + commentBody.WriteString(*job.CommitId) + commentBody.WriteString(" | ") + commentBody.WriteString(*job.JobId) + commentBody.WriteString(" | ") + commentBody.WriteString(fmt.Sprintf("%c%s", jobStatusToEmoji[job.Status], job.Status)) + commentBody.WriteString(" | ") + commentBody.WriteString(fmt.Sprintf("[%[1]s](https://%[1]s.%s.%s)", *branch.DisplayName, appID, amplifyDefaultDomain)) + commentBody.WriteString(" | ") + commentBody.WriteString(updateTime.Format(time.DateTime)) + commentBody.WriteByte('\n') + } return commentBody.String() } diff --git a/tools/amplify-preview/main.go b/tools/amplify-preview/main.go index 91b8bee..033a12a 100644 --- a/tools/amplify-preview/main.go +++ b/tools/amplify-preview/main.go @@ -40,7 +40,7 @@ var ( createBranches = kingpin.Flag("crate-branches", "Defines whether Amplify branches should be created if missing, or just lookup existing ones").Envar("CREATE_BRANCHES").Default("false").Bool() wait = kingpin.Flag("wait", - "Wait for pending/running job to complete").Envar("wait").Default("false").Bool() + "Wait for pending/running job to complete").Envar("WAIT").Default("false").Bool() ) const ( @@ -89,7 +89,7 @@ func main() { } // check if existing branch was/being deployed already - job, err := amp.GetLatestJob(ctx, branch, nil) + latestJob, activeJob, err := amp.GetLatestAndActiveJobs(ctx, branch) if err != nil { if !*createBranches && !errors.Is(err, errNoJobForBranch) { logger.Error("failed to get amplify job", logKeyBranchName, *gitBranchName, "error", err) @@ -97,39 +97,39 @@ func main() { } // if job not found and branch was just created - start new job - job, err = amp.StartJob(ctx, branch) + latestJob, err = amp.StartJob(ctx, branch) if err != nil { logger.Error("failed to start amplify job", logKeyBranchName, *gitBranchName, "error", err) os.Exit(1) } } - if err := postPreviewURL(ctx, amplifyJobToMarkdown(job, branch)); err != nil { + if err := postPreviewURL(ctx, amplifyJobsToMarkdown(branch, latestJob, activeJob)); err != nil { logger.Error("failed to post preview URL", "error", err) os.Exit(1) } logger.Info("Successfully posted PR comment") if *wait { - for i := 0; !isAmplifyJobCompleted(job) && i < jobWaitTimeAttempts; i++ { - job, err := amp.GetLatestJob(ctx, branch, nil) + for i := 0; !isAmplifyJobCompleted(latestJob) && i < jobWaitTimeAttempts; i++ { + latestJob, activeJob, err = amp.GetLatestAndActiveJobs(ctx, branch) if err != nil { logger.Error("failed to get amplify job", logKeyBranchName, *gitBranchName, "error", err) os.Exit(1) } - logger.Info("Job is not in a completed state yet. Sleeping...", logKeyBranchName, *gitBranchName, "job_status", job.Status, "job_id", job.JobId) + logger.Info("Job is not in a completed state yet. Sleeping...", logKeyBranchName, *gitBranchName, "job_status", latestJob.Status, "job_id", latestJob.JobId) time.Sleep(jobWaitSleepTime) } - if err := postPreviewURL(ctx, amplifyJobToMarkdown(job, branch)); err != nil { + if err := postPreviewURL(ctx, amplifyJobsToMarkdown(branch, latestJob, activeJob)); err != nil { logger.Error("failed to post preview URL", "error", err) os.Exit(1) } } - if job.Status == types.JobStatusFailed { - logger.Error("amplify job is in failed state", logKeyBranchName, *gitBranchName, "job_status", job.Status, "job_id", job.JobId) + if latestJob.Status == types.JobStatusFailed { + logger.Error("amplify job is in failed state", logKeyBranchName, *gitBranchName, "job_status", latestJob.Status, "job_id", latestJob.JobId) os.Exit(1) } }