mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-10-27 20:41:01 +00:00
[CLI] implement forgejo-cli
(cherry picked from commit2555e315f7) (cherry picked from commit51b9c9092e) [CLI] implement forgejo-cli (squash) support initDB (cherry picked from commit5c31ae602a) (cherry picked from commitbbf76489a7) Conflicts: because ofd0dbe52e76upgrade to https://pkg.go.dev/github.com/urfave/cli/v2 (cherry picked from commitb6c1bcc008) [CLI] implement forgejo-cli actions (cherry picked from commit08be2b226e) (cherry picked from commitb6cfa88c6e) (cherry picked from commit59704200de) [CLI] implement forgejo-cli actions generate-secret (cherry picked from commit6f7905c8ec) (cherry picked from commite085d6d273) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2) [CLI] implement forgejo-cli actions register (cherry picked from commit2f95143000) (cherry picked from commit42f2f8731e) [CLI] implement forgejo-cli actions register (squash) no private Do not go through the private API, directly modify the database (cherry picked from commit1ba7c0d39d) [CLI] implement forgejo-cli actions (cherry picked from commit6f7905c8ec) (cherry picked from commite085d6d273) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2) (cherry picked from commit4c121ef022) Conflicts: cmd/forgejo/actions.go tests/integration/cmd_forgejo_actions_test.go (cherry picked from commit36997a48e3) [CLI] implement forgejo-cli actions (squash) restore --version Refs: https://codeberg.org/forgejo/forgejo/issues/1134 (cherry picked from commit9739eb52d8) [CI] implement forgejo-cli (squash) the actions subcommand needs config (cherry picked from commit def638475122a26082ab3835842c84cd03839154) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1209 (cherry picked from commita1758a3910) (cherry picked from commit935fa650c7) (cherry picked from commitcd21026bc9) (cherry picked from commit1700b8973a) (cherry picked from commit1def42a379) (cherry picked from commit839d97521d) (cherry picked from commitfd8c13be6b) (cherry picked from commit588e5d552f) (cherry picked from commit151a726620) [v1.22] [CLI] implement forgejo-cli https://codeberg.org/forgejo/forgejo/pulls/1541 (cherry picked from commit46708de7b9) (cherry picked from commita8e5c1369e) (cherry picked from commitc8a32aaf24) Conflicts: models/actions/main_test.go https://codeberg.org/forgejo/forgejo/pulls/1656 (cherry picked from commit79f4553063) (cherry picked from commit0379da0cf5)
This commit is contained in:
parent
6b70773ad8
commit
331d58c085
9 changed files with 805 additions and 2 deletions
68
models/actions/forgejo.go
Normal file
68
models/actions/forgejo.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
gouuid "github.com/google/uuid"
|
||||
)
|
||||
|
||||
func RegisterRunner(ctx context.Context, ownerID, repoID int64, token string, labels []string, name, version string) (*ActionRunner, error) {
|
||||
uuid, err := gouuid.FromBytes([]byte(token[:16]))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gouuid.FromBytes %v", err)
|
||||
}
|
||||
uuidString := uuid.String()
|
||||
|
||||
var runner ActionRunner
|
||||
|
||||
has, err := db.GetEngine(ctx).Where("uuid=?", uuidString).Get(&runner)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetRunner %v", err)
|
||||
} else if !has {
|
||||
//
|
||||
// The runner does not exist yet, create it
|
||||
//
|
||||
saltBytes, err := util.CryptoRandomBytes(16)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("CryptoRandomBytes %v", err)
|
||||
}
|
||||
salt := hex.EncodeToString(saltBytes)
|
||||
|
||||
hash := auth_model.HashToken(token, salt)
|
||||
|
||||
runner = ActionRunner{
|
||||
UUID: uuidString,
|
||||
TokenHash: hash,
|
||||
TokenSalt: salt,
|
||||
}
|
||||
|
||||
if err := CreateRunner(ctx, &runner); err != nil {
|
||||
return &runner, fmt.Errorf("can't create new runner %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Update the existing runner
|
||||
//
|
||||
name, _ = util.SplitStringAtByteN(name, 255)
|
||||
|
||||
runner.Name = name
|
||||
runner.OwnerID = ownerID
|
||||
runner.RepoID = repoID
|
||||
runner.Version = version
|
||||
runner.AgentLabels = labels
|
||||
|
||||
if err := UpdateRunner(ctx, &runner, "name", "owner_id", "repo_id", "version", "agent_labels"); err != nil {
|
||||
return &runner, fmt.Errorf("can't update the runner %+v %w", runner, err)
|
||||
}
|
||||
|
||||
return &runner, nil
|
||||
}
|
||||
29
models/actions/forgejo_test.go
Normal file
29
models/actions/forgejo_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"testing"
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestActions_RegisterRunner(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
ownerID := int64(0)
|
||||
repoID := int64(0)
|
||||
token := "0123456789012345678901234567890123456789"
|
||||
labels := []string{}
|
||||
name := "runner"
|
||||
version := "v1.2.3"
|
||||
runner, err := RegisterRunner(db.DefaultContext, ownerID, repoID, token, labels, name, version)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, name, runner.Name)
|
||||
|
||||
assert.EqualValues(t, 1, subtle.ConstantTimeCompare([]byte(runner.TokenHash), []byte(auth_model.HashToken(token, runner.TokenSalt))), "the token cannot be verified with the same method as routers/api/actions/runner/interceptor.go as of 8228751c55d6a4263f0fec2932ca16181c09c97d")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue