feat(actions): add workflow-run cancel command

Signed-off-by: Noel <n@noeljackson.com>
This commit is contained in:
Noel 2026-06-29 16:44:30 +02:00
parent f34697c5ed
commit d271eb767d
No known key found for this signature in database
GPG key ID: ACB59B76E29853EA
5 changed files with 131 additions and 6 deletions

View file

@ -21,6 +21,7 @@ var CmdActionsRuns = cli.Command{
Commands: []*cli.Command{
&runs.CmdRunsList,
&runs.CmdRunsView,
&runs.CmdRunsCancel,
&runs.CmdRunsDelete,
&runs.CmdRunsLogs,
},

View file

@ -0,0 +1,64 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package runs
import (
stdctx "context"
"fmt"
"strconv"
gitea "gitea.dev/sdk"
"gitea.dev/tea/cmd/flags"
"gitea.dev/tea/modules/context"
"github.com/urfave/cli/v3"
)
type actionsRunCancelService interface {
CancelRepoRun(ctx stdctx.Context, owner, repo string, runID int64) (*gitea.ActionsWorkflowRun, *gitea.Response, error)
}
// CmdRunsCancel represents a sub command to cancel a workflow run.
var CmdRunsCancel = cli.Command{
Name: "cancel",
Usage: "Cancel a workflow run",
Description: "Request cancellation of a queued or running workflow run",
ArgsUsage: "<run-id>",
Action: runRunsCancel,
Flags: flags.AllDefaultFlags,
}
func runRunsCancel(ctx stdctx.Context, cmd *cli.Command) error {
if cmd.Args().Len() == 0 {
return fmt.Errorf("run ID is required")
}
c, err := context.InitCommand(cmd)
if err != nil {
return err
}
if err := c.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
return err
}
runIDText := cmd.Args().First()
runID, err := strconv.ParseInt(runIDText, 10, 64)
if err != nil {
return fmt.Errorf("invalid run ID: %s", runIDText)
}
if err := cancelRun(ctx, c.Login.Client().Actions, c.Owner, c.Repo, runID); err != nil {
return err
}
fmt.Printf("Cancellation requested for run %d\n", runID)
return nil
}
func cancelRun(ctx stdctx.Context, client actionsRunCancelService, owner, repo string, runID int64) error {
if _, _, err := client.CancelRepoRun(ctx, owner, repo, runID); err != nil {
return fmt.Errorf("failed to cancel run %d: %w", runID, err)
}
return nil
}

View file

@ -0,0 +1,48 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package runs
import (
stdctx "context"
"errors"
"testing"
gitea "gitea.dev/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeActionsRunCancelService struct {
owner string
repo string
runID int64
err error
}
func (f *fakeActionsRunCancelService) CancelRepoRun(_ stdctx.Context, owner, repo string, runID int64) (*gitea.ActionsWorkflowRun, *gitea.Response, error) {
f.owner = owner
f.repo = repo
f.runID = runID
return &gitea.ActionsWorkflowRun{ID: runID}, nil, f.err
}
func TestCancelRun(t *testing.T) {
t.Parallel()
client := &fakeActionsRunCancelService{}
require.NoError(t, cancelRun(t.Context(), client, "owner", "repo", 12))
assert.Equal(t, "owner", client.owner)
assert.Equal(t, "repo", client.repo)
assert.EqualValues(t, 12, client.runID)
}
func TestCancelRunError(t *testing.T) {
t.Parallel()
client := &fakeActionsRunCancelService{err: errors.New("conflict")}
err := cancelRun(t.Context(), client, "owner", "repo", 12)
require.ErrorContains(t, err, "failed to cancel run 12")
assert.ErrorContains(t, err, "conflict")
}

View file

@ -14,12 +14,12 @@ import (
"github.com/urfave/cli/v3"
)
// CmdRunsDelete represents a sub command to delete/cancel workflow runs
// CmdRunsDelete represents a sub command to delete 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",
Aliases: []string{"remove", "rm"},
Usage: "Delete a workflow run",
Description: "Delete a completed workflow run from the repository",
ArgsUsage: "<run-id>",
Action: runRunsDelete,
Flags: append([]cli.Flag{

View file

@ -1607,9 +1607,21 @@ View workflow run details
**--repo, -r**="": Override local repository path or gitea repository slug to interact with. Optional
#### delete, remove, rm, cancel
#### cancel
Delete or cancel a workflow run
Cancel a workflow run
**--login, -l**="": Use a different Gitea Login. Optional
**--output, -o**="": Output format. (simple, table, csv, tsv, yaml, json)
**--remote, -R**="": Discover Gitea login from remote. Optional
**--repo, -r**="": Override local repository path or gitea repository slug to interact with. Optional
#### delete, remove, rm
Delete a workflow run
**--confirm, -y**: confirm deletion without prompting