summaryrefslogtreecommitdiffstats
path: root/modules/context/repo.go
diff options
context:
space:
mode:
authorJohn Olheiser <john.olheiser@gmail.com>2020-09-11 09:48:39 -0500
committerGitHub <noreply@github.com>2020-09-11 10:48:39 -0400
commit26c4a049da178993e5ccddcb50e7edc70a6bde5d (patch)
tree494106117720ff3ad5f9e77a380c9397c3cfe10b /modules/context/repo.go
parentdd1a651b5895cfdb8a141a56aa824ed4d082c41a (diff)
downloadgitea-26c4a049da178993e5ccddcb50e7edc70a6bde5d.tar.gz
gitea-26c4a049da178993e5ccddcb50e7edc70a6bde5d.zip
Issue templates directory (#11450)
* Issue templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add some comments, appease the linter Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add docs and re-use dir candidates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add default labels to issue templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Generate swagger Signed-off-by: jolheiser <john.olheiser@gmail.com> * Suggested changes Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update issue.go * Suggestions Signed-off-by: jolheiser <john.olheiser@gmail.com> * Extract metadata from legacy if possible Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules/context/repo.go')
-rw-r--r--modules/context/repo.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go
index 4aac0c05aa..2c77361460 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -16,13 +16,27 @@ import (
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
+ api "code.gitea.io/gitea/modules/structs"
"gitea.com/macaron/macaron"
"github.com/editorconfig/editorconfig-core-go/v2"
"github.com/unknwon/com"
)
+// IssueTemplateDirCandidates issue templates directory
+var IssueTemplateDirCandidates = []string{
+ "ISSUE_TEMPLATE",
+ "issue_template",
+ ".gitea/ISSUE_TEMPLATE",
+ ".gitea/issue_template",
+ ".github/ISSUE_TEMPLATE",
+ ".github/issue_template",
+ ".gitlab/ISSUE_TEMPLATE",
+ ".gitlab/issue_template",
+}
+
// PullRequest contains informations to make a pull request
type PullRequest struct {
BaseRepo *models.Repository
@@ -821,3 +835,60 @@ func UnitTypes() macaron.Handler {
ctx.Data["UnitTypeProjects"] = models.UnitTypeProjects
}
}
+
+// IssueTemplatesFromDefaultBranch checks for issue templates in the repo's default branch
+func (ctx *Context) IssueTemplatesFromDefaultBranch() []api.IssueTemplate {
+ var issueTemplates []api.IssueTemplate
+ if ctx.Repo.Commit == nil {
+ var err error
+ ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
+ if err != nil {
+ return issueTemplates
+ }
+ }
+
+ for _, dirName := range IssueTemplateDirCandidates {
+ tree, err := ctx.Repo.Commit.SubTree(dirName)
+ if err != nil {
+ continue
+ }
+ entries, err := tree.ListEntries()
+ if err != nil {
+ return issueTemplates
+ }
+ for _, entry := range entries {
+ if strings.HasSuffix(entry.Name(), ".md") {
+ if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
+ log.Debug("Issue template is too large: %s", entry.Name())
+ continue
+ }
+ r, err := entry.Blob().DataAsync()
+ if err != nil {
+ log.Debug("DataAsync: %v", err)
+ continue
+ }
+ defer r.Close()
+ data, err := ioutil.ReadAll(r)
+ if err != nil {
+ log.Debug("ReadAll: %v", err)
+ continue
+ }
+ var it api.IssueTemplate
+ content, err := markdown.ExtractMetadata(string(data), &it)
+ if err != nil {
+ log.Debug("ExtractMetadata: %v", err)
+ continue
+ }
+ it.Content = content
+ it.FileName = entry.Name()
+ if it.Valid() {
+ issueTemplates = append(issueTemplates, it)
+ }
+ }
+ }
+ if len(issueTemplates) > 0 {
+ return issueTemplates
+ }
+ }
+ return issueTemplates
+}