diff options
author | 6543 <6543@obermui.de> | 2021-06-17 10:58:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-17 10:58:10 +0200 |
commit | 6ad5d0a3062966515730aa1f8d62db5d2a7704ee (patch) | |
tree | 711b5814e4eb096e88b6f2ccbe597b0b00029275 /routers | |
parent | c9d053f0caa14e6fec4d04c009c79c9a0940c450 (diff) | |
download | gitea-6ad5d0a3062966515730aa1f8d62db5d2a7704ee.tar.gz gitea-6ad5d0a3062966515730aa1f8d62db5d2a7704ee.zip |
[API] ListReleases add filter for draft and pre-releases (#16175)
* invent ctx.QueryOptionalBool
* [API] ListReleases add draft and pre-release filter
* Add X-Total-Count header
* Add a release to fixtures
* Add TEST for API ListReleases
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/release.go | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 327a2d790b..1b52de55ff 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -5,6 +5,7 @@ package repo import ( + "fmt" "net/http" "code.gitea.io/gitea/models" @@ -83,6 +84,14 @@ func ListReleases(ctx *context.APIContext) { // description: name of the repo // type: string // required: true + // - name: draft + // in: query + // description: filter (exclude / include) drafts, if you dont have repo write access none will show + // type: boolean + // - name: pre-release + // in: query + // description: filter (exclude / include) pre-releases + // type: boolean // - name: per_page // in: query // description: page size of results, deprecated - use limit @@ -100,15 +109,19 @@ func ListReleases(ctx *context.APIContext) { // "200": // "$ref": "#/responses/ReleaseList" listOptions := utils.GetListOptions(ctx) - if ctx.QueryInt("per_page") != 0 { + if listOptions.PageSize == 0 && ctx.QueryInt("per_page") != 0 { listOptions.PageSize = ctx.QueryInt("per_page") } - releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ + opts := models.FindReleasesOptions{ ListOptions: listOptions, IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite, IncludeTags: false, - }) + IsDraft: ctx.QueryOptionalBool("draft"), + IsPreRelease: ctx.QueryOptionalBool("pre-release"), + } + + releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts) if err != nil { ctx.Error(http.StatusInternalServerError, "GetReleasesByRepoID", err) return @@ -121,6 +134,16 @@ func ListReleases(ctx *context.APIContext) { } rels[i] = convert.ToRelease(release) } + + filteredCount, err := models.CountReleasesByRepoID(ctx.Repo.Repository.ID, opts) + if err != nil { + ctx.InternalServerError(err) + return + } + + ctx.SetLinkHeader(int(filteredCount), listOptions.PageSize) + ctx.Header().Set("X-Total-Count", fmt.Sprint(filteredCount)) + ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") ctx.JSON(http.StatusOK, rels) } |