forked from NYANDEV/forgejo
b8d4953405
(cherry picked from commit 481d813539762af6670b2cb4d3e893ab27dfe544)
address oliverpool comments
(cherry picked from commit d8f313f9e21ceea7b113fc04938fa4ffea577d9e)
s/Printf/Print/
(cherry picked from commit 6621b04458b9fbaf8b6664efd86886b2ebbec01d)
do not run initDB within integration tests
(cherry picked from commit 5a0428ede3e8f242860d121ab3ee8712a1fe2f14)
(cherry picked from commit e8287b753d70d51e9bec23e732d797fe900a4911)
(cherry picked from commit fc2b49e571
)
(cherry picked from commit a291b39b230d62bd28a0cb0728e0ae6f6c60a5ab)
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
// CmdActions represents the available actions sub-command.
|
|
var CmdActions = cli.Command{
|
|
Name: "actions",
|
|
Usage: "Actions",
|
|
Description: "Actions",
|
|
Action: runActions,
|
|
Flags: []cli.Flag{
|
|
cli.BoolFlag{
|
|
Name: "registration-token-admin",
|
|
Usage: "Show the runner registration admin token",
|
|
},
|
|
},
|
|
}
|
|
|
|
func maybeInitDB(stdCtx context.Context) error {
|
|
if setting.Database.Type == "" {
|
|
if err := initDB(stdCtx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runActions(ctx *cli.Context) error {
|
|
stdCtx := context.Background()
|
|
|
|
if err := maybeInitDB(stdCtx); err != nil {
|
|
log.Fatalf("maybeInitDB %v", err)
|
|
}
|
|
|
|
if ctx.Bool("registration-token-admin") {
|
|
// ownid=0,repo_id=0,means this token is used for global
|
|
return runActionsRegistrationToken(stdCtx, 0, 0)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runActionsRegistrationToken(stdCtx context.Context, ownerID, repoID int64) error {
|
|
var token *actions_model.ActionRunnerToken
|
|
token, err := actions_model.GetUnactivatedRunnerToken(stdCtx, ownerID, repoID)
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
token, err = actions_model.NewRunnerToken(stdCtx, ownerID, repoID)
|
|
if err != nil {
|
|
log.Fatalf("CreateRunnerToken %v", err)
|
|
}
|
|
} else if err != nil {
|
|
log.Fatalf("GetUnactivatedRunnerToken %v", err)
|
|
}
|
|
fmt.Print(token.Token)
|
|
return nil
|
|
}
|