feat(cli): rework developer CLI with non-blocking tasks and GitHub CI checks (#6651)
This commit is contained in:
1 parent
f0f9539466
commit
81aa704537
56 files changed
+7950
-2548
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -197,3 +198,151 @@ func GetPR(ctx context.Context, number int) (*PR, error) {
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Check struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
Bucket string `json:"bucket"`
|
||||
Workflow string `json:"workflow"`
|
||||
Link string `json:"link"`
|
||||
StartedAt string `json:"startedAt"`
|
||||
CompletedAt string `json:"completedAt"`
|
||||
Description string `json:"description"`
|
||||
Event string `json:"event"`
|
||||
}
|
||||
|
||||
var checkBucketKeys = map[string]string{
|
||||
"pass": "pass",
|
||||
"fail": "fail",
|
||||
"pending": "pending",
|
||||
"skipping": "skipping",
|
||||
"cancel": "cancel",
|
||||
}
|
||||
|
||||
func (c Check) BucketKey() string {
|
||||
if key, found := checkBucketKeys[strings.ToLower(c.Bucket)]; found {
|
||||
return key
|
||||
}
|
||||
return "pending"
|
||||
}
|
||||
|
||||
func (c Check) IsPending() bool {
|
||||
return c.Bucket == "pending" || c.State == "PENDING" || c.State == "IN_PROGRESS" || c.State == "STARTING"
|
||||
}
|
||||
|
||||
func (c Check) IsSuccess() bool {
|
||||
return c.Bucket == "pass" || c.State == "SUCCESS"
|
||||
}
|
||||
|
||||
func (c Check) IsFailure() bool {
|
||||
return c.Bucket == "fail" || c.State == "FAILURE" || c.State == "ERROR"
|
||||
}
|
||||
|
||||
func (c Check) IsSkipped() bool {
|
||||
return c.Bucket == "skipping" || c.Bucket == "cancel" || c.State == "SKIPPED" || c.State == "CANCELLED"
|
||||
}
|
||||
|
||||
func (c Check) Duration() string {
|
||||
if c.StartedAt == "" {
|
||||
return ""
|
||||
}
|
||||
start, err := time.Parse(time.RFC3339, c.StartedAt)
|
||||
if err != nil || start.IsZero() {
|
||||
return ""
|
||||
}
|
||||
end := time.Now()
|
||||
if c.CompletedAt != "" {
|
||||
if tEnd, err := time.Parse(time.RFC3339, c.CompletedAt); err == nil && !tEnd.IsZero() {
|
||||
end = tEnd
|
||||
}
|
||||
}
|
||||
d := end.Sub(start).Round(time.Second)
|
||||
if d < 0 {
|
||||
return ""
|
||||
}
|
||||
if d < time.Minute {
|
||||
return fmt.Sprintf("%ds", int(d.Seconds()))
|
||||
}
|
||||
if d < time.Hour {
|
||||
return fmt.Sprintf("%dm%02ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
return fmt.Sprintf("%dh%02dm", int(d.Hours()), int(d.Minutes())%60)
|
||||
}
|
||||
|
||||
var prChecksCache = struct {
|
||||
sync.Mutex
|
||||
entries map[int]cachedChecks
|
||||
}{entries: make(map[int]cachedChecks)}
|
||||
|
||||
type cachedChecks struct {
|
||||
checks []Check
|
||||
fetchedAt time.Time
|
||||
}
|
||||
|
||||
func GetPRChecks(ctx context.Context, number int, refresh bool) ([]Check, error) {
|
||||
if number <= 0 {
|
||||
return nil, fmt.Errorf("invalid PR number: %d", number)
|
||||
}
|
||||
prChecksCache.Lock()
|
||||
entry, found := prChecksCache.entries[number]
|
||||
prChecksCache.Unlock()
|
||||
if !refresh && found && time.Since(entry.fetchedAt) < 15*time.Second {
|
||||
return append([]Check(nil), entry.checks...), nil
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, "gh", "pr", "checks", fmt.Sprint(number),
|
||||
"--repo", Repo,
|
||||
"--json", "name,state,bucket,workflow,link,startedAt,completedAt,description,event",
|
||||
)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil && len(out) == 0 {
|
||||
return nil, commandError(err, out)
|
||||
}
|
||||
|
||||
var checks []Check
|
||||
if jsonErr := json.Unmarshal(out, &checks); jsonErr != nil {
|
||||
if err != nil {
|
||||
return nil, commandError(err, out)
|
||||
}
|
||||
return nil, fmt.Errorf("parse checks JSON: %w", jsonErr)
|
||||
}
|
||||
|
||||
prChecksCache.Lock()
|
||||
prChecksCache.entries[number] = cachedChecks{checks: checks, fetchedAt: time.Now()}
|
||||
prChecksCache.Unlock()
|
||||
|
||||
return checks, nil
|
||||
}
|
||||
|
||||
func WatchPRChecks(ctx context.Context, number int) error {
|
||||
cmd := exec.CommandContext(ctx, "gh", "pr", "checks", fmt.Sprint(number), "--repo", Repo, "--watch")
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func CurrentBranchPR(ctx context.Context) (*PR, error) {
|
||||
out, err := exec.CommandContext(ctx, "gh", "pr", "view",
|
||||
"--repo", Repo,
|
||||
"--json", "number,title,author,headRefName,headRefOid,updatedAt,isDraft,statusCheckRollup",
|
||||
).CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, commandError(err, out)
|
||||
}
|
||||
|
||||
var r rawPR
|
||||
if err := json.Unmarshal(out, &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PR{
|
||||
Number: r.Number,
|
||||
Title: r.Title,
|
||||
Author: r.Author.Login,
|
||||
HeadRef: r.HeadRefName,
|
||||
HeadSHA: r.HeadRefOID,
|
||||
CIState: rollupState(r.StatusCheckRollup),
|
||||
IsDraft: r.IsDraft,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -89,3 +89,47 @@ func TestListPRsExcludesBotsIntegration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHelpersAndDuration(t *testing.T) {
|
||||
c1 := Check{
|
||||
Name: "Test Job",
|
||||
State: "SUCCESS",
|
||||
Bucket: "pass",
|
||||
StartedAt: "2026-08-21T10:00:00Z",
|
||||
CompletedAt: "2026-08-21T10:05:30Z",
|
||||
}
|
||||
if !c1.IsSuccess() || c1.IsFailure() || c1.IsPending() || c1.IsSkipped() {
|
||||
t.Fatalf("unexpected state helpers for c1: success=%v, fail=%v, pend=%v, skip=%v", c1.IsSuccess(), c1.IsFailure(), c1.IsPending(), c1.IsSkipped())
|
||||
}
|
||||
if d := c1.Duration(); d != "5m30s" {
|
||||
t.Fatalf("Duration() = %q, want 5m30s", d)
|
||||
}
|
||||
|
||||
c2 := Check{
|
||||
Name: "Fail Job",
|
||||
State: "FAILURE",
|
||||
Bucket: "fail",
|
||||
}
|
||||
if !c2.IsFailure() {
|
||||
t.Fatalf("expected c2 to be failure")
|
||||
}
|
||||
if c2.Duration() != "" {
|
||||
t.Fatalf("expected empty duration for missing timestamps")
|
||||
}
|
||||
|
||||
c3 := Check{
|
||||
Name: "Pending Job",
|
||||
Bucket: "pending",
|
||||
}
|
||||
if !c3.IsPending() {
|
||||
t.Fatalf("expected c3 to be pending")
|
||||
}
|
||||
|
||||
c4 := Check{
|
||||
Name: "Skipped Job",
|
||||
Bucket: "skipping",
|
||||
}
|
||||
if !c4.IsSkipped() {
|
||||
t.Fatalf("expected c4 to be skipped")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user