mirror of
https://github.com/go-gitea/gitea
synced 2024-12-23 09:17:53 +01:00
Use a new command to set runner register token
This commit is contained in:
parent
aeb6603794
commit
58332666a8
@ -19,6 +19,7 @@ var (
|
|||||||
Usage: "Manage Gitea Actions",
|
Usage: "Manage Gitea Actions",
|
||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
subcmdActionsGenRunnerToken,
|
subcmdActionsGenRunnerToken,
|
||||||
|
subcmdActionsSetRunnerToken,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,8 +35,23 @@ var (
|
|||||||
Value: "",
|
Value: "",
|
||||||
Usage: "{owner}[/{repo}] - leave empty for a global runner",
|
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{
|
&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"},
|
Aliases: []string{"t"},
|
||||||
Value: "",
|
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]",
|
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()
|
setting.MustInstalled()
|
||||||
|
|
||||||
scope := c.String("scope")
|
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() {
|
if extra.HasError() {
|
||||||
return handleCliResponseExtra(extra)
|
return handleCliResponseExtra(extra)
|
||||||
}
|
}
|
||||||
|
@ -84,11 +84,10 @@ func NewRunnerToken(ctx context.Context, ownerID, repoID int64, preDefinedToken
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else if len(token) != 40 || !util.IsRandomStringValid(token) {
|
||||||
if len(token) != 40 || !util.IsRandomStringValid(token) {
|
|
||||||
return nil, util.NewInvalidArgumentErrorf("invalid token: %s", token)
|
return nil, util.NewInvalidArgumentErrorf("invalid token: %s", token)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
runnerToken := &ActionRunnerToken{
|
runnerToken := &ActionRunnerToken{
|
||||||
OwnerID: ownerID,
|
OwnerID: ownerID,
|
||||||
RepoID: repoID,
|
RepoID: repoID,
|
||||||
|
@ -15,12 +15,28 @@ type GenerateTokenRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
|
// 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"
|
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
|
||||||
|
|
||||||
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
|
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
|
||||||
Scope: scope,
|
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{})
|
return requestJSONResp(req, &ResponseText{})
|
||||||
|
@ -93,3 +93,44 @@ func parseScope(ctx *context.PrivateContext, scope string) (ownerID, repoID int6
|
|||||||
repoID = r.ID
|
repoID = r.ID
|
||||||
return ownerID, repoID, nil
|
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)
|
||||||
|
}
|
||||||
|
@ -82,6 +82,7 @@ func Routes() *web.Router {
|
|||||||
r.Post("/mail/send", SendEmail)
|
r.Post("/mail/send", SendEmail)
|
||||||
r.Post("/restore_repo", RestoreRepo)
|
r.Post("/restore_repo", RestoreRepo)
|
||||||
r.Post("/actions/generate_actions_runner_token", GenerateActionsRunnerToken)
|
r.Post("/actions/generate_actions_runner_token", GenerateActionsRunnerToken)
|
||||||
|
r.Post("/actions/set_actions_runner_token", SetActionsRunnerToken)
|
||||||
|
|
||||||
r.Group("/repo", func() {
|
r.Group("/repo", func() {
|
||||||
// FIXME: it is not right to use context.Contexter here because all routes here should use PrivateContext
|
// FIXME: it is not right to use context.Contexter here because all routes here should use PrivateContext
|
||||||
|
Loading…
Reference in New Issue
Block a user