[ACTIONS] forgejo actions --registration-token-admin
This commit is contained in:
parent
bc4ecc7025
commit
481d813539
3 changed files with 117 additions and 0 deletions
66
cmd/actions.go
Normal file
66
cmd/actions.go
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
actions_model "code.gitea.io/gitea/models/actions"
|
||||||
|
"code.gitea.io/gitea/models/db"
|
||||||
|
"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 runActions(ctx *cli.Context) error {
|
||||||
|
setting.InitProviderFromExistingFile()
|
||||||
|
setting.LoadCommonSettings()
|
||||||
|
setting.LoadDBSetting()
|
||||||
|
|
||||||
|
stdCtx, cancel := installSignals()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := db.InitEngine(stdCtx); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Bool("registration-token-admin") {
|
||||||
|
return runActionsRegistrationToken(ctx, stdCtx, 0, 0)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runActionsRegistrationToken(ctx *cli.Context, 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", err)
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
log.Fatalf("GetUnactivatedRunnerToken", err)
|
||||||
|
}
|
||||||
|
fmt.Printf(token.Token)
|
||||||
|
return nil
|
||||||
|
}
|
1
main.go
1
main.go
|
@ -58,6 +58,7 @@ func main() {
|
||||||
arguments - which can alternatively be run by running the subcommand web.`
|
arguments - which can alternatively be run by running the subcommand web.`
|
||||||
app.Version = Version + formatBuiltWith()
|
app.Version = Version + formatBuiltWith()
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
|
cmd.CmdActions,
|
||||||
cmd.CmdWeb,
|
cmd.CmdWeb,
|
||||||
cmd.CmdServ,
|
cmd.CmdServ,
|
||||||
cmd.CmdHook,
|
cmd.CmdHook,
|
||||||
|
|
50
tests/integration/cmd_actions_test.go
Normal file
50
tests/integration/cmd_actions_test.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
|
"io"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/cmd"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_CmdActions(t *testing.T) {
|
||||||
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
wantErr bool
|
||||||
|
expectedOutput func(string)
|
||||||
|
}{
|
||||||
|
{"test_registration-token-admin", []string{"actions", "--registration-token-admin"}, false, func(output string) { assert.EqualValues(t, 40, len(output), output) }},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
realStdout := os.Stdout
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stdout = w
|
||||||
|
|
||||||
|
set := flag.NewFlagSet("actions", 0)
|
||||||
|
_ = set.Parse(tt.args)
|
||||||
|
context := cli.NewContext(&cli.App{Writer: os.Stdout}, set, nil)
|
||||||
|
err := cmd.CmdActions.Run(context)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("CmdActions.Run() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
w.Close()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
io.Copy(&buf, r)
|
||||||
|
tt.expectedOutput(buf.String())
|
||||||
|
os.Stdout = realStdout
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue