From 68a2af6d68bb54a704bf98a3ba0ceada4d7808b9 Mon Sep 17 00:00:00 2001
From: 6543 <6543@obermui.de>
Date: Wed, 10 Jun 2020 14:05:46 +0200
Subject: [PATCH] API expose usefull General Repo settings settings (#11758)

* GeneralRepoSettings expose MirrorsDisabled, HTTPGitDisabled, MaxCreationLimit

* Apply suggestions from code review

Co-authored-by: proton <25139420+proton-ab@users.noreply.github.com>

* lint

Co-authored-by: proton <25139420+proton-ab@users.noreply.github.com>
---
 modules/structs/settings.go     | 11 ++++++++++
 routers/api/v1/api.go           |  1 +
 routers/api/v1/misc/settings.go | 17 +++++++++++++++
 routers/api/v1/swagger/misc.go  |  7 ++++++
 templates/swagger/v1_json.tmpl  | 38 +++++++++++++++++++++++++++++++++
 5 files changed, 74 insertions(+)
 create mode 100644 modules/structs/settings.go

diff --git a/modules/structs/settings.go b/modules/structs/settings.go
new file mode 100644
index 0000000000..dcd1b5cabd
--- /dev/null
+++ b/modules/structs/settings.go
@@ -0,0 +1,11 @@
+// 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 structs
+
+// GeneralRepoSettings contains global repository settings exposed by API
+type GeneralRepoSettings struct {
+	MirrorsDisabled bool `json:"mirrors_disabled"`
+	HTTPGitDisabled bool `json:"http_git_disabled"`
+}
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index 0567c3560c..f356d7a2bc 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -514,6 +514,7 @@ func RegisterRoutes(m *macaron.Macaron) {
 		m.Post("/markdown/raw", misc.MarkdownRaw)
 		m.Group("/settings", func() {
 			m.Get("/allowed_reactions", misc.SettingGetsAllowedReactions)
+			m.Get("/repository", misc.GetGeneralRepoSettings)
 		})
 
 		// Notifications
diff --git a/routers/api/v1/misc/settings.go b/routers/api/v1/misc/settings.go
index e9493050ca..925cde835c 100644
--- a/routers/api/v1/misc/settings.go
+++ b/routers/api/v1/misc/settings.go
@@ -9,6 +9,7 @@ import (
 
 	"code.gitea.io/gitea/modules/context"
 	"code.gitea.io/gitea/modules/setting"
+	api "code.gitea.io/gitea/modules/structs"
 )
 
 // SettingGetsAllowedReactions return allowed reactions
@@ -23,3 +24,19 @@ func SettingGetsAllowedReactions(ctx *context.APIContext) {
 	//     "$ref": "#/responses/StringSlice"
 	ctx.JSON(http.StatusOK, setting.UI.Reactions)
 }
+
+// GetGeneralRepoSettings returns instance's global settings for repositories
+func GetGeneralRepoSettings(ctx *context.APIContext) {
+	// swagger:operation GET /settings/repository miscellaneous getGeneralRepositorySettings
+	// ---
+	// summary: Get instance's global settings for repositories
+	// produces:
+	// - application/json
+	// responses:
+	//   "200":
+	//     "$ref": "#/responses/GeneralRepoSettings"
+	ctx.JSON(http.StatusOK, api.GeneralRepoSettings{
+		MirrorsDisabled: setting.Repository.DisableMirrors,
+		HTTPGitDisabled: setting.Repository.DisableHTTPGit,
+	})
+}
diff --git a/routers/api/v1/swagger/misc.go b/routers/api/v1/swagger/misc.go
index 1d3c257efa..50f5cbdbf5 100644
--- a/routers/api/v1/swagger/misc.go
+++ b/routers/api/v1/swagger/misc.go
@@ -21,3 +21,10 @@ type swaggerResponseStringSlice struct {
 	// in:body
 	Body []string `json:"body"`
 }
+
+// GeneralRepoSettings
+// swagger:response GeneralRepoSettings
+type swaggerResponseGeneralRepoSettings struct {
+	// in:body
+	Body api.GeneralRepoSettings `json:"body"`
+}
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index bf011285f8..34a4b42f2f 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -8487,6 +8487,23 @@
         }
       }
     },
+    "/settings/repository": {
+      "get": {
+        "produces": [
+          "application/json"
+        ],
+        "tags": [
+          "miscellaneous"
+        ],
+        "summary": "Get instance's global settings for repositories",
+        "operationId": "getGeneralRepositorySettings",
+        "responses": {
+          "200": {
+            "$ref": "#/responses/GeneralRepoSettings"
+          }
+        }
+      }
+    },
     "/signing-key.gpg": {
       "get": {
         "produces": [
@@ -12721,6 +12738,21 @@
       },
       "x-go-package": "code.gitea.io/gitea/modules/structs"
     },
+    "GeneralRepoSettings": {
+      "description": "GeneralRepoSettings contains global repository settings exposed by API",
+      "type": "object",
+      "properties": {
+        "http_git_disabled": {
+          "type": "boolean",
+          "x-go-name": "HTTPGitDisabled"
+        },
+        "mirrors_disabled": {
+          "type": "boolean",
+          "x-go-name": "MirrorsDisabled"
+        }
+      },
+      "x-go-package": "code.gitea.io/gitea/modules/structs"
+    },
     "GitBlobResponse": {
       "description": "GitBlobResponse represents a git blob",
       "type": "object",
@@ -14890,6 +14922,12 @@
         }
       }
     },
+    "GeneralRepoSettings": {
+      "description": "GeneralRepoSettings",
+      "schema": {
+        "$ref": "#/definitions/GeneralRepoSettings"
+      }
+    },
     "GitBlobResponse": {
       "description": "GitBlobResponse",
       "schema": {