Skip to content

Commit 9d943bf

Browse files
qwerty287KN4CK3R6543
authored
Add missing X-Total-Count and fix some related bugs (#17968)
* Add missing `X-Total-Count` and fix some related bugs Adds `X-Total-Count` header to APIs that return a list but doesn't have it yet. Fixed bugs: * not returned after reporting error (https://github.com/qwerty287/gitea/blob/39eb82446c6fe5da3d79124e1f701f3795625b69/routers/api/v1/user/star.go#L70) * crash with index out of bounds, API issue/issueSubscriptions I also found various endpoints that return lists but do not apply/support pagination yet: ``` /repos/{owner}/{repo}/issues/{index}/labels /repos/{owner}/{repo}/issues/comments/{id}/reactions /repos/{owner}/{repo}/branch_protections /repos/{owner}/{repo}/contents /repos/{owner}/{repo}/hooks/git /repos/{owner}/{repo}/issue_templates /repos/{owner}/{repo}/releases/{id}/assets /repos/{owner}/{repo}/reviewers /repos/{owner}/{repo}/teams /user/emails /users/{username}/heatmap ``` If this is not expected, an new issue should be opened. Closes #13043 * fmt * Update routers/api/v1/repo/issue_subscription.go Co-authored-by: KN4CK3R <[email protected]> * Use FindAndCount Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 790e6cf commit 9d943bf

File tree

24 files changed

+73
-36
lines changed

24 files changed

+73
-36
lines changed

models/commit_status.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ type CommitStatusIndex struct {
231231
}
232232

233233
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
234-
func GetLatestCommitStatus(repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
234+
func GetLatestCommitStatus(repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
235235
return getLatestCommitStatus(db.GetEngine(db.DefaultContext), repoID, sha, listOptions)
236236
}
237237

238-
func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
238+
func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
239239
ids := make([]int64, 0, 10)
240240
sess := e.Table(&CommitStatus{}).
241241
Where("repo_id = ?", repoID).And("sha = ?", sha).
@@ -244,15 +244,15 @@ func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions db
244244

245245
sess = db.SetSessionPagination(sess, &listOptions)
246246

247-
err := sess.Find(&ids)
247+
count, err := sess.FindAndCount(&ids)
248248
if err != nil {
249-
return nil, err
249+
return nil, count, err
250250
}
251251
statuses := make([]*CommitStatus, 0, len(ids))
252252
if len(ids) == 0 {
253-
return statuses, nil
253+
return statuses, count, nil
254254
}
255-
return statuses, e.In("id", ids).Find(&statuses)
255+
return statuses, count, e.In("id", ids).Find(&statuses)
256256
}
257257

258258
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
@@ -340,7 +340,7 @@ func ParseCommitsWithStatus(oldCommits []*asymkey_model.SignCommit, repo *repo_m
340340
commit := &SignCommitWithStatuses{
341341
SignCommit: c,
342342
}
343-
statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), db.ListOptions{})
343+
statuses, _, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), db.ListOptions{})
344344
if err != nil {
345345
log.Error("GetLatestCommitStatus: %v", err)
346346
} else {

models/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (issue *Issue) loadReactions(ctx context.Context) (err error) {
238238
return nil
239239
}
240240
e := db.GetEngine(ctx)
241-
reactions, err := findReactions(e, FindReactionsOptions{
241+
reactions, _, err := findReactions(e, FindReactionsOptions{
242242
IssueID: issue.ID,
243243
})
244244
if err != nil {

models/issue_comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ func (c *Comment) loadReactions(e db.Engine, repo *repo_model.Repository) (err e
593593
if c.Reactions != nil {
594594
return nil
595595
}
596-
c.Reactions, err = findReactions(e, FindReactionsOptions{
596+
c.Reactions, _, err = findReactions(e, FindReactionsOptions{
597597
IssueID: c.IssueID,
598598
CommentID: c.ID,
599599
})

models/issue_reaction.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,38 @@ func (opts *FindReactionsOptions) toConds() builder.Cond {
7171
}
7272

7373
// FindCommentReactions returns a ReactionList of all reactions from an comment
74-
func FindCommentReactions(comment *Comment) (ReactionList, error) {
74+
func FindCommentReactions(comment *Comment) (ReactionList, int64, error) {
7575
return findReactions(db.GetEngine(db.DefaultContext), FindReactionsOptions{
7676
IssueID: comment.IssueID,
7777
CommentID: comment.ID,
7878
})
7979
}
8080

8181
// FindIssueReactions returns a ReactionList of all reactions from an issue
82-
func FindIssueReactions(issue *Issue, listOptions db.ListOptions) (ReactionList, error) {
82+
func FindIssueReactions(issue *Issue, listOptions db.ListOptions) (ReactionList, int64, error) {
8383
return findReactions(db.GetEngine(db.DefaultContext), FindReactionsOptions{
8484
ListOptions: listOptions,
8585
IssueID: issue.ID,
8686
CommentID: -1,
8787
})
8888
}
8989

90-
func findReactions(e db.Engine, opts FindReactionsOptions) ([]*Reaction, error) {
91-
e = e.
90+
func findReactions(e db.Engine, opts FindReactionsOptions) ([]*Reaction, int64, error) {
91+
sess := e.
9292
Where(opts.toConds()).
9393
In("reaction.`type`", setting.UI.Reactions).
9494
Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id")
9595
if opts.Page != 0 {
96-
e = db.SetEnginePagination(e, &opts)
96+
sess = db.SetSessionPagination(sess, &opts)
9797

9898
reactions := make([]*Reaction, 0, opts.PageSize)
99-
return reactions, e.Find(&reactions)
99+
count, err := sess.FindAndCount(&reactions)
100+
return reactions, count, err
100101
}
101102

102103
reactions := make([]*Reaction, 0, 10)
103-
return reactions, e.Find(&reactions)
104+
count, err := sess.FindAndCount(&reactions)
105+
return reactions, count, err
104106
}
105107

106108
func createReaction(e db.Engine, opts *ReactionOptions) (*Reaction, error) {
@@ -120,7 +122,7 @@ func createReaction(e db.Engine, opts *ReactionOptions) (*Reaction, error) {
120122
findOpts.CommentID = opts.Comment.ID
121123
}
122124

123-
existingR, err := findReactions(e, findOpts)
125+
existingR, _, err := findReactions(e, findOpts)
124126
if err != nil {
125127
return nil, err
126128
}

models/issue_watch.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ func getIssueWatchers(e db.Engine, issueID int64, listOptions db.ListOptions) (I
126126
return watches, sess.Find(&watches)
127127
}
128128

129+
// CountIssueWatchers count watchers/unwatchers of a given issue
130+
func CountIssueWatchers(issueID int64) (int64, error) {
131+
return countIssueWatchers(db.GetEngine(db.DefaultContext), issueID)
132+
}
133+
134+
func countIssueWatchers(e db.Engine, issueID int64) (int64, error) {
135+
return e.
136+
Where("`issue_watch`.issue_id = ?", issueID).
137+
And("`issue_watch`.is_watching = ?", true).
138+
And("`user`.is_active = ?", true).
139+
And("`user`.prohibit_login = ?", false).
140+
Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id").Count(new(IssueWatch))
141+
}
142+
129143
func removeIssueWatchersByRepoID(e db.Engine, userID, repoID int64) error {
130144
_, err := e.
131145
Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", repoID).

modules/gitgraph/graph_models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (graph *Graph) LoadAndProcessCommits(repository *repo_model.Repository, git
120120
return models.IsUserRepoAdmin(repository, user)
121121
}, &keyMap)
122122

123-
statuses, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), db.ListOptions{})
123+
statuses, _, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), db.ListOptions{})
124124
if err != nil {
125125
log.Error("GetLatestCommitStatus: %v", err)
126126
} else {

modules/indexer/code/indexer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"path/filepath"
99
"testing"
1010

11-
_ "code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/models/unittest"
1312

13+
_ "code.gitea.io/gitea/models"
14+
1415
"github.com/stretchr/testify/assert"
1516
)
1617

modules/indexer/issues/indexer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
"testing"
1212
"time"
1313

14-
_ "code.gitea.io/gitea/models"
1514
"code.gitea.io/gitea/models/unittest"
1615
"code.gitea.io/gitea/modules/setting"
1716
"code.gitea.io/gitea/modules/util"
1817

18+
_ "code.gitea.io/gitea/models"
19+
1920
"github.com/stretchr/testify/assert"
2021
"gopkg.in/ini.v1"
2122
)

modules/indexer/stats/indexer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"testing"
1010
"time"
1111

12-
_ "code.gitea.io/gitea/models"
1312
repo_model "code.gitea.io/gitea/models/repo"
1413
"code.gitea.io/gitea/models/unittest"
1514
"code.gitea.io/gitea/modules/setting"
1615

16+
_ "code.gitea.io/gitea/models"
17+
1718
"github.com/stretchr/testify/assert"
1819
"gopkg.in/ini.v1"
1920
)

routers/api/v1/org/team.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ func GetTeamRepos(ctx *context.APIContext) {
509509
}
510510
repos[i] = convert.ToRepo(repo, access)
511511
}
512+
ctx.SetTotalCountHeader(int64(team.NumRepos))
512513
ctx.JSON(http.StatusOK, repos)
513514
}
514515

0 commit comments

Comments
 (0)