diff --git a/routers/hat/hat.go b/routers/hat/hat.go index a95c880..816d917 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -6,11 +6,14 @@ import ( "net/http" "strings" + actions_model "code.gitea.io/gitea/models/actions" + gitea_api "code.gitea.io/gitea/modules/structs" hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" "gitea.com/go-chi/binding" "code.gitea.io/gitea/models/organization" + "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" @@ -115,7 +118,6 @@ func Routers(ctx gocontext.Context) *web.Route { SignInRequired: setting.Service.RequireSignInView, })) - reqRepoCodeReader := context.RequireRepoReader(unit_model.TypeCode) m.Group("", func() { m.Get("/version", misc.Version) m.Post("/create_pr_version", bind(gitea_api.PullRequestPayload{}), repo.CreatePrVersion) @@ -140,17 +142,16 @@ func Routers(ctx gocontext.Context) *web.Route { m.Get("/*", repo.GetReadmeContentsByPath) }) m.Get("/commits_slice", repo.GetAllCommitsSliceByTime) - m.Get("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, - repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.CompareDiff) + m.Get("/compare/*", reqRepoReader(unit_model.TypeCode), repo.CompareDiff) m.Group("/pulls", func() { m.Group("/{index}", func() { m.Combo("").Get(repo.GetPullRequest). Patch(bind(gitea_api.EditPullRequestOption{}), repo.EditPullRequest) - m.Get("/commits", context.RepoRef(), repo.GetPullCommits) - m.Get("/files", context.RepoRef(), repo.GetPullFiles) + m.Get("/commits", context.ReferencesGitRepo(), repo.GetPullCommits) + m.Get("/files", context.ReferencesGitRepo(), repo.GetPullFiles) m.Group("/versions", func() { m.Get("", repo.ListPullRequestVersions) - m.Get("/{versionId}/diff", context.RepoRef(), repo.GetPullRequestVersionDiff) + m.Get("/{versionId}/diff", context.ReferencesGitRepo(), repo.GetPullRequestVersionDiff) }) }) }, mustAllowPulls, reqRepoReader(unit_model.TypeCode), context.ReferencesGitRepo()) @@ -187,14 +188,14 @@ func Routers(ctx gocontext.Context) *web.Route { m.Post("", bind(hat_api.BatchChangeFileOptions{}), repo.BatchChangeFile) }, reqRepoWriter(unit.TypeCode), reqToken()) }, reqRepoReader(unit.TypeCode)) - m.Get("/find", context.RepoRef(), reqRepoReader(unit.TypeCode), repo.FindFiles) + m.Get("/find", context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode), repo.FindFiles) m.Group("/git", func() { m.Group("/commits", func() { m.Get("/{sha}", repo.GetSingleCommit) }) }, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode)) - m.Get("/blame", context.RepoRef(), repo.GetRepoRefBlame) - m.Get("/code_stats", context.RepoRef(), repo.ListCodeStats) + m.Get("/blame", context.ReferencesGitRepo(), repo.GetRepoRefBlame) + m.Get("/code_stats", context.ReferencesGitRepo(), repo.ListCodeStats) }, repoAssignment()) }) m.Group("/users", func() { @@ -321,10 +322,39 @@ func repoAssignment() func(ctx *context.APIContext) { repo.Owner = owner ctx.Repo.Repository = repo - ctx.Repo.Permission, err = access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) - if err != nil { - ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) - return + if ctx.Doer != nil && ctx.Doer.ID == user_model.ActionsUserID { + taskID := ctx.Data["ActionsTaskID"].(int64) + task, err := actions_model.GetTaskByID(ctx, taskID) + if err != nil { + ctx.Error(http.StatusInternalServerError, "actions_model.GetTaskByID", err) + return + } + if task.RepoID != repo.ID { + ctx.NotFound() + return + } + + if task.IsForkPullRequest { + ctx.Repo.Permission.AccessMode = perm.AccessModeRead + } else { + ctx.Repo.Permission.AccessMode = perm.AccessModeWrite + } + + if err := ctx.Repo.Repository.LoadUnits(ctx); err != nil { + ctx.Error(http.StatusInternalServerError, "LoadUnits", err) + return + } + ctx.Repo.Permission.Units = ctx.Repo.Repository.Units + ctx.Repo.Permission.UnitsMode = make(map[unit.Type]perm.AccessMode) + for _, u := range ctx.Repo.Repository.Units { + ctx.Repo.Permission.UnitsMode[u.Type] = ctx.Repo.Permission.AccessMode + } + } else { + ctx.Repo.Permission, err = access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) + return + } } if !ctx.Repo.HasAccess() {