mirror of
https://gitea.com/gitea/tea.git
synced 2026-09-10 07:26:33 -04:00
404 lines
10 KiB
Go
404 lines
10 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package print
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"gitea.dev/sdk"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// formatDurationMinutes formats duration in a human-readable way
|
|
func formatDurationMinutes(started, completed time.Time) string {
|
|
if started.IsZero() {
|
|
return ""
|
|
}
|
|
|
|
end := completed
|
|
if end.IsZero() {
|
|
end = time.Now()
|
|
}
|
|
|
|
duration := end.Sub(started)
|
|
if duration < 0 {
|
|
return ""
|
|
}
|
|
if duration < time.Minute {
|
|
return fmt.Sprintf("%ds", int(duration.Seconds()))
|
|
}
|
|
if duration < time.Hour {
|
|
return fmt.Sprintf("%dm", int(duration.Minutes()))
|
|
}
|
|
hours := int(duration.Hours())
|
|
minutes := int(duration.Minutes()) % 60
|
|
return fmt.Sprintf("%dh%dm", hours, minutes)
|
|
}
|
|
|
|
// validCompletion returns a completion timestamp only when it can represent a
|
|
// real completion for the supplied start time. Some Gitea versions serialize
|
|
// an absent completion as the Unix epoch.
|
|
func validCompletion(started, completed time.Time) time.Time {
|
|
if completed.IsZero() || (!started.IsZero() && completed.Before(started)) {
|
|
return time.Time{}
|
|
}
|
|
return completed
|
|
}
|
|
|
|
// getWorkflowDisplayName returns the display title or falls back to path
|
|
func getWorkflowDisplayName(run *gitea.ActionWorkflowRun) string {
|
|
if run.DisplayTitle != "" {
|
|
return run.DisplayTitle
|
|
}
|
|
return run.Path
|
|
}
|
|
|
|
// ActionRunsList prints a list of workflow runs
|
|
func ActionRunsList(runs []*gitea.ActionWorkflowRun, output string) error {
|
|
machineReadable := isMachineReadable(output)
|
|
var headers []string
|
|
if machineReadable {
|
|
headers = []string{
|
|
"ID",
|
|
"Status",
|
|
"Conclusion",
|
|
"Workflow",
|
|
"Path",
|
|
"Branch",
|
|
"Event",
|
|
"HeadSHA",
|
|
"StartedAt",
|
|
"CompletedAt",
|
|
"Duration",
|
|
}
|
|
} else {
|
|
headers = []string{"ID", "Status", "Workflow", "Branch", "Event", "Started", "Duration"}
|
|
}
|
|
t := table{headers: headers}
|
|
|
|
for _, run := range runs {
|
|
workflowName := getWorkflowDisplayName(run)
|
|
completedAt := validCompletion(run.StartedAt, run.CompletedAt)
|
|
duration := formatDurationMinutes(run.StartedAt, completedAt)
|
|
|
|
if machineReadable {
|
|
t.addRow(
|
|
fmt.Sprintf("%d", run.ID),
|
|
run.Status,
|
|
run.Conclusion,
|
|
workflowName,
|
|
run.Path,
|
|
run.HeadBranch,
|
|
run.Event,
|
|
run.HeadSha,
|
|
FormatTime(run.StartedAt, true),
|
|
FormatTime(completedAt, true),
|
|
duration,
|
|
)
|
|
} else {
|
|
t.addRow(
|
|
fmt.Sprintf("%d", run.ID),
|
|
run.Status,
|
|
workflowName,
|
|
run.HeadBranch,
|
|
run.Event,
|
|
FormatTime(run.StartedAt, false),
|
|
duration,
|
|
)
|
|
}
|
|
}
|
|
|
|
if len(runs) == 0 {
|
|
if machineReadable {
|
|
return t.print(output)
|
|
}
|
|
fmt.Printf("No workflow runs found\n")
|
|
return nil
|
|
}
|
|
|
|
t.sort(0, true)
|
|
return t.print(output)
|
|
}
|
|
|
|
// ActionRunDetails prints detailed information about a workflow run
|
|
func ActionRunDetails(run *gitea.ActionWorkflowRun) {
|
|
workflowName := getWorkflowDisplayName(run)
|
|
|
|
fmt.Printf("Run ID: %d\n", run.ID)
|
|
fmt.Printf("Run Number: %d\n", run.RunNumber)
|
|
fmt.Printf("Status: %s\n", run.Status)
|
|
if run.Conclusion != "" {
|
|
fmt.Printf("Conclusion: %s\n", run.Conclusion)
|
|
}
|
|
fmt.Printf("Workflow: %s\n", workflowName)
|
|
fmt.Printf("Path: %s\n", run.Path)
|
|
fmt.Printf("Branch: %s\n", run.HeadBranch)
|
|
fmt.Printf("Event: %s\n", run.Event)
|
|
fmt.Printf("Head SHA: %s\n", run.HeadSha)
|
|
fmt.Printf("Started: %s\n", FormatTime(run.StartedAt, false))
|
|
completedAt := validCompletion(run.StartedAt, run.CompletedAt)
|
|
if !completedAt.IsZero() {
|
|
fmt.Printf("Completed: %s\n", FormatTime(completedAt, false))
|
|
duration := formatDurationMinutes(run.StartedAt, completedAt)
|
|
fmt.Printf("Duration: %s\n", duration)
|
|
}
|
|
if run.RunAttempt > 1 {
|
|
fmt.Printf("Attempt: %d\n", run.RunAttempt)
|
|
}
|
|
if run.Actor != nil {
|
|
fmt.Printf("Triggered by: %s\n", run.Actor.UserName)
|
|
}
|
|
if run.HTMLURL != "" {
|
|
fmt.Printf("URL: %s\n", run.HTMLURL)
|
|
}
|
|
}
|
|
|
|
type actionRunOutput struct {
|
|
ID int64 `json:"id" yaml:"id"`
|
|
RunNumber int64 `json:"run_number" yaml:"run_number"`
|
|
Status string `json:"status" yaml:"status"`
|
|
Conclusion string `json:"conclusion" yaml:"conclusion"`
|
|
Workflow string `json:"workflow" yaml:"workflow"`
|
|
Path string `json:"path" yaml:"path"`
|
|
Branch string `json:"branch" yaml:"branch"`
|
|
Event string `json:"event" yaml:"event"`
|
|
HeadSHA string `json:"head_sha" yaml:"head_sha"`
|
|
StartedAt string `json:"started_at" yaml:"started_at"`
|
|
CompletedAt string `json:"completed_at" yaml:"completed_at"`
|
|
Duration string `json:"duration" yaml:"duration"`
|
|
Attempt int64 `json:"attempt" yaml:"attempt"`
|
|
Actor string `json:"actor" yaml:"actor"`
|
|
URL string `json:"url" yaml:"url"`
|
|
}
|
|
|
|
type actionJobOutput struct {
|
|
ID int64 `json:"id" yaml:"id"`
|
|
Name string `json:"name" yaml:"name"`
|
|
Status string `json:"status" yaml:"status"`
|
|
Conclusion string `json:"conclusion" yaml:"conclusion"`
|
|
Runner string `json:"runner" yaml:"runner"`
|
|
HeadSHA string `json:"head_sha" yaml:"head_sha"`
|
|
StartedAt string `json:"started_at" yaml:"started_at"`
|
|
CompletedAt string `json:"completed_at" yaml:"completed_at"`
|
|
Duration string `json:"duration" yaml:"duration"`
|
|
}
|
|
|
|
type actionRunViewOutput struct {
|
|
Run actionRunOutput `json:"run" yaml:"run"`
|
|
Jobs []actionJobOutput `json:"jobs" yaml:"jobs"`
|
|
}
|
|
|
|
func newActionRunOutput(run *gitea.ActionWorkflowRun) actionRunOutput {
|
|
completedAt := validCompletion(run.StartedAt, run.CompletedAt)
|
|
actor := ""
|
|
if run.Actor != nil {
|
|
actor = run.Actor.UserName
|
|
}
|
|
return actionRunOutput{
|
|
ID: run.ID,
|
|
RunNumber: run.RunNumber,
|
|
Status: run.Status,
|
|
Conclusion: run.Conclusion,
|
|
Workflow: getWorkflowDisplayName(run),
|
|
Path: run.Path,
|
|
Branch: run.HeadBranch,
|
|
Event: run.Event,
|
|
HeadSHA: run.HeadSha,
|
|
StartedAt: FormatTime(run.StartedAt, true),
|
|
CompletedAt: FormatTime(completedAt, true),
|
|
Duration: formatDurationMinutes(run.StartedAt, completedAt),
|
|
Attempt: run.RunAttempt,
|
|
Actor: actor,
|
|
URL: run.HTMLURL,
|
|
}
|
|
}
|
|
|
|
func newActionJobOutput(job *gitea.ActionWorkflowJob) actionJobOutput {
|
|
completedAt := validCompletion(job.StartedAt, job.CompletedAt)
|
|
runner := job.RunnerName
|
|
if runner == "" {
|
|
runner = "-"
|
|
}
|
|
return actionJobOutput{
|
|
ID: job.ID,
|
|
Name: job.Name,
|
|
Status: job.Status,
|
|
Conclusion: job.Conclusion,
|
|
Runner: runner,
|
|
HeadSHA: job.HeadSha,
|
|
StartedAt: FormatTime(job.StartedAt, true),
|
|
CompletedAt: FormatTime(completedAt, true),
|
|
Duration: formatDurationMinutes(job.StartedAt, completedAt),
|
|
}
|
|
}
|
|
|
|
// ActionRunView prints a workflow run and its jobs as a single document for
|
|
// machine-readable output formats.
|
|
func ActionRunView(run *gitea.ActionWorkflowRun, jobs []*gitea.ActionWorkflowJob, output string) error {
|
|
switch output {
|
|
case "json", "yaml", "yml":
|
|
return fprintActionRunView(os.Stdout, run, jobs, output)
|
|
case "csv", "tsv":
|
|
return fmt.Errorf("workflow run view does not support %s output; use json or yaml for structured output", output)
|
|
default:
|
|
ActionRunDetails(run)
|
|
if len(jobs) > 0 {
|
|
fmt.Printf("\nJobs:\n\n")
|
|
return ActionWorkflowJobsList(jobs, output)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func fprintActionRunView(w io.Writer, run *gitea.ActionWorkflowRun, jobs []*gitea.ActionWorkflowJob, output string) error {
|
|
view := actionRunViewOutput{
|
|
Run: newActionRunOutput(run),
|
|
Jobs: make([]actionJobOutput, 0, len(jobs)),
|
|
}
|
|
for _, job := range jobs {
|
|
view.Jobs = append(view.Jobs, newActionJobOutput(job))
|
|
}
|
|
|
|
if output == "json" {
|
|
encoder := json.NewEncoder(w)
|
|
encoder.SetIndent("", " ")
|
|
return encoder.Encode(view)
|
|
}
|
|
encoder := yaml.NewEncoder(w)
|
|
if err := encoder.Encode(view); err != nil {
|
|
_ = encoder.Close()
|
|
return err
|
|
}
|
|
return encoder.Close()
|
|
}
|
|
|
|
// ActionWorkflowJobsList prints a list of workflow jobs
|
|
func ActionWorkflowJobsList(jobs []*gitea.ActionWorkflowJob, output string) error {
|
|
machineReadable := isMachineReadable(output)
|
|
var headers []string
|
|
if machineReadable {
|
|
headers = []string{
|
|
"ID",
|
|
"Name",
|
|
"Status",
|
|
"Conclusion",
|
|
"Runner",
|
|
"HeadSHA",
|
|
"StartedAt",
|
|
"CompletedAt",
|
|
"Duration",
|
|
}
|
|
} else {
|
|
headers = []string{"ID", "Name", "Status", "Runner", "Started", "Duration"}
|
|
}
|
|
t := table{headers: headers}
|
|
|
|
for _, job := range jobs {
|
|
completedAt := validCompletion(job.StartedAt, job.CompletedAt)
|
|
duration := formatDurationMinutes(job.StartedAt, completedAt)
|
|
runner := job.RunnerName
|
|
if runner == "" {
|
|
runner = "-"
|
|
}
|
|
|
|
if machineReadable {
|
|
t.addRow(
|
|
fmt.Sprintf("%d", job.ID),
|
|
job.Name,
|
|
job.Status,
|
|
job.Conclusion,
|
|
runner,
|
|
job.HeadSha,
|
|
FormatTime(job.StartedAt, true),
|
|
FormatTime(completedAt, true),
|
|
duration,
|
|
)
|
|
} else {
|
|
t.addRow(
|
|
fmt.Sprintf("%d", job.ID),
|
|
job.Name,
|
|
job.Status,
|
|
runner,
|
|
FormatTime(job.StartedAt, false),
|
|
duration,
|
|
)
|
|
}
|
|
}
|
|
|
|
if len(jobs) == 0 {
|
|
if machineReadable {
|
|
return t.print(output)
|
|
}
|
|
fmt.Printf("No jobs found\n")
|
|
return nil
|
|
}
|
|
|
|
t.sort(0, true)
|
|
return t.print(output)
|
|
}
|
|
|
|
// ActionWorkflowsList prints a list of workflows from the workflow API
|
|
func ActionWorkflowsList(workflows []*gitea.ActionWorkflow, output string) error {
|
|
t := table{
|
|
headers: []string{
|
|
"ID",
|
|
"Name",
|
|
"Path",
|
|
"State",
|
|
},
|
|
}
|
|
|
|
for _, wf := range workflows {
|
|
t.addRow(
|
|
wf.ID,
|
|
wf.Name,
|
|
wf.Path,
|
|
wf.State,
|
|
)
|
|
}
|
|
|
|
if len(workflows) == 0 {
|
|
fmt.Printf("No workflows found\n")
|
|
return nil
|
|
}
|
|
|
|
t.sort(1, true) // Sort by name column
|
|
return t.print(output)
|
|
}
|
|
|
|
// ActionWorkflowDetails prints detailed information about a workflow
|
|
func ActionWorkflowDetails(wf *gitea.ActionWorkflow) {
|
|
fmt.Printf("ID: %s\n", wf.ID)
|
|
fmt.Printf("Name: %s\n", wf.Name)
|
|
fmt.Printf("Path: %s\n", wf.Path)
|
|
fmt.Printf("State: %s\n", wf.State)
|
|
if wf.HTMLURL != "" {
|
|
fmt.Printf("URL: %s\n", wf.HTMLURL)
|
|
}
|
|
if wf.BadgeURL != "" {
|
|
fmt.Printf("Badge: %s\n", wf.BadgeURL)
|
|
}
|
|
if !wf.CreatedAt.IsZero() {
|
|
fmt.Printf("Created: %s\n", FormatTime(wf.CreatedAt, false))
|
|
}
|
|
if !wf.UpdatedAt.IsZero() {
|
|
fmt.Printf("Updated: %s\n", FormatTime(wf.UpdatedAt, false))
|
|
}
|
|
}
|
|
|
|
// ActionWorkflowDispatchResult prints the result of a workflow dispatch
|
|
func ActionWorkflowDispatchResult(details *gitea.RunDetails) {
|
|
fmt.Printf("Workflow dispatched successfully\n")
|
|
if details != nil {
|
|
fmt.Printf("Run ID: %d\n", details.WorkflowRunID)
|
|
if details.HTMLURL != "" {
|
|
fmt.Printf("URL: %s\n", details.HTMLURL)
|
|
}
|
|
}
|
|
}
|