Browse Source

Merge pull request #2647 from andreynering/issue-template

Implement issue and pull request templates
tags/v1.2.0-rc1
Unknwon 9 years ago
parent
commit
beaaf5b8f2
3 changed files with 60 additions and 1 deletions
  1. +48
    -0
      routers/repo/issue.go
  2. +11
    -0
      routers/repo/pull.go
  3. +1
    -1
      templates/repo/issue/comment_tab.tmpl

+ 48
- 0
routers/repo/issue.go View File

@@ -7,6 +7,8 @@ package repo
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@@ -34,11 +36,19 @@ const (
MILESTONE base.TplName = "repo/issue/milestones" MILESTONE base.TplName = "repo/issue/milestones"
MILESTONE_NEW base.TplName = "repo/issue/milestone_new" MILESTONE_NEW base.TplName = "repo/issue/milestone_new"
MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit" MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit"

ISSUE_TEMPLATE_KEY = "IssueTemplate"
) )


var ( var (
ErrFileTypeForbidden = errors.New("File type is not allowed") ErrFileTypeForbidden = errors.New("File type is not allowed")
ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded") ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")

IssueTemplateCandidates = []string{
"ISSUE_TEMPLATE.md",
".gogs/ISSUE_TEMPLATE.md",
".github/ISSUE_TEMPLATE.md",
}
) )


func MustEnableIssues(ctx *middleware.Context) { func MustEnableIssues(ctx *middleware.Context) {
@@ -281,9 +291,47 @@ func RetrieveRepoMetas(ctx *middleware.Context, repo *models.Repository) []*mode
return labels return labels
} }


func getFileContentFromDefaultBranch(ctx *middleware.Context, filename string) (string, bool) {
var r io.Reader
var bytes []byte

if ctx.Repo.Commit == nil {
var err error
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
return "", false
}
}

entry, err := ctx.Repo.Commit.GetTreeEntryByPath(filename)
if err != nil {
return "", false
}
r, err = entry.Blob().Data()
if err != nil {
return "", false
}
bytes, err = ioutil.ReadAll(r)
if err != nil {
return "", false
}
return string(bytes), true
}

func setTemplateIfExists(ctx *middleware.Context, ctxDataKey string, possibleFiles []string) {
for _, filename := range possibleFiles {
content, found := getFileContentFromDefaultBranch(ctx, filename)
if found {
ctx.Data[ctxDataKey] = content
return
}
}
}

func NewIssue(ctx *middleware.Context) { func NewIssue(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["Title"] = ctx.Tr("repo.issues.new")
ctx.Data["PageIsIssueList"] = true ctx.Data["PageIsIssueList"] = true
setTemplateIfExists(ctx, ISSUE_TEMPLATE_KEY, IssueTemplateCandidates)
renderAttachmentSettings(ctx) renderAttachmentSettings(ctx)


RetrieveRepoMetas(ctx, ctx.Repo.Repository) RetrieveRepoMetas(ctx, ctx.Repo.Repository)


+ 11
- 0
routers/repo/pull.go View File

@@ -26,6 +26,16 @@ const (
COMPARE_PULL base.TplName = "repo/pulls/compare" COMPARE_PULL base.TplName = "repo/pulls/compare"
PULL_COMMITS base.TplName = "repo/pulls/commits" PULL_COMMITS base.TplName = "repo/pulls/commits"
PULL_FILES base.TplName = "repo/pulls/files" PULL_FILES base.TplName = "repo/pulls/files"

PULL_REQUEST_TEMPLATE_KEY = "PullRequestTemplate"
)

var (
PullRequestTemplateCandidates = []string{
"PULL_REQUEST.md",
".gogs/PULL_REQUEST.md",
".github/PULL_REQUEST.md",
}
) )


func getForkRepository(ctx *middleware.Context) *models.Repository { func getForkRepository(ctx *middleware.Context) *models.Repository {
@@ -540,6 +550,7 @@ func CompareAndPullRequest(ctx *middleware.Context) {
ctx.Data["PageIsComparePull"] = true ctx.Data["PageIsComparePull"] = true
ctx.Data["IsDiffCompare"] = true ctx.Data["IsDiffCompare"] = true
ctx.Data["RequireHighlightJS"] = true ctx.Data["RequireHighlightJS"] = true
setTemplateIfExists(ctx, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
renderAttachmentSettings(ctx) renderAttachmentSettings(ctx)


headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx) headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)


+ 1
- 1
templates/repo/issue/comment_tab.tmpl View File

@@ -4,7 +4,7 @@
<a class="item" data-tab="preview" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}">{{.i18n.Tr "repo.release.preview"}}</a> <a class="item" data-tab="preview" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}">{{.i18n.Tr "repo.release.preview"}}</a>
</div> </div>
<div class="ui bottom attached active tab segment" data-tab="write"> <div class="ui bottom attached active tab segment" data-tab="write">
<textarea id="content" name="content" tabindex="4"></textarea>
<textarea id="content" name="content" tabindex="4">{{if .IssueTemplate}}{{.IssueTemplate}}{{end}}{{if .PullRequestTemplate}}{{.PullRequestTemplate}}{{end}}</textarea>
</div> </div>
<div class="ui bottom attached tab segment markdown" data-tab="preview"> <div class="ui bottom attached tab segment markdown" data-tab="preview">
{{.i18n.Tr "repo.release.loading"}} {{.i18n.Tr "repo.release.loading"}}


Loading…
Cancel
Save