gitea源码

template.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. treeEntry, err := commit.GetTreeEntryByPath(path)
  47. if err != nil {
  48. return GetDefaultTemplateConfig(), err
  49. }
  50. reader, err := treeEntry.Blob().DataAsync()
  51. if err != nil {
  52. log.Debug("DataAsync: %v", err)
  53. return GetDefaultTemplateConfig(), nil
  54. }
  55. defer reader.Close()
  56. configContent, err := io.ReadAll(reader)
  57. if err != nil {
  58. return GetDefaultTemplateConfig(), err
  59. }
  60. issueConfig := GetDefaultTemplateConfig()
  61. if err := yaml.Unmarshal(configContent, &issueConfig); err != nil {
  62. return GetDefaultTemplateConfig(), err
  63. }
  64. for pos, link := range issueConfig.ContactLinks {
  65. if link.Name == "" {
  66. return GetDefaultTemplateConfig(), fmt.Errorf("contact_link at position %d is missing name key", pos+1)
  67. }
  68. if link.URL == "" {
  69. return GetDefaultTemplateConfig(), fmt.Errorf("contact_link at position %d is missing url key", pos+1)
  70. }
  71. if link.About == "" {
  72. return GetDefaultTemplateConfig(), fmt.Errorf("contact_link at position %d is missing about key", pos+1)
  73. }
  74. _, err = url.ParseRequestURI(link.URL)
  75. if err != nil {
  76. return GetDefaultTemplateConfig(), fmt.Errorf("%s is not a valid URL", link.URL)
  77. }
  78. }
  79. return issueConfig, nil
  80. }
  81. // IsTemplateConfig returns if the given path is a issue config file.
  82. func IsTemplateConfig(path string) bool {
  83. for _, configName := range templateConfigCandidates {
  84. if path == configName+".yaml" || path == configName+".yml" {
  85. return true
  86. }
  87. }
  88. return false
  89. }
  90. // ParseTemplatesFromDefaultBranch parses the issue templates in the repo's default branch,
  91. // returns valid templates and the errors of invalid template files (the errors map is guaranteed to be non-nil).
  92. func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (ret struct {
  93. IssueTemplates []*api.IssueTemplate
  94. TemplateErrors map[string]error
  95. },
  96. ) {
  97. ret.TemplateErrors = map[string]error{}
  98. if repo.IsEmpty {
  99. return ret
  100. }
  101. commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
  102. if err != nil {
  103. return ret
  104. }
  105. for _, dirName := range templateDirCandidates {
  106. tree, err := commit.SubTree(dirName)
  107. if err != nil {
  108. log.Debug("get sub tree of %s: %v", dirName, err)
  109. continue
  110. }
  111. entries, err := tree.ListEntries()
  112. if err != nil {
  113. log.Debug("list entries in %s: %v", dirName, err)
  114. return ret
  115. }
  116. for _, entry := range entries {
  117. if !template.CouldBe(entry.Name()) {
  118. continue
  119. }
  120. fullName := path.Join(dirName, entry.Name())
  121. if it, err := template.UnmarshalFromEntry(entry, dirName); err != nil {
  122. ret.TemplateErrors[fullName] = err
  123. } else {
  124. if !strings.HasPrefix(it.Ref, "refs/") { // Assume that the ref intended is always a branch - for tags users should use refs/tags/<ref>
  125. it.Ref = git.BranchPrefix + it.Ref
  126. }
  127. ret.IssueTemplates = append(ret.IssueTemplates, it)
  128. }
  129. }
  130. }
  131. return ret
  132. }
  133. // GetTemplateConfigFromDefaultBranch returns the issue config for this repo.
  134. // It never returns a nil config.
  135. func GetTemplateConfigFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (api.IssueConfig, error) {
  136. if repo.IsEmpty {
  137. return GetDefaultTemplateConfig(), nil
  138. }
  139. commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
  140. if err != nil {
  141. return GetDefaultTemplateConfig(), err
  142. }
  143. for _, configName := range templateConfigCandidates {
  144. if _, err := commit.GetTreeEntryByPath(configName + ".yaml"); err == nil {
  145. return GetTemplateConfig(gitRepo, configName+".yaml", commit)
  146. }
  147. if _, err := commit.GetTreeEntryByPath(configName + ".yml"); err == nil {
  148. return GetTemplateConfig(gitRepo, configName+".yml", commit)
  149. }
  150. }
  151. return GetDefaultTemplateConfig(), nil
  152. }
  153. func HasTemplatesOrContactLinks(repo *repo.Repository, gitRepo *git.Repository) bool {
  154. ret := ParseTemplatesFromDefaultBranch(repo, gitRepo)
  155. if len(ret.IssueTemplates) > 0 {
  156. return true
  157. }
  158. issueConfig, _ := GetTemplateConfigFromDefaultBranch(repo, gitRepo)
  159. return len(issueConfig.ContactLinks) > 0
  160. }