Skip to content

Commit

Permalink
Support multiple jobs in a table
Browse files Browse the repository at this point in the history
  • Loading branch information
taraspos committed Dec 16, 2024
1 parent b1f7d99 commit 5e7aaf5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 49 deletions.
90 changes: 51 additions & 39 deletions tools/amplify-preview/amplify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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, &amplify.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, &amplify.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) {
Expand Down Expand Up @@ -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{
Expand All @@ -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()
}
20 changes: 10 additions & 10 deletions tools/amplify-preview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -89,47 +89,47 @@ 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)
os.Exit(1)
}

// 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)
}
}

0 comments on commit 5e7aaf5

Please sign in to comment.