mirror of https://github.com/gogits/gogs.git
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.
1264 lines
30 KiB
1264 lines
30 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package repo
|
||
|
|
||
11 years ago
|
import (
|
||
|
"fmt"
|
||
9 years ago
|
"io"
|
||
|
"io/ioutil"
|
||
11 years ago
|
"net/http"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/Unknwon/com"
|
||
10 years ago
|
"github.com/Unknwon/paginater"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
11 years ago
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
|
"github.com/gogits/gogs/pkg/markup"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
11 years ago
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
ISSUES = "repo/issue/list"
|
||
|
ISSUE_NEW = "repo/issue/new"
|
||
|
ISSUE_VIEW = "repo/issue/view"
|
||
11 years ago
|
|
||
8 years ago
|
LABELS = "repo/issue/labels"
|
||
10 years ago
|
|
||
8 years ago
|
MILESTONE = "repo/issue/milestones"
|
||
|
MILESTONE_NEW = "repo/issue/milestone_new"
|
||
|
MILESTONE_EDIT = "repo/issue/milestone_edit"
|
||
9 years ago
|
|
||
|
ISSUE_TEMPLATE_KEY = "IssueTemplate"
|
||
11 years ago
|
)
|
||
|
|
||
|
var (
|
||
|
ErrFileTypeForbidden = errors.New("File type is not allowed")
|
||
|
ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
|
||
9 years ago
|
|
||
|
IssueTemplateCandidates = []string{
|
||
|
"ISSUE_TEMPLATE.md",
|
||
|
".gogs/ISSUE_TEMPLATE.md",
|
||
|
".github/ISSUE_TEMPLATE.md",
|
||
|
}
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
func MustEnableIssues(c *context.Context) {
|
||
|
if !c.Repo.Repository.EnableIssues {
|
||
|
c.Handle(404, "MustEnableIssues", nil)
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if c.Repo.Repository.EnableExternalTracker {
|
||
|
c.Redirect(c.Repo.Repository.ExternalTrackerURL)
|
||
8 years ago
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func MustAllowPulls(c *context.Context) {
|
||
|
if !c.Repo.Repository.AllowsPulls() {
|
||
|
c.Handle(404, "MustAllowPulls", nil)
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// User can send pull request if owns a forked repository.
|
||
8 years ago
|
if c.IsLogged && c.User.HasForkedRepo(c.Repo.Repository.ID) {
|
||
|
c.Repo.PullRequest.Allowed = true
|
||
|
c.Repo.PullRequest.HeadInfo = c.User.Name + ":" + c.Repo.BranchName
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func RetrieveLabels(c *context.Context) {
|
||
|
labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "RetrieveLabels.GetLabels", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
for _, l := range labels {
|
||
|
l.CalOpenIssues()
|
||
|
}
|
||
8 years ago
|
c.Data["Labels"] = labels
|
||
|
c.Data["NumLabels"] = len(labels)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func issues(c *context.Context, isPullList bool) {
|
||
9 years ago
|
if isPullList {
|
||
8 years ago
|
MustAllowPulls(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Title"] = c.Tr("repo.pulls")
|
||
|
c.Data["PageIsPullList"] = true
|
||
9 years ago
|
|
||
9 years ago
|
} else {
|
||
8 years ago
|
MustEnableIssues(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Title"] = c.Tr("repo.issues")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
9 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
viewType := c.Query("type")
|
||
|
sortType := c.Query("sort")
|
||
11 years ago
|
types := []string{"assigned", "created_by", "mentioned"}
|
||
|
if !com.IsSliceContainsStr(types, viewType) {
|
||
|
viewType = "all"
|
||
|
}
|
||
|
|
||
10 years ago
|
// Must sign in to see issues about you.
|
||
8 years ago
|
if viewType != "all" && !c.IsLogged {
|
||
|
c.SetCookie("redirect_to", "/"+url.QueryEscape(setting.AppSubURL+c.Req.RequestURI), 0, setting.AppSubURL)
|
||
|
c.Redirect(setting.AppSubURL + "/user/login")
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
var (
|
||
8 years ago
|
assigneeID = c.QueryInt64("assignee")
|
||
10 years ago
|
posterID int64
|
||
|
)
|
||
8 years ago
|
filterMode := models.FILTER_MODE_YOUR_REPOS
|
||
11 years ago
|
switch viewType {
|
||
|
case "assigned":
|
||
8 years ago
|
filterMode = models.FILTER_MODE_ASSIGN
|
||
8 years ago
|
assigneeID = c.User.ID
|
||
11 years ago
|
case "created_by":
|
||
8 years ago
|
filterMode = models.FILTER_MODE_CREATE
|
||
8 years ago
|
posterID = c.User.ID
|
||
11 years ago
|
case "mentioned":
|
||
8 years ago
|
filterMode = models.FILTER_MODE_MENTION
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
var uid int64 = -1
|
||
8 years ago
|
if c.IsLogged {
|
||
|
uid = c.User.ID
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
repo := c.Repo.Repository
|
||
|
selectLabels := c.Query("labels")
|
||
|
milestoneID := c.QueryInt64("milestone")
|
||
|
isShowClosed := c.Query("state") == "closed"
|
||
9 years ago
|
issueStats := models.GetIssueStats(&models.IssueStatsOptions{
|
||
|
RepoID: repo.ID,
|
||
|
UserID: uid,
|
||
9 years ago
|
Labels: selectLabels,
|
||
9 years ago
|
MilestoneID: milestoneID,
|
||
|
AssigneeID: assigneeID,
|
||
|
FilterMode: filterMode,
|
||
|
IsPull: isPullList,
|
||
|
})
|
||
10 years ago
|
|
||
8 years ago
|
page := c.QueryInt("page")
|
||
10 years ago
|
if page <= 1 {
|
||
|
page = 1
|
||
|
}
|
||
10 years ago
|
|
||
|
var total int
|
||
|
if !isShowClosed {
|
||
|
total = int(issueStats.OpenCount)
|
||
|
} else {
|
||
|
total = int(issueStats.ClosedCount)
|
||
10 years ago
|
}
|
||
9 years ago
|
pager := paginater.New(total, setting.UI.IssuePagingNum, page, 5)
|
||
8 years ago
|
c.Data["Page"] = pager
|
||
11 years ago
|
|
||
9 years ago
|
issues, err := models.Issues(&models.IssuesOptions{
|
||
|
UserID: uid,
|
||
|
AssigneeID: assigneeID,
|
||
|
RepoID: repo.ID,
|
||
|
PosterID: posterID,
|
||
|
MilestoneID: milestoneID,
|
||
9 years ago
|
Page: pager.Current(),
|
||
9 years ago
|
IsClosed: isShowClosed,
|
||
8 years ago
|
IsMention: filterMode == models.FILTER_MODE_MENTION,
|
||
9 years ago
|
IsPull: isPullList,
|
||
|
Labels: selectLabels,
|
||
|
SortType: sortType,
|
||
|
})
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Issues", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
// Get issue-user relations.
|
||
|
pairs, err := models.GetIssueUsers(repo.ID, posterID, isShowClosed)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetIssueUsers", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Get posters.
|
||
|
for i := range issues {
|
||
8 years ago
|
if !c.IsLogged {
|
||
10 years ago
|
issues[i].IsRead = true
|
||
|
continue
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
// Check read status.
|
||
8 years ago
|
idx := models.PairsContains(pairs, issues[i].ID, c.User.ID)
|
||
10 years ago
|
if idx > -1 {
|
||
|
issues[i].IsRead = pairs[idx].IsRead
|
||
|
} else {
|
||
|
issues[i].IsRead = true
|
||
11 years ago
|
}
|
||
|
}
|
||
8 years ago
|
c.Data["Issues"] = issues
|
||
10 years ago
|
|
||
|
// Get milestones.
|
||
8 years ago
|
c.Data["Milestones"], err = models.GetMilestonesByRepoID(repo.ID)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetAllRepoMilestones", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
|
// Get assignees.
|
||
8 years ago
|
c.Data["Assignees"], err = repo.GetAssignees()
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetAssignees", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
if viewType == "assigned" {
|
||
|
assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["IssueStats"] = issueStats
|
||
|
c.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
|
||
|
c.Data["ViewType"] = viewType
|
||
|
c.Data["SortType"] = sortType
|
||
|
c.Data["MilestoneID"] = milestoneID
|
||
|
c.Data["AssigneeID"] = assigneeID
|
||
|
c.Data["IsShowClosed"] = isShowClosed
|
||
11 years ago
|
if isShowClosed {
|
||
8 years ago
|
c.Data["State"] = "closed"
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Data["State"] = "open"
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
c.HTML(200, ISSUES)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func Issues(c *context.Context) {
|
||
|
issues(c, false)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func Pulls(c *context.Context) {
|
||
|
issues(c, true)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func renderAttachmentSettings(c *context.Context) {
|
||
|
c.Data["RequireDropzone"] = true
|
||
|
c.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled
|
||
|
c.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes
|
||
|
c.Data["AttachmentMaxSize"] = setting.AttachmentMaxSize
|
||
|
c.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func RetrieveRepoMilestonesAndAssignees(c *context.Context, repo *models.Repository) {
|
||
9 years ago
|
var err error
|
||
8 years ago
|
c.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMilestones", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMilestones", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["Assignees"], err = repo.GetAssignees()
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetAssignees", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func RetrieveRepoMetas(c *context.Context, repo *models.Repository) []*models.Label {
|
||
|
if !c.Repo.IsWriter() {
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
labels, err := models.GetLabelsByRepoID(repo.ID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetLabelsByRepoID", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
c.Data["Labels"] = labels
|
||
9 years ago
|
|
||
8 years ago
|
RetrieveRepoMilestonesAndAssignees(c, repo)
|
||
|
if c.Written() {
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
return labels
|
||
|
}
|
||
|
|
||
8 years ago
|
func getFileContentFromDefaultBranch(c *context.Context, filename string) (string, bool) {
|
||
9 years ago
|
var r io.Reader
|
||
|
var bytes []byte
|
||
|
|
||
8 years ago
|
if c.Repo.Commit == nil {
|
||
9 years ago
|
var err error
|
||
8 years ago
|
c.Repo.Commit, err = c.Repo.GitRepo.GetBranchCommit(c.Repo.Repository.DefaultBranch)
|
||
9 years ago
|
if err != nil {
|
||
|
return "", false
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
entry, err := c.Repo.Commit.GetTreeEntryByPath(filename)
|
||
9 years ago
|
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
|
||
|
}
|
||
|
|
||
8 years ago
|
func setTemplateIfExists(c *context.Context, ctxDataKey string, possibleFiles []string) {
|
||
9 years ago
|
for _, filename := range possibleFiles {
|
||
8 years ago
|
content, found := getFileContentFromDefaultBranch(c, filename)
|
||
9 years ago
|
if found {
|
||
8 years ago
|
c.Data[ctxDataKey] = content
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewIssue(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.issues.new")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
|
c.Data["RequireHighlightJS"] = true
|
||
|
c.Data["RequireSimpleMDE"] = true
|
||
|
setTemplateIfExists(c, ISSUE_TEMPLATE_KEY, IssueTemplateCandidates)
|
||
|
renderAttachmentSettings(c)
|
||
10 years ago
|
|
||
8 years ago
|
RetrieveRepoMetas(c, c.Repo.Repository)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
c.HTML(200, ISSUE_NEW)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func ValidateRepoMetas(c *context.Context, f form.NewIssue) ([]int64, int64, int64) {
|
||
9 years ago
|
var (
|
||
8 years ago
|
repo = c.Repo.Repository
|
||
9 years ago
|
err error
|
||
|
)
|
||
|
|
||
8 years ago
|
labels := RetrieveRepoMetas(c, c.Repo.Repository)
|
||
|
if c.Written() {
|
||
9 years ago
|
return nil, 0, 0
|
||
|
}
|
||
|
|
||
8 years ago
|
if !c.Repo.IsWriter() {
|
||
9 years ago
|
return nil, 0, 0
|
||
|
}
|
||
|
|
||
|
// Check labels.
|
||
8 years ago
|
labelIDs := tool.StringsToInt64s(strings.Split(f.LabelIDs, ","))
|
||
|
labelIDMark := tool.Int64sToMap(labelIDs)
|
||
9 years ago
|
hasSelected := false
|
||
|
for i := range labels {
|
||
|
if labelIDMark[labels[i].ID] {
|
||
|
labels[i].IsChecked = true
|
||
|
hasSelected = true
|
||
|
}
|
||
|
}
|
||
8 years ago
|
c.Data["HasSelectedLabel"] = hasSelected
|
||
|
c.Data["label_ids"] = f.LabelIDs
|
||
|
c.Data["Labels"] = labels
|
||
9 years ago
|
|
||
|
// Check milestone.
|
||
8 years ago
|
milestoneID := f.MilestoneID
|
||
9 years ago
|
if milestoneID > 0 {
|
||
8 years ago
|
c.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMilestoneByID", err)
|
||
9 years ago
|
return nil, 0, 0
|
||
|
}
|
||
8 years ago
|
c.Data["milestone_id"] = milestoneID
|
||
9 years ago
|
}
|
||
|
|
||
|
// Check assignee.
|
||
8 years ago
|
assigneeID := f.AssigneeID
|
||
9 years ago
|
if assigneeID > 0 {
|
||
8 years ago
|
c.Data["Assignee"], err = repo.GetAssigneeByID(assigneeID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetAssigneeByID", err)
|
||
9 years ago
|
return nil, 0, 0
|
||
|
}
|
||
8 years ago
|
c.Data["assignee_id"] = assigneeID
|
||
9 years ago
|
}
|
||
|
|
||
|
return labelIDs, milestoneID, assigneeID
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewIssuePost(c *context.Context, f form.NewIssue) {
|
||
|
c.Data["Title"] = c.Tr("repo.issues.new")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
|
c.Data["RequireHighlightJS"] = true
|
||
|
c.Data["RequireSimpleMDE"] = true
|
||
|
renderAttachmentSettings(c)
|
||
11 years ago
|
|
||
8 years ago
|
labelIDs, milestoneID, assigneeID := ValidateRepoMetas(c, f)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, ISSUE_NEW)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var attachments []string
|
||
|
if setting.AttachmentEnabled {
|
||
|
attachments = f.Files
|
||
|
}
|
||
|
|
||
11 years ago
|
issue := &models.Issue{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
8 years ago
|
Title: f.Title,
|
||
8 years ago
|
PosterID: c.User.ID,
|
||
|
Poster: c.User,
|
||
10 years ago
|
MilestoneID: milestoneID,
|
||
10 years ago
|
AssigneeID: assigneeID,
|
||
8 years ago
|
Content: f.Content,
|
||
11 years ago
|
}
|
||
8 years ago
|
if err := models.NewIssue(c.Repo.Repository, issue, labelIDs, attachments); err != nil {
|
||
|
c.Handle(500, "NewIssue", err)
|
||
11 years ago
|
return
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
log.Trace("Issue created: %d/%d", c.Repo.Repository.ID, issue.ID)
|
||
|
c.Redirect(c.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func uploadAttachment(c *context.Context, allowedTypes []string) {
|
||
|
file, header, err := c.Req.FormFile("file")
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Error(500, fmt.Sprintf("FormFile: %v", err))
|
||
10 years ago
|
return
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
buf := make([]byte, 1024)
|
||
|
n, _ := file.Read(buf)
|
||
|
if n > 0 {
|
||
|
buf = buf[:n]
|
||
|
}
|
||
|
fileType := http.DetectContentType(buf)
|
||
|
|
||
|
allowed := false
|
||
|
for _, t := range allowedTypes {
|
||
|
t := strings.Trim(t, " ")
|
||
|
if t == "*/*" || t == fileType {
|
||
|
allowed = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if !allowed {
|
||
8 years ago
|
c.Error(400, ErrFileTypeForbidden.Error())
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
attach, err := models.NewAttachment(header.Filename, buf, file)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Error(500, fmt.Sprintf("NewAttachment: %v", err))
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
log.Trace("New attachment uploaded: %s", attach.UUID)
|
||
8 years ago
|
c.JSON(200, map[string]string{
|
||
10 years ago
|
"uuid": attach.UUID,
|
||
10 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func UploadIssueAttachment(c *context.Context) {
|
||
8 years ago
|
if !setting.AttachmentEnabled {
|
||
8 years ago
|
c.NotFound()
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
uploadAttachment(c, strings.Split(setting.AttachmentAllowedTypes, ","))
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func viewIssue(c *context.Context, isPullList bool) {
|
||
|
c.Data["RequireHighlightJS"] = true
|
||
|
c.Data["RequireDropzone"] = true
|
||
|
renderAttachmentSettings(c)
|
||
11 years ago
|
|
||
8 years ago
|
index := c.ParamsInt64(":index")
|
||
8 years ago
|
if index <= 0 {
|
||
8 years ago
|
c.NotFound()
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, index)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Title"] = issue.Title
|
||
11 years ago
|
|
||
9 years ago
|
// Make sure type and URL matches.
|
||
8 years ago
|
if !isPullList && issue.IsPull {
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
|
||
9 years ago
|
return
|
||
8 years ago
|
} else if isPullList && !issue.IsPull {
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if issue.IsPull {
|
||
8 years ago
|
MustAllowPulls(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["PageIsPullList"] = true
|
||
|
c.Data["PageIsPullConversation"] = true
|
||
9 years ago
|
} else {
|
||
8 years ago
|
MustEnableIssues(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["PageIsIssueList"] = true
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
issue.RenderedContent = string(markup.Markdown(issue.Content, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
|
||
11 years ago
|
|
||
8 years ago
|
repo := c.Repo.Repository
|
||
10 years ago
|
|
||
9 years ago
|
// Get more information if it's a pull request.
|
||
|
if issue.IsPull {
|
||
8 years ago
|
if issue.PullRequest.HasMerged {
|
||
8 years ago
|
c.Data["DisableStatusChange"] = issue.PullRequest.HasMerged
|
||
|
PrepareMergedViewPullInfo(c, issue)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
PrepareViewPullInfo(c, issue)
|
||
9 years ago
|
}
|
||
8 years ago
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// Metas.
|
||
10 years ago
|
// Check labels.
|
||
|
labelIDMark := make(map[int64]bool)
|
||
|
for i := range issue.Labels {
|
||
|
labelIDMark[issue.Labels[i].ID] = true
|
||
|
}
|
||
|
labels, err := models.GetLabelsByRepoID(repo.ID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetLabelsByRepoID", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
hasSelected := false
|
||
|
for i := range labels {
|
||
|
if labelIDMark[labels[i].ID] {
|
||
|
labels[i].IsChecked = true
|
||
|
hasSelected = true
|
||
|
}
|
||
|
}
|
||
8 years ago
|
c.Data["HasSelectedLabel"] = hasSelected
|
||
|
c.Data["Labels"] = labels
|
||
10 years ago
|
|
||
|
// Check milestone and assignee.
|
||
8 years ago
|
if c.Repo.IsWriter() {
|
||
|
RetrieveRepoMilestonesAndAssignees(c, repo)
|
||
|
if c.Written() {
|
||
10 years ago
|
return
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
if c.IsLogged {
|
||
10 years ago
|
// Update issue-user.
|
||
8 years ago
|
if err = issue.ReadBy(c.User.ID); err != nil {
|
||
|
c.Handle(500, "ReadBy", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
var (
|
||
9 years ago
|
tag models.CommentTag
|
||
|
ok bool
|
||
|
marked = make(map[int64]models.CommentTag)
|
||
|
comment *models.Comment
|
||
9 years ago
|
participants = make([]*models.User, 1, 10)
|
||
10 years ago
|
)
|
||
9 years ago
|
|
||
|
// Render comments and and fetch participants.
|
||
|
participants[0] = issue.Poster
|
||
10 years ago
|
for _, comment = range issue.Comments {
|
||
|
if comment.Type == models.COMMENT_TYPE_COMMENT {
|
||
8 years ago
|
comment.RenderedContent = string(markup.Markdown(comment.Content, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
|
||
10 years ago
|
|
||
|
// Check tag.
|
||
|
tag, ok = marked[comment.PosterID]
|
||
|
if ok {
|
||
|
comment.ShowTag = tag
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if repo.IsOwnedBy(comment.PosterID) ||
|
||
|
(repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(comment.PosterID)) {
|
||
|
comment.ShowTag = models.COMMENT_TAG_OWNER
|
||
9 years ago
|
} else if comment.Poster.IsWriterOfRepo(repo) {
|
||
|
comment.ShowTag = models.COMMENT_TAG_WRITER
|
||
10 years ago
|
} else if comment.PosterID == issue.PosterID {
|
||
|
comment.ShowTag = models.COMMENT_TAG_POSTER
|
||
|
}
|
||
|
|
||
|
marked[comment.PosterID] = comment.ShowTag
|
||
9 years ago
|
|
||
9 years ago
|
isAdded := false
|
||
9 years ago
|
for j := range participants {
|
||
|
if comment.Poster == participants[j] {
|
||
9 years ago
|
isAdded = true
|
||
9 years ago
|
break
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
if !isAdded && !issue.IsPoster(comment.Poster.ID) {
|
||
9 years ago
|
participants = append(participants, comment.Poster)
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
if issue.IsPull && issue.PullRequest.HasMerged {
|
||
9 years ago
|
pull := issue.PullRequest
|
||
8 years ago
|
c.Data["IsPullBranchDeletable"] = pull.BaseRepoID == pull.HeadRepoID &&
|
||
|
c.Repo.IsWriter() && c.Repo.GitRepo.IsBranchExist(pull.HeadBranch)
|
||
9 years ago
|
|
||
8 years ago
|
deleteBranchUrl := c.Repo.RepoLink + "/branches/delete/" + pull.HeadBranch
|
||
|
c.Data["DeleteBranchLink"] = fmt.Sprintf("%s?commit=%s&redirect_to=%s", deleteBranchUrl, pull.MergedCommitID, c.Data["Link"])
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Data["Participants"] = participants
|
||
|
c.Data["NumParticipants"] = len(participants)
|
||
|
c.Data["Issue"] = issue
|
||
|
c.Data["IsIssueOwner"] = c.Repo.IsWriter() || (c.IsLogged && issue.IsPoster(c.User.ID))
|
||
|
c.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + c.Data["Link"].(string)
|
||
|
c.HTML(200, ISSUE_VIEW)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func ViewIssue(c *context.Context) {
|
||
|
viewIssue(c, false)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func ViewPull(c *context.Context) {
|
||
|
viewIssue(c, true)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func getActionIssue(c *context.Context) *models.Issue {
|
||
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
|
||
|
// Prevent guests accessing pull requests
|
||
8 years ago
|
if !c.Repo.HasAccess() && issue.IsPull {
|
||
|
c.NotFound()
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
return issue
|
||
|
}
|
||
|
|
||
8 years ago
|
func UpdateIssueTitle(c *context.Context) {
|
||
|
issue := getActionIssue(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if !c.IsLogged || (!issue.IsPoster(c.User.ID) && !c.Repo.IsWriter()) {
|
||
|
c.Error(403)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
title := c.QueryTrim("title")
|
||
8 years ago
|
if len(title) == 0 {
|
||
8 years ago
|
c.Error(204)
|
||
9 years ago
|
return
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if err := issue.ChangeTitle(c.User, title); err != nil {
|
||
|
c.Handle(500, "ChangeTitle", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
8 years ago
|
"title": issue.Title,
|
||
11 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func UpdateIssueContent(c *context.Context) {
|
||
|
issue := getActionIssue(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if !c.IsLogged || (c.User.ID != issue.PosterID && !c.Repo.IsWriter()) {
|
||
|
c.Error(403)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
content := c.Query("content")
|
||
|
if err := issue.ChangeContent(c.User, content); err != nil {
|
||
|
c.Handle(500, "ChangeContent", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]string{
|
||
|
"content": string(markup.Markdown(issue.Content, c.Query("context"), c.Repo.Repository.ComposeMetas())),
|
||
9 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func UpdateIssueLabel(c *context.Context) {
|
||
|
issue := getActionIssue(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.Query("action") == "clear" {
|
||
|
if err := issue.ClearLabels(c.User); err != nil {
|
||
|
c.Handle(500, "ClearLabels", err)
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
|
} else {
|
||
8 years ago
|
isAttach := c.Query("action") == "attach"
|
||
|
label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id"))
|
||
10 years ago
|
if err != nil {
|
||
|
if models.IsErrLabelNotExist(err) {
|
||
8 years ago
|
c.Error(404, "GetLabelByID")
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetLabelByID", err)
|
||
10 years ago
|
}
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
if isAttach && !issue.HasLabel(label.ID) {
|
||
8 years ago
|
if err = issue.AddLabel(c.User, label); err != nil {
|
||
|
c.Handle(500, "AddLabel", err)
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
10 years ago
|
} else if !isAttach && issue.HasLabel(label.ID) {
|
||
8 years ago
|
if err = issue.RemoveLabel(c.User, label); err != nil {
|
||
|
c.Handle(500, "RemoveLabel", err)
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
11 years ago
|
"ok": true,
|
||
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func UpdateIssueMilestone(c *context.Context) {
|
||
|
issue := getActionIssue(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
oldMilestoneID := issue.MilestoneID
|
||
8 years ago
|
milestoneID := c.QueryInt64("id")
|
||
8 years ago
|
if oldMilestoneID == milestoneID {
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
11 years ago
|
"ok": true,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Not check for invalid milestone id and give responsibility to owners.
|
||
8 years ago
|
issue.MilestoneID = milestoneID
|
||
8 years ago
|
if err := models.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil {
|
||
|
c.Handle(500, "ChangeMilestoneAssign", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
11 years ago
|
"ok": true,
|
||
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func UpdateIssueAssignee(c *context.Context) {
|
||
|
issue := getActionIssue(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
assigneeID := c.QueryInt64("id")
|
||
8 years ago
|
if issue.AssigneeID == assigneeID {
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
10 years ago
|
"ok": true,
|
||
|
})
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if err := issue.ChangeAssignee(c.User, assigneeID); err != nil {
|
||
|
c.Handle(500, "ChangeAssignee", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
11 years ago
|
"ok": true,
|
||
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewComment(c *context.Context, f form.CreateComment) {
|
||
|
issue := getActionIssue(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
var attachments []string
|
||
|
if setting.AttachmentEnabled {
|
||
8 years ago
|
attachments = f.Files
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Flash.Error(c.Data["ErrorMsg"].(string))
|
||
|
c.Redirect(fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issue.Index))
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
var err error
|
||
9 years ago
|
var comment *models.Comment
|
||
9 years ago
|
defer func() {
|
||
9 years ago
|
// Check if issue admin/poster changes the status of issue.
|
||
8 years ago
|
if (c.Repo.IsWriter() || (c.IsLogged && issue.IsPoster(c.User.ID))) &&
|
||
8 years ago
|
(f.Status == "reopen" || f.Status == "close") &&
|
||
8 years ago
|
!(issue.IsPull && issue.PullRequest.HasMerged) {
|
||
9 years ago
|
|
||
9 years ago
|
// Duplication and conflict check should apply to reopen pull request.
|
||
9 years ago
|
var pr *models.PullRequest
|
||
|
|
||
8 years ago
|
if f.Status == "reopen" && issue.IsPull {
|
||
9 years ago
|
pull := issue.PullRequest
|
||
|
pr, err = models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
|
||
|
if err != nil {
|
||
|
if !models.IsErrPullRequestNotExist(err) {
|
||
8 years ago
|
c.ServerError("GetUnmergedPullRequest", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
// Regenerate patch and test conflict.
|
||
|
if pr == nil {
|
||
8 years ago
|
if err = issue.PullRequest.UpdatePatch(); err != nil {
|
||
8 years ago
|
c.ServerError("UpdatePatch", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
issue.PullRequest.AddToTaskQueue()
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
if pr != nil {
|
||
8 years ago
|
c.Flash.Info(c.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
if err = issue.ChangeStatus(c.User, c.Repo.Repository, f.Status == "close"); err != nil {
|
||
|
log.Error(2, "ChangeStatus: %v", err)
|
||
9 years ago
|
} else {
|
||
9 years ago
|
log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
// Redirect to comment hashtag if there is any actual content.
|
||
|
typeName := "issues"
|
||
|
if issue.IsPull {
|
||
|
typeName = "pulls"
|
||
|
}
|
||
|
if comment != nil {
|
||
8 years ago
|
c.Redirect(fmt.Sprintf("%s/%s/%d#%s", c.Repo.RepoLink, typeName, issue.Index, comment.HashTag()))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Redirect(fmt.Sprintf("%s/%s/%d", c.Repo.RepoLink, typeName, issue.Index))
|
||
9 years ago
|
}
|
||
9 years ago
|
}()
|
||
|
|
||
10 years ago
|
// Fix #321: Allow empty comments, as long as we have attachments.
|
||
8 years ago
|
if len(f.Content) == 0 && len(attachments) == 0 {
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
comment, err = models.CreateIssueComment(c.User, c.Repo.Repository, issue, f.Content, attachments)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("CreateIssueComment", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
log.Trace("Comment created: %d/%d/%d", c.Repo.Repository.ID, issue.ID, comment.ID)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func UpdateCommentContent(c *context.Context) {
|
||
|
comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.UserID() != comment.PosterID && !c.Repo.IsAdmin() {
|
||
|
c.Error(404)
|
||
9 years ago
|
return
|
||
|
} else if comment.Type != models.COMMENT_TYPE_COMMENT {
|
||
8 years ago
|
c.Error(204)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
oldContent := comment.Content
|
||
8 years ago
|
comment.Content = c.Query("content")
|
||
9 years ago
|
if len(comment.Content) == 0 {
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
9 years ago
|
"content": "",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
8 years ago
|
if err = models.UpdateComment(c.User, comment, oldContent); err != nil {
|
||
|
c.Handle(500, "UpdateComment", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]string{
|
||
|
"content": string(markup.Markdown(comment.Content, c.Query("context"), c.Repo.Repository.ComposeMetas())),
|
||
9 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func DeleteComment(c *context.Context) {
|
||
|
comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.UserID() != comment.PosterID && !c.Repo.IsAdmin() {
|
||
|
c.Error(404)
|
||
9 years ago
|
return
|
||
|
} else if comment.Type != models.COMMENT_TYPE_COMMENT {
|
||
8 years ago
|
c.Error(204)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if err = models.DeleteCommentByID(c.User, comment.ID); err != nil {
|
||
|
c.Handle(500, "DeleteCommentByID", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Status(200)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Labels(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.labels")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
|
c.Data["PageIsLabels"] = true
|
||
|
c.Data["RequireMinicolors"] = true
|
||
|
c.Data["LabelTemplates"] = models.LabelTemplates
|
||
|
c.HTML(200, LABELS)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func InitializeLabels(c *context.Context, f form.InitializeLabels) {
|
||
|
if c.HasError() {
|
||
|
c.Redirect(c.Repo.RepoLink + "/labels")
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
list, err := models.GetLabelTemplateFile(f.TemplateName)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Flash.Error(c.Tr("repo.issues.label_templates.fail_to_load_file", f.TemplateName, err))
|
||
|
c.Redirect(c.Repo.RepoLink + "/labels")
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
labels := make([]*models.Label, len(list))
|
||
|
for i := 0; i < len(list); i++ {
|
||
|
labels[i] = &models.Label{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
8 years ago
|
Name: list[i][0],
|
||
|
Color: list[i][1],
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
if err := models.NewLabels(labels...); err != nil {
|
||
8 years ago
|
c.Handle(500, "NewLabels", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/labels")
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewLabel(c *context.Context, f form.CreateLabel) {
|
||
|
c.Data["Title"] = c.Tr("repo.labels")
|
||
|
c.Data["PageIsLabels"] = true
|
||
10 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Flash.Error(c.Data["ErrorMsg"].(string))
|
||
|
c.Redirect(c.Repo.RepoLink + "/labels")
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
l := &models.Label{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
8 years ago
|
Name: f.Title,
|
||
|
Color: f.Color,
|
||
11 years ago
|
}
|
||
8 years ago
|
if err := models.NewLabels(l); err != nil {
|
||
8 years ago
|
c.Handle(500, "NewLabel", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/labels")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func UpdateLabel(c *context.Context, f form.CreateLabel) {
|
||
8 years ago
|
l, err := models.GetLabelByID(f.ID)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
switch {
|
||
|
case models.IsErrLabelNotExist(err):
|
||
8 years ago
|
c.Error(404)
|
||
10 years ago
|
default:
|
||
8 years ago
|
c.Handle(500, "UpdateLabel", err)
|
||
10 years ago
|
}
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
l.Name = f.Title
|
||
|
l.Color = f.Color
|
||
11 years ago
|
if err := models.UpdateLabel(l); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateLabel", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/labels")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func DeleteLabel(c *context.Context) {
|
||
|
if err := models.DeleteLabel(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
|
||
|
c.Flash.Error("DeleteLabel: " + err.Error())
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.issues.label_deletion_success"))
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
|
"redirect": c.Repo.RepoLink + "/labels",
|
||
11 years ago
|
})
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func Milestones(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.milestones")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
|
c.Data["PageIsMilestones"] = true
|
||
11 years ago
|
|
||
8 years ago
|
isShowClosed := c.Query("state") == "closed"
|
||
|
openCount, closedCount := models.MilestoneStats(c.Repo.Repository.ID)
|
||
|
c.Data["OpenCount"] = openCount
|
||
|
c.Data["ClosedCount"] = closedCount
|
||
10 years ago
|
|
||
8 years ago
|
page := c.QueryInt("page")
|
||
10 years ago
|
if page <= 1 {
|
||
|
page = 1
|
||
|
}
|
||
|
|
||
|
var total int
|
||
|
if !isShowClosed {
|
||
|
total = int(openCount)
|
||
|
} else {
|
||
|
total = int(closedCount)
|
||
|
}
|
||
8 years ago
|
c.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
|
||
11 years ago
|
|
||
8 years ago
|
miles, err := models.GetMilestones(c.Repo.Repository.ID, page, isShowClosed)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMilestones", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
for _, m := range miles {
|
||
8 years ago
|
m.NumOpenIssues = int(m.CountIssues(false, false))
|
||
|
m.NumClosedIssues = int(m.CountIssues(true, false))
|
||
|
if m.NumOpenIssues+m.NumClosedIssues > 0 {
|
||
|
m.Completeness = m.NumClosedIssues * 100 / (m.NumOpenIssues + m.NumClosedIssues)
|
||
|
}
|
||
8 years ago
|
m.RenderedContent = string(markup.Markdown(m.Content, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Data["Milestones"] = miles
|
||
11 years ago
|
|
||
|
if isShowClosed {
|
||
8 years ago
|
c.Data["State"] = "closed"
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Data["State"] = "open"
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
c.Data["IsShowClosed"] = isShowClosed
|
||
|
c.HTML(200, MILESTONE)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewMilestone(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.milestones.new")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
|
c.Data["PageIsMilestones"] = true
|
||
|
c.Data["RequireDatetimepicker"] = true
|
||
|
c.Data["DateLang"] = setting.DateLang(c.Locale.Language())
|
||
|
c.HTML(200, MILESTONE_NEW)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewMilestonePost(c *context.Context, f form.CreateMilestone) {
|
||
|
c.Data["Title"] = c.Tr("repo.milestones.new")
|
||
|
c.Data["PageIsIssueList"] = true
|
||
|
c.Data["PageIsMilestones"] = true
|
||
|
c.Data["RequireDatetimepicker"] = true
|
||
|
c.Data["DateLang"] = setting.DateLang(c.Locale.Language())
|
||
11 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, MILESTONE_NEW)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if len(f.Deadline) == 0 {
|
||
|
f.Deadline = "9999-12-31"
|
||
11 years ago
|
}
|
||
8 years ago
|
deadline, err := time.ParseInLocation("2006-01-02", f.Deadline, time.Local)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Data["Err_Deadline"] = true
|
||
|
c.RenderWithErr(c.Tr("repo.milestones.invalid_due_date_format"), MILESTONE_NEW, &f)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
if err = models.NewMilestone(&models.Milestone{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
8 years ago
|
Name: f.Title,
|
||
|
Content: f.Content,
|
||
11 years ago
|
Deadline: deadline,
|
||
10 years ago
|
}); err != nil {
|
||
8 years ago
|
c.Handle(500, "NewMilestone", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.milestones.create_success", f.Title))
|
||
|
c.Redirect(c.Repo.RepoLink + "/milestones")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditMilestone(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.milestones.edit")
|
||
|
c.Data["PageIsMilestones"] = true
|
||
|
c.Data["PageIsEditMilestone"] = true
|
||
|
c.Data["RequireDatetimepicker"] = true
|
||
|
c.Data["DateLang"] = setting.DateLang(c.Locale.Language())
|
||
10 years ago
|
|
||
8 years ago
|
m, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
||
10 years ago
|
if err != nil {
|
||
|
if models.IsErrMilestoneNotExist(err) {
|
||
8 years ago
|
c.Handle(404, "", nil)
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetMilestoneByRepoID", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["title"] = m.Name
|
||
|
c.Data["content"] = m.Content
|
||
10 years ago
|
if len(m.DeadlineString) > 0 {
|
||
8 years ago
|
c.Data["deadline"] = m.DeadlineString
|
||
10 years ago
|
}
|
||
8 years ago
|
c.HTML(200, MILESTONE_NEW)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditMilestonePost(c *context.Context, f form.CreateMilestone) {
|
||
|
c.Data["Title"] = c.Tr("repo.milestones.edit")
|
||
|
c.Data["PageIsMilestones"] = true
|
||
|
c.Data["PageIsEditMilestone"] = true
|
||
|
c.Data["RequireDatetimepicker"] = true
|
||
|
c.Data["DateLang"] = setting.DateLang(c.Locale.Language())
|
||
10 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, MILESTONE_NEW)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if len(f.Deadline) == 0 {
|
||
|
f.Deadline = "9999-12-31"
|
||
10 years ago
|
}
|
||
8 years ago
|
deadline, err := time.ParseInLocation("2006-01-02", f.Deadline, time.Local)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Data["Err_Deadline"] = true
|
||
|
c.RenderWithErr(c.Tr("repo.milestones.invalid_due_date_format"), MILESTONE_NEW, &f)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
m, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
||
10 years ago
|
if err != nil {
|
||
|
if models.IsErrMilestoneNotExist(err) {
|
||
8 years ago
|
c.Handle(404, "", nil)
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetMilestoneByRepoID", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
m.Name = f.Title
|
||
|
m.Content = f.Content
|
||
10 years ago
|
m.Deadline = deadline
|
||
|
if err = models.UpdateMilestone(m); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateMilestone", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.milestones.edit_success", m.Name))
|
||
|
c.Redirect(c.Repo.RepoLink + "/milestones")
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
func ChangeMilestonStatus(c *context.Context) {
|
||
|
m, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
||
11 years ago
|
if err != nil {
|
||
10 years ago
|
if models.IsErrMilestoneNotExist(err) {
|
||
8 years ago
|
c.Handle(404, "", err)
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetMilestoneByRepoID", err)
|
||
11 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
switch c.Params(":action") {
|
||
10 years ago
|
case "open":
|
||
|
if m.IsClosed {
|
||
|
if err = models.ChangeMilestoneStatus(m, false); err != nil {
|
||
8 years ago
|
c.Handle(500, "ChangeMilestoneStatus", err)
|
||
10 years ago
|
return
|
||
11 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/milestones?state=open")
|
||
10 years ago
|
case "close":
|
||
|
if !m.IsClosed {
|
||
|
m.ClosedDate = time.Now()
|
||
|
if err = models.ChangeMilestoneStatus(m, true); err != nil {
|
||
8 years ago
|
c.Handle(500, "ChangeMilestoneStatus", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/milestones?state=closed")
|
||
10 years ago
|
default:
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/milestones")
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func DeleteMilestone(c *context.Context) {
|
||
|
if err := models.DeleteMilestoneOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
|
||
|
c.Flash.Error("DeleteMilestoneByRepoID: " + err.Error())
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.milestones.deletion_success"))
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
|
"redirect": c.Repo.RepoLink + "/milestones",
|
||
10 years ago
|
})
|
||
11 years ago
|
}
|