From 9c09160ef5f2bc40a87a125276662be2f7893003 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 4 Sep 2020 17:25:37 +0200 Subject: [PATCH 1/2] API: Expose its limitation settings --- modules/structs/settings.go | 8 +++++ routers/api/v1/api.go | 1 + routers/api/v1/settings/settings.go | 18 +++++++++++ routers/api/v1/swagger/settings.go | 7 ++++ templates/swagger/v1_json.tmpl | 50 +++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+) diff --git a/modules/structs/settings.go b/modules/structs/settings.go index 4a6e0ce5a84b9..f1ff1344ea03c 100644 --- a/modules/structs/settings.go +++ b/modules/structs/settings.go @@ -14,3 +14,11 @@ type GeneralRepoSettings struct { type GeneralUISettings struct { AllowedReactions []string `json:"allowed_reactions"` } + +// GeneralAPISettings contains global api settings exposed by it +type GeneralAPISettings struct { + MaxResponseItems int `json:"max_response_items"` + DefaultPagingNum int `json:"default_paging_num"` + DefaultGitTreesPerPage int `json:"default_git_trees_per_page"` + DefaultMaxBlobSize int64 `json:"default_max_blob_size"` +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ab7ef6d6f714a..027c6abc97797 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -521,6 +521,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/markdown/raw", misc.MarkdownRaw) m.Group("/settings", func() { m.Get("/ui", settings.GetGeneralUISettings) + m.Get("/api", settings.GetGeneralAPISettings) m.Get("/repository", settings.GetGeneralRepoSettings) }) diff --git a/routers/api/v1/settings/settings.go b/routers/api/v1/settings/settings.go index 8403a51d3eaa8..7ec0d74c83ad8 100644 --- a/routers/api/v1/settings/settings.go +++ b/routers/api/v1/settings/settings.go @@ -27,6 +27,24 @@ func GetGeneralUISettings(ctx *context.APIContext) { }) } +// GetGeneralAPISettings returns instance's global settings for api +func GetGeneralAPISettings(ctx *context.APIContext) { + // swagger:operation GET /settings/api settings getGeneralAPISettings + // --- + // summary: Get instance's global settings for api + // produces: + // - application/json + // responses: + // "200": + // "$ref": "#/responses/GeneralAPISettings" + ctx.JSON(http.StatusOK, api.GeneralAPISettings{ + MaxResponseItems: setting.API.MaxResponseItems, + DefaultPagingNum: setting.API.DefaultPagingNum, + DefaultGitTreesPerPage: setting.API.DefaultGitTreesPerPage, + DefaultMaxBlobSize: setting.API.DefaultMaxBlobSize, + }) +} + // GetGeneralRepoSettings returns instance's global settings for repositories func GetGeneralRepoSettings(ctx *context.APIContext) { // swagger:operation GET /settings/repository settings getGeneralRepositorySettings diff --git a/routers/api/v1/swagger/settings.go b/routers/api/v1/swagger/settings.go index 45266e51df6b3..ad0d891ddefad 100644 --- a/routers/api/v1/swagger/settings.go +++ b/routers/api/v1/swagger/settings.go @@ -19,3 +19,10 @@ type swaggerResponseGeneralUISettings struct { // in:body Body api.GeneralUISettings `json:"body"` } + +// GeneralAPISettings +// swagger:response GeneralAPISettings +type swaggerResponseGeneralAPISettings struct { + // in:body + Body api.GeneralAPISettings `json:"body"` +} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 4d6333ac4e8c7..86dd817edef82 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8672,6 +8672,23 @@ } } }, + "/settings/api": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "settings" + ], + "summary": "Get instance's global settings for api", + "operationId": "getGeneralAPISettings", + "responses": { + "200": { + "$ref": "#/responses/GeneralAPISettings" + } + } + } + }, "/settings/repository": { "get": { "produces": [ @@ -12977,6 +12994,33 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "GeneralAPISettings": { + "description": "GeneralAPISettings contains global api settings exposed by it", + "type": "object", + "properties": { + "default_git_trees_per_page": { + "type": "integer", + "format": "int64", + "x-go-name": "DefaultGitTreesPerPage" + }, + "default_max_blob_size": { + "type": "integer", + "format": "int64", + "x-go-name": "DefaultMaxBlobSize" + }, + "default_paging_num": { + "type": "integer", + "format": "int64", + "x-go-name": "DefaultPagingNum" + }, + "max_response_items": { + "type": "integer", + "format": "int64", + "x-go-name": "MaxResponseItems" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "GeneralRepoSettings": { "description": "GeneralRepoSettings contains global repository settings exposed by API", "type": "object", @@ -15197,6 +15241,12 @@ } } }, + "GeneralAPISettings": { + "description": "GeneralAPISettings", + "schema": { + "$ref": "#/definitions/GeneralAPISettings" + } + }, "GeneralRepoSettings": { "description": "GeneralRepoSettings", "schema": { From 3c077f45569fff31adeb43b64af7f8759efce607 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 4 Sep 2020 18:09:49 +0200 Subject: [PATCH 2/2] TESTs --- integrations/api_issue_reaction_test.go | 14 ------- integrations/api_settings_test.go | 49 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 integrations/api_settings_test.go diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index abbc6429fb49a..1906b8d09082c 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -11,25 +11,11 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) -func TestAPIAllowedReactions(t *testing.T) { - defer prepareTestEnv(t)() - - a := new(api.GeneralUISettings) - - req := NewRequest(t, "GET", "/api/v1/settings/ui") - resp := MakeRequest(t, req, http.StatusOK) - - DecodeJSON(t, resp, &a) - assert.Len(t, a.AllowedReactions, len(setting.UI.Reactions)) - assert.ElementsMatch(t, setting.UI.Reactions, a.AllowedReactions) -} - func TestAPIIssuesReactions(t *testing.T) { defer prepareTestEnv(t)() diff --git a/integrations/api_settings_test.go b/integrations/api_settings_test.go new file mode 100644 index 0000000000000..3eb8e930443d8 --- /dev/null +++ b/integrations/api_settings_test.go @@ -0,0 +1,49 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "net/http" + "testing" + + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" + + "github.com/stretchr/testify/assert" +) + +func TestAPIExposedSettings(t *testing.T) { + defer prepareTestEnv(t)() + + ui := new(api.GeneralUISettings) + req := NewRequest(t, "GET", "/api/v1/settings/ui") + resp := MakeRequest(t, req, http.StatusOK) + + DecodeJSON(t, resp, &ui) + assert.Len(t, ui.AllowedReactions, len(setting.UI.Reactions)) + assert.ElementsMatch(t, setting.UI.Reactions, ui.AllowedReactions) + + apiSettings := new(api.GeneralAPISettings) + req = NewRequest(t, "GET", "/api/v1/settings/api") + resp = MakeRequest(t, req, http.StatusOK) + + DecodeJSON(t, resp, &apiSettings) + assert.EqualValues(t, &api.GeneralAPISettings{ + MaxResponseItems: setting.API.MaxResponseItems, + DefaultPagingNum: setting.API.DefaultPagingNum, + DefaultGitTreesPerPage: setting.API.DefaultGitTreesPerPage, + DefaultMaxBlobSize: setting.API.DefaultMaxBlobSize, + }, apiSettings) + + repo := new(api.GeneralRepoSettings) + req = NewRequest(t, "GET", "/api/v1/settings/repository") + resp = MakeRequest(t, req, http.StatusOK) + + DecodeJSON(t, resp, &repo) + assert.EqualValues(t, &api.GeneralRepoSettings{ + MirrorsDisabled: setting.Repository.DisableMirrors, + HTTPGitDisabled: setting.Repository.DisableHTTPGit, + }, repo) +}