From b6cfa88c6e2ae00e30c832ce4cf93c9e3f2cd6e4 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sun, 9 Jul 2023 14:52:41 +0200 Subject: [PATCH] [CLI] implement forgejo-cli actions generate-runner-token (cherry picked from commit 08be2b226e46d9f41e08f66e936b317bcfb4a257) --- cmd/forgejo/actions.go | 60 +++++++++++++++++++ cmd/forgejo/forgejo.go | 10 ++-- tests/integration/cmd_forgejo_actions_test.go | 22 +++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 cmd/forgejo/actions.go create mode 100644 tests/integration/cmd_forgejo_actions_test.go diff --git a/cmd/forgejo/actions.go b/cmd/forgejo/actions.go new file mode 100644 index 0000000000..1262a02584 --- /dev/null +++ b/cmd/forgejo/actions.go @@ -0,0 +1,60 @@ +// Copyright The Forgejo Authors. +// SPDX-License-Identifier: MIT + +package forgejo + +import ( + "context" + "fmt" + + "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/setting" + + "github.com/urfave/cli" +) + +func CmdActions(ctx context.Context) cli.Command { + return cli.Command{ + Name: "actions", + Usage: "Commands for managing Forgejo Actions", + Subcommands: []cli.Command{ + SubcmdActionsGenRunnerToken(ctx), + }, + } +} + +func SubcmdActionsGenRunnerToken(ctx context.Context) cli.Command { + return cli.Command{ + Name: "generate-runner-token", + Usage: "Generate a new token for a runner to use to register with the server", + Action: func(cliCtx *cli.Context) error { return RunGenerateActionsRunnerToken(ctx, cliCtx) }, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "scope, s", + Value: "", + Usage: "{owner}[/{repo}] - leave empty for a global runner", + }, + }, + } +} + +func RunGenerateActionsRunnerToken(ctx context.Context, cliCtx *cli.Context) error { + if !ContextGetNoInstallSignals(ctx) { + var cancel context.CancelFunc + ctx, cancel = installSignals(ctx) + defer cancel() + } + + setting.MustInstalled() + + scope := cliCtx.String("scope") + + respText, extra := private.GenerateActionsRunnerToken(ctx, scope) + if extra.HasError() { + return handleCliResponseExtra(extra) + } + if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", respText); err != nil { + panic(err) + } + return nil +} diff --git a/cmd/forgejo/forgejo.go b/cmd/forgejo/forgejo.go index 07953e30c8..f9cbe38a4d 100644 --- a/cmd/forgejo/forgejo.go +++ b/cmd/forgejo/forgejo.go @@ -28,10 +28,12 @@ const ( func CmdForgejo(ctx context.Context) cli.Command { return cli.Command{ - Name: "forgejo-cli", - Usage: "Forgejo CLI", - Flags: []cli.Flag{}, - Subcommands: []cli.Command{}, + Name: "forgejo-cli", + Usage: "Forgejo CLI", + Flags: []cli.Flag{}, + Subcommands: []cli.Command{ + CmdActions(ctx), + }, } } diff --git a/tests/integration/cmd_forgejo_actions_test.go b/tests/integration/cmd_forgejo_actions_test.go new file mode 100644 index 0000000000..9767d03675 --- /dev/null +++ b/tests/integration/cmd_forgejo_actions_test.go @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT + +package integration + +import ( + "net/url" + "testing" + + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" + + "github.com/stretchr/testify/assert" +) + +func Test_CmdForgejo_Actions(t *testing.T) { + onGiteaRun(t, func(*testing.T, *url.URL) { + defer test.MockVariable(&setting.Actions.Enabled, true)() + + output := cmdForgejoCaptureOutput(t, []string{"forgejo-cli", "actions", "generate-runner-token"}) + assert.EqualValues(t, 40, len(output)) + }) +}