mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-24 02:00:04 +02:00

urfave/cli v2 will eventually become unmaintained, switch over to v3 which is the latest supported version. Note: the `docs` command would be a lot of work to restore with v3 ([the package is still in alpha](https://github.com/urfave/cli-docs)) An alternative to avoid a breaking change would be to not upgrade from v2 to v3 for that reason alone. Note: these commits were cherry-picked from https://code.forgejo.org/forgefriends/forgefriends Note: it is best reviewed side by side with no display of whitespace changes (there are a lot of those when converting vars to func). - a few functional changes were necessary and are noted in context in the file changes tab - https://cli.urfave.org/migrate-v2-to-v3/ upgrade instructions were followed in the most minimal way possible - upgrade gof3 to v3.10.8 which includes and upgrade from urfave/cli v2 to urfave/cli v3 - upgrade gitlab.com/gitlab-org/api/client-go v0.129.0 because it is an indirect dependency of gof3 and requires a change because of a deprecated field that otherwise triggers a lint error but nothing else otherwise - verified that the [script](https://codeberg.org/forgejo/docs/src/branch/next/scripts/cli-docs.sh) that generates the [CLI documentation](https://codeberg.org/forgejo/docs/src/branch/next/scripts/cli-docs.sh) still works. There are cosmetic differences and the **help** subcommand is no longer advertised (although it is still supported) but the `--help` option is advertised as expected so it is fine. - end-to-end tests [passed](https://code.forgejo.org/forgejo/end-to-end/pulls/667) (they use the Forgejo CLI to some extent) ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [x] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Breaking features - [PR](https://codeberg.org/forgejo/forgejo/pulls/8035): <!--number 8035 --><!--line 0 --><!--description VGhlIGBmb3JnZWpvIGRvY3NgIGNvbW1hbmQgaXMgZGVwcmVjYXRlZCBhbmQgQ0xJIGVycm9ycyBhcmUgbm93IGRpc3BsYXllZCBvbiBzdGRlcnIgaW5zdGVhZCBvZiBzdGRvdXQuIFRoZXNlIGJyZWFraW5nIGNoYW5nZXMgaGFwcGVuZWQgYmVjYXVzZSB0aGUgcGFja2FnZSB1c2VkIHRvIHBhcnNlIHRoZSBjb21tYW5kIGxpbmUgYXJndW1lbnRzIHdhcyBbdXBncmFkZWQgZnJvbSB2MiB0byB2M10oaHR0cHM6Ly9jbGkudXJmYXZlLm9yZy9taWdyYXRlLXYyLXRvLXYzLykuIEEgW3NlcGFyYXRlIHByb2plY3Qgd2FzIGluaXRpYXRlZF0oaHR0cHM6Ly9naXRodWIuY29tL3VyZmF2ZS9jbGktZG9jcykgdG8gcmUtaW1wbGVtZW50IHRoZSBgZG9jc2AgY29tbWFuZCwgYnV0IGl0IGlzIG5vdCB5ZXQgcHJvZHVjdGlvbiByZWFkeS4=-->The `forgejo docs` command is deprecated and CLI errors are now displayed on stderr instead of stdout. These breaking changes happened because the package used to parse the command line arguments was [upgraded from v2 to v3](https://cli.urfave.org/migrate-v2-to-v3/). A [separate project was initiated](https://github.com/urfave/cli-docs) to re-implement the `docs` command, but it is not yet production ready.<!--description--> <!--end release-notes-assistant--> Co-authored-by: limiting-factor <limiting-factor@posteo.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8035 Reviewed-by: Gusted <gusted@noreply.codeberg.org>
243 lines
6.3 KiB
Go
243 lines
6.3 KiB
Go
// Copyright The Forgejo Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgejo
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
actions_model "forgejo.org/models/actions"
|
|
"forgejo.org/modules/private"
|
|
"forgejo.org/modules/setting"
|
|
private_routers "forgejo.org/routers/private"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func CmdActions(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "actions",
|
|
Usage: "Commands for managing Forgejo Actions",
|
|
Commands: []*cli.Command{
|
|
SubcmdActionsGenerateRunnerToken(ctx),
|
|
SubcmdActionsGenerateRunnerSecret(ctx),
|
|
SubcmdActionsRegister(ctx),
|
|
},
|
|
}
|
|
}
|
|
|
|
func SubcmdActionsGenerateRunnerToken(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",
|
|
Before: prepareWorkPathAndCustomConf(ctx),
|
|
Action: RunGenerateActionsRunnerToken,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "scope",
|
|
Aliases: []string{"s"},
|
|
Value: "",
|
|
Usage: "{owner}[/{repo}] - leave empty for a global runner",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func SubcmdActionsGenerateRunnerSecret(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "generate-secret",
|
|
Usage: "Generate a secret suitable for input to the register subcommand",
|
|
Action: RunGenerateSecret,
|
|
}
|
|
}
|
|
|
|
func SubcmdActionsRegister(ctx context.Context) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "register",
|
|
Usage: "Idempotent registration of a runner using a shared secret",
|
|
Before: prepareWorkPathAndCustomConf(ctx),
|
|
Action: RunRegister,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "secret",
|
|
Usage: "the secret the runner will use to connect as a 40 character hexadecimal string",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "secret-stdin",
|
|
Usage: "the secret the runner will use to connect as a 40 character hexadecimal string, read from stdin",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "secret-file",
|
|
Usage: "path to the file containing the secret the runner will use to connect as a 40 character hexadecimal string",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "scope",
|
|
Aliases: []string{"s"},
|
|
Value: "",
|
|
Usage: "{owner}[/{repo}] - leave empty for a global runner",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "labels",
|
|
Value: "",
|
|
Usage: "comma separated list of labels supported by the runner (e.g. docker,ubuntu-latest,self-hosted) (not required since v1.21)",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "keep-labels",
|
|
Value: false,
|
|
Usage: "do not affect the labels when updating an existing runner",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "name",
|
|
Value: "runner",
|
|
Usage: "name of the runner (default runner)",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "version",
|
|
Value: "",
|
|
Usage: "version of the runner (not required since v1.21)",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func readSecret(ctx context.Context, cli *cli.Command) (string, error) {
|
|
if cli.IsSet("secret") {
|
|
return cli.String("secret"), nil
|
|
}
|
|
if cli.IsSet("secret-stdin") {
|
|
buf, err := io.ReadAll(ContextGetStdin(ctx))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(buf), nil
|
|
}
|
|
if cli.IsSet("secret-file") {
|
|
path := cli.String("secret-file")
|
|
buf, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(buf), nil
|
|
}
|
|
return "", errors.New("at least one of the --secret, --secret-stdin, --secret-file options is required")
|
|
}
|
|
|
|
func validateSecret(secret string) error {
|
|
secretLen := len(secret)
|
|
if secretLen != 40 {
|
|
return fmt.Errorf("the secret must be exactly 40 characters long, not %d: generate-secret can provide a secret matching the requirements", secretLen)
|
|
}
|
|
if _, err := hex.DecodeString(secret); err != nil {
|
|
return fmt.Errorf("the secret must be an hexadecimal string: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getLabels(cli *cli.Command) (*[]string, error) {
|
|
if !cli.Bool("keep-labels") {
|
|
lblValue := strings.Split(cli.String("labels"), ",")
|
|
return &lblValue, nil
|
|
}
|
|
if cli.String("labels") != "" {
|
|
return nil, errors.New("--labels and --keep-labels should not be used together")
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func RunRegister(ctx context.Context, cli *cli.Command) error {
|
|
var cancel context.CancelFunc
|
|
if !ContextGetNoInit(ctx) {
|
|
ctx, cancel = installSignals(ctx)
|
|
defer cancel()
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
setting.MustInstalled()
|
|
|
|
secret, err := readSecret(ctx, cli)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := validateSecret(secret); err != nil {
|
|
return err
|
|
}
|
|
scope := cli.String("scope")
|
|
name := cli.String("name")
|
|
version := cli.String("version")
|
|
labels, err := getLabels(cli)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
//
|
|
// There are two kinds of tokens
|
|
//
|
|
// - "registration token" only used when a runner interacts to
|
|
// register
|
|
//
|
|
// - "token" obtained after a successful registration and stored by
|
|
// the runner to authenticate
|
|
//
|
|
// The register subcommand does not need a "registration token", it
|
|
// needs a "token". Using the same name is confusing and secret is
|
|
// preferred for this reason in the cli.
|
|
//
|
|
// The ActionsRunnerRegister argument is token to be consistent with
|
|
// the internal naming. It is still confusing to the developer but
|
|
// not to the user.
|
|
//
|
|
owner, repo, err := private_routers.ParseScope(ctx, scope)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
runner, err := actions_model.RegisterRunner(ctx, owner, repo, secret, labels, name, version)
|
|
if err != nil {
|
|
return fmt.Errorf("error while registering runner: %v", err)
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", runner.UUID); err != nil {
|
|
panic(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func RunGenerateSecret(ctx context.Context, cli *cli.Command) error {
|
|
runner := actions_model.ActionRunner{}
|
|
if err := runner.GenerateToken(); err != nil {
|
|
return err
|
|
}
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", runner.Token); err != nil {
|
|
panic(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func RunGenerateActionsRunnerToken(ctx context.Context, cli *cli.Command) error {
|
|
if !ContextGetNoInit(ctx) {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = installSignals(ctx)
|
|
defer cancel()
|
|
}
|
|
|
|
setting.MustInstalled()
|
|
|
|
scope := cli.String("scope")
|
|
|
|
respText, extra := private.GenerateActionsRunnerToken(ctx, scope)
|
|
if extra.HasError() {
|
|
return handleCliResponseExtra(ctx, extra)
|
|
}
|
|
if _, err := fmt.Fprintf(ContextGetStdout(ctx), "%s", respText.Text); err != nil {
|
|
panic(err)
|
|
}
|
|
return nil
|
|
}
|