diff --git a/models/issue.go b/models/issue.go index 0997f3bbc..a661ea4be 100644 --- a/models/issue.go +++ b/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) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index b03f949f7..85599568f 100644 --- a/routers/api/v1/api.go +++ b/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) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index fe967ae0a..059c6f743 100644 --- a/routers/api/v1/repo/issue.go +++ b/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) }