Skip to content

Add API route to rename a branch #23061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
91 changes: 91 additions & 0 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the branch is named "test/branch/rename"? Won't the http router look for the branched named "test/branch"?

// ---
// 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
Expand Down
3 changes: 3 additions & 0 deletions routers/api/v1/swagger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type swaggerParameterBodies struct {
// in:body
CreateBranchRepoOption api.CreateBranchRepoOption

// in:body
RenameBranchReopOption api.RenameBranchReopOption

// in:body
CreateBranchProtectionOption api.CreateBranchProtectionOption

Expand Down
77 changes: 76 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -21383,4 +21458,4 @@
"TOTPHeader": []
}
]
}
}