From 57824e637468832c465b807995adc932cd046f8e Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Mon, 27 May 2024 18:50:40 +0200 Subject: [PATCH] sg: cloud ephemeral - handle multiple job reasons (#62929) * sg: cloud ephemeral handle multiple job reasons * update cloud printers to show overall job status * nogo --- dev/sg/internal/cloud/instance.go | 14 ++++++++++++++ dev/sg/internal/cloud/printers.go | 21 +++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/dev/sg/internal/cloud/instance.go b/dev/sg/internal/cloud/instance.go index 88f4805df92cc..72a0b0aa45590 100644 --- a/dev/sg/internal/cloud/instance.go +++ b/dev/sg/internal/cloud/instance.go @@ -3,6 +3,7 @@ package cloud import ( "fmt" "strconv" + "strings" "time" "github.com/grafana/regexp" @@ -106,6 +107,7 @@ type InstanceStatus struct { type StatusReason struct { Step string `json:"step"` Phase string `json:"phase"` + JobCount int `json:"job_count"` JobURL string `json:"job_url"` JobState string `json:"job_state"` Overall string `json:"overall"` @@ -115,6 +117,17 @@ func newStatusReason(reason string) (StatusReason, error) { if reason == "" { return StatusReason{}, nil } + + // TODO(burmudar): handle storing of multiple jobs + jobCount := 1 + // if the reason contains a semicolon it means there are multiple jobs, we only want the last job + if strings.Contains(reason, ";") { + parts := strings.Split(reason, ";") + // we want to know the number of jobs + jobCount = len(parts) + // we want to know the last job + reason = strings.TrimSpace(parts[jobCount-1]) + } // step 1/3:creating instance, job-url:https://github.com/sourcegraph/cloud/actions/runs/9209264595, state:in_progress, conclusion: failed statusRegex := regexp.MustCompile(`^step (\d\/\d):(.*), job-url:(.*), state:(\w+)(, conclusion:(\w+))?$`) matches := statusRegex.FindStringSubmatch(reason) @@ -129,6 +142,7 @@ func newStatusReason(reason string) (StatusReason, error) { return StatusReason{ Step: matches[1], Phase: matches[2], + JobCount: jobCount, JobURL: matches[3], JobState: matches[4], Overall: conclusion, diff --git a/dev/sg/internal/cloud/printers.go b/dev/sg/internal/cloud/printers.go index ef79803056344..265f5d7eaa992 100644 --- a/dev/sg/internal/cloud/printers.go +++ b/dev/sg/internal/cloud/printers.go @@ -43,24 +43,25 @@ func newDefaultTerminalInstancePrinter() *terminalInstancePrinter { } } - actionURL := "n/a" - if inst.Status.Reason.JobURL != "" { - actionURL = inst.Status.Reason.JobURL - if inst.Status.Reason.JobState != "" { - actionURL += " (" + inst.Status.Reason.JobState + ")" - } - } expiresAt := "n/a" if !inst.ExpiresAt.IsZero() { expiresAt = inst.ExpiresAt.Format(time.RFC3339) } + var jobCount = inst.Status.Reason.JobCount + var overallJobStatus = inst.Status.Reason.Overall + if inst.Status.Status == InstanceStatusCompleted { + overallJobStatus = "completed" + } else if overallJobStatus == "" { + overallJobStatus = "n/a" + } + return []any{ - name, expiresAt, status, actionURL, + name, expiresAt, status, jobCount, overallJobStatus, } } - return newTerminalInstancePrinter(valueFunc, "%-40s %-20s %-40s %s", "Name", "Expires At", "Status", "JobURL") + return newTerminalInstancePrinter(valueFunc, "%-40s %-20s %-40s %-5s %s", "Name", "Expires at", "Status", "Jobs", "Overall job status") } func newTerminalInstancePrinter(valueFunc func(i *Instance) []any, headingFmt string, headings ...string) *terminalInstancePrinter { @@ -81,7 +82,7 @@ func (p *terminalInstancePrinter) Print(items ...*Instance) error { std.Out.WriteLine(output.Line("", output.StyleBold, heading)) for _, inst := range items { values := p.valueFunc(inst) - line := fmt.Sprintf("%-40s %-20s %-40s %s", values...) + line := fmt.Sprintf("%-40s %-20s %-40s %-5d %s", values...) std.Out.WriteLine(output.Line("", output.StyleGrey, line)) }