diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 9015f6fbaa74..c06044e95e74 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -281,6 +281,11 @@ type CreateBranchRepoOption struct { // RenameBranchOption options when rename a branch in a repository // swagger:model type RenameBranchRepoOption struct { + // Old branch name + // + // required: true + // unique: true + OldName string `json:"old_name" binding:"Required;GitRefName;MaxSize(100)"` // New branch name // // required: true diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 40a2f76b9bc4..5ed01493d896 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1195,7 +1195,7 @@ func Routes() *web.Router { m.Get("/*", repo.GetBranch) m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteBranch) m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateBranchRepoOption{}), repo.CreateBranch) - m.Post("/{name}/rename", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqRepoWriter(unit.TypeCode), bind(api.RenameBranchRepoOption{}), repo.RenameBranch) + m.Post("/rename", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqRepoWriter(unit.TypeCode), bind(api.RenameBranchRepoOption{}), repo.RenameBranch) }, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode)) m.Group("/branch_protections", func() { m.Get("", repo.ListBranchProtections) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 0ee7ab8b7b3b..5dcc5e08ae63 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -398,7 +398,7 @@ func ListBranches(ctx *context.APIContext) { // RenameBranch renames a repository's branch. func RenameBranch(ctx *context.APIContext) { - // swagger:operation POST /repos/{owner}/{repo}/branches/{name}/rename repository repoRenameBranch + // swagger:operation POST /repos/{owner}/{repo}/branches/rename repository repoRenameBranch // --- // summary: Rename a branch // consumes: @@ -416,11 +416,6 @@ func RenameBranch(ctx *context.APIContext) { // description: name of the repo // type: string // required: true - // - name: name - // in: path - // description: original name of the branch - // type: string - // required: true // - name: body // in: body // schema: @@ -448,7 +443,7 @@ func RenameBranch(ctx *context.APIContext) { return } - msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, ctx.PathParam("name"), opt.NewName) + msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, opt.OldName, opt.NewName) if err != nil { ctx.Error(http.StatusInternalServerError, "RenameBranch", err) return diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 19b5b97e0d2c..fd486c1ea601 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -4905,6 +4905,58 @@ } } }, + "/repos/{owner}/{repo}/branches/rename": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Rename a branch", + "operationId": "repoRenameBranch", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/RenameBranchRepoOption" + } + } + ], + "responses": { + "201": { + "$ref": "#/responses/Branch" + }, + "403": { + "$ref": "#/responses/forbidden" + }, + "404": { + "$ref": "#/responses/notFound" + }, + "422": { + "$ref": "#/responses/validationError" + } + } + } + }, "/repos/{owner}/{repo}/branches/{branch}": { "get": { "produces": [ @@ -4995,65 +5047,6 @@ } } }, - "/repos/{owner}/{repo}/branches/{name}/rename": { - "post": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "repository" - ], - "summary": "Rename a branch", - "operationId": "repoRenameBranch", - "parameters": [ - { - "type": "string", - "description": "owner of the repo", - "name": "owner", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "name of the repo", - "name": "repo", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "original name of the branch", - "name": "name", - "in": "path", - "required": true - }, - { - "name": "body", - "in": "body", - "schema": { - "$ref": "#/definitions/RenameBranchRepoOption" - } - } - ], - "responses": { - "201": { - "$ref": "#/responses/Branch" - }, - "403": { - "$ref": "#/responses/forbidden" - }, - "404": { - "$ref": "#/responses/notFound" - }, - "422": { - "$ref": "#/responses/validationError" - } - } - } - }, "/repos/{owner}/{repo}/collaborators": { "get": { "produces": [ @@ -24068,6 +24061,7 @@ "description": "RenameBranchOption options when rename a branch in a repository", "type": "object", "required": [ + "old_name", "new_name" ], "properties": { @@ -24076,6 +24070,12 @@ "type": "string", "uniqueItems": true, "x-go-name": "NewName" + }, + "old_name": { + "description": "Old branch name", + "type": "string", + "uniqueItems": true, + "x-go-name": "OldName" } }, "x-go-package": "code.gitea.io/gitea/modules/structs" diff --git a/tests/integration/api_branch_test.go b/tests/integration/api_branch_test.go index 33fa9e5e3d19..12cf3150f105 100644 --- a/tests/integration/api_branch_test.go +++ b/tests/integration/api_branch_test.go @@ -207,7 +207,8 @@ func TestAPIRenameBranch(t *testing.T) { func testAPIRenameBranch(t *testing.T, ownerName, repoName, from, to string, expectedHTTPStatus int) { token := getUserToken(t, ownerName, auth_model.AccessTokenScopeWriteRepository) - req := NewRequestWithJSON(t, "POST", "api/v1/repos/"+ownerName+"/"+repoName+"/branches/"+from+"/rename", &api.RenameBranchRepoOption{ + req := NewRequestWithJSON(t, "POST", "api/v1/repos/"+ownerName+"/"+repoName+"/branches/rename", &api.RenameBranchRepoOption{ + OldName: from, NewName: to, }).AddTokenAuth(token) MakeRequest(t, req, expectedHTTPStatus)