Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize list header for API #16551

Merged
merged 65 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
041d023
Add info about list endpoints to CONTRIBUTING.md
6543 Jul 26, 2021
0b2f858
Let List Notifications return X-Total-Count header
6543 Jul 26, 2021
b1f875f
ListAccessTokens too
6543 Jul 26, 2021
c1f2c9c
ListCronTasks
6543 Jul 26, 2021
29657d9
ListForks
6543 Jul 26, 2021
28662da
ListStargazers
6543 Jul 26, 2021
cab3f66
Add only TODOs
6543 Jul 26, 2021
b933671
nit
6543 Jul 26, 2021
e22371a
models: webhook: introduce ListWebhooksByOpts
6543 Jul 26, 2021
6897dee
ListHooks
6543 Jul 26, 2021
5a9a23c
ListLabels
6543 Jul 26, 2021
0b89620
Org#ListMembers
6543 Jul 26, 2021
c6102e7
GetTeamMembers
6543 Jul 26, 2021
bf5accc
OrgListTeams
6543 Jul 26, 2021
516354d
fix
6543 Jul 26, 2021
55e0021
ListUserTeams
6543 Jul 26, 2021
d04f6c5
ListCollaborators
6543 Jul 26, 2021
0b4984e
fix bug
6543 Jul 26, 2021
99dbf98
Repo#ListHooks
6543 Jul 26, 2021
6c243ac
ListIssueComments, ListRepoIssueComments
6543 Jul 26, 2021
251fe3d
Fix models/issue_stopwatch.go
6543 Jul 26, 2021
a4577d8
GetStopwatches
6543 Jul 26, 2021
badb37e
TrackedTimes
6543 Jul 26, 2021
c45c9d0
refactor models.ListDeployKeys
6543 Jul 26, 2021
6c8ec0a
Repo#ListDeployKeys
6543 Jul 26, 2021
3216180
Update models/issue_stopwatch.go
6543 Jul 26, 2021
aff32b3
Merge branch 'master' into api-generalize-list-header
6543 Jul 26, 2021
5a39632
CONTRIBUTING: mention Access-Control-Expose-Headers
6543 Jul 26, 2021
27398cf
fix CountComments
6543 Jul 26, 2021
7ae321d
Repo#ListLabels
6543 Jul 26, 2021
2696598
ListMilestones
6543 Jul 26, 2021
faf8817
ListPullReviews
6543 Jul 26, 2021
5eacdb7
fix getLatestCommitStatus
6543 Jul 26, 2021
e32426a
todo ...
6543 Jul 26, 2021
a57a8d1
ListSubscribers
6543 Jul 26, 2021
69ae323
no pagination supportet here
6543 Jul 26, 2021
9334a42
ListTags
6543 Jul 26, 2021
0d4632d
ListOauth2Applications
6543 Jul 26, 2021
02ada65
ListTopics
6543 Jul 26, 2021
02d19a7
TopicSearch
6543 Jul 27, 2021
0193622
Followers and Following
6543 Jul 27, 2021
b132c7a
listGPGKeys
6543 Jul 27, 2021
dceacf9
listPublicKeys
6543 Jul 27, 2021
fae949e
GetMyStarredRepos
6543 Jul 27, 2021
6505d16
getWatchedRepos
6543 Jul 27, 2021
dd766da
Merge branch 'main' into api-generalize-list-header
6543 Jul 27, 2021
2f5545f
fix CountTrackedTimes
6543 Jul 27, 2021
b26aa31
Merge branch 'master' into api-generalize-list-header
6543 Jul 27, 2021
c706d0f
Fix APIRepoTopic
6543 Jul 27, 2021
0ed695b
Add TestAPITopicSearch
6543 Jul 27, 2021
4435617
refert back routers/api/v1/org/member.go
6543 Jul 27, 2021
3861004
Just use count topics instead of getting the topics from the db
zeripath Jul 28, 2021
89158d2
Merge branch 'master' into api-generalize-list-header
6543 Jul 29, 2021
8958bbf
oops
zeripath Jul 29, 2021
db237b7
Merge branch 'main' into api-generalize-list-header
6543 Jul 29, 2021
bd82612
Merge branch 'main' into api-generalize-list-header
6543 Aug 9, 2021
7aae98c
introduce helper func and use them for SetLinkHeader related func
6543 Aug 9, 2021
8fa7079
use SetTotalCountHeader everywhere
6543 Aug 10, 2021
af4086d
update docs
6543 Aug 10, 2021
cc15aa6
Merge branch 'main' into api-generalize-list-header
6543 Aug 10, 2021
fb5934d
Merge branch 'main' into api-generalize-list-header
6543 Aug 11, 2021
f9453ea
Merge branch 'main' into api-generalize-list-header
6543 Aug 11, 2021
261109e
Merge branch 'main' into api-generalize-list-header
6543 Aug 12, 2021
926e468
Merge branch 'main' into api-generalize-list-header
6543 Aug 12, 2021
d499354
Merge branch 'main' into api-generalize-list-header
6543 Aug 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ In general, HTTP methods are chosen as follows:

An endpoint which changes/edits an object expects all fields to be optional (except ones to identify the object, which are required).

### Endpoints returning lists should
* support pagination (`page` & `limit` options in query)
* add `X-Total-Count` header ([example](https://github.com/go-gitea/gitea/blob/e76f8cac9a2ba727bec6b5beab2496be9dafabef/routers/api/v1/repo/issue.go#L445))
6543 marked this conversation as resolved.
Show resolved Hide resolved


## Developer Certificate of Origin (DCO)

Expand Down
5 changes: 5 additions & 0 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func GetNotifications(opts *FindNotificationOptions) (NotificationList, error) {
return getNotifications(x, opts)
}

// CountNotifications count all notifications that fit to the given options and ignore pagination.
func CountNotifications(opts *FindNotificationOptions) (int64, error) {
return x.Where(opts.ToCond()).Count(&Notification{})
}

// CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) error {
sess := x.NewSession()
Expand Down
9 changes: 9 additions & 0 deletions models/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ func UpdateAccessToken(t *AccessToken) error {
return err
}

// CountAccessTokens count access tokens belongs to given user by options
func CountAccessTokens(opts ListAccessTokensOptions) (int64, error) {
sess := x.Where("uid=?", opts.UserID)
if len(opts.Name) != 0 {
sess = sess.Where("name=?", opts.Name)
}
return sess.Count(&AccessToken{})
}

// DeleteAccessTokenByID deletes access token by given ID.
func DeleteAccessTokenByID(id, userID int64) error {
cnt, err := x.ID(id).Delete(&AccessToken{
Expand Down
13 changes: 8 additions & 5 deletions routers/api/v1/admin/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package admin

import (
"fmt"
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/cron"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/v1/utils"
)

Expand All @@ -36,12 +38,10 @@ func ListCronTasks(ctx *context.APIContext) {
// "403":
// "$ref": "#/responses/forbidden"
tasks := cron.ListTasks()
listOpts := utils.GetListOptions(ctx)
start, end := listOpts.GetStartEnd()
count := len(tasks)

if len(tasks) > listOpts.PageSize {
tasks = tasks[start:end]
}
listOpts := utils.GetListOptions(ctx)
tasks = util.PaginateSlice(tasks, listOpts.Page, listOpts.PageSize).(cron.TaskTable)

res := make([]structs.Cron, len(tasks))
for i, task := range tasks {
Expand All @@ -53,6 +53,9 @@ func ListCronTasks(ctx *context.APIContext) {
ExecTimes: task.ExecTimes,
}
}

ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count")
ctx.JSON(http.StatusOK, res)
}

Expand Down
9 changes: 9 additions & 0 deletions routers/api/v1/notify/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package notify

import (
"fmt"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -108,6 +109,12 @@ func ListRepoNotifications(ctx *context.APIContext) {
}
opts.RepoID = ctx.Repo.Repository.ID

totalCount, err := models.CountNotifications(opts)
if err != nil {
ctx.InternalServerError(err)
return
}

nl, err := models.GetNotifications(opts)
if err != nil {
ctx.InternalServerError(err)
Expand All @@ -119,6 +126,8 @@ func ListRepoNotifications(ctx *context.APIContext) {
return
}

ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalCount))
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count")
ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
}

Expand Down
9 changes: 9 additions & 0 deletions routers/api/v1/notify/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package notify

import (
"fmt"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -69,6 +70,12 @@ func ListNotifications(ctx *context.APIContext) {
return
}

totalCount, err := models.CountNotifications(opts)
if err != nil {
ctx.InternalServerError(err)
return
}

nl, err := models.GetNotifications(opts)
if err != nil {
ctx.InternalServerError(err)
Expand All @@ -80,6 +87,8 @@ func ListNotifications(ctx *context.APIContext) {
return
}

ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalCount))
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count")
ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
}

Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/org/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func ListHooks(ctx *context.APIContext) {
for i, hook := range orgHooks {
hooks[i] = convert.ToHook(org.HomeLink(), hook)
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, hooks)
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/org/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ListLabels(ctx *context.APIContext) {
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/org/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func listMembers(ctx *context.APIContext, publicOnly bool) {
apiMembers[i] = convert.ToUser(member, ctx.User)
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, apiMembers)
}

Expand Down
6 changes: 6 additions & 0 deletions routers/api/v1/org/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func ListTeams(ctx *context.APIContext) {

apiTeams[i] = convert.ToTeam(org.Teams[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, apiTeams)
}

Expand Down Expand Up @@ -107,6 +109,8 @@ func ListUserTeams(ctx *context.APIContext) {
apiTeams[i] = convert.ToTeam(teams[i])
apiTeams[i].Organization = apiOrg
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, apiTeams)
}

Expand Down Expand Up @@ -339,6 +343,8 @@ func GetTeamMembers(ctx *context.APIContext) {
for i, member := range team.Members {
members[i] = convert.ToUser(member, ctx.User)
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, members)
}

Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/collaborators.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func ListCollaborators(ctx *context.APIContext) {
for i, collaborator := range collaborators {
users[i] = convert.ToUser(collaborator.User, ctx.User)
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, users)
}

Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func ListForks(ctx *context.APIContext) {
}
apiForks[i] = convert.ToRepo(fork, access)
}

ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", ctx.Repo.Repository.NumForks))
ctx.JSON(http.StatusOK, apiForks)
}

Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func ListHooks(ctx *context.APIContext) {
for i := range hooks {
apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiHooks)
}

Expand Down
4 changes: 4 additions & 0 deletions routers/api/v1/repo/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func ListIssueComments(ctx *context.APIContext) {
comment.Issue = issue
apiComments[i] = convert.ToComment(comments[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiComments)
}

Expand Down Expand Up @@ -171,6 +173,8 @@ func ListRepoIssueComments(ctx *context.APIContext) {
for i := range comments {
apiComments[i] = convert.ToComment(comments[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiComments)
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/repo/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,6 @@ func GetStopwatches(ctx *context.APIContext) {
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, apiSWs)
}
5 changes: 5 additions & 0 deletions routers/api/v1/repo/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func ListTrackedTimes(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
}

Expand Down Expand Up @@ -540,6 +542,8 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
}

Expand Down Expand Up @@ -596,5 +600,6 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
}
1 change: 1 addition & 0 deletions routers/api/v1/repo/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func ListDeployKeys(ctx *context.APIContext) {
}
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiKeys)
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/repo/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func ListLabels(ctx *context.APIContext) {
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
}

Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func ListMilestones(ctx *context.APIContext) {
for i := range milestones {
apiMilestones[i] = convert.ToAPIMilestone(milestones[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiMilestones)
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func ListPullReviews(ctx *context.APIContext) {
return
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiReviews)
}

Expand Down
4 changes: 4 additions & 0 deletions routers/api/v1/repo/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package repo

import (
"fmt"
"net/http"

"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -52,5 +53,8 @@ func ListStargazers(ctx *context.APIContext) {
for i, stargazer := range stargazers {
users[i] = convert.ToUser(stargazer, ctx.User)
}

ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", ctx.Repo.Repository.NumStars))
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count")
ctx.JSON(http.StatusOK, users)
}
1 change: 1 addition & 0 deletions routers/api/v1/repo/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,6 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {

combiStatus := convert.ToCombinedStatus(statuses, convert.ToRepo(repo, ctx.Repo.AccessMode))

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
Copy link
Member Author

@6543 6543 Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intended ... follow up for me in next pull

ctx.JSON(http.StatusOK, combiStatus)
}
2 changes: 2 additions & 0 deletions routers/api/v1/repo/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ func ListSubscribers(ctx *context.APIContext) {
for i, subscriber := range subscribers {
users[i] = convert.ToUser(subscriber, ctx.User)
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, users)
}
1 change: 1 addition & 0 deletions routers/api/v1/repo/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func ListTags(ctx *context.APIContext) {
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, &apiTags)
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/repo/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func ListTeams(ctx *context.APIContext) {
apiTeams[i] = convert.ToTeam(teams[i])
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, apiTeams)
}

Expand Down
4 changes: 4 additions & 0 deletions routers/api/v1/repo/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func ListTopics(ctx *context.APIContext) {
for i, topic := range topics {
topicNames[i] = topic.Name
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, map[string]interface{}{
"topics": topicNames,
})
Expand Down Expand Up @@ -292,6 +294,8 @@ func TopicSearch(ctx *context.APIContext) {
for i, topic := range topics {
topicResponses[i] = convert.ToTopicResponse(topic)
}

// TODO: ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
ctx.JSON(http.StatusOK, map[string]interface{}{
"topics": topicResponses,
})
Expand Down
Loading