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.

template.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issue
  4. import (
  5. "fmt"
  6. "io"
  7. "net/url"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/issue/template"
  13. "code.gitea.io/gitea/modules/log"
  14. api "code.gitea.io/gitea/modules/structs"
  15. "gopkg.in/yaml.v3"
  16. )
  17. // templateDirCandidates issue templates directory
  18. var templateDirCandidates = []string{
  19. "ISSUE_TEMPLATE",
  20. "issue_template",
  21. ".gitea/ISSUE_TEMPLATE",
  22. ".gitea/issue_template",
  23. ".github/ISSUE_TEMPLATE",
  24. ".github/issue_template",
  25. ".gitlab/ISSUE_TEMPLATE",
  26. ".gitlab/issue_template",
  27. }
  28. var templateConfigCandidates = []string{
  29. ".gitea/ISSUE_TEMPLATE/config",
  30. ".gitea/issue_template/config",
  31. ".github/ISSUE_TEMPLATE/config",
  32. ".github/issue_template/config",
  33. }
  34. func GetDefaultTemplateConfig() api.IssueConfig {
  35. return api.IssueConfig{
  36. BlankIssuesEnabled: true,
  37. ContactLinks: make([]api.IssueConfigContactLink, 0),
  38. }
  39. }
  40. // GetTemplateConfig loads the given issue config file.
  41. // It never returns a nil config.
  42. func GetTemplateConfig(gitRepo *git.Repository, path string, commit *git.Commit) (api.IssueConfig, error) {
  43. if gitRepo == nil {
  44. return GetDefaultTemplateConfig(), nil
  45. }
  46. var err error
  47. treeEntry, err := commit.GetTreeEntryByPath(path)
  48. if err != nil {
  49. return GetDefaultTemplateConfig(), err
  50. }
  51. reader, err := treeEntry.Blob().DataAsync()
  52. if err != nil {
  53. log.Debug("DataAsync: %v", err)
  54. return GetDefaultTemplateConfig(), nil
  55. }
  56. defer reader.Close()
  57. configContent, err := io.ReadAll(reader)
  58. if err != nil {
  59. return GetDefaultTemplateConfig(), err
  60. }
  61. issueConfig := GetDefaultTemplateConfig()
  62. if err := yaml.Unmarshal(configContent, &issueConfig); err != nil {
  63. return GetDefaultTemplateConfig(), err
  64. }
  65. for pos, link := range issueConfig.ContactLinks {
  66. if link.Name == "" {
  67. return GetDefaultTemplateConfig(), fmt.Errorf("contact_link at position %d is missing name key", pos+1)
  68. }
  69. if link.URL == "" {
  70. return GetDefaultTemplateConfig(), fmt.Errorf("contact_link at position %d is missing url key", pos+1)
  71. }
  72. if link.About == "" {
  73. return GetDefaultTemplateConfig(), fmt.Errorf("contact_link at position %d is missing about key", pos+1)
  74. }
  75. _, err = url.ParseRequestURI(link.URL)
  76. if err != nil {
  77. return GetDefaultTemplateConfig(), fmt.Errorf("%s is not a valid URL", link.URL)
  78. }
  79. }
  80. return issueConfig, nil
  81. }
  82. // IsTemplateConfig returns if the given path is a issue config file.
  83. func IsTemplateConfig(path string) bool {
  84. for _, configName := range templateConfigCandidates {
  85. if path == configName+".yaml" || path == configName+".yml" {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. // ParseTemplatesFromDefaultBranch parses the issue templates in the repo's default branch,
  92. // returns valid templates and the errors of invalid template files (the errors map is guaranteed to be non-nil).
  93. func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (ret struct {
  94. IssueTemplates []*api.IssueTemplate
  95. TemplateErrors map[string]error
  96. },
  97. ) {
  98. ret.TemplateErrors = map[string]error{}
  99. if repo.IsEmpty {
  100. return ret
  101. }
  102. commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
  103. if err != nil {
  104. return ret
  105. }
  106. for _, dirName := range templateDirCandidates {
  107. tree, err := commit.SubTree(dirName)
  108. if err != nil {
  109. log.Debug("get sub tree of %s: %v", dirName, err)
  110. continue
  111. }
  112. entries, err := tree.ListEntries()
  113. if err != nil {
  114. log.Debug("list entries in %s: %v", dirName, err)
  115. return ret
  116. }
  117. for _, entry := range entries {
  118. if !template.CouldBe(entry.Name()) {
  119. continue
  120. }
  121. fullName := path.Join(dirName, entry.Name())
  122. if it, err := template.UnmarshalFromEntry(entry, dirName); err != nil {
  123. ret.TemplateErrors[fullName] = err
  124. } else {
  125. if !strings.HasPrefix(it.Ref, "refs/") { // Assume that the ref intended is always a branch - for tags users should use refs/tags/<ref>
  126. it.Ref = git.BranchPrefix + it.Ref
  127. }
  128. ret.IssueTemplates = append(ret.IssueTemplates, it)
  129. }
  130. }
  131. }
  132. return ret
  133. }
  134. // GetTemplateConfigFromDefaultBranch returns the issue config for this repo.
  135. // It never returns a nil config.
  136. func GetTemplateConfigFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (api.IssueConfig, error) {
  137. if repo.IsEmpty {
  138. return GetDefaultTemplateConfig(), nil
  139. }
  140. commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
  141. if err != nil {
  142. return GetDefaultTemplateConfig(), err
  143. }
  144. for _, configName := range templateConfigCandidates {
  145. if _, err := commit.GetTreeEntryByPath(configName + ".yaml"); err == nil {
  146. return GetTemplateConfig(gitRepo, configName+".yaml", commit)
  147. }
  148. if _, err := commit.GetTreeEntryByPath(configName + ".yml"); err == nil {
  149. return GetTemplateConfig(gitRepo, configName+".yml", commit)
  150. }
  151. }
  152. return GetDefaultTemplateConfig(), nil
  153. }
  154. func HasTemplatesOrContactLinks(repo *repo.Repository, gitRepo *git.Repository) bool {
  155. ret := ParseTemplatesFromDefaultBranch(repo, gitRepo)
  156. if len(ret.IssueTemplates) > 0 {
  157. return true
  158. }
  159. issueConfig, _ := GetTemplateConfigFromDefaultBranch(repo, gitRepo)
  160. return len(issueConfig.ContactLinks) > 0
  161. }