diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-09 07:30:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-09 01:30:14 +0200 |
commit | def4956122ea2364f247712b13856383ee496add (patch) | |
tree | 311e7a077aba83825815881b7bf35fff28930533 /routers | |
parent | c4303efc23ea19f16ee826809f43888ee4583ebb (diff) | |
download | gitea-def4956122ea2364f247712b13856383ee496add.tar.gz gitea-def4956122ea2364f247712b13856383ee496add.zip |
Improve Gitea's web context, decouple "issue template" code into service package (#24590)
1. Remove unused fields/methods in web context.
2. Make callers call target function directly instead of the light
wrapper like "IsUserRepoReaderSpecific"
3. The "issue template" code shouldn't be put in the "modules/context"
package, so move them to the service package.
---------
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 6 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 13 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 16 | ||||
-rw-r--r-- | routers/web/repo/milestone.go | 5 | ||||
-rw-r--r-- | routers/web/repo/view.go | 5 |
5 files changed, 27 insertions, 18 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 9a733b832f..a67a5420ac 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -316,7 +316,7 @@ func reqSiteAdmin() func(ctx *context.APIContext) { // reqOwner user should be the owner of the repo or site admin. func reqOwner() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if !ctx.IsUserRepoOwner() && !ctx.IsUserSiteAdmin() { + if !ctx.Repo.IsOwner() && !ctx.IsUserSiteAdmin() { ctx.Error(http.StatusForbidden, "reqOwner", "user should be the owner of the repo") return } @@ -355,7 +355,7 @@ func reqRepoBranchWriter(ctx *context.APIContext) { // reqRepoReader user should have specific read permission or be a repo admin or a site admin func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if !ctx.IsUserRepoReaderSpecific(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() { + if !ctx.Repo.CanRead(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() { ctx.Error(http.StatusForbidden, "reqRepoReader", "user should have specific read permission or be a repo admin or a site admin") return } @@ -365,7 +365,7 @@ func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) { // reqAnyRepoReader user should have any permission to read repository or permissions of site admin func reqAnyRepoReader() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if !ctx.IsUserRepoReaderAny() && !ctx.IsUserSiteAdmin() { + if !ctx.Repo.HasAccess() && !ctx.IsUserSiteAdmin() { ctx.Error(http.StatusForbidden, "reqAnyRepoReader", "user should have any permission to read repository or permissions of site admin") return } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 480ca397d4..114b93534a 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -30,6 +30,7 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/convert" + "code.gitea.io/gitea/services/issue" repo_service "code.gitea.io/gitea/services/repository" ) @@ -1144,8 +1145,12 @@ func GetIssueTemplates(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/IssueTemplates" - - ctx.JSON(http.StatusOK, ctx.IssueTemplatesFromDefaultBranch()) + ret, err := issue.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetTemplatesFromDefaultBranch", err) + return + } + ctx.JSON(http.StatusOK, ret) } // GetIssueConfig returns the issue config for a repo @@ -1169,7 +1174,7 @@ func GetIssueConfig(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepoIssueConfig" - issueConfig, _ := ctx.IssueConfigFromDefaultBranch() + issueConfig, _ := issue.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.JSON(http.StatusOK, issueConfig) } @@ -1194,7 +1199,7 @@ func ValidateIssueConfig(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepoIssueConfigValidation" - _, err := ctx.IssueConfigFromDefaultBranch() + _, err := issue.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) if err == nil { ctx.JSON(http.StatusOK, api.IssueConfigValidation{Valid: true, Message: ""}) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 4efac5c38c..c2f30a01f4 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -431,7 +431,7 @@ func Issues(ctx *context.Context) { } ctx.Data["Title"] = ctx.Tr("repo.issues") ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = ctx.HasIssueTemplatesOrContactLinks() + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) } issues(ctx, ctx.FormInt64("milestone"), ctx.FormInt64("project"), util.OptionalBoolOf(isPullList)) @@ -862,7 +862,7 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles func NewIssue(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = ctx.HasIssueTemplatesOrContactLinks() + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes title := ctx.FormString("title") ctx.Data["TitleQuery"] = title @@ -904,7 +904,7 @@ func NewIssue(ctx *context.Context) { RetrieveRepoMetas(ctx, ctx.Repo.Repository, false) - _, templateErrs := ctx.IssueTemplatesErrorsFromDefaultBranch() + _, templateErrs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) if errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates); len(errs) > 0 { for k, v := range errs { templateErrs[k] = v @@ -952,20 +952,20 @@ func NewIssueChooseTemplate(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true - issueTemplates, errs := ctx.IssueTemplatesErrorsFromDefaultBranch() + issueTemplates, errs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["IssueTemplates"] = issueTemplates if len(errs) > 0 { ctx.Flash.Warning(renderErrorOfTemplates(ctx, errs), true) } - if !ctx.HasIssueTemplatesOrContactLinks() { + if !issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) { // The "issues/new" and "issues/new/choose" share the same query parameters "project" and "milestone", if no template here, just redirect to the "issues/new" page with these parameters. ctx.Redirect(fmt.Sprintf("%s/issues/new?%s", ctx.Repo.Repository.Link(), ctx.Req.URL.RawQuery), http.StatusSeeOther) return } - issueConfig, err := ctx.IssueConfigFromDefaultBranch() + issueConfig, err := issue_service.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["IssueConfig"] = issueConfig ctx.Data["IssueConfigError"] = err // ctx.Flash.Err makes problems here @@ -1103,7 +1103,7 @@ func NewIssuePost(ctx *context.Context) { form := web.GetForm(ctx).(*forms.CreateIssueForm) ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = ctx.HasIssueTemplatesOrContactLinks() + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled upload.AddUploadContext(ctx, "comment") @@ -1297,7 +1297,7 @@ func ViewIssue(ctx *context.Context) { return } ctx.Data["PageIsIssueList"] = true - ctx.Data["NewIssueChooseTemplate"] = ctx.HasIssueTemplatesOrContactLinks() + ctx.Data["NewIssueChooseTemplate"] = issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo) } if issue.IsPull && !ctx.Repo.CanRead(unit.TypeIssues) { diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index d712df1001..4b33fbcb16 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" + "code.gitea.io/gitea/services/issue" "xorm.io/builder" ) @@ -289,7 +290,9 @@ func MilestoneIssuesAndPulls(ctx *context.Context) { ctx.Data["Milestone"] = milestone issues(ctx, milestoneID, 0, util.OptionalBoolNone) - ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0 + + ret, _ := issue.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo) + ctx.Data["NewIssueChooseTemplate"] = len(ret) > 0 ctx.Data["CanWriteIssues"] = ctx.Repo.CanWriteIssuesOrPulls(false) ctx.Data["CanWritePulls"] = ctx.Repo.CanWriteIssuesOrPulls(true) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 2bf293cbda..2fd893f91c 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -40,6 +40,7 @@ import ( "code.gitea.io/gitea/modules/typesniffer" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/web/feed" + issue_service "code.gitea.io/gitea/services/issue" "github.com/nektos/act/pkg/model" ) @@ -346,8 +347,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st if editorconfigErr != nil { ctx.Data["FileError"] = strings.TrimSpace(editorconfigErr.Error()) } - } else if ctx.Repo.IsIssueConfig(ctx.Repo.TreePath) { - _, issueConfigErr := ctx.Repo.GetIssueConfig(ctx.Repo.TreePath, ctx.Repo.Commit) + } else if issue_service.IsTemplateConfig(ctx.Repo.TreePath) { + _, issueConfigErr := issue_service.GetTemplateConfig(ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.Commit) if issueConfigErr != nil { ctx.Data["FileError"] = strings.TrimSpace(issueConfigErr.Error()) } |