mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 00:44:15 -05:00
e90db3f5cc
(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 ofd0dbe52e76
upgrade 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
) (cherry picked from commit331d58c085
) (cherry picked from commit89705502c4
) (cherry picked from commit4723d5febf
) (cherry picked from commite71b260130
) (cherry picked from commit6a376a5b48
) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1969 (cherry picked from commit6ba97cf4b5
) (cherry picked from commite0a6ebfeca
) (cherry picked from commit5702aeab2d
) (cherry picked from commitf919c4d6c1
) (cherry picked from commita26799a88a
) (cherry picked from commitb6ab473395
) (cherry picked from commitcf054a0461
)
97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package private
|
|
|
|
import (
|
|
gocontext "context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/json"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/private"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// GenerateActionsRunnerToken generates a new runner token for a given scope
|
|
func GenerateActionsRunnerToken(ctx *context.PrivateContext) {
|
|
var genRequest private.GenerateTokenRequest
|
|
rd := ctx.Req.Body
|
|
defer rd.Close()
|
|
|
|
if err := json.NewDecoder(rd).Decode(&genRequest); err != nil {
|
|
log.Error("%v", err)
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
Err: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
owner, repo, err := parseScope(ctx, genRequest.Scope)
|
|
if err != nil {
|
|
log.Error("%v", err)
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
Err: err.Error(),
|
|
})
|
|
}
|
|
|
|
token, err := actions_model.GetLatestRunnerToken(ctx, owner, repo)
|
|
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
|
|
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
|
|
if err != nil {
|
|
err := fmt.Sprintf("error while creating runner token: %v", err)
|
|
log.Error("%v", err)
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
Err: err,
|
|
})
|
|
return
|
|
}
|
|
} else if err != nil {
|
|
err := fmt.Sprintf("could not get unactivated runner token: %v", err)
|
|
log.Error("%v", err)
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
Err: err,
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx.PlainText(http.StatusOK, token.Token)
|
|
}
|
|
|
|
func ParseScope(ctx gocontext.Context, scope string) (ownerID, repoID int64, err error) {
|
|
return parseScope(ctx, scope)
|
|
}
|
|
|
|
func parseScope(ctx gocontext.Context, scope string) (ownerID, repoID int64, err error) {
|
|
ownerID = 0
|
|
repoID = 0
|
|
if scope == "" {
|
|
return ownerID, repoID, nil
|
|
}
|
|
|
|
ownerName, repoName, found := strings.Cut(scope, "/")
|
|
|
|
u, err := user_model.GetUserByName(ctx, ownerName)
|
|
if err != nil {
|
|
return ownerID, repoID, err
|
|
}
|
|
ownerID = u.ID
|
|
|
|
if !found {
|
|
return ownerID, repoID, nil
|
|
}
|
|
|
|
r, err := repo_model.GetRepositoryByName(ctx, u.ID, repoName)
|
|
if err != nil {
|
|
return ownerID, repoID, err
|
|
}
|
|
repoID = r.ID
|
|
return ownerID, repoID, nil
|
|
}
|