2023-04-17 22:37:13 +05:30
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
2023-07-09 18:22:21 +05:30
|
|
|
gocontext "context"
|
2023-04-17 22:37:13 +05:30
|
|
|
"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"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-04-17 22:37:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("JSON Decode failed: %v", err)
|
2023-04-17 22:37:13 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
owner, repo, err := parseScope(ctx, genRequest.Scope)
|
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
log.Error("parseScope failed: %v", err)
|
2023-04-17 22:37:13 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-18 21:22:44 +05:30
|
|
|
token, err := actions_model.GetLatestRunnerToken(ctx, owner, repo)
|
2023-09-27 11:07:48 +05:30
|
|
|
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
|
2023-04-17 22:37:13 +05:30
|
|
|
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
|
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
errMsg := fmt.Sprintf("error while creating runner token: %v", err)
|
|
|
|
log.Error("NewRunnerToken failed: %v", errMsg)
|
2023-04-17 22:37:13 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
2024-04-07 16:47:06 +05:30
|
|
|
Err: errMsg,
|
2023-04-17 22:37:13 +05:30
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
errMsg := fmt.Sprintf("could not get unactivated runner token: %v", err)
|
|
|
|
log.Error("GetLatestRunnerToken failed: %v", errMsg)
|
2023-04-17 22:37:13 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
2024-04-07 16:47:06 +05:30
|
|
|
Err: errMsg,
|
2023-04-17 22:37:13 +05:30
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.PlainText(http.StatusOK, token.Token)
|
|
|
|
}
|
|
|
|
|
2023-07-09 18:22:21 +05:30
|
|
|
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) {
|
2023-04-17 22:37:13 +05:30
|
|
|
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
|
|
|
|
}
|
2023-07-19 18:51:51 +05:30
|
|
|
ownerID = u.ID
|
2023-04-17 22:37:13 +05:30
|
|
|
|
|
|
|
if !found {
|
2023-07-19 18:51:51 +05:30
|
|
|
return ownerID, repoID, nil
|
2023-04-17 22:37:13 +05:30
|
|
|
}
|
|
|
|
|
2023-10-11 09:54:07 +05:30
|
|
|
r, err := repo_model.GetRepositoryByName(ctx, u.ID, repoName)
|
2023-04-17 22:37:13 +05:30
|
|
|
if err != nil {
|
|
|
|
return ownerID, repoID, err
|
|
|
|
}
|
|
|
|
repoID = r.ID
|
|
|
|
return ownerID, repoID, nil
|
|
|
|
}
|