mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-22 08:42:32 -05:00
894d9b2836
Since `modules/context` has to depend on `models` and many other packages, it should be moved from `modules/context` to `services/context` according to design principles. There is no logic code change on this PR, only move packages. - Move `code.gitea.io/gitea/modules/context` to `code.gitea.io/gitea/services/context` - Move `code.gitea.io/gitea/modules/contexttest` to `code.gitea.io/gitea/services/contexttest` because of depending on context - Move `code.gitea.io/gitea/modules/upload` to `code.gitea.io/gitea/services/context/upload` because of depending on context (cherry picked from commit 29f149bd9f517225a3c9f1ca3fb0a7b5325af696) Conflicts: routers/api/packages/alpine/alpine.go routers/api/v1/repo/issue_reaction.go routers/install/install.go routers/web/admin/config.go routers/web/passkey.go routers/web/repo/search.go routers/web/repo/setting/default_branch.go routers/web/user/home.go routers/web/user/profile.go tests/integration/editor_test.go tests/integration/integration_test.go tests/integration/mirror_push_test.go trivial context conflicts also modified all other occurrences in Forgejo specific files
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/json"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/private"
|
|
"code.gitea.io/gitea/modules/util"
|
|
"code.gitea.io/gitea/services/context"
|
|
)
|
|
|
|
// 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
|
|
}
|