Skip to content

Commit 8d0343e

Browse files
JakobDevsilverwinddelvh
authored
Fix issue templates when blank isses are disabled (#27061)
Fixes #27060 --------- Co-authored-by: silverwind <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent 198a9ca commit 8d0343e

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ func CompareDiff(ctx *context.Context) {
804804

805805
ctx.Data["IsRepoToolbarCommits"] = true
806806
ctx.Data["IsDiffCompare"] = true
807-
templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
807+
_, templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
808808

809809
if len(templateErrs) > 0 {
810810
ctx.Flash.Warning(renderErrorOfTemplates(ctx, templateErrs), true)

routers/web/repo/issue.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,11 @@ func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull
830830
return labels
831831
}
832832

833-
func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) map[string]error {
833+
// Tries to load and set an issue template. The first return value indicates if a template was loaded.
834+
func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) (bool, map[string]error) {
834835
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
835836
if err != nil {
836-
return nil
837+
return false, nil
837838
}
838839

839840
templateCandidates := make([]string, 0, 1+len(possibleFiles))
@@ -896,20 +897,15 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles
896897
ctx.Data["label_ids"] = strings.Join(labelIDs, ",")
897898
ctx.Data["Reference"] = template.Ref
898899
ctx.Data["RefEndName"] = git.RefName(template.Ref).ShortName()
899-
return templateErrs
900+
return true, templateErrs
900901
}
901-
return templateErrs
902+
return false, templateErrs
902903
}
903904

904905
// NewIssue render creating issue page
905906
func NewIssue(ctx *context.Context) {
906907
issueConfig, _ := issue_service.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
907908
hasTemplates := issue_service.HasTemplatesOrContactLinks(ctx.Repo.Repository, ctx.Repo.GitRepo)
908-
if !issueConfig.BlankIssuesEnabled && hasTemplates {
909-
// 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.
910-
ctx.Redirect(fmt.Sprintf("%s/issues/new/choose?%s", ctx.Repo.Repository.Link(), ctx.Req.URL.RawQuery), http.StatusSeeOther)
911-
return
912-
}
913909

914910
ctx.Data["Title"] = ctx.Tr("repo.issues.new")
915911
ctx.Data["PageIsIssueList"] = true
@@ -963,7 +959,8 @@ func NewIssue(ctx *context.Context) {
963959
ctx.Data["Tags"] = tags
964960

965961
_, templateErrs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
966-
if errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates); len(errs) > 0 {
962+
templateLoaded, errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
963+
if len(errs) > 0 {
967964
for k, v := range errs {
968965
templateErrs[k] = v
969966
}
@@ -978,6 +975,12 @@ func NewIssue(ctx *context.Context) {
978975

979976
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(unit.TypeIssues)
980977

978+
if !issueConfig.BlankIssuesEnabled && hasTemplates && !templateLoaded {
979+
// 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.
980+
ctx.Redirect(fmt.Sprintf("%s/issues/new/choose?%s", ctx.Repo.Repository.Link(), ctx.Req.URL.RawQuery), http.StatusSeeOther)
981+
return
982+
}
983+
981984
ctx.HTML(http.StatusOK, tplIssueNew)
982985
}
983986

0 commit comments

Comments
 (0)