From fe076a899a176b61ab059ac39ae894a1197a1bce Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 28 Mar 2023 00:47:46 -0400 Subject: [PATCH 01/14] add CLI command to register runner tokens --- cmd/actions.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 104 insertions(+) create mode 100644 cmd/actions.go diff --git a/cmd/actions.go b/cmd/actions.go new file mode 100644 index 0000000000000..3b3f46f8f1321 --- /dev/null +++ b/cmd/actions.go @@ -0,0 +1,103 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package cmd + +import ( + "context" + "errors" + "fmt" + "strings" + + actions_model "code.gitea.io/gitea/models/actions" + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/util" + + "github.com/urfave/cli" +) + +// Cmdembedded represents the available extract sub-command. +var ( + CmdActions = cli.Command{ + Name: "actions", + Usage: "", + Description: "Tasks for managing Gitea Actions", + Subcommands: []cli.Command{ + subcmdList, + }, + } + + subcmdList = cli.Command{ + Name: "generate-runner-token", + Usage: "Generate a new token for a runner to use to register with the server", + Action: runGenerateActionsRunnerToken, + Aliases: []string{"grt"}, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "scope, s", + Value: "", + Usage: "{owner}[/{repo}]", + }, + }, + } +) + +func runGenerateActionsRunnerToken(c *cli.Context) error { + + ctx, cancel := installSignals() + defer cancel() + + if err := initDB(ctx); err != nil { + return err + } + + scope := c.String("scope") + + owner, repo, err := parseScope(ctx, scope) + if err != nil { + return err + } + + token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo) + if errors.Is(err, util.ErrNotExist) { + token, err = actions_model.NewRunnerToken(ctx, owner, repo) + if err != nil { + return fmt.Errorf("CreateRunnerToken", err) + } + } else if err != nil { + return fmt.Errorf("GetUnactivatedRunnerToken", err) + } + + fmt.Printf("%s", token.Token) + + return nil + +} + +func parseScope(ctx context.Context, scope string) (owner, repo int64, err error) { + owner = 0 + repo = 0 + if scope == "" { + return owner, repo, nil + } + + before, after, found := strings.Cut(scope, "/") + + u, err := user_model.GetUserByName(ctx, before) + if err != nil { + return owner, repo, nil + } + + if !found { + return u.ID, repo, nil + } + + r, err := repo_model.GetRepositoryByName(u.ID, after) + if err != nil { + return owner, repo, err + } + repo = r.ID + return + +} diff --git a/main.go b/main.go index eeedf54c27419..1589fa97db434 100644 --- a/main.go +++ b/main.go @@ -75,6 +75,7 @@ arguments - which can alternatively be run by running the subcommand web.` cmd.CmdDocs, cmd.CmdDumpRepository, cmd.CmdRestoreRepository, + cmd.CmdActions, } // Now adjust these commands to add our global configuration options From a15f46e5dcb6217a2b883739bcb779ac47450371 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 28 Mar 2023 01:26:34 -0400 Subject: [PATCH 02/14] fix lint t& fmt --- cmd/actions.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/cmd/actions.go b/cmd/actions.go index 3b3f46f8f1321..089cc89e094af 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -44,7 +44,6 @@ var ( ) func runGenerateActionsRunnerToken(c *cli.Context) error { - ctx, cancel := installSignals() defer cancel() @@ -63,16 +62,15 @@ func runGenerateActionsRunnerToken(c *cli.Context) error { if errors.Is(err, util.ErrNotExist) { token, err = actions_model.NewRunnerToken(ctx, owner, repo) if err != nil { - return fmt.Errorf("CreateRunnerToken", err) + return fmt.Errorf("CreateRunnerToken: %s", err) } } else if err != nil { - return fmt.Errorf("GetUnactivatedRunnerToken", err) + return fmt.Errorf("GetUnactivatedRunnerToken: %s", err) } fmt.Printf("%s", token.Token) return nil - } func parseScope(ctx context.Context, scope string) (owner, repo int64, err error) { @@ -98,6 +96,4 @@ func parseScope(ctx context.Context, scope string) (owner, repo int64, err error return owner, repo, err } repo = r.ID - return - } From 6e9a3b1a7e5273586c5163e825b10f8554bf6f09 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 28 Mar 2023 01:31:24 -0400 Subject: [PATCH 03/14] whoops --- cmd/actions.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/actions.go b/cmd/actions.go index 089cc89e094af..db2497343259d 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -96,4 +96,5 @@ func parseScope(ctx context.Context, scope string) (owner, repo int64, err error return owner, repo, err } repo = r.ID + return owner, repo, nil } From d7546ff19cfa939750e80859d14d5d6f5b16fee7 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 28 Mar 2023 01:38:37 -0400 Subject: [PATCH 04/14] update cmd name --- cmd/actions.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/actions.go b/cmd/actions.go index db2497343259d..349fef3c1e508 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -24,11 +24,11 @@ var ( Usage: "", Description: "Tasks for managing Gitea Actions", Subcommands: []cli.Command{ - subcmdList, + subcmdActionsGenRunnerToken, }, } - subcmdList = cli.Command{ + subcmdActionsGenRunnerToken = cli.Command{ Name: "generate-runner-token", Usage: "Generate a new token for a runner to use to register with the server", Action: runGenerateActionsRunnerToken, From 26345074484923918034655f60b4b73810947718 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Thu, 6 Apr 2023 15:44:58 -0400 Subject: [PATCH 05/14] update var names per feedback --- cmd/actions.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/actions.go b/cmd/actions.go index 349fef3c1e508..d00ed22826f79 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -73,28 +73,28 @@ func runGenerateActionsRunnerToken(c *cli.Context) error { return nil } -func parseScope(ctx context.Context, scope string) (owner, repo int64, err error) { - owner = 0 - repo = 0 +func parseScope(ctx context.Context, scope string) (ownerID, repoID int64, err error) { + ownerID = 0 + repoID = 0 if scope == "" { - return owner, repo, nil + return ownerID, repoID, nil } - before, after, found := strings.Cut(scope, "/") + ownerName, repoName, found := strings.Cut(scope, "/") - u, err := user_model.GetUserByName(ctx, before) + u, err := user_model.GetUserByName(ctx, ownerName) if err != nil { - return owner, repo, nil + return ownerID, repoID, nil } if !found { - return u.ID, repo, nil + return u.ID, repoID, nil } - r, err := repo_model.GetRepositoryByName(u.ID, after) + r, err := repo_model.GetRepositoryByName(u.ID, repoName) if err != nil { - return owner, repo, err + return ownerID, repoID, err } - repo = r.ID - return owner, repo, nil + repoID = r.ID + return ownerID, repoID, nil } From cd9cb3ad8501cd1e957640b168328dd4e4733eab Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Thu, 6 Apr 2023 15:45:52 -0400 Subject: [PATCH 06/14] return err per feedback --- cmd/actions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/actions.go b/cmd/actions.go index d00ed22826f79..37f5aa9c2450d 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -84,7 +84,7 @@ func parseScope(ctx context.Context, scope string) (ownerID, repoID int64, err e u, err := user_model.GetUserByName(ctx, ownerName) if err != nil { - return ownerID, repoID, nil + return ownerID, repoID, err } if !found { From 021a33c54c9a5ec7379619845a987095ffbb3a43 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Thu, 6 Apr 2023 15:52:41 -0400 Subject: [PATCH 07/14] add docs --- .../doc/administration/command-line.en-us.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/content/doc/administration/command-line.en-us.md b/docs/content/doc/administration/command-line.en-us.md index d3362e573138b..ad932a46b8f04 100644 --- a/docs/content/doc/administration/command-line.en-us.md +++ b/docs/content/doc/administration/command-line.en-us.md @@ -551,3 +551,28 @@ Restore-repo restore repository data from disk dir: - `--owner_name lunny`: Restore destination owner name - `--repo_name tango`: Restore destination repository name - `--units `: Which items will be restored, one or more units should be separated as comma. wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units. + +### actions generate-runner-token + +Generate a new token for a runner to use to register with the server + +- Options: + - `--socpe {owner}[/{repo}]`, `-s {owner}[/{repo}]`: To limit the scope of the runner, no scope means the runner can be used for all repos, but you can also limit it to a specific repo or owner + +To register a global runner: + +``` +gitea actions generate-runner-token +``` + +To register a runner for a specific organization, in this case `org`: + +``` +gitea actions generate-runner-token -s org +``` + +To register a runner for a specific repo, in this case `username/test-repo`: + +``` +gitea actions generate-runner-token -s username/test-repo +``` From 83732fe002d3d5f273f8470e75370a59c4a0a13d Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 11 Apr 2023 10:39:41 -0400 Subject: [PATCH 08/14] Update cmd/actions.go Co-authored-by: delvh --- cmd/actions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/actions.go b/cmd/actions.go index 37f5aa9c2450d..af13c8b03f8f8 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -22,7 +22,7 @@ var ( CmdActions = cli.Command{ Name: "actions", Usage: "", - Description: "Tasks for managing Gitea Actions", + Description: "Commands for managing Gitea Actions", Subcommands: []cli.Command{ subcmdActionsGenRunnerToken, }, From 4c56aac2d9e956a43f0ab0f0af1f5ded95885483 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 11 Apr 2023 10:39:51 -0400 Subject: [PATCH 09/14] Update docs/content/doc/administration/command-line.en-us.md Co-authored-by: delvh --- docs/content/doc/administration/command-line.en-us.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/doc/administration/command-line.en-us.md b/docs/content/doc/administration/command-line.en-us.md index ad932a46b8f04..4d01d6e640ef0 100644 --- a/docs/content/doc/administration/command-line.en-us.md +++ b/docs/content/doc/administration/command-line.en-us.md @@ -557,7 +557,7 @@ Restore-repo restore repository data from disk dir: Generate a new token for a runner to use to register with the server - Options: - - `--socpe {owner}[/{repo}]`, `-s {owner}[/{repo}]`: To limit the scope of the runner, no scope means the runner can be used for all repos, but you can also limit it to a specific repo or owner + - `--scope {owner}[/{repo}]`, `-s {owner}[/{repo}]`: To limit the scope of the runner, no scope means the runner can be used for all repos, but you can also limit it to a specific repo or owner To register a global runner: From 79f307f1324357074827e9047c6c713af42a4c76 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 11 Apr 2023 10:39:58 -0400 Subject: [PATCH 10/14] Update cmd/actions.go Co-authored-by: delvh --- cmd/actions.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/actions.go b/cmd/actions.go index af13c8b03f8f8..23f14599061d9 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -62,10 +62,10 @@ func runGenerateActionsRunnerToken(c *cli.Context) error { if errors.Is(err, util.ErrNotExist) { token, err = actions_model.NewRunnerToken(ctx, owner, repo) if err != nil { - return fmt.Errorf("CreateRunnerToken: %s", err) + return fmt.Errorf("error while creating runner token: %w", err) } } else if err != nil { - return fmt.Errorf("GetUnactivatedRunnerToken: %s", err) + return fmt.Errorf("could not get unactivated runner token: %w", err) } fmt.Printf("%s", token.Token) From 3c44ae8e52aaf2eb81e8417b58957bf699763eb5 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 11 Apr 2023 11:15:01 -0400 Subject: [PATCH 11/14] move logic to internal route --- cmd/actions.go | 63 ++++--------------------- modules/private/actions.go | 27 +++++++++++ routers/private/actions.go | 91 +++++++++++++++++++++++++++++++++++++ routers/private/internal.go | 1 + 4 files changed, 128 insertions(+), 54 deletions(-) create mode 100644 modules/private/actions.go create mode 100644 routers/private/actions.go diff --git a/cmd/actions.go b/cmd/actions.go index 23f14599061d9..e26b08847e15e 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -4,21 +4,15 @@ package cmd import ( - "context" - "errors" "fmt" - "strings" - - actions_model "code.gitea.io/gitea/models/actions" - repo_model "code.gitea.io/gitea/models/repo" - user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/setting" "github.com/urfave/cli" ) -// Cmdembedded represents the available extract sub-command. var ( + // CmdActions represents the available actions sub-commands. CmdActions = cli.Command{ Name: "actions", Usage: "", @@ -47,54 +41,15 @@ func runGenerateActionsRunnerToken(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - if err := initDB(ctx); err != nil { - return err - } + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() scope := c.String("scope") - owner, repo, err := parseScope(ctx, scope) - if err != nil { - return err - } - - token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo) - if errors.Is(err, util.ErrNotExist) { - token, err = actions_model.NewRunnerToken(ctx, owner, repo) - if err != nil { - return fmt.Errorf("error while creating runner token: %w", err) - } - } else if err != nil { - return fmt.Errorf("could not get unactivated runner token: %w", err) + respText, extra := private.GenerateActionsRunnerToken(ctx, scope) + if extra.HasError() { + return handleCliResponseExtra(extra) } - - fmt.Printf("%s", token.Token) - + _, _ = fmt.Printf("%s\n", respText) return nil } - -func parseScope(ctx context.Context, scope string) (ownerID, repoID int64, err error) { - ownerID = 0 - repoID = 0 - if scope == "" { - return ownerID, repoID, nil - } - - ownerName, repoName, found := strings.Cut(scope, "/") - - u, err := user_model.GetUserByName(ctx, ownerName) - if err != nil { - return ownerID, repoID, err - } - - if !found { - return u.ID, repoID, nil - } - - r, err := repo_model.GetRepositoryByName(u.ID, repoName) - if err != nil { - return ownerID, repoID, err - } - repoID = r.ID - return ownerID, repoID, nil -} diff --git a/modules/private/actions.go b/modules/private/actions.go new file mode 100644 index 0000000000000..be24e16d3ff3f --- /dev/null +++ b/modules/private/actions.go @@ -0,0 +1,27 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package private + +import ( + "context" + + "code.gitea.io/gitea/modules/setting" +) + +// Email structure holds a data for sending general emails +type GenerateTokenRequest struct { + Scope string +} + +// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function +func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, ResponseExtra) { + reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token" + + req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{ + Scope: scope, + }) + + resp, extra := requestJSONResp(req, &responseText{}) + return resp.Text, extra +} diff --git a/routers/private/actions.go b/routers/private/actions.go new file mode 100644 index 0000000000000..b7e416f56a33b --- /dev/null +++ b/routers/private/actions.go @@ -0,0 +1,91 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package private + +import ( + "errors" + "fmt" + "net/http" + "strings" + + actions_model "code.gitea.io/gitea/models/actions" + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/util" +) + +// GenerateActionsRunnerToken generates a new runner token for a given scope +func GenerateActionsRunnerToken(ctx *context.PrivateContext) { + var genRequest private.GenerateTokenRequest + rd := ctx.Req.Body + defer rd.Close() + + if err := json.NewDecoder(rd).Decode(&genRequest); err != nil { + log.Error("%v", err) + ctx.JSON(http.StatusInternalServerError, private.Response{ + Err: err.Error(), + }) + return + } + + owner, repo, err := parseScope(ctx, genRequest.Scope) + if err != nil { + log.Error("%v", err) + ctx.JSON(http.StatusInternalServerError, private.Response{ + Err: err.Error(), + }) + } + + token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo) + if errors.Is(err, util.ErrNotExist) { + token, err = actions_model.NewRunnerToken(ctx, owner, repo) + if err != nil { + err := fmt.Sprintf("error while creating runner token: %v", err) + log.Error("%v", err) + ctx.JSON(http.StatusInternalServerError, private.Response{ + Err: err, + }) + return + } + } else if err != nil { + err := fmt.Sprintf("could not get unactivated runner token: %v", err) + log.Error("%v", err) + ctx.JSON(http.StatusInternalServerError, private.Response{ + Err: err, + }) + return + } + + ctx.PlainText(http.StatusOK, token.Token) +} + +func parseScope(ctx *context.PrivateContext, scope string) (ownerID, repoID int64, err error) { + ownerID = 0 + repoID = 0 + if scope == "" { + return ownerID, repoID, nil + } + + ownerName, repoName, found := strings.Cut(scope, "/") + + u, err := user_model.GetUserByName(ctx, ownerName) + if err != nil { + return ownerID, repoID, err + } + + if !found { + return u.ID, repoID, nil + } + + r, err := repo_model.GetRepositoryByName(u.ID, repoName) + if err != nil { + return ownerID, repoID, err + } + repoID = r.ID + return ownerID, repoID, nil +} diff --git a/routers/private/internal.go b/routers/private/internal.go index 4acede33705d6..5844245b3cee9 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -77,6 +77,7 @@ func Routes() *web.Route { r.Get("/manager/processes", Processes) r.Post("/mail/send", SendEmail) r.Post("/restore_repo", RestoreRepo) + r.Post("/generate_actions_runner_token", GenerateActionsRunnerToken) return r } From a7a096ccc00ebd3a4008d4fc2467ad89b5183158 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 11 Apr 2023 13:44:21 -0400 Subject: [PATCH 12/14] fix route typo --- routers/private/internal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/private/internal.go b/routers/private/internal.go index 5844245b3cee9..b4d32c37a6439 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -77,7 +77,7 @@ func Routes() *web.Route { r.Get("/manager/processes", Processes) r.Post("/mail/send", SendEmail) r.Post("/restore_repo", RestoreRepo) - r.Post("/generate_actions_runner_token", GenerateActionsRunnerToken) + r.Post("/actions/generate_actions_runner_token", GenerateActionsRunnerToken) return r } From 35e8a2ed132f1b3ddfb9b2dd631ad3d5189bf129 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Wed, 12 Apr 2023 16:23:09 -0400 Subject: [PATCH 13/14] fix lint --- cmd/actions.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/actions.go b/cmd/actions.go index e26b08847e15e..bf55a23c407fc 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/setting" + "github.com/urfave/cli" ) From a14ec33792a1fea85c272edb79762c5f9f10989e Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Mon, 17 Apr 2023 11:43:01 -0400 Subject: [PATCH 14/14] Update cmd/actions.go Co-authored-by: delvh --- cmd/actions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/actions.go b/cmd/actions.go index bf55a23c407fc..66ad336da508e 100644 --- a/cmd/actions.go +++ b/cmd/actions.go @@ -32,7 +32,7 @@ var ( cli.StringFlag{ Name: "scope, s", Value: "", - Usage: "{owner}[/{repo}]", + Usage: "{owner}[/{repo}] - leave empty for a global runner", }, }, }