add LoadRepository for task

This commit is contained in:
Zettat123 2024-11-27 12:14:51 +08:00
parent 5a033a0524
commit e7c6adb1a0
2 changed files with 17 additions and 8 deletions

View File

@ -11,6 +11,7 @@ import (
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
@ -37,9 +38,10 @@ type ActionTask struct {
Started timeutil.TimeStamp `xorm:"index"`
Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"`
RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"`
RepoID int64 `xorm:"index"`
Repo *repo_model.Repository `xorm:"-"`
OwnerID int64 `xorm:"index"`
CommitSHA string `xorm:"index"`
IsForkPullRequest bool
Token string `xorm:"-"`
@ -151,6 +153,14 @@ func (task *ActionTask) GenerateToken() (err error) {
return err
}
func (task *ActionTask) LoadRepository(ctx context.Context) (err error) {
if task.Repo != nil {
return nil
}
task.Repo, err = repo_model.GetRepositoryByID(ctx, task.RepoID)
return
}
func GetTaskByID(ctx context.Context, id int64) (*ActionTask, error) {
var task ActionTask
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&task)

View File

@ -195,13 +195,12 @@ func httpBase(ctx *context.Context) *serviceHandler {
return nil
}
if task.RepoID != repo.ID {
actionsCfg := repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
taskRepo, err := repo_model.GetRepositoryByID(ctx, task.RepoID)
if err != nil {
ctx.ServerError("GetRepositoryByID", err)
if err := task.LoadRepository(ctx); err != nil {
ctx.ServerError("LoadRepository", err)
return nil
}
if !actionsCfg.IsCollaborativeOwner(taskRepo.OwnerID) || taskRepo.OwnerID != repo.OwnerID || !taskRepo.IsPrivate {
actionsCfg := repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
if !actionsCfg.IsCollaborativeOwner(task.Repo.OwnerID) || !task.Repo.IsPrivate {
// See https://docs.github.com/en/actions/sharing-automations/sharing-actions-and-workflows-from-your-private-repository
// Any actions or reusable workflows stored in the private repository can be used in
// workflows defined in other private repositories owned by the same organization or user.