Use a new command to set runner register token

This commit is contained in:
Lunny Xiao 2024-12-17 19:16:06 -08:00
parent aeb6603794
commit 58332666a8
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
5 changed files with 99 additions and 10 deletions

View File

@ -19,6 +19,7 @@ var (
Usage: "Manage Gitea Actions",
Subcommands: []*cli.Command{
subcmdActionsGenRunnerToken,
subcmdActionsSetRunnerToken,
},
}
@ -34,8 +35,23 @@ var (
Value: "",
Usage: "{owner}[/{repo}] - leave empty for a global runner",
},
},
}
subcmdActionsSetRunnerToken = &cli.Command{
Name: "set-runner-token",
Usage: "Set a new token for a runner to as register token",
Action: runSetActionsRunnerToken,
Aliases: []string{"grt"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "put-token",
Name: "scope",
Aliases: []string{"s"},
Value: "",
Usage: "{owner}[/{repo}] - leave empty for a global runner",
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Value: "",
Usage: "[{token}] - leave empty will generate a new token, otherwise will update the token to database. The token MUST be a 40 digital string containing only [0-9a-zA-Z]",
@ -51,9 +67,25 @@ func runGenerateActionsRunnerToken(c *cli.Context) error {
setting.MustInstalled()
scope := c.String("scope")
putToken := c.String("put-token")
respText, extra := private.GenerateActionsRunnerToken(ctx, scope, putToken)
respText, extra := private.GenerateActionsRunnerToken(ctx, scope)
if extra.HasError() {
return handleCliResponseExtra(extra)
}
_, _ = fmt.Printf("%s\n", respText.Text)
return nil
}
func runSetActionsRunnerToken(c *cli.Context) error {
ctx, cancel := installSignals()
defer cancel()
setting.MustInstalled()
scope := c.String("scope")
putToken := c.String("token")
respText, extra := private.SetActionsRunnerToken(ctx, scope, putToken)
if extra.HasError() {
return handleCliResponseExtra(extra)
}

View File

@ -84,11 +84,10 @@ func NewRunnerToken(ctx context.Context, ownerID, repoID int64, preDefinedToken
if err != nil {
return nil, err
}
} else {
if len(token) != 40 || !util.IsRandomStringValid(token) {
} else if len(token) != 40 || !util.IsRandomStringValid(token) {
return nil, util.NewInvalidArgumentErrorf("invalid token: %s", token)
}
}
runnerToken := &ActionRunnerToken{
OwnerID: ownerID,
RepoID: repoID,

View File

@ -15,12 +15,28 @@ type GenerateTokenRequest struct {
}
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
func GenerateActionsRunnerToken(ctx context.Context, scope, putToken string) (*ResponseText, ResponseExtra) {
func GenerateActionsRunnerToken(ctx context.Context, scope string) (*ResponseText, ResponseExtra) {
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
Scope: scope,
PutToken: putToken,
})
return requestJSONResp(req, &ResponseText{})
}
type SetTokenRequest struct {
Scope string
Token string
}
// SetActionsRunnerToken calls the internal GenerateActionsRunnerToken function
func SetActionsRunnerToken(ctx context.Context, scope, token string) (*ResponseText, ResponseExtra) {
reqURL := setting.LocalURL + "api/internal/actions/set_actions_runner_token"
req := newInternalRequest(ctx, reqURL, "POST", SetTokenRequest{
Scope: scope,
Token: token,
})
return requestJSONResp(req, &ResponseText{})

View File

@ -93,3 +93,44 @@ func parseScope(ctx *context.PrivateContext, scope string) (ownerID, repoID int6
repoID = r.ID
return ownerID, repoID, nil
}
// SetActionsRunnerToken set a runner token for a given scope
func SetActionsRunnerToken(ctx *context.PrivateContext) {
var setRequest private.SetTokenRequest
rd := ctx.Req.Body
defer rd.Close()
if err := json.NewDecoder(rd).Decode(&setRequest); err != nil {
log.Error("JSON Decode failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
return
}
owner, repo, err := parseScope(ctx, setRequest.Scope)
if err != nil {
log.Error("parseScope failed: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
}
if setRequest.Token == "" {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: "token is empty",
})
return
}
token, err := actions_model.NewRunnerToken(ctx, owner, repo, setRequest.Token)
if err != nil {
errMsg := fmt.Sprintf("error while creating runner token: %v", err)
log.Error("NewRunnerToken failed: %v", errMsg)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: errMsg,
})
return
}
ctx.PlainText(http.StatusOK, token.Token)
}

View File

@ -82,6 +82,7 @@ func Routes() *web.Router {
r.Post("/mail/send", SendEmail)
r.Post("/restore_repo", RestoreRepo)
r.Post("/actions/generate_actions_runner_token", GenerateActionsRunnerToken)
r.Post("/actions/set_actions_runner_token", SetActionsRunnerToken)
r.Group("/repo", func() {
// FIXME: it is not right to use context.Contexter here because all routes here should use PrivateContext