Skip to content

Commit 4014200

Browse files
add CLI command to register runner tokens (#23762)
This is a CLI command to generate new tokens for the runners to register with Fix #23643 --------- Co-authored-by: delvh <dev.lh@web.de>
1 parent 1819c4b commit 4014200

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

cmd/actions.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
9+
"code.gitea.io/gitea/modules/private"
10+
"code.gitea.io/gitea/modules/setting"
11+
12+
"github.com/urfave/cli"
13+
)
14+
15+
var (
16+
// CmdActions represents the available actions sub-commands.
17+
CmdActions = cli.Command{
18+
Name: "actions",
19+
Usage: "",
20+
Description: "Commands for managing Gitea Actions",
21+
Subcommands: []cli.Command{
22+
subcmdActionsGenRunnerToken,
23+
},
24+
}
25+
26+
subcmdActionsGenRunnerToken = cli.Command{
27+
Name: "generate-runner-token",
28+
Usage: "Generate a new token for a runner to use to register with the server",
29+
Action: runGenerateActionsRunnerToken,
30+
Aliases: []string{"grt"},
31+
Flags: []cli.Flag{
32+
cli.StringFlag{
33+
Name: "scope, s",
34+
Value: "",
35+
Usage: "{owner}[/{repo}] - leave empty for a global runner",
36+
},
37+
},
38+
}
39+
)
40+
41+
func runGenerateActionsRunnerToken(c *cli.Context) error {
42+
ctx, cancel := installSignals()
43+
defer cancel()
44+
45+
setting.InitProviderFromExistingFile()
46+
setting.LoadCommonSettings()
47+
48+
scope := c.String("scope")
49+
50+
respText, extra := private.GenerateActionsRunnerToken(ctx, scope)
51+
if extra.HasError() {
52+
return handleCliResponseExtra(extra)
53+
}
54+
_, _ = fmt.Printf("%s\n", respText)
55+
return nil
56+
}

docs/content/doc/administration/command-line.en-us.md

+25
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,28 @@ Restore-repo restore repository data from disk dir:
551551
- `--owner_name lunny`: Restore destination owner name
552552
- `--repo_name tango`: Restore destination repository name
553553
- `--units <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.
554+
555+
### actions generate-runner-token
556+
557+
Generate a new token for a runner to use to register with the server
558+
559+
- Options:
560+
- `--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
561+
562+
To register a global runner:
563+
564+
```
565+
gitea actions generate-runner-token
566+
```
567+
568+
To register a runner for a specific organization, in this case `org`:
569+
570+
```
571+
gitea actions generate-runner-token -s org
572+
```
573+
574+
To register a runner for a specific repo, in this case `username/test-repo`:
575+
576+
```
577+
gitea actions generate-runner-token -s username/test-repo
578+
```

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ arguments - which can alternatively be run by running the subcommand web.`
7575
cmd.CmdDocs,
7676
cmd.CmdDumpRepository,
7777
cmd.CmdRestoreRepository,
78+
cmd.CmdActions,
7879
}
7980
// Now adjust these commands to add our global configuration options
8081

modules/private/actions.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package private
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/setting"
10+
)
11+
12+
// Email structure holds a data for sending general emails
13+
type GenerateTokenRequest struct {
14+
Scope string
15+
}
16+
17+
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
18+
func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, ResponseExtra) {
19+
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
20+
21+
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
22+
Scope: scope,
23+
})
24+
25+
resp, extra := requestJSONResp(req, &responseText{})
26+
return resp.Text, extra
27+
}

routers/private/actions.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package private
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"net/http"
10+
"strings"
11+
12+
actions_model "code.gitea.io/gitea/models/actions"
13+
repo_model "code.gitea.io/gitea/models/repo"
14+
user_model "code.gitea.io/gitea/models/user"
15+
"code.gitea.io/gitea/modules/context"
16+
"code.gitea.io/gitea/modules/json"
17+
"code.gitea.io/gitea/modules/log"
18+
"code.gitea.io/gitea/modules/private"
19+
"code.gitea.io/gitea/modules/util"
20+
)
21+
22+
// GenerateActionsRunnerToken generates a new runner token for a given scope
23+
func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
24+
var genRequest private.GenerateTokenRequest
25+
rd := ctx.Req.Body
26+
defer rd.Close()
27+
28+
if err := json.NewDecoder(rd).Decode(&genRequest); err != nil {
29+
log.Error("%v", err)
30+
ctx.JSON(http.StatusInternalServerError, private.Response{
31+
Err: err.Error(),
32+
})
33+
return
34+
}
35+
36+
owner, repo, err := parseScope(ctx, genRequest.Scope)
37+
if err != nil {
38+
log.Error("%v", err)
39+
ctx.JSON(http.StatusInternalServerError, private.Response{
40+
Err: err.Error(),
41+
})
42+
}
43+
44+
token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo)
45+
if errors.Is(err, util.ErrNotExist) {
46+
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
47+
if err != nil {
48+
err := fmt.Sprintf("error while creating runner token: %v", err)
49+
log.Error("%v", err)
50+
ctx.JSON(http.StatusInternalServerError, private.Response{
51+
Err: err,
52+
})
53+
return
54+
}
55+
} else if err != nil {
56+
err := fmt.Sprintf("could not get unactivated runner token: %v", err)
57+
log.Error("%v", err)
58+
ctx.JSON(http.StatusInternalServerError, private.Response{
59+
Err: err,
60+
})
61+
return
62+
}
63+
64+
ctx.PlainText(http.StatusOK, token.Token)
65+
}
66+
67+
func parseScope(ctx *context.PrivateContext, scope string) (ownerID, repoID int64, err error) {
68+
ownerID = 0
69+
repoID = 0
70+
if scope == "" {
71+
return ownerID, repoID, nil
72+
}
73+
74+
ownerName, repoName, found := strings.Cut(scope, "/")
75+
76+
u, err := user_model.GetUserByName(ctx, ownerName)
77+
if err != nil {
78+
return ownerID, repoID, err
79+
}
80+
81+
if !found {
82+
return u.ID, repoID, nil
83+
}
84+
85+
r, err := repo_model.GetRepositoryByName(u.ID, repoName)
86+
if err != nil {
87+
return ownerID, repoID, err
88+
}
89+
repoID = r.ID
90+
return ownerID, repoID, nil
91+
}

routers/private/internal.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func Routes() *web.Route {
7777
r.Get("/manager/processes", Processes)
7878
r.Post("/mail/send", SendEmail)
7979
r.Post("/restore_repo", RestoreRepo)
80+
r.Post("/actions/generate_actions_runner_token", GenerateActionsRunnerToken)
8081

8182
return r
8283
}

0 commit comments

Comments
 (0)