From a8c1b966f8878f870b749080ee8552cd2556fc9c Mon Sep 17 00:00:00 2001 From: Eric de Ruiter Date: Wed, 20 Nov 2019 11:14:08 +0100 Subject: [PATCH 1/4] deprecate /api/v1/org/{org}/repos in favour of /api/v1/orgs/{org}/repos + cleanup api repository routes a bit --- routers/api/v1/api.go | 13 +++++----- routers/api/v1/repo/repo.go | 52 +++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 47c9c95c7fe00..b2e600f6c8412 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -590,17 +590,15 @@ func RegisterRoutes(m *macaron.Macaron) { }, reqToken()) // Repositories - m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo) + m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepoDeprecated) + + m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID) m.Group("/repos", func() { m.Get("/search", repo.Search) - }) - m.Get("/repos/issues/search", repo.SearchIssues) + m.Get("/issues/search", repo.SearchIssues) - m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID) - - m.Group("/repos", func() { m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) m.Group("/:username/:reponame", func() { @@ -796,10 +794,11 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/users/:username/orgs", org.ListUserOrgs) m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create) m.Group("/orgs/:orgname", func() { - m.Get("/repos", user.ListOrgRepos) m.Combo("").Get(org.Get). Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit). Delete(reqToken(), reqOrgOwnership(), org.Delete) + m.Combo("/repos").Get(user.ListOrgRepos). + Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo) m.Group("/members", func() { m.Get("", org.ListMembers) m.Combo("/:username").Get(org.IsMember). diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index cab0fc07e0e3a..8ab4ef9af42fd 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -281,10 +281,11 @@ func Create(ctx *context.APIContext, opt api.CreateRepoOption) { } // CreateOrgRepo create one repository of the organization -func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { - // swagger:operation POST /org/{org}/repos organization createOrgRepo +func CreateOrgRepoDeprecated(ctx *context.APIContext, opt api.CreateRepoOption) { + // swagger:operation POST /org/{org}/repos organization createOrgRepoDeprecated // --- // summary: Create a repository in an organization + // deprecated: true // consumes: // - application/json // produces: @@ -334,6 +335,53 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { CreateUserRepo(ctx, org, opt) } +// CreateOrgRepo create one repository of the organization +func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { + // swagger:operation POST /orgs/{org}/repos organization createOrgRepo + // --- + // summary: Create a repository in an organization + // deprecated: true + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: org + // in: path + // description: name of organization + // type: string + // required: true + // - name: body + // in: body + // schema: + // "$ref": "#/definitions/CreateRepoOption" + // responses: + // "201": + // "$ref": "#/responses/Repository" + // "404": + // "$ref": "#/responses/notFound" + // "403": + // "$ref": "#/responses/forbidden" + org := ctx.Org.Organization + + if !models.HasOrgVisible(org, ctx.User) { + ctx.NotFound("HasOrgVisible", nil) + return + } + + if !ctx.User.IsAdmin { + isOwner, err := org.IsOwnedBy(ctx.User.ID) + if err != nil { + ctx.ServerError("IsOwnedBy", err) + return + } else if !isOwner { + ctx.Error(403, "", "Given user is not owner of organization.") + return + } + } + CreateUserRepo(ctx, org, opt) +} + // Migrate migrate remote git repository to gitea func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { // swagger:operation POST /repos/migrate repository repoMigrate From c0a5032af641b2fbd57ddc927f43b333144c5c09 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 Nov 2019 14:54:20 +0100 Subject: [PATCH 2/4] remove redundant code --- routers/api/v1/repo/repo.go | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 8ab4ef9af42fd..2dbf24bf09ef5 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -280,7 +280,7 @@ func Create(ctx *context.APIContext, opt api.CreateRepoOption) { CreateUserRepo(ctx, ctx.User, opt) } -// CreateOrgRepo create one repository of the organization +// CreateOrgRepoDeprecated create one repository of the organization func CreateOrgRepoDeprecated(ctx *context.APIContext, opt api.CreateRepoOption) { // swagger:operation POST /org/{org}/repos organization createOrgRepoDeprecated // --- @@ -307,32 +307,7 @@ func CreateOrgRepoDeprecated(ctx *context.APIContext, opt api.CreateRepoOption) // "$ref": "#/responses/validationError" // "403": // "$ref": "#/responses/forbidden" - org, err := models.GetOrgByName(ctx.Params(":org")) - if err != nil { - if models.IsErrOrgNotExist(err) { - ctx.Error(422, "", err) - } else { - ctx.Error(500, "GetOrgByName", err) - } - return - } - - if !models.HasOrgVisible(org, ctx.User) { - ctx.NotFound("HasOrgVisible", nil) - return - } - - if !ctx.User.IsAdmin { - canCreate, err := org.CanCreateOrgRepo(ctx.User.ID) - if err != nil { - ctx.ServerError("CanCreateOrgRepo", err) - return - } else if !canCreate { - ctx.Error(403, "", "Given user is not allowed to create repository in organization.") - return - } - } - CreateUserRepo(ctx, org, opt) + CreateOrgRepo(ctx, opt) } // CreateOrgRepo create one repository of the organization From ee15fa4b0da19fa4ade485760cda692657f51cca Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 Nov 2019 14:54:48 +0100 Subject: [PATCH 3/4] use upstream function for api cal --- routers/api/v1/repo/repo.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 2dbf24bf09ef5..287cae7be94a1 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -337,7 +337,15 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { // "$ref": "#/responses/notFound" // "403": // "$ref": "#/responses/forbidden" - org := ctx.Org.Organization + org, err := models.GetOrgByName(ctx.Params(":org")) + if err != nil { + if models.IsErrOrgNotExist(err) { + ctx.Error(422, "", err) + } else { + ctx.Error(500, "GetOrgByName", err) + } + return + } if !models.HasOrgVisible(org, ctx.User) { ctx.NotFound("HasOrgVisible", nil) @@ -345,12 +353,12 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { } if !ctx.User.IsAdmin { - isOwner, err := org.IsOwnedBy(ctx.User.ID) + canCreate, err := org.CanCreateOrgRepo(ctx.User.ID) if err != nil { - ctx.ServerError("IsOwnedBy", err) + ctx.ServerError("CanCreateOrgRepo", err) return - } else if !isOwner { - ctx.Error(403, "", "Given user is not owner of organization.") + } else if !canCreate { + ctx.Error(403, "", "Given user is not allowed to create repository in organization.") return } } From 6810fc1a0313ef33f0c7e21d850b2fd9168e3e41 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 Nov 2019 14:59:18 +0100 Subject: [PATCH 4/4] make generate-swagger --- templates/swagger/v1_json.tmpl | 44 +++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 338ab104e9e94..6ac1d8e21719a 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -428,7 +428,8 @@ "organization" ], "summary": "Create a repository in an organization", - "operationId": "createOrgRepo", + "operationId": "createOrgRepoDeprecated", + "deprecated": true, "parameters": [ { "type": "string", @@ -985,6 +986,47 @@ "$ref": "#/responses/RepositoryList" } } + }, + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "organization" + ], + "summary": "Create a repository in an organization", + "operationId": "createOrgRepo", + "deprecated": true, + "parameters": [ + { + "type": "string", + "description": "name of organization", + "name": "org", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/CreateRepoOption" + } + } + ], + "responses": { + "201": { + "$ref": "#/responses/Repository" + }, + "403": { + "$ref": "#/responses/forbidden" + }, + "404": { + "$ref": "#/responses/notFound" + } + } } }, "/orgs/{org}/teams": {