Skip to content

Commit b139234

Browse files
GiteaBotJakobDevsilverwinddelvh
authored
Fix issue templates when blank isses are disabled (#27061) (#27082)
Backport #27061 by @JakobDev Fixes #27060 Co-authored-by: JakobDev <[email protected]> Co-authored-by: silverwind <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent d8b3932 commit b139234

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

routers/web/repo/compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ func CompareDiff(ctx *context.Context) {
785785

786786
ctx.Data["IsRepoToolbarCommits"] = true
787787
ctx.Data["IsDiffCompare"] = true
788-
templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
788+
_, templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
789789

790790
if len(templateErrs) > 0 {
791791
ctx.Flash.Warning(renderErrorOfTemplates(ctx, templateErrs), true)

routers/web/repo/issue.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,11 @@ func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull
804804
return labels
805805
}
806806

807-
func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) map[string]error {
807+
// Tries to load and set an issue template. The first return value indicates if a template was loaded.
808+
func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) (bool, map[string]error) {
808809
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
809810
if err != nil {
810-
return nil
811+
return false, nil
811812
}
812813

813814
templateCandidates := make([]string, 0, 1+len(possibleFiles))
@@ -870,20 +871,15 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles
870871
ctx.Data["label_ids"] = strings.Join(labelIDs, ",")
871872
ctx.Data["Reference"] = template.Ref
872873
ctx.Data["RefEndName"] = git.RefName(template.Ref).ShortName()
873-
return templateErrs
874+
return true, templateErrs
874875
}
875-
return templateErrs
876+
return false, templateErrs
876877
}
877878

878879
// NewIssue render creating issue page
879880
func NewIssue(ctx *context.Context) {
880881
issueConfig, _ := issue_service.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
881882
hasTemplates := issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo)
882-
if !issueConfig.BlankIssuesEnabled && hasTemplates {
883-
// The "issues/new" and "issues/new/choose" share the same query parameters "project" and "milestone", if blank issues are disabled, just redirect to the "issues/choose" page with these parameters.
884-
ctx.Redirect(fmt.Sprintf("%s/issues/new/choose?%s", ctx.Repo.Repository.Link(), ctx.Req.URL.RawQuery), http.StatusSeeOther)
885-
return
886-
}
887883

888884
ctx.Data["Title"] = ctx.Tr("repo.issues.new")
889885
ctx.Data["PageIsIssueList"] = true
@@ -930,7 +926,8 @@ func NewIssue(ctx *context.Context) {
930926
RetrieveRepoMetas(ctx, ctx.Repo.Repository, false)
931927

932928
_, templateErrs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
933-
if errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates); len(errs) > 0 {
929+
templateLoaded, errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
930+
if len(errs) > 0 {
934931
for k, v := range errs {
935932
templateErrs[k] = v
936933
}
@@ -945,6 +942,12 @@ func NewIssue(ctx *context.Context) {
945942

946943
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(unit.TypeIssues)
947944

945+
if !issueConfig.BlankIssuesEnabled && hasTemplates && !templateLoaded {
946+
// The "issues/new" and "issues/new/choose" share the same query parameters "project" and "milestone", if blank issues are disabled, just redirect to the "issues/choose" page with these parameters.
947+
ctx.Redirect(fmt.Sprintf("%s/issues/new/choose?%s", ctx.Repo.Repository.Link(), ctx.Req.URL.RawQuery), http.StatusSeeOther)
948+
return
949+
}
950+
948951
ctx.HTML(http.StatusOK, tplIssueNew)
949952
}
950953

0 commit comments

Comments
 (0)