You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package template
  4. import (
  5. "fmt"
  6. "io"
  7. "path"
  8. "strconv"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/markup/markdown"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/util"
  14. "gopkg.in/yaml.v3"
  15. )
  16. // CouldBe indicates a file with the filename could be a template,
  17. // it is a low cost check before further processing.
  18. func CouldBe(filename string) bool {
  19. it := &api.IssueTemplate{
  20. FileName: filename,
  21. }
  22. return it.Type() != ""
  23. }
  24. // Unmarshal parses out a valid template from the content
  25. func Unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {
  26. it, err := unmarshal(filename, content)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if err := Validate(it); err != nil {
  31. return nil, err
  32. }
  33. return it, nil
  34. }
  35. // UnmarshalFromEntry parses out a valid template from the blob in entry
  36. func UnmarshalFromEntry(entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) {
  37. return unmarshalFromEntry(entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here
  38. }
  39. // UnmarshalFromCommit parses out a valid template from the commit
  40. func UnmarshalFromCommit(commit *git.Commit, filename string) (*api.IssueTemplate, error) {
  41. entry, err := commit.GetTreeEntryByPath(filename)
  42. if err != nil {
  43. return nil, fmt.Errorf("get entry for %q: %w", filename, err)
  44. }
  45. return unmarshalFromEntry(entry, filename)
  46. }
  47. // UnmarshalFromRepo parses out a valid template from the head commit of the branch
  48. func UnmarshalFromRepo(repo *git.Repository, branch, filename string) (*api.IssueTemplate, error) {
  49. commit, err := repo.GetBranchCommit(branch)
  50. if err != nil {
  51. return nil, fmt.Errorf("get commit on branch %q: %w", branch, err)
  52. }
  53. return UnmarshalFromCommit(commit, filename)
  54. }
  55. func unmarshalFromEntry(entry *git.TreeEntry, filename string) (*api.IssueTemplate, error) {
  56. if size := entry.Blob().Size(); size > setting.UI.MaxDisplayFileSize {
  57. return nil, fmt.Errorf("too large: %v > MaxDisplayFileSize", size)
  58. }
  59. r, err := entry.Blob().DataAsync()
  60. if err != nil {
  61. return nil, fmt.Errorf("data async: %w", err)
  62. }
  63. defer r.Close()
  64. content, err := io.ReadAll(r)
  65. if err != nil {
  66. return nil, fmt.Errorf("read all: %w", err)
  67. }
  68. return Unmarshal(filename, content)
  69. }
  70. func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {
  71. it := &api.IssueTemplate{
  72. FileName: filename,
  73. }
  74. // Compatible with treating description as about
  75. compatibleTemplate := &struct {
  76. About string `yaml:"description"`
  77. }{}
  78. if typ := it.Type(); typ == api.IssueTemplateTypeMarkdown {
  79. if templateBody, err := markdown.ExtractMetadata(string(content), it); err != nil {
  80. // The only thing we know here is that we can't extract metadata from the content,
  81. // it's hard to tell if metadata doesn't exist or metadata isn't valid.
  82. // There's an example template:
  83. //
  84. // ---
  85. // # Title
  86. // ---
  87. // Content
  88. //
  89. // It could be a valid markdown with two horizontal lines, or an invalid markdown with wrong metadata.
  90. it.Content = string(content)
  91. it.Name = path.Base(it.FileName) // paths in Git are always '/' separated - do not use filepath!
  92. it.About, _ = util.SplitStringAtByteN(it.Content, 80)
  93. } else {
  94. it.Content = templateBody
  95. if it.About == "" {
  96. if _, err := markdown.ExtractMetadata(string(content), compatibleTemplate); err == nil && compatibleTemplate.About != "" {
  97. it.About = compatibleTemplate.About
  98. }
  99. }
  100. }
  101. } else if typ == api.IssueTemplateTypeYaml {
  102. if err := yaml.Unmarshal(content, it); err != nil {
  103. return nil, fmt.Errorf("yaml unmarshal: %w", err)
  104. }
  105. if it.About == "" {
  106. if err := yaml.Unmarshal(content, compatibleTemplate); err == nil && compatibleTemplate.About != "" {
  107. it.About = compatibleTemplate.About
  108. }
  109. }
  110. for i, v := range it.Fields {
  111. if v.ID == "" {
  112. v.ID = strconv.Itoa(i)
  113. }
  114. }
  115. }
  116. return it, nil
  117. }