diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 6d3e2c2909267..9519991abe5a8 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -254,6 +254,16 @@ type CreateBranchRepoOption struct { OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"` } +// RenameBranchOption options when rename a branch in a repository +// swagger:model +type RenameBranchReopOption struct { + // Name of the branch after modified + // + // required: true + // unique: true + NewName string `json:"new_name" binding:"Required;GitRefName;MaxSize(100)"` +} + // TransferRepoOption options when transfer a repository's ownership // swagger:model type TransferRepoOption struct { diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index c3a875e737b6b..813bcb376b37d 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -915,6 +915,7 @@ func Routes(ctx gocontext.Context) *web.Route { m.Get("/*", repo.GetBranch) m.Delete("/*", reqToken(auth_model.AccessTokenScopeRepo), reqRepoWriter(unit.TypeCode), repo.DeleteBranch) m.Post("", reqToken(auth_model.AccessTokenScopeRepo), reqRepoWriter(unit.TypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch) + m.Post("/{name}/rename", reqToken(auth_model.AccessTokenScopeRepo), reqRepoWriter(unit.TypeCode), bind(api.RenameBranchReopOption{}), 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 dff47fbcf15b7..f5072a13feefa 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -300,6 +300,97 @@ func ListBranches(ctx *context.APIContext) { ctx.JSON(http.StatusOK, apiBranches) } +// RenameBranch rename a branch for a user's repository +func RenameBranch(ctx *context.APIContext) { + // swagger:operation POST /repos/{owner}/{repo}/branches/{name}/rename repository repoRenameBranch + // --- + // summary: Rename a branch + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // 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: + // "$ref": "#/definitions/RenameBranchReopOption" + // responses: + // "201": + // "$ref": "#/responses/Branch" + // "403": + // description: Forbidden + // "400": + // description: Bad request(target exist, branch not exist,) + // "409": + // description: The branch with the same name already exists. + + opt := web.GetForm(ctx).(*api.RenameBranchReopOption) + if ctx.Repo.Repository.IsEmpty { + ctx.Error(http.StatusNotFound, "", "Git Repository is empty.") + return + } + + if !ctx.Repo.CanCreateBranch() { + ctx.Error(http.StatusForbidden, "", "Access Forbidden") + return + } + + if ctx.HasError() { + ctx.Error(http.StatusInternalServerError, "", ctx.GetErrMsg()) + return + } + + msg, err := repo_service.RenameBranch(ctx.Repo.Repository, ctx.Doer, ctx.Repo.GitRepo, ctx.Params("name"), opt.NewName) + if msg != "" { + ctx.Error(http.StatusBadRequest, "", msg) + return + } + if err != nil { + ctx.Error(http.StatusInternalServerError, "", err) + } + + branch, err := ctx.Repo.GitRepo.GetBranch(opt.NewName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranch", err) + return + } + + commit, err := branch.GetCommit() + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) + return + } + + bp, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branch.Name) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetFirstMatchProtectedBranchRule", err) + return + } + + br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch, commit, bp, ctx.Doer, ctx.Repo.IsAdmin()) + if err != nil { + ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) + return + } + + ctx.JSON(http.StatusCreated, br) +} + // GetBranchProtection gets a branch protection func GetBranchProtection(ctx *context.APIContext) { // swagger:operation GET /repos/{owner}/{repo}/branch_protections/{name} repository repoGetBranchProtection diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index 0c8d3d353feca..f6dc044bafd9c 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -133,6 +133,9 @@ type swaggerParameterBodies struct { // in:body CreateBranchRepoOption api.CreateBranchRepoOption + // in:body + RenameBranchReopOption api.RenameBranchReopOption + // in:body CreateBranchProtectionOption api.CreateBranchProtectionOption diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index ddcdc94b81c54..4c384afd50292 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3298,6 +3298,65 @@ } } }, + "/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/RenameBranchReopOption" + } + } + ], + "responses": { + "201": { + "$ref": "#/responses/Branch" + }, + "400": { + "description": "Bad request(target exist, branch not exist,)" + }, + "403": { + "description": "Forbidden" + }, + "409": { + "description": "The branch with the same name already exists." + } + } + } + }, "/repos/{owner}/{repo}/collaborators": { "get": { "produces": [ @@ -19256,6 +19315,22 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "RenameBranchReopOption": { + "description": "RenameBranchOption options when rename a branch in a repository", + "type": "object", + "required": [ + "new_name" + ], + "properties": { + "new_name": { + "description": "Name of the branch after modified", + "type": "string", + "uniqueItems": true, + "x-go-name": "NewName" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "RenameUserOption": { "description": "RenameUserOption options when renaming a user", "type": "object", @@ -21383,4 +21458,4 @@ "TOTPHeader": [] } ] -} +} \ No newline at end of file