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.
202 lines
4.8 KiB
202 lines
4.8 KiB
9 years ago
|
// Copyright 2016 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
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"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/setting"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
func listIssues(c *context.APIContext, opts *models.IssuesOptions) {
|
||
8 years ago
|
issues, err := models.Issues(opts)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "Issues", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
count, err := models.IssuesCount(opts)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "IssuesCount", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// FIXME: use IssueList to improve performance.
|
||
9 years ago
|
apiIssues := make([]*api.Issue, len(issues))
|
||
|
for i := range issues {
|
||
8 years ago
|
if err = issues[i].LoadAttributes(); err != nil {
|
||
8 years ago
|
c.Error(500, "LoadAttributes", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
apiIssues[i] = issues[i].APIFormat()
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.SetLinkHeader(int(count), setting.UI.IssuePagingNum)
|
||
|
c.JSON(200, &apiIssues)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func ListUserIssues(c *context.APIContext) {
|
||
8 years ago
|
opts := models.IssuesOptions{
|
||
8 years ago
|
AssigneeID: c.User.ID,
|
||
|
Page: c.QueryInt("page"),
|
||
8 years ago
|
IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
listIssues(c, &opts)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func ListIssues(c *context.APIContext) {
|
||
8 years ago
|
opts := models.IssuesOptions{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
|
Page: c.QueryInt("page"),
|
||
|
IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
listIssues(c, &opts)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func GetIssue(c *context.APIContext) {
|
||
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsIssueNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetIssueByIndex", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
c.JSON(200, issue.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
|
||
9 years ago
|
issue := &models.Issue{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
8 years ago
|
Title: form.Title,
|
||
8 years ago
|
PosterID: c.User.ID,
|
||
|
Poster: c.User,
|
||
9 years ago
|
Content: form.Body,
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.Repo.IsWriter() {
|
||
9 years ago
|
if len(form.Assignee) > 0 {
|
||
|
assignee, err := models.GetUserByName(form.Assignee)
|
||
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetUserByName", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
issue.AssigneeID = assignee.ID
|
||
9 years ago
|
}
|
||
|
issue.MilestoneID = form.Milestone
|
||
|
} else {
|
||
|
form.Labels = nil
|
||
|
}
|
||
|
|
||
8 years ago
|
if err := models.NewIssue(c.Repo.Repository, issue, form.Labels, nil); err != nil {
|
||
|
c.Error(500, "NewIssue", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if form.Closed {
|
||
8 years ago
|
if err := issue.ChangeStatus(c.User, c.Repo.Repository, true); err != nil {
|
||
|
c.Error(500, "ChangeStatus", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Refetch from database to assign some automatic values
|
||
|
var err error
|
||
|
issue, err = models.GetIssueByID(issue.ID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "GetIssueByID", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.JSON(201, issue.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditIssue(c *context.APIContext, form api.EditIssueOption) {
|
||
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsIssueNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetIssueByIndex", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if !issue.IsPoster(c.User.ID) && !c.Repo.IsWriter() {
|
||
|
c.Status(403)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if len(form.Title) > 0 {
|
||
8 years ago
|
issue.Title = form.Title
|
||
9 years ago
|
}
|
||
|
if form.Body != nil {
|
||
|
issue.Content = *form.Body
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.Repo.IsWriter() && form.Assignee != nil &&
|
||
9 years ago
|
(issue.Assignee == nil || issue.Assignee.LowerName != strings.ToLower(*form.Assignee)) {
|
||
|
if len(*form.Assignee) == 0 {
|
||
|
issue.AssigneeID = 0
|
||
|
} else {
|
||
|
assignee, err := models.GetUserByName(*form.Assignee)
|
||
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetUserByName", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
issue.AssigneeID = assignee.ID
|
||
9 years ago
|
}
|
||
|
|
||
|
if err = models.UpdateIssueUserByAssignee(issue); err != nil {
|
||
8 years ago
|
c.Error(500, "UpdateIssueUserByAssignee", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
8 years ago
|
if c.Repo.IsWriter() && form.Milestone != nil &&
|
||
9 years ago
|
issue.MilestoneID != *form.Milestone {
|
||
8 years ago
|
oldMilestoneID := issue.MilestoneID
|
||
9 years ago
|
issue.MilestoneID = *form.Milestone
|
||
8 years ago
|
if err = models.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil {
|
||
|
c.Error(500, "ChangeMilestoneAssign", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err = models.UpdateIssue(issue); err != nil {
|
||
8 years ago
|
c.Error(500, "UpdateIssue", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
if form.State != nil {
|
||
8 years ago
|
if err = issue.ChangeStatus(c.User, c.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
|
||
|
c.Error(500, "ChangeStatus", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
// Refetch from database to assign some automatic values
|
||
|
issue, err = models.GetIssueByID(issue.ID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "GetIssueByID", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.JSON(201, issue.APIFormat())
|
||
9 years ago
|
}
|