summaryrefslogtreecommitdiffstats
path: root/modules/context/repo.go
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-09-02 15:58:49 +0800
committerGitHub <noreply@github.com>2022-09-02 15:58:49 +0800
commit84447df4d366324ab81894b028b00fd66be85caf (patch)
tree5291442a85faccb6bc17b54ca71a53c16530dfe3 /modules/context/repo.go
parentb7a4b45ff83dc19febcfb85279215ea6bd224033 (diff)
downloadgitea-84447df4d366324ab81894b028b00fd66be85caf.tar.gz
gitea-84447df4d366324ab81894b028b00fd66be85caf.zip
Support Issue forms and PR forms (#20987)
* feat: extend issue template for yaml * feat: support yaml template * feat: render form to markdown * feat: support yaml template for pr * chore: rename to Fields * feat: template unmarshal * feat: split template * feat: render to markdown * feat: use full name as template file name * chore: remove useless file * feat: use dropdown of fomantic ui * feat: update input style * docs: more comments * fix: render text without render * chore: fix lint error * fix: support use description as about in markdown * fix: add field class in form * chore: generate swagger * feat: validate template * feat: support is_nummber and regex * test: fix broken unit tests * fix: ignore empty body of md template * fix: make multiple easymde editors work in one page * feat: better UI * fix: js error in pr form * chore: generate swagger * feat: support regex validation * chore: generate swagger * fix: refresh each markdown editor * chore: give up required validation * fix: correct issue template candidates * fix: correct checkboxes style * chore: ignore .hugo_build.lock in docs * docs: separate out a new doc for merge templates * docs: introduce syntax of yaml template * feat: show a alert for invalid templates * test: add case for a valid template * fix: correct attributes of required checkbox * fix: add class not-under-easymde for dropzone * fix: use more back-quotes * chore: remove translation in zh-CN * fix EasyMDE statusbar margin * fix: remove repeated blocks * fix: reuse regex for quotes Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/context/repo.go')
-rw-r--r--modules/context/repo.go71
1 files changed, 26 insertions, 45 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go
index 5404acc05a..6a336c45f7 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -9,7 +9,6 @@ import (
"context"
"fmt"
"html"
- "io"
"net/http"
"net/url"
"path"
@@ -26,8 +25,8 @@ import (
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
code_indexer "code.gitea.io/gitea/modules/indexer/code"
+ "code.gitea.io/gitea/modules/issue/template"
"code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/markup/markdown"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -1034,70 +1033,52 @@ func UnitTypes() func(ctx *Context) {
}
}
-// IssueTemplatesFromDefaultBranch checks for issue templates in the repo's default branch
-func (ctx *Context) IssueTemplatesFromDefaultBranch() []api.IssueTemplate {
- var issueTemplates []api.IssueTemplate
+// IssueTemplatesFromDefaultBranch checks for valid issue templates in the repo's default branch,
+func (ctx *Context) IssueTemplatesFromDefaultBranch() []*api.IssueTemplate {
+ ret, _ := ctx.IssueTemplatesErrorsFromDefaultBranch()
+ return ret
+}
+
+// IssueTemplatesErrorsFromDefaultBranch checks for issue templates in the repo's default branch,
+// returns valid templates and the errors of invalid template files.
+func (ctx *Context) IssueTemplatesErrorsFromDefaultBranch() ([]*api.IssueTemplate, map[string]error) {
+ var issueTemplates []*api.IssueTemplate
if ctx.Repo.Repository.IsEmpty {
- return issueTemplates
+ return issueTemplates, nil
}
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
+ return issueTemplates, nil
}
}
+ invalidFiles := map[string]error{}
for _, dirName := range IssueTemplateDirCandidates {
tree, err := ctx.Repo.Commit.SubTree(dirName)
if err != nil {
+ log.Debug("get sub tree of %s: %v", dirName, err)
continue
}
entries, err := tree.ListEntries()
if err != nil {
- return issueTemplates
+ log.Debug("list entries in %s: %v", dirName, err)
+ return issueTemplates, nil
}
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
- }
- closed := false
- defer func() {
- if !closed {
- _ = r.Close()
- }
- }()
- data, err := io.ReadAll(r)
- if err != nil {
- log.Debug("ReadAll: %v", err)
- continue
- }
- _ = r.Close()
- 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 !template.CouldBe(entry.Name()) {
+ continue
+ }
+ fullName := path.Join(dirName, entry.Name())
+ if it, err := template.UnmarshalFromEntry(entry, dirName); err != nil {
+ invalidFiles[fullName] = err
+ } else {
+ issueTemplates = append(issueTemplates, it)
}
- }
- if len(issueTemplates) > 0 {
- return issueTemplates
}
}
- return issueTemplates
+ return issueTemplates, invalidFiles
}