mirror of
https://gitea.com/gitea/tea.git
synced 2026-02-22 06:13:32 +01:00
Add tea actions runs and workflows commands (#880)
Implements comprehensive workflow execution tracking for Gitea Actions using tea CLI ## Features ### tea actions runs list - List workflow runs with filtering (status, branch, event, actor, time) - Time filters: relative (24h, 7d) and absolute dates - Status symbols: ✓ success, ✘ failure, ⭮ pending, ⊘ skipped/cancelled, ⚠ blocked - Multiple output formats: table, json, yaml, csv, tsv ### tea actions runs view - View run details with metadata (ID, status, workflow, branch, event, trigger info) - Shows jobs table with status, runner, duration - Optional --jobs flag to toggle jobs display ### tea actions runs delete - Delete/cancel workflow runs with confirmation prompt - Supports --confirm/-y to skip prompt ### tea actions runs logs - View job logs for all jobs or specific job (--job <id>) - **New: --follow/-f flag for real-time log following** (like tail -f) - Polls API every 2 seconds, only shows new content - Auto-detects completion and exits ### tea actions workflows list - List workflow files (.yml and .yaml) in repository - Searches in .gitea/workflows and .github/workflows - Shows active (✓) or inactive (✗) status based on recent runs - Displays workflow name, path, and file size ## Commands `tea actions runs list --status success --since 24h` `tea actions runs view 123` `tea actions runs delete 123 --confirm` `tea actions runs logs 123 --job 456 --follow` `tea actions workflows list` ## Tests - 19 unit tests across all commands - Full test suite passing - Manual testing successful --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/880 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: yousfi saad <yousfi.saad@gmail.com> Co-committed-by: yousfi saad <yousfi.saad@gmail.com>
This commit is contained in:
31
cmd/actions/runs.go
Normal file
31
cmd/actions/runs.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
|
||||
"code.gitea.io/tea/cmd/actions/runs"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdActionsRuns represents the actions runs command
|
||||
var CmdActionsRuns = cli.Command{
|
||||
Name: "runs",
|
||||
Aliases: []string{"run"},
|
||||
Usage: "Manage workflow runs",
|
||||
Description: "List, view, and manage workflow runs for repository actions",
|
||||
Action: runRunsDefault,
|
||||
Commands: []*cli.Command{
|
||||
&runs.CmdRunsList,
|
||||
&runs.CmdRunsView,
|
||||
&runs.CmdRunsDelete,
|
||||
&runs.CmdRunsLogs,
|
||||
},
|
||||
}
|
||||
|
||||
func runRunsDefault(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
return runs.RunRunsList(ctx, cmd)
|
||||
}
|
||||
65
cmd/actions/runs/delete.go
Normal file
65
cmd/actions/runs/delete.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package runs
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdRunsDelete represents a sub command to delete/cancel workflow runs
|
||||
var CmdRunsDelete = cli.Command{
|
||||
Name: "delete",
|
||||
Aliases: []string{"remove", "rm", "cancel"},
|
||||
Usage: "Delete or cancel a workflow run",
|
||||
Description: "Delete (cancel) a workflow run from the repository",
|
||||
ArgsUsage: "<run-id>",
|
||||
Action: runRunsDelete,
|
||||
Flags: append([]cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "confirm",
|
||||
Aliases: []string{"y"},
|
||||
Usage: "confirm deletion without prompting",
|
||||
},
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
func runRunsDelete(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
if cmd.Args().Len() == 0 {
|
||||
return fmt.Errorf("run ID is required")
|
||||
}
|
||||
|
||||
c := context.InitCommand(cmd)
|
||||
client := c.Login.Client()
|
||||
|
||||
runIDStr := cmd.Args().First()
|
||||
runID, err := strconv.ParseInt(runIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid run ID: %s", runIDStr)
|
||||
}
|
||||
|
||||
if !cmd.Bool("confirm") {
|
||||
fmt.Printf("Are you sure you want to delete run %d? [y/N] ", runID)
|
||||
var response string
|
||||
fmt.Scanln(&response)
|
||||
if response != "y" && response != "Y" && response != "yes" {
|
||||
fmt.Println("Deletion canceled.")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
_, err = client.DeleteRepoActionRun(c.Owner, c.Repo, runID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete run: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Run %d deleted successfully\n", runID)
|
||||
return nil
|
||||
}
|
||||
144
cmd/actions/runs/list.go
Normal file
144
cmd/actions/runs/list.go
Normal file
@@ -0,0 +1,144 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package runs
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/print"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdRunsList represents a sub command to list workflow runs
|
||||
var CmdRunsList = cli.Command{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Usage: "List workflow runs",
|
||||
Description: "List workflow runs for repository actions with optional filtering",
|
||||
Action: RunRunsList,
|
||||
Flags: append([]cli.Flag{
|
||||
&flags.PaginationPageFlag,
|
||||
&flags.PaginationLimitFlag,
|
||||
&cli.StringFlag{
|
||||
Name: "status",
|
||||
Usage: "Filter by status (success, failure, pending, queued, in_progress, skipped, canceled)",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "branch",
|
||||
Usage: "Filter by branch name",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "event",
|
||||
Usage: "Filter by event type (push, pull_request, etc.)",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "actor",
|
||||
Usage: "Filter by actor username (who triggered the run)",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "since",
|
||||
Usage: "Show runs started after this time (e.g., '24h', '2024-01-01')",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "until",
|
||||
Usage: "Show runs started before this time (e.g., '2024-01-01')",
|
||||
},
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
// parseTimeFlag parses time flags like "24h" or "2024-01-01"
|
||||
func parseTimeFlag(value string) (time.Time, error) {
|
||||
if value == "" {
|
||||
return time.Time{}, nil
|
||||
}
|
||||
|
||||
// Try parsing as duration (e.g., "24h", "168h")
|
||||
if duration, err := time.ParseDuration(value); err == nil {
|
||||
return time.Now().Add(-duration), nil
|
||||
}
|
||||
|
||||
// Try parsing as date
|
||||
formats := []string{
|
||||
"2006-01-02",
|
||||
"2006-01-02 15:04",
|
||||
"2006-01-02T15:04:05",
|
||||
time.RFC3339,
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
if t, err := time.Parse(format, value); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
|
||||
return time.Time{}, fmt.Errorf("unable to parse time: %s", value)
|
||||
}
|
||||
|
||||
// RunRunsList lists workflow runs
|
||||
func RunRunsList(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
c := context.InitCommand(cmd)
|
||||
client := c.Login.Client()
|
||||
|
||||
// Parse time filters
|
||||
since, err := parseTimeFlag(cmd.String("since"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid --since value: %w", err)
|
||||
}
|
||||
|
||||
until, err := parseTimeFlag(cmd.String("until"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid --until value: %w", err)
|
||||
}
|
||||
|
||||
// Build list options
|
||||
listOpts := flags.GetListOptions()
|
||||
|
||||
runs, _, err := client.ListRepoActionRuns(c.Owner, c.Repo, gitea.ListRepoActionRunsOptions{
|
||||
ListOptions: listOpts,
|
||||
Status: cmd.String("status"),
|
||||
Branch: cmd.String("branch"),
|
||||
Event: cmd.String("event"),
|
||||
Actor: cmd.String("actor"),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if runs == nil {
|
||||
print.ActionRunsList(nil, c.Output)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Filter by time if specified
|
||||
filteredRuns := filterRunsByTime(runs.WorkflowRuns, since, until)
|
||||
|
||||
print.ActionRunsList(filteredRuns, c.Output)
|
||||
return nil
|
||||
}
|
||||
|
||||
// filterRunsByTime filters runs based on time range
|
||||
func filterRunsByTime(runs []*gitea.ActionWorkflowRun, since, until time.Time) []*gitea.ActionWorkflowRun {
|
||||
if since.IsZero() && until.IsZero() {
|
||||
return runs
|
||||
}
|
||||
|
||||
var filtered []*gitea.ActionWorkflowRun
|
||||
for _, run := range runs {
|
||||
if !since.IsZero() && run.StartedAt.Before(since) {
|
||||
continue
|
||||
}
|
||||
if !until.IsZero() && run.StartedAt.After(until) {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, run)
|
||||
}
|
||||
|
||||
return filtered
|
||||
}
|
||||
77
cmd/actions/runs/list_test.go
Normal file
77
cmd/actions/runs/list_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package runs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
func TestFilterRunsByTime(t *testing.T) {
|
||||
now := time.Now()
|
||||
runs := []*gitea.ActionWorkflowRun{
|
||||
{ID: 1, StartedAt: now.Add(-1 * time.Hour)},
|
||||
{ID: 2, StartedAt: now.Add(-2 * time.Hour)},
|
||||
{ID: 3, StartedAt: now.Add(-3 * time.Hour)},
|
||||
{ID: 4, StartedAt: now.Add(-4 * time.Hour)},
|
||||
{ID: 5, StartedAt: now.Add(-5 * time.Hour)},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
since time.Time
|
||||
until time.Time
|
||||
expected []int64
|
||||
}{
|
||||
{
|
||||
name: "no filter",
|
||||
since: time.Time{},
|
||||
until: time.Time{},
|
||||
expected: []int64{1, 2, 3, 4, 5},
|
||||
},
|
||||
{
|
||||
name: "since 2.5 hours ago",
|
||||
since: now.Add(-150 * time.Minute),
|
||||
until: time.Time{},
|
||||
expected: []int64{1, 2},
|
||||
},
|
||||
{
|
||||
name: "until 2.5 hours ago",
|
||||
since: time.Time{},
|
||||
until: now.Add(-150 * time.Minute),
|
||||
expected: []int64{3, 4, 5},
|
||||
},
|
||||
{
|
||||
name: "between 2 and 4 hours ago",
|
||||
since: now.Add(-4 * time.Hour),
|
||||
until: now.Add(-2 * time.Hour),
|
||||
expected: []int64{2, 3, 4},
|
||||
},
|
||||
{
|
||||
name: "filter excludes all",
|
||||
since: now.Add(-30 * time.Minute),
|
||||
until: time.Time{},
|
||||
expected: []int64{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := filterRunsByTime(runs, tt.since, tt.until)
|
||||
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("filterRunsByTime() returned %d runs, want %d", len(result), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, run := range result {
|
||||
if run.ID != tt.expected[i] {
|
||||
t.Errorf("filterRunsByTime()[%d].ID = %d, want %d", i, run.ID, tt.expected[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
169
cmd/actions/runs/logs.go
Normal file
169
cmd/actions/runs/logs.go
Normal file
@@ -0,0 +1,169 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package runs
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdRunsLogs represents a sub command to view workflow run logs
|
||||
var CmdRunsLogs = cli.Command{
|
||||
Name: "logs",
|
||||
Aliases: []string{"log"},
|
||||
Usage: "View workflow run logs",
|
||||
Description: "View logs for a workflow run or specific job",
|
||||
ArgsUsage: "<run-id>",
|
||||
Action: runRunsLogs,
|
||||
Flags: append([]cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "job",
|
||||
Usage: "specific job ID to view logs for (if omitted, shows all jobs)",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "follow",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "follow log output (like tail -f), requires job to be in progress",
|
||||
},
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
func runRunsLogs(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
if cmd.Args().Len() == 0 {
|
||||
return fmt.Errorf("run ID is required")
|
||||
}
|
||||
|
||||
c := context.InitCommand(cmd)
|
||||
client := c.Login.Client()
|
||||
|
||||
runIDStr := cmd.Args().First()
|
||||
runID, err := strconv.ParseInt(runIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid run ID: %s", runIDStr)
|
||||
}
|
||||
|
||||
// Check if follow mode is enabled
|
||||
follow := cmd.Bool("follow")
|
||||
|
||||
// If specific job ID provided, fetch only that job's logs
|
||||
jobIDStr := cmd.String("job")
|
||||
if jobIDStr != "" {
|
||||
jobID, err := strconv.ParseInt(jobIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid job ID: %s", jobIDStr)
|
||||
}
|
||||
|
||||
if follow {
|
||||
return followJobLogs(client, c, jobID, "")
|
||||
}
|
||||
|
||||
logs, _, err := client.GetRepoActionJobLogs(c.Owner, c.Repo, jobID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get logs for job %d: %w", jobID, err)
|
||||
}
|
||||
|
||||
fmt.Printf("Logs for job %d:\n", jobID)
|
||||
fmt.Printf("---\n%s\n", string(logs))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Otherwise, fetch all jobs and their logs
|
||||
jobs, _, err := client.ListRepoActionRunJobs(c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{
|
||||
ListOptions: flags.GetListOptions(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get jobs: %w", err)
|
||||
}
|
||||
|
||||
if len(jobs.Jobs) == 0 {
|
||||
fmt.Printf("No jobs found for run %d\n", runID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// If following and multiple jobs, require --job flag
|
||||
if follow && len(jobs.Jobs) > 1 {
|
||||
return fmt.Errorf("--follow requires --job when run has multiple jobs (found %d jobs)", len(jobs.Jobs))
|
||||
}
|
||||
|
||||
// If following with single job, follow it
|
||||
if follow && len(jobs.Jobs) == 1 {
|
||||
return followJobLogs(client, c, jobs.Jobs[0].ID, jobs.Jobs[0].Name)
|
||||
}
|
||||
|
||||
// Fetch logs for each job
|
||||
for i, job := range jobs.Jobs {
|
||||
if i > 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
fmt.Printf("Job: %s (ID: %d)\n", job.Name, job.ID)
|
||||
fmt.Printf("Status: %s\n", job.Status)
|
||||
fmt.Println("---")
|
||||
|
||||
logs, _, err := client.GetRepoActionJobLogs(c.Owner, c.Repo, job.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("Error fetching logs: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(string(logs))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// followJobLogs continuously fetches and displays logs for a running job
|
||||
func followJobLogs(client *gitea.Client, c *context.TeaContext, jobID int64, jobName string) error {
|
||||
var lastLogLength int
|
||||
|
||||
if jobName != "" {
|
||||
fmt.Printf("Following logs for job '%s' (ID: %d) - press Ctrl+C to stop...\n", jobName, jobID)
|
||||
} else {
|
||||
fmt.Printf("Following logs for job %d (press Ctrl+C to stop)...\n", jobID)
|
||||
}
|
||||
fmt.Println("---")
|
||||
|
||||
for {
|
||||
// Fetch job status
|
||||
job, _, err := client.GetRepoActionJob(c.Owner, c.Repo, jobID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get job: %w", err)
|
||||
}
|
||||
|
||||
// Check if job is still running
|
||||
isRunning := job.Status == "in_progress" || job.Status == "queued" || job.Status == "pending"
|
||||
|
||||
// Fetch logs
|
||||
logs, _, err := client.GetRepoActionJobLogs(c.Owner, c.Repo, jobID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get logs: %w", err)
|
||||
}
|
||||
|
||||
// Display new content only
|
||||
if len(logs) > lastLogLength {
|
||||
newLogs := string(logs)[lastLogLength:]
|
||||
fmt.Print(newLogs)
|
||||
lastLogLength = len(logs)
|
||||
}
|
||||
|
||||
// If job is complete, exit
|
||||
if !isRunning {
|
||||
fmt.Printf("\n---\nJob completed with status: %s\n", job.Status)
|
||||
break
|
||||
}
|
||||
|
||||
// Wait before next poll
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
75
cmd/actions/runs/view.go
Normal file
75
cmd/actions/runs/view.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package runs
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/print"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdRunsView represents a sub command to view workflow run details
|
||||
var CmdRunsView = cli.Command{
|
||||
Name: "view",
|
||||
Aliases: []string{"show", "get"},
|
||||
Usage: "View workflow run details",
|
||||
Description: "View details of a specific workflow run including jobs",
|
||||
ArgsUsage: "<run-id>",
|
||||
Action: runRunsView,
|
||||
Flags: append([]cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "jobs",
|
||||
Usage: "show jobs table",
|
||||
Value: true,
|
||||
},
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
func runRunsView(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
if cmd.Args().Len() == 0 {
|
||||
return fmt.Errorf("run ID is required")
|
||||
}
|
||||
|
||||
c := context.InitCommand(cmd)
|
||||
client := c.Login.Client()
|
||||
|
||||
runIDStr := cmd.Args().First()
|
||||
runID, err := strconv.ParseInt(runIDStr, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid run ID: %s", runIDStr)
|
||||
}
|
||||
|
||||
// Fetch run details
|
||||
run, _, err := client.GetRepoActionRun(c.Owner, c.Repo, runID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get run: %w", err)
|
||||
}
|
||||
|
||||
// Print run details
|
||||
print.ActionRunDetails(run)
|
||||
|
||||
// Fetch and print jobs if requested
|
||||
if cmd.Bool("jobs") {
|
||||
jobs, _, err := client.ListRepoActionRunJobs(c.Owner, c.Repo, runID, gitea.ListRepoActionJobsOptions{
|
||||
ListOptions: flags.GetListOptions(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get jobs: %w", err)
|
||||
}
|
||||
|
||||
if jobs != nil && len(jobs.Jobs) > 0 {
|
||||
fmt.Printf("\nJobs:\n\n")
|
||||
print.ActionWorkflowJobsList(jobs.Jobs, c.Output)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
28
cmd/actions/workflows.go
Normal file
28
cmd/actions/workflows.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
|
||||
"code.gitea.io/tea/cmd/actions/workflows"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdActionsWorkflows represents the actions workflows command
|
||||
var CmdActionsWorkflows = cli.Command{
|
||||
Name: "workflows",
|
||||
Aliases: []string{"workflow"},
|
||||
Usage: "Manage repository workflows",
|
||||
Description: "List and manage repository action workflows",
|
||||
Action: runWorkflowsDefault,
|
||||
Commands: []*cli.Command{
|
||||
&workflows.CmdWorkflowsList,
|
||||
},
|
||||
}
|
||||
|
||||
func runWorkflowsDefault(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
return workflows.RunWorkflowsList(ctx, cmd)
|
||||
}
|
||||
86
cmd/actions/workflows/list.go
Normal file
86
cmd/actions/workflows/list.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package workflows
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/print"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdWorkflowsList represents a sub command to list workflows
|
||||
var CmdWorkflowsList = cli.Command{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Usage: "List repository workflows",
|
||||
Description: "List workflow files in the repository with active/inactive status",
|
||||
Action: RunWorkflowsList,
|
||||
Flags: append([]cli.Flag{
|
||||
&flags.PaginationPageFlag,
|
||||
&flags.PaginationLimitFlag,
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
// RunWorkflowsList lists workflow files in the repository
|
||||
func RunWorkflowsList(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
c := context.InitCommand(cmd)
|
||||
client := c.Login.Client()
|
||||
|
||||
// Try to list workflow files from .gitea/workflows directory
|
||||
var workflows []*gitea.ContentsResponse
|
||||
|
||||
// Try .gitea/workflows first, then .github/workflows
|
||||
workflowDir := ".gitea/workflows"
|
||||
contents, _, err := client.ListContents(c.Owner, c.Repo, "", workflowDir)
|
||||
if err != nil {
|
||||
workflowDir = ".github/workflows"
|
||||
contents, _, err = client.ListContents(c.Owner, c.Repo, "", workflowDir)
|
||||
if err != nil {
|
||||
fmt.Printf("No workflow files found\n")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Filter for workflow files (.yml and .yaml)
|
||||
for _, content := range contents {
|
||||
if content.Type == "file" {
|
||||
ext := strings.ToLower(filepath.Ext(content.Name))
|
||||
if ext == ".yml" || ext == ".yaml" {
|
||||
content.Path = workflowDir + "/" + content.Name
|
||||
workflows = append(workflows, content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(workflows) == 0 {
|
||||
fmt.Printf("No workflow files found\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check which workflows have runs to determine active status
|
||||
workflowStatus := make(map[string]bool)
|
||||
|
||||
// Get recent runs to check activity
|
||||
runs, _, err := client.ListRepoActionRuns(c.Owner, c.Repo, gitea.ListRepoActionRunsOptions{
|
||||
ListOptions: flags.GetListOptions(),
|
||||
})
|
||||
if err == nil && runs != nil {
|
||||
for _, run := range runs.WorkflowRuns {
|
||||
// Extract workflow file name from path
|
||||
workflowFile := filepath.Base(run.Path)
|
||||
workflowStatus[workflowFile] = true
|
||||
}
|
||||
}
|
||||
|
||||
print.WorkflowsList(workflows, workflowStatus, c.Output)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user