From 20569b44568eea48a745087a4b6df04b2b28601b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 2 Jun 2020 16:14:37 +0200 Subject: [PATCH 1/4] [API] Expose allowed Reactions --- routers/api/v1/api.go | 3 +++ routers/api/v1/misc/settings.go | 23 +++++++++++++++++++++++ routers/api/v1/swagger/misc.go | 7 +++++++ templates/swagger/v1_json.tmpl | 26 ++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 routers/api/v1/misc/settings.go diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 2eb39f607008e..0f8a0326e98ff 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -512,6 +512,9 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/signing-key.gpg", misc.SigningKey) m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown) m.Post("/markdown/raw", misc.MarkdownRaw) + m.Group("/settings", func() { + m.Get("/allowed_reactions", misc.SettingGetsAllowedReactions) + }) // Notifications m.Group("/notifications", func() { diff --git a/routers/api/v1/misc/settings.go b/routers/api/v1/misc/settings.go new file mode 100644 index 0000000000000..7404d559ada4b --- /dev/null +++ b/routers/api/v1/misc/settings.go @@ -0,0 +1,23 @@ +// 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 misc + +import ( + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/setting" +) + +// SettingGetsAllowedReactions return allowed reactions +func SettingGetsAllowedReactions(ctx *context.APIContext) { + // swagger:operation GET /settings/allowed_reactions miscellaneous getAllowedReactions + // --- + // summary: Returns the version of the Gitea application + // produces: + // - application/json + // responses: + // "200": + // "$ref": "#/responses/StringSlice" + ctx.JSON(200, setting.UI.Reactions) +} diff --git a/routers/api/v1/swagger/misc.go b/routers/api/v1/swagger/misc.go index 9fd0ab5b00c66..1d3c257efacb2 100644 --- a/routers/api/v1/swagger/misc.go +++ b/routers/api/v1/swagger/misc.go @@ -14,3 +14,10 @@ type swaggerResponseServerVersion struct { // in:body Body api.ServerVersion `json:"body"` } + +// StringSlice +// swagger:response StringSlice +type swaggerResponseStringSlice struct { + // in:body + Body []string `json:"body"` +} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 1f159d64a077e..7cc994c724284 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8343,6 +8343,23 @@ } } }, + "/settings/allowed_reactions": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "miscellaneous" + ], + "summary": "Returns the version of the Gitea application", + "operationId": "getAllowedReactions", + "responses": { + "200": { + "$ref": "#/responses/StringSlice" + } + } + } + }, "/signing-key.gpg": { "get": { "produces": [ @@ -15042,6 +15059,15 @@ } } }, + "StringSlice": { + "description": "StringSlice", + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, "Tag": { "description": "Tag", "schema": { From 8964381327ba8a24b6ef45b81e0498dc899d9b7d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 2 Jun 2020 19:19:41 +0200 Subject: [PATCH 2/4] dont be in soutch a rush --- routers/api/v1/misc/settings.go | 2 +- templates/swagger/v1_json.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/misc/settings.go b/routers/api/v1/misc/settings.go index 7404d559ada4b..11c7eef726f94 100644 --- a/routers/api/v1/misc/settings.go +++ b/routers/api/v1/misc/settings.go @@ -13,7 +13,7 @@ import ( func SettingGetsAllowedReactions(ctx *context.APIContext) { // swagger:operation GET /settings/allowed_reactions miscellaneous getAllowedReactions // --- - // summary: Returns the version of the Gitea application + // summary: Returns string array of allowed reactions // produces: // - application/json // responses: diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 7cc994c724284..687a6bc5e4cc6 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8351,7 +8351,7 @@ "tags": [ "miscellaneous" ], - "summary": "Returns the version of the Gitea application", + "summary": "Returns string array of allowed reactions", "operationId": "getAllowedReactions", "responses": { "200": { From a0f06d5d2d6278d9a63b8f0d45a9c5d933307e99 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 2 Jun 2020 19:33:09 +0200 Subject: [PATCH 3/4] add TEST --- integrations/api_issue_reaction_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index 1906b8d09082c..3e6e03869f250 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -11,11 +11,30 @@ 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)() + + type allowed []string + + a := new(allowed) + + req := NewRequest(t, "GET", "/api/v1/settings/allowed_reactions") + resp := MakeRequest(t, req, http.StatusOK) + + DecodeJSON(t, resp, &a) + assert.Len(t, *a, len(setting.UI.Reactions)) + + for _, r := range *a { + assert.True(t, setting.UI.ReactionsMap[r]) + } +} + func TestAPIIssuesReactions(t *testing.T) { defer prepareTestEnv(t)() From 68b8b459296a1b9422a2e8a858f417417bebfbcf Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 3 Jun 2020 18:19:46 +0200 Subject: [PATCH 4/4] use ElementsMatch --- integrations/api_issue_reaction_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index 3e6e03869f250..ab3de7c11625f 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -29,10 +29,7 @@ func TestAPIAllowedReactions(t *testing.T) { DecodeJSON(t, resp, &a) assert.Len(t, *a, len(setting.UI.Reactions)) - - for _, r := range *a { - assert.True(t, setting.UI.ReactionsMap[r]) - } + assert.ElementsMatch(t, setting.UI.Reactions, *a) } func TestAPIIssuesReactions(t *testing.T) {