@@ -65,6 +65,7 @@ const (
6565
6666 issueTemplateKey = "IssueTemplate"
6767 issueFormTemplateKey = "IssueFormTemplate"
68+ issueFormErrorsKey = "IssueTemplateErrors"
6869 issueTemplateTitleKey = "IssueTemplateTitle"
6970)
7071
@@ -408,7 +409,8 @@ func Issues(ctx *context.Context) {
408409 }
409410 ctx .Data ["Title" ] = ctx .Tr ("repo.issues" )
410411 ctx .Data ["PageIsIssueList" ] = true
411- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
412+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
413+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
412414 }
413415
414416 issues (ctx , ctx .FormInt64 ("milestone" ), ctx .FormInt64 ("project" ), util .OptionalBoolOf (isPullList ))
@@ -751,7 +753,9 @@ func getFileContentFromDefaultBranch(repo *context.Repository, filename string)
751753 return string (bytes ), true
752754}
753755
754- func getTemplate (repo * context.Repository , template string , possibleDirs , possibleFiles []string ) (* api.IssueTemplate , string , * api.IssueFormTemplate , error ) {
756+ func getTemplate (repo * context.Repository , template string , possibleDirs , possibleFiles []string ) (* api.IssueTemplate , string , * api.IssueFormTemplate , map [string ][]string , error ) {
757+ validationErrs := make (map [string ][]string )
758+
755759 // Add `possibleFiles` and each `{possibleDirs}/{template}` to `templateCandidates`
756760 templateCandidates := make ([]string , 0 , len (possibleFiles ))
757761 if template != "" {
@@ -772,36 +776,42 @@ func getTemplate(repo *context.Repository, template string, possibleDirs, possib
772776
773777 if strings .HasSuffix (filename , ".md" ) {
774778 // Parse markdown template
775- templateBody , err = markdown .ExtractMetadata (templateContent , meta )
779+ templateBody , err = markdown .ExtractMetadata (templateContent , & meta )
776780 } else if strings .HasSuffix (filename , ".yaml" ) || strings .HasSuffix (filename , ".yml" ) {
777781 // Parse yaml (form) template
778- formTemplateBody , err = context .ExtractTemplateFromYaml ([]byte (templateContent ), & meta )
779- formTemplateBody .FileName = path .Base (filename )
782+ var tmplValidationErrs []string
783+ formTemplateBody , tmplValidationErrs , err = context .ExtractTemplateFromYaml ([]byte (templateContent ), & meta )
784+ if err == nil {
785+ formTemplateBody .FileName = path .Base (filename )
786+ } else if tmplValidationErrs != nil {
787+ validationErrs [path .Base (filename )] = tmplValidationErrs
788+ }
780789 } else {
781790 err = errors .New ("invalid template type" )
782791 }
783792 if err != nil {
784793 log .Debug ("could not extract metadata from %s [%s]: %v" , filename , repo .Repository .FullName (), err )
785- templateBody = templateContent
786- err = nil
787794 }
788795
789- return & meta , templateBody , formTemplateBody , err
796+ return & meta , templateBody , formTemplateBody , validationErrs , err
790797 }
791798 }
792799
793- return nil , "" , nil , errors .New ("no template found" )
800+ return nil , "" , nil , validationErrs , errors .New ("no template found" )
794801}
795802
796803func setTemplateIfExists (ctx * context.Context , ctxDataKey string , possibleDirs , possibleFiles []string ) {
797- templateMeta , templateBody , formTemplateBody , err := getTemplate (ctx .Repo , ctx .FormString ("template" ), possibleDirs , possibleFiles )
804+ templateMeta , templateBody , formTemplateBody , validationErrs , err := getTemplate (ctx .Repo , ctx .FormString ("template" ), possibleDirs , possibleFiles )
798805 if err != nil {
799806 return
800807 }
801808
802809 if formTemplateBody != nil {
803810 ctx .Data [issueFormTemplateKey ] = formTemplateBody
804811 }
812+ if validationErrs != nil && len (validationErrs ) > 0 {
813+ ctx .Data [issueFormErrorsKey ] = validationErrs
814+ }
805815
806816 ctx .Data [issueTemplateTitleKey ] = templateMeta .Title
807817 ctx .Data [ctxDataKey ] = templateBody
@@ -836,7 +846,8 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleDirs,
836846func NewIssue (ctx * context.Context ) {
837847 ctx .Data ["Title" ] = ctx .Tr ("repo.issues.new" )
838848 ctx .Data ["PageIsIssueList" ] = true
839- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
849+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
850+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
840851 ctx .Data ["RequireTribute" ] = true
841852 ctx .Data ["PullRequestWorkInProgressPrefixes" ] = setting .Repository .PullRequest .WorkInProgressPrefixes
842853 title := ctx .FormString ("title" )
@@ -893,7 +904,10 @@ func NewIssueChooseTemplate(ctx *context.Context) {
893904 ctx .Data ["Title" ] = ctx .Tr ("repo.issues.new" )
894905 ctx .Data ["PageIsIssueList" ] = true
895906
896- issueTemplates := ctx .IssueTemplatesFromDefaultBranch ()
907+ issueTemplates , validationErrs := ctx .IssueTemplatesFromDefaultBranch ()
908+ if validationErrs != nil && len (validationErrs ) > 0 {
909+ ctx .Data [issueFormErrorsKey ] = validationErrs
910+ }
897911 ctx .Data ["IssueTemplates" ] = issueTemplates
898912
899913 if len (issueTemplates ) == 0 {
@@ -1039,7 +1053,7 @@ func renderIssueFormValues(ctx *context.Context, form *url.Values) (string, erro
10391053 }
10401054
10411055 // Fetch template
1042- _ , _ , formTemplateBody , err := getTemplate (
1056+ _ , _ , formTemplateBody , _ , err := getTemplate (
10431057 ctx .Repo ,
10441058 form .Get ("form-type" ),
10451059 context .IssueTemplateDirCandidates ,
@@ -1094,7 +1108,8 @@ func NewIssuePost(ctx *context.Context) {
10941108 form := web .GetForm (ctx ).(* forms.CreateIssueForm )
10951109 ctx .Data ["Title" ] = ctx .Tr ("repo.issues.new" )
10961110 ctx .Data ["PageIsIssueList" ] = true
1097- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
1111+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
1112+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
10981113 ctx .Data ["PullRequestWorkInProgressPrefixes" ] = setting .Repository .PullRequest .WorkInProgressPrefixes
10991114 ctx .Data ["IsAttachmentEnabled" ] = setting .Attachment .Enabled
11001115 upload .AddUploadContext (ctx , "comment" )
@@ -1287,7 +1302,8 @@ func ViewIssue(ctx *context.Context) {
12871302 return
12881303 }
12891304 ctx .Data ["PageIsIssueList" ] = true
1290- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
1305+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
1306+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
12911307 }
12921308
12931309 if issue .IsPull && ! ctx .Repo .CanRead (unit .TypeIssues ) {
0 commit comments