Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
sg: cloud ephemeral - handle multiple job reasons (#62929)
Browse files Browse the repository at this point in the history
* sg: cloud ephemeral handle multiple job reasons

* update cloud printers to show overall job status

* nogo
  • Loading branch information
burmudar authored May 27, 2024
1 parent 75bd631 commit 57824e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
14 changes: 14 additions & 0 deletions dev/sg/internal/cloud/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud
import (
"fmt"
"strconv"
"strings"
"time"

"github.com/grafana/regexp"
Expand Down Expand Up @@ -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"`
Expand All @@ -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)
Expand All @@ -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,
Expand Down
21 changes: 11 additions & 10 deletions dev/sg/internal/cloud/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}

Expand Down

0 comments on commit 57824e6

Please sign in to comment.