summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2021-08-12 14:43:08 +0200
committerGitHub <noreply@github.com>2021-08-12 14:43:08 +0200
commit2289580bb7ef8dfa4124c2b3bfb89897dbb35f46 (patch)
treee68aae604bb3d738b44156504a1415adaf042c68 /modules
parentca13e1d56c561f72cf8ad251fe61b1898abfec51 (diff)
downloadgitea-2289580bb7ef8dfa4124c2b3bfb89897dbb35f46.tar.gz
gitea-2289580bb7ef8dfa4124c2b3bfb89897dbb35f46.zip
[API] generalize list header (#16551)
* Add info about list endpoints to CONTRIBUTING.md * Let all list endpoints return X-Total-Count header * Add TODOs for GetCombinedCommitStatusByRef * Fix models/issue_stopwatch.go * Rrefactor models.ListDeployKeys * Introduce helper func and use them for SetLinkHeader related func
Diffstat (limited to 'modules')
-rw-r--r--modules/context/api.go17
-rw-r--r--modules/context/org.go4
-rw-r--r--modules/git/repo_tag.go19
-rw-r--r--modules/git/repo_tag_test.go3
-rw-r--r--modules/migrations/gitea_uploader_test.go4
5 files changed, 30 insertions, 17 deletions
diff --git a/modules/context/api.go b/modules/context/api.go
index b543c8bac8..47ea8acfe0 100644
--- a/modules/context/api.go
+++ b/modules/context/api.go
@@ -181,6 +181,23 @@ func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
if len(links) > 0 {
ctx.Header().Set("Link", strings.Join(links, ","))
+ ctx.AppendAccessControlExposeHeaders("Link")
+ }
+}
+
+// SetTotalCountHeader set "X-Total-Count" header
+func (ctx *APIContext) SetTotalCountHeader(total int64) {
+ ctx.Header().Set("X-Total-Count", fmt.Sprint(total))
+ ctx.AppendAccessControlExposeHeaders("X-Total-Count")
+}
+
+// AppendAccessControlExposeHeaders append headers by name to "Access-Control-Expose-Headers" header
+func (ctx *APIContext) AppendAccessControlExposeHeaders(names ...string) {
+ val := ctx.Header().Get("Access-Control-Expose-Headers")
+ if len(val) != 0 {
+ ctx.Header().Set("Access-Control-Expose-Headers", fmt.Sprintf("%s, %s", val, strings.Join(names, ", ")))
+ } else {
+ ctx.Header().Set("Access-Control-Expose-Headers", strings.Join(names, ", "))
}
}
diff --git a/modules/context/org.go b/modules/context/org.go
index 527ccfbcaa..fd67212a10 100644
--- a/modules/context/org.go
+++ b/modules/context/org.go
@@ -123,8 +123,8 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
// Team.
if ctx.Org.IsMember {
if ctx.Org.IsOwner {
- if err := org.GetTeams(&models.SearchTeamOptions{}); err != nil {
- ctx.ServerError("GetTeams", err)
+ if err := org.LoadTeams(); err != nil {
+ ctx.ServerError("LoadTeams", err)
return
}
} else {
diff --git a/modules/git/repo_tag.go b/modules/git/repo_tag.go
index d91c3ca979..44d7a048bc 100644
--- a/modules/git/repo_tag.go
+++ b/modules/git/repo_tag.go
@@ -10,6 +10,7 @@ import (
"strings"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/util"
)
// TagPrefix tags prefix path on the repository
@@ -160,24 +161,18 @@ func (repo *Repository) GetTag(name string) (*Tag, error) {
}
// GetTagInfos returns all tag infos of the repository.
-func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, error) {
+func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, int, error) {
// TODO this a slow implementation, makes one git command per tag
stdout, err := NewCommand("tag").RunInDir(repo.Path)
if err != nil {
- return nil, err
+ return nil, 0, err
}
tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n")
+ tagsTotal := len(tagNames)
if page != 0 {
- skip := (page - 1) * pageSize
- if skip >= len(tagNames) {
- return nil, nil
- }
- if (len(tagNames) - skip) < pageSize {
- pageSize = len(tagNames) - skip
- }
- tagNames = tagNames[skip : skip+pageSize]
+ tagNames = util.PaginateSlice(tagNames, page, pageSize).([]string)
}
var tags = make([]*Tag, 0, len(tagNames))
@@ -189,13 +184,13 @@ func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, error) {
tag, err := repo.GetTag(tagName)
if err != nil {
- return nil, err
+ return nil, tagsTotal, err
}
tag.Name = tagName
tags = append(tags, tag)
}
sortTagsByTime(tags)
- return tags, nil
+ return tags, tagsTotal, nil
}
// GetTagType gets the type of the tag, either commit (simple) or tag (annotated)
diff --git a/modules/git/repo_tag_test.go b/modules/git/repo_tag_test.go
index 6feae8d913..cfab9edd8f 100644
--- a/modules/git/repo_tag_test.go
+++ b/modules/git/repo_tag_test.go
@@ -18,9 +18,10 @@ func TestRepository_GetTags(t *testing.T) {
assert.NoError(t, err)
defer bareRepo1.Close()
- tags, err := bareRepo1.GetTagInfos(0, 0)
+ tags, total, err := bareRepo1.GetTagInfos(0, 0)
assert.NoError(t, err)
assert.Len(t, tags, 1)
+ assert.Equal(t, len(tags), total)
assert.EqualValues(t, "test", tags[0].Name)
assert.EqualValues(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", tags[0].ID.String())
assert.EqualValues(t, "tag", tags[0].Type)
diff --git a/modules/migrations/gitea_uploader_test.go b/modules/migrations/gitea_uploader_test.go
index 5f36d54584..2f854ba368 100644
--- a/modules/migrations/gitea_uploader_test.go
+++ b/modules/migrations/gitea_uploader_test.go
@@ -54,14 +54,14 @@ func TestGiteaUploadRepo(t *testing.T) {
assert.True(t, repo.HasWiki())
assert.EqualValues(t, models.RepositoryReady, repo.Status)
- milestones, err := models.GetMilestones(models.GetMilestonesOption{
+ milestones, _, err := models.GetMilestones(models.GetMilestonesOption{
RepoID: repo.ID,
State: structs.StateOpen,
})
assert.NoError(t, err)
assert.Len(t, milestones, 1)
- milestones, err = models.GetMilestones(models.GetMilestonesOption{
+ milestones, _, err = models.GetMilestones(models.GetMilestonesOption{
RepoID: repo.ID,
State: structs.StateClosed,
})