-
Notifications
You must be signed in to change notification settings - Fork 15
feat(ci): add step summary command #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d1f1e55
feat(ci): add step summary command
121watts d6be8fa
fix(ci): add json output for summaries
121watts 2488b57
fix(ci): simplify summary resolution
121watts 25be950
fix(ci): keep summary json stdout clean
121watts 0802bb1
Merge branch 'main' into watts/dep-4421-ci-step-summaries-cli
121watts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package ci | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "connectrpc.com/connect" | ||
| "github.com/depot/cli/pkg/api" | ||
| "github.com/depot/cli/pkg/config" | ||
| "github.com/depot/cli/pkg/helpers" | ||
| civ1 "github.com/depot/cli/pkg/proto/depot/ci/v1" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| ciGetJobSummary = api.CIGetJobSummary | ||
| ) | ||
|
|
||
| func NewCmdSummary() *cobra.Command { | ||
| var ( | ||
| orgID string | ||
| token string | ||
| output string | ||
| ) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "summary <attempt-id | job-id>", | ||
| Short: "Fetch CI step summary markdown", | ||
| Long: "Fetch authored CI step summary markdown for one job attempt or the current/latest attempt of one job.", | ||
| Example: ` depot ci summary <attempt-id> | ||
| depot ci summary <job-id> | ||
| depot ci summary <attempt-id> --output json`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := validateTextOrJSONOutput(output); err != nil { | ||
| return err | ||
| } | ||
| if len(args) == 0 { | ||
| if outputIsJSON(output) { | ||
| cmd.SilenceUsage = true | ||
| return fmt.Errorf("expected exactly one attempt or job ID") | ||
| } | ||
| return cmd.Help() | ||
| } | ||
| if len(args) > 1 { | ||
| return fmt.Errorf("expected exactly one attempt or job ID") | ||
| } | ||
|
|
||
| ctx := cmd.Context() | ||
| id := args[0] | ||
|
|
||
| if orgID == "" { | ||
| orgID = config.GetCurrentOrganization() | ||
| } | ||
|
|
||
| tokenVal, err := helpers.ResolveOrgAuth(ctx, token) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if tokenVal == "" { | ||
| return fmt.Errorf("missing API token, please run `depot login`") | ||
| } | ||
|
|
||
| resp, jobErr := ciGetJobSummary(ctx, tokenVal, orgID, &civ1.GetJobSummaryRequest{JobId: id}) | ||
| if jobErr == nil { | ||
| if outputIsJSON(output) { | ||
| return writeJSON(buildSummaryJSON(resp)) | ||
| } | ||
| if resp.GetAttemptId() != "" { | ||
| fmt.Fprintf(cmd.ErrOrStderr(), "Using attempt #%d %s for job %s.\n", resp.GetAttempt(), resp.GetAttemptId(), resp.GetJobId()) | ||
| } | ||
| return printSummaryResponse(cmd.OutOrStdout(), resp) | ||
| } | ||
| if connect.CodeOf(jobErr) != connect.CodeNotFound { | ||
| return fmt.Errorf("failed to get job summary: %w", jobErr) | ||
| } | ||
|
|
||
| resp, attemptErr := ciGetJobSummary(ctx, tokenVal, orgID, &civ1.GetJobSummaryRequest{AttemptId: id}) | ||
| if attemptErr != nil { | ||
| if connect.CodeOf(attemptErr) == connect.CodeNotFound { | ||
| return fmt.Errorf( | ||
| "could not resolve %q as an attempt or job ID:\n as attempt: %v\n as job: %v", | ||
| id, | ||
| attemptErr, | ||
| jobErr, | ||
| ) | ||
| } | ||
| return fmt.Errorf("failed to get attempt summary: %w", attemptErr) | ||
| } | ||
|
|
||
| if outputIsJSON(output) { | ||
| return writeJSON(buildSummaryJSON(resp)) | ||
| } | ||
| return printSummaryResponse(cmd.OutOrStdout(), resp) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&orgID, "org", "", "Organization ID (required when user is a member of multiple organizations)") | ||
| cmd.Flags().StringVar(&token, "token", "", "Depot API token") | ||
| cmd.Flags().StringVarP(&output, "output", "o", "", "Output format (text, json)") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| type summaryJSONDocument struct { | ||
| OrgID string `json:"org_id"` | ||
| RunID string `json:"run_id"` | ||
| WorkflowID string `json:"workflow_id"` | ||
| JobID string `json:"job_id"` | ||
| AttemptID string `json:"attempt_id"` | ||
| Attempt int32 `json:"attempt"` | ||
| JobStatus string `json:"job_status"` | ||
| AttemptStatus string `json:"attempt_status"` | ||
| HasSummary bool `json:"has_summary"` | ||
| EmptyReason string `json:"empty_reason"` | ||
| StepCount uint32 `json:"step_count"` | ||
| Markdown string `json:"markdown"` | ||
| } | ||
|
|
||
| func buildSummaryJSON(resp *civ1.GetJobSummaryResponse) summaryJSONDocument { | ||
| return summaryJSONDocument{ | ||
| OrgID: resp.GetOrgId(), | ||
| RunID: resp.GetRunId(), | ||
| WorkflowID: resp.GetWorkflowId(), | ||
| JobID: resp.GetJobId(), | ||
| AttemptID: resp.GetAttemptId(), | ||
| Attempt: resp.GetAttempt(), | ||
| JobStatus: resp.GetJobStatus(), | ||
| AttemptStatus: resp.GetAttemptStatus(), | ||
| HasSummary: resp.GetHasSummary(), | ||
| EmptyReason: resp.GetEmptyReason(), | ||
| StepCount: resp.GetStepCount(), | ||
| Markdown: resp.GetMarkdown(), | ||
| } | ||
| } | ||
|
|
||
| func printSummaryResponse(w io.Writer, resp *civ1.GetJobSummaryResponse) error { | ||
| if resp.GetHasSummary() { | ||
| fmt.Fprint(w, resp.GetMarkdown()) | ||
| if !strings.HasSuffix(resp.GetMarkdown(), "\n") { | ||
| fmt.Fprintln(w) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| fmt.Fprintln(w, emptySummaryMessage(resp)) | ||
| return nil | ||
| } | ||
|
|
||
| func emptySummaryMessage(resp *civ1.GetJobSummaryResponse) string { | ||
| switch resp.GetEmptyReason() { | ||
| case "no_attempt": | ||
| return "No CI job attempts exist yet, so no step summary is available." | ||
| default: | ||
| if resp.GetAttemptId() != "" { | ||
| return "No CI step summary was produced for this attempt." | ||
| } | ||
| return "No CI step summary was produced." | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.