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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // GetTemplatesFromDefaultBranch checks for issue templates in the repo's default branch,
  92. // returns valid templates and the errors of invalid template files.
  93. func GetTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) ([]*api.IssueTemplate, map[string]error) {
  94. var issueTemplates []*api.IssueTemplate
  95. if repo.IsEmpty {
  96. return issueTemplates, nil
  97. }
  98. commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
  99. if err != nil {
  100. return issueTemplates, nil
  101. }
  102. invalidFiles := map[string]error{}
  103. for _, dirName := range templateDirCandidates {
  104. tree, err := commit.SubTree(dirName)
  105. if err != nil {
  106. log.Debug("get sub tree of %s: %v", dirName, err)
  107. continue
  108. }
  109. entries, err := tree.ListEntries()
  110. if err != nil {
  111. log.Debug("list entries in %s: %v", dirName, err)
  112. return issueTemplates, nil
  113. }
  114. for _, entry := range entries {
  115. if !template.CouldBe(entry.Name()) {
  116. continue
  117. }
  118. fullName := path.Join(dirName, entry.Name())
  119. if it, err := template.UnmarshalFromEntry(entry, dirName); err != nil {
  120. invalidFiles[fullName] = err
  121. } else {
  122. if !strings.HasPrefix(it.Ref, "refs/") { // Assume that the ref intended is always a branch - for tags users should use refs/tags/<ref>
  123. it.Ref = git.BranchPrefix + it.Ref
  124. }
  125. issueTemplates = append(issueTemplates, it)
  126. }
  127. }
  128. }
  129. return issueTemplates, invalidFiles
  130. }
  131. // GetTemplateConfigFromDefaultBranch returns the issue config for this repo.
  132. // It never returns a nil config.
  133. func GetTemplateConfigFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (api.IssueConfig, error) {
  134. if repo.IsEmpty {
  135. return GetDefaultTemplateConfig(), nil
  136. }
  137. commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
  138. if err != nil {
  139. return GetDefaultTemplateConfig(), err
  140. }
  141. for _, configName := range templateConfigCandidates {
  142. if _, err := commit.GetTreeEntryByPath(configName + ".yaml"); err == nil {
  143. return GetTemplateConfig(gitRepo, configName+".yaml", commit)
  144. }
  145. if _, err := commit.GetTreeEntryByPath(configName + ".yml"); err == nil {
  146. return GetTemplateConfig(gitRepo, configName+".yml", commit)
  147. }
  148. }
  149. return GetDefaultTemplateConfig(), nil
  150. }
  151. func HasTemplatesOrContactLinks(repo *repo.Repository, gitRepo *git.Repository) bool {
  152. ret, _ := GetTemplatesFromDefaultBranch(repo, gitRepo)
  153. if len(ret) > 0 {
  154. return true
  155. }
  156. issueConfig, _ := GetTemplateConfigFromDefaultBranch(repo, gitRepo)
  157. return len(issueConfig.ContactLinks) > 0
  158. }