@@ -65,6 +65,7 @@ const (
65
65
66
66
issueTemplateKey = "IssueTemplate"
67
67
issueFormTemplateKey = "IssueFormTemplate"
68
+ issueFormErrorsKey = "IssueTemplateErrors"
68
69
issueTemplateTitleKey = "IssueTemplateTitle"
69
70
)
70
71
@@ -408,7 +409,8 @@ func Issues(ctx *context.Context) {
408
409
}
409
410
ctx .Data ["Title" ] = ctx .Tr ("repo.issues" )
410
411
ctx .Data ["PageIsIssueList" ] = true
411
- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
412
+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
413
+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
412
414
}
413
415
414
416
issues (ctx , ctx .FormInt64 ("milestone" ), ctx .FormInt64 ("project" ), util .OptionalBoolOf (isPullList ))
@@ -751,7 +753,9 @@ func getFileContentFromDefaultBranch(repo *context.Repository, filename string)
751
753
return string (bytes ), true
752
754
}
753
755
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
+
755
759
// Add `possibleFiles` and each `{possibleDirs}/{template}` to `templateCandidates`
756
760
templateCandidates := make ([]string , 0 , len (possibleFiles ))
757
761
if template != "" {
@@ -772,36 +776,42 @@ func getTemplate(repo *context.Repository, template string, possibleDirs, possib
772
776
773
777
if strings .HasSuffix (filename , ".md" ) {
774
778
// Parse markdown template
775
- templateBody , err = markdown .ExtractMetadata (templateContent , meta )
779
+ templateBody , err = markdown .ExtractMetadata (templateContent , & meta )
776
780
} else if strings .HasSuffix (filename , ".yaml" ) || strings .HasSuffix (filename , ".yml" ) {
777
781
// 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
+ }
780
789
} else {
781
790
err = errors .New ("invalid template type" )
782
791
}
783
792
if err != nil {
784
793
log .Debug ("could not extract metadata from %s [%s]: %v" , filename , repo .Repository .FullName (), err )
785
- templateBody = templateContent
786
- err = nil
787
794
}
788
795
789
- return & meta , templateBody , formTemplateBody , err
796
+ return & meta , templateBody , formTemplateBody , validationErrs , err
790
797
}
791
798
}
792
799
793
- return nil , "" , nil , errors .New ("no template found" )
800
+ return nil , "" , nil , validationErrs , errors .New ("no template found" )
794
801
}
795
802
796
803
func 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 )
798
805
if err != nil {
799
806
return
800
807
}
801
808
802
809
if formTemplateBody != nil {
803
810
ctx .Data [issueFormTemplateKey ] = formTemplateBody
804
811
}
812
+ if validationErrs != nil && len (validationErrs ) > 0 {
813
+ ctx .Data [issueFormErrorsKey ] = validationErrs
814
+ }
805
815
806
816
ctx .Data [issueTemplateTitleKey ] = templateMeta .Title
807
817
ctx .Data [ctxDataKey ] = templateBody
@@ -836,7 +846,8 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleDirs,
836
846
func NewIssue (ctx * context.Context ) {
837
847
ctx .Data ["Title" ] = ctx .Tr ("repo.issues.new" )
838
848
ctx .Data ["PageIsIssueList" ] = true
839
- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
849
+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
850
+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
840
851
ctx .Data ["RequireTribute" ] = true
841
852
ctx .Data ["PullRequestWorkInProgressPrefixes" ] = setting .Repository .PullRequest .WorkInProgressPrefixes
842
853
title := ctx .FormString ("title" )
@@ -893,7 +904,10 @@ func NewIssueChooseTemplate(ctx *context.Context) {
893
904
ctx .Data ["Title" ] = ctx .Tr ("repo.issues.new" )
894
905
ctx .Data ["PageIsIssueList" ] = true
895
906
896
- issueTemplates := ctx .IssueTemplatesFromDefaultBranch ()
907
+ issueTemplates , validationErrs := ctx .IssueTemplatesFromDefaultBranch ()
908
+ if validationErrs != nil && len (validationErrs ) > 0 {
909
+ ctx .Data [issueFormErrorsKey ] = validationErrs
910
+ }
897
911
ctx .Data ["IssueTemplates" ] = issueTemplates
898
912
899
913
if len (issueTemplates ) == 0 {
@@ -1039,7 +1053,7 @@ func renderIssueFormValues(ctx *context.Context, form *url.Values) (string, erro
1039
1053
}
1040
1054
1041
1055
// Fetch template
1042
- _ , _ , formTemplateBody , err := getTemplate (
1056
+ _ , _ , formTemplateBody , _ , err := getTemplate (
1043
1057
ctx .Repo ,
1044
1058
form .Get ("form-type" ),
1045
1059
context .IssueTemplateDirCandidates ,
@@ -1094,7 +1108,8 @@ func NewIssuePost(ctx *context.Context) {
1094
1108
form := web .GetForm (ctx ).(* forms.CreateIssueForm )
1095
1109
ctx .Data ["Title" ] = ctx .Tr ("repo.issues.new" )
1096
1110
ctx .Data ["PageIsIssueList" ] = true
1097
- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
1111
+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
1112
+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
1098
1113
ctx .Data ["PullRequestWorkInProgressPrefixes" ] = setting .Repository .PullRequest .WorkInProgressPrefixes
1099
1114
ctx .Data ["IsAttachmentEnabled" ] = setting .Attachment .Enabled
1100
1115
upload .AddUploadContext (ctx , "comment" )
@@ -1287,7 +1302,8 @@ func ViewIssue(ctx *context.Context) {
1287
1302
return
1288
1303
}
1289
1304
ctx .Data ["PageIsIssueList" ] = true
1290
- ctx .Data ["NewIssueChooseTemplate" ] = len (ctx .IssueTemplatesFromDefaultBranch ()) > 0
1305
+ issueTemplates , _ := ctx .IssueTemplatesFromDefaultBranch ()
1306
+ ctx .Data ["NewIssueChooseTemplate" ] = len (issueTemplates ) > 0
1291
1307
}
1292
1308
1293
1309
if issue .IsPull && ! ctx .Repo .CanRead (unit .TypeIssues ) {
0 commit comments