소스 검색

Implement more issue-endpoints (#3688)

Kim "BKC" Carlbäcker 8 년 전
부모
커밋
99d86c7175
3개의 변경된 파일53개의 추가작업 그리고 8개의 파일을 삭제
  1. 26 5
      models/issue.go
  2. 4 0
      routers/api/v1/api.go
  3. 23 3
      routers/api/v1/repo/issue.go

+ 26 - 5
models/issue.go

@@ -813,20 +813,19 @@ type IssuesOptions struct {
 	SortType    string
 }
 
-// Issues returns a list of issues by given conditions.
-func Issues(opts *IssuesOptions) ([]*Issue, error) {
+func buildIssuesQuery(opts *IssuesOptions) *xorm.Session {
+	sess := x.NewSession()
+
 	if opts.Page <= 0 {
 		opts.Page = 1
 	}
 
-	sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
-
 	if opts.RepoID > 0 {
 		sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
 	} else if opts.RepoIDs != nil {
 		// In case repository IDs are provided but actually no repository has issue.
 		if len(opts.RepoIDs) == 0 {
-			return make([]*Issue, 0), nil
+			return nil
 		}
 		sess.In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).And("issue.is_closed=?", opts.IsClosed)
 	} else {
@@ -877,6 +876,28 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
 		}
 	}
 
+	return sess
+}
+
+// IssuesCount returns the number of issues by given conditions.
+func IssuesCount(opts *IssuesOptions) (int64, error) {
+	sess := buildIssuesQuery(opts)
+	if sess == nil {
+		return 0, nil
+	}
+
+	return sess.Count(&Issue{})
+}
+
+// Issues returns a list of issues by given conditions.
+func Issues(opts *IssuesOptions) ([]*Issue, error) {
+	sess := buildIssuesQuery(opts)
+	if sess == nil {
+		return make([]*Issue, 0), nil
+	}
+
+	sess.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
+
 	issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
 	if err := sess.Find(&issues); err != nil {
 		return nil, fmt.Errorf("Find: %v", err)

+ 4 - 0
routers/api/v1/api.go

@@ -221,6 +221,8 @@ func RegisterRoutes(m *macaron.Macaron) {
 				m.Combo("/:id").Get(user.GetPublicKey).
 					Delete(user.DeletePublicKey)
 			})
+
+			m.Combo("/issues", reqToken()).Get(repo.ListUserIssues)
 		}, reqToken())
 
 		// Repositories
@@ -300,6 +302,8 @@ func RegisterRoutes(m *macaron.Macaron) {
 			}, repoAssignment())
 		}, reqToken())
 
+		m.Get("/issues", reqToken(), repo.ListUserIssues)
+
 		// Organizations
 		m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
 		m.Get("/users/:username/orgs", org.ListUserOrgs)

+ 23 - 3
routers/api/v1/repo/issue.go

@@ -15,15 +15,35 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
+func ListUserIssues(ctx *context.APIContext) {
+	opts := models.IssuesOptions{
+		AssigneeID: ctx.User.ID,
+		Page:       ctx.QueryInt("page"),
+	}
+
+	listIssues(ctx, &opts)
+}
+
 func ListIssues(ctx *context.APIContext) {
-	issues, err := models.Issues(&models.IssuesOptions{
+	opts := models.IssuesOptions{
 		RepoID: ctx.Repo.Repository.ID,
 		Page:   ctx.QueryInt("page"),
-	})
+	}
+
+	listIssues(ctx, &opts)
+}
+
+func listIssues(ctx *context.APIContext, opts *models.IssuesOptions) {
+	issues, err := models.Issues(opts)
 	if err != nil {
 		ctx.Error(500, "Issues", err)
 		return
 	}
+	count, err := models.IssuesCount(opts)
+	if err != nil {
+		ctx.Error(500, "IssuesCount", err)
+		return
+	}
 
 	// FIXME: use IssueList to improve performance.
 	apiIssues := make([]*api.Issue, len(issues))
@@ -35,7 +55,7 @@ func ListIssues(ctx *context.APIContext) {
 		apiIssues[i] = issues[i].APIFormat()
 	}
 
-	ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.UI.IssuePagingNum)
+	ctx.SetLinkHeader(int(count), setting.UI.IssuePagingNum)
 	ctx.JSON(200, &apiIssues)
 }