From d28b4fbab0066d62e1abddf1a2058a3a7b741647 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Mon, 31 Oct 2022 16:40:03 +0800 Subject: [PATCH 1/4] fix: deal with markdown template without medadata --- modules/issue/template/unmarshal.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/issue/template/unmarshal.go b/modules/issue/template/unmarshal.go index e695d1e1cc66..9186481a6cad 100644 --- a/modules/issue/template/unmarshal.go +++ b/modules/issue/template/unmarshal.go @@ -95,14 +95,20 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { }{} if typ := it.Type(); typ == api.IssueTemplateTypeMarkdown { - templateBody, err := markdown.ExtractMetadata(string(content), it) - if err != nil { - return nil, err - } - it.Content = templateBody - if it.About == "" { - if _, err := markdown.ExtractMetadata(string(content), compatibleTemplate); err == nil && compatibleTemplate.About != "" { - it.About = compatibleTemplate.About + if templateBody, err := markdown.ExtractMetadata(string(content), it); err != nil { + // can't extract metadata, so it could be a pure markdown. + it.Content = string(content) + it.Name = filepath.Base(it.FileName) + it.About = it.Content + if max := 80; len(it.About) > max { + it.About = it.About[:max] + "..." + } + } else { + it.Content = templateBody + if it.About == "" { + if _, err := markdown.ExtractMetadata(string(content), compatibleTemplate); err == nil && compatibleTemplate.About != "" { + it.About = compatibleTemplate.About + } } } } else if typ == api.IssueTemplateTypeYaml { From 3a86983fb4599f7b7a833dc63bfe64ae0bc4b9f1 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Mon, 31 Oct 2022 16:42:53 +0800 Subject: [PATCH 2/4] fix: ignore files that are neither yaml nor md --- modules/structs/issue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/structs/issue.go b/modules/structs/issue.go index 27ec81f7283f..70f5e1ba8eaa 100644 --- a/modules/structs/issue.go +++ b/modules/structs/issue.go @@ -170,7 +170,7 @@ func (it IssueTemplate) Type() IssueTemplateType { if ext := filepath.Ext(it.FileName); ext == ".md" { return IssueTemplateTypeMarkdown } else if ext == ".yaml" || ext == ".yml" { - return "yaml" + return IssueTemplateTypeYaml } - return IssueTemplateTypeYaml + return "" } From f0623c0b2868b1126ec3585d6e0f552eb53ae9c4 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Mon, 31 Oct 2022 17:03:22 +0800 Subject: [PATCH 3/4] fix: truncate by runes --- modules/issue/template/unmarshal.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/issue/template/unmarshal.go b/modules/issue/template/unmarshal.go index 9186481a6cad..60683d015d1e 100644 --- a/modules/issue/template/unmarshal.go +++ b/modules/issue/template/unmarshal.go @@ -100,8 +100,8 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { it.Content = string(content) it.Name = filepath.Base(it.FileName) it.About = it.Content - if max := 80; len(it.About) > max { - it.About = it.About[:max] + "..." + if max, runes := 80, []rune(it.About); len(runes) > max { + it.About = string(runes[:max]) + "..." } } else { it.Content = templateBody From eda130e321b74fea81abeb822f190f7a32833c41 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Mon, 31 Oct 2022 18:19:02 +0800 Subject: [PATCH 4/4] fix: simplify code and add comments --- modules/issue/template/unmarshal.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/issue/template/unmarshal.go b/modules/issue/template/unmarshal.go index 60683d015d1e..24587b0fed2b 100644 --- a/modules/issue/template/unmarshal.go +++ b/modules/issue/template/unmarshal.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" "gopkg.in/yaml.v2" ) @@ -96,13 +97,20 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { if typ := it.Type(); typ == api.IssueTemplateTypeMarkdown { if templateBody, err := markdown.ExtractMetadata(string(content), it); err != nil { - // can't extract metadata, so it could be a pure markdown. + // The only thing we know here is that we can't extract metadata from the content, + // it's hard to tell if metadata doesn't exist or metadata isn't valid. + // There's an example template: + // + // --- + // # Title + // --- + // Content + // + // It could be a valid markdown with two horizontal lines, or an invalid markdown with wrong metadata. + it.Content = string(content) it.Name = filepath.Base(it.FileName) - it.About = it.Content - if max, runes := 80, []rune(it.About); len(runes) > max { - it.About = string(runes[:max]) + "..." - } + it.About, _ = util.SplitStringAtByteN(it.Content, 80) } else { it.Content = templateBody if it.About == "" {