Skip to content

Commit 16b1050

Browse files
kemzebwxiaoguang
andcommitted
Reimplement as PATCH /branches/* endpoint
Co-authored-by: wxiaoguang <[email protected]>
1 parent 36829ec commit 16b1050

File tree

6 files changed

+130
-106
lines changed

6 files changed

+130
-106
lines changed

modules/structs/repo.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,13 @@ type CreateBranchRepoOption struct {
278278
OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"`
279279
}
280280

281-
// RenameBranchOption options when rename a branch in a repository
281+
// UpdateBranchRepoOption options when updating a branch in a repository
282282
// swagger:model
283-
type RenameBranchRepoOption struct {
284-
// Old branch name
285-
//
286-
// required: true
287-
// unique: true
288-
OldName string `json:"old_name" binding:"Required;GitRefName;MaxSize(100)"`
283+
type UpdateBranchRepoOption struct {
289284
// New branch name
290285
//
291-
// required: true
292286
// unique: true
293-
NewName string `json:"new_name" binding:"Required;GitRefName;MaxSize(100)"`
287+
Name string `json:"name" binding:"GitRefName;MaxSize(100)"`
294288
}
295289

296290
// TransferRepoOption options when transfer a repository's ownership

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ func Routes() *web.Router {
11951195
m.Get("/*", repo.GetBranch)
11961196
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteBranch)
11971197
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateBranchRepoOption{}), repo.CreateBranch)
1198-
m.Post("/rename", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqRepoWriter(unit.TypeCode), bind(api.RenameBranchRepoOption{}), repo.RenameBranch)
1198+
m.Patch("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.UpdateBranchRepoOption{}), repo.UpdateBranch)
11991199
}, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode))
12001200
m.Group("/branch_protections", func() {
12011201
m.Get("", repo.ListBranchProtections)

routers/api/v1/repo/branch.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ func ListBranches(ctx *context.APIContext) {
396396
ctx.JSON(http.StatusOK, apiBranches)
397397
}
398398

399-
// RenameBranch renames a repository's branch.
400-
func RenameBranch(ctx *context.APIContext) {
401-
// swagger:operation POST /repos/{owner}/{repo}/branches/rename repository repoRenameBranch
399+
// UpdateBranch renames a repository's branch.
400+
func UpdateBranch(ctx *context.APIContext) {
401+
// swagger:operation PATCH /repos/{owner}/{repo}/branches/{branch} repository repoUpdateBranch
402402
// ---
403-
// summary: Rename a branch
403+
// summary: Update a branch
404404
// consumes:
405405
// - application/json
406406
// produces:
@@ -416,12 +416,16 @@ func RenameBranch(ctx *context.APIContext) {
416416
// description: name of the repo
417417
// type: string
418418
// required: true
419+
// - name: branch
420+
// in: path
421+
// description: name of the branch
422+
// type: string
419423
// - name: body
420424
// in: body
421425
// schema:
422-
// "$ref": "#/definitions/RenameBranchRepoOption"
426+
// "$ref": "#/definitions/UpdateBranchRepoOption"
423427
// responses:
424-
// "201":
428+
// "200":
425429
// "$ref": "#/responses/Branch"
426430
// "403":
427431
// "$ref": "#/responses/forbidden"
@@ -430,7 +434,9 @@ func RenameBranch(ctx *context.APIContext) {
430434
// "422":
431435
// "$ref": "#/responses/validationError"
432436

433-
opt := web.GetForm(ctx).(*api.RenameBranchRepoOption)
437+
opt := web.GetForm(ctx).(*api.UpdateBranchRepoOption)
438+
439+
oldName := ctx.PathParam("*")
434440
repo := ctx.Repo.Repository
435441

436442
if repo.IsEmpty {
@@ -443,17 +449,26 @@ func RenameBranch(ctx *context.APIContext) {
443449
return
444450
}
445451

446-
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, opt.OldName, opt.NewName)
447-
if err != nil {
448-
ctx.Error(http.StatusInternalServerError, "RenameBranch", err)
449-
return
450-
}
451-
if msg != "" {
452-
ctx.Error(http.StatusUnprocessableEntity, "", msg)
453-
return
452+
branchName := opt.Name
453+
if branchName != "" {
454+
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, oldName, branchName)
455+
if err != nil {
456+
ctx.Error(http.StatusInternalServerError, "RenameBranch", err)
457+
return
458+
}
459+
if msg == "target_exist" {
460+
ctx.Error(http.StatusUnprocessableEntity, "", "Cannot rename a branch using the same name or rename to a branch that already exists.")
461+
return
462+
}
463+
if msg == "from_not_exist" {
464+
ctx.Error(http.StatusUnprocessableEntity, "", "Branch doesn't exist.")
465+
return
466+
}
467+
} else {
468+
branchName = oldName
454469
}
455470

456-
branch, err := ctx.Repo.GitRepo.GetBranch(opt.NewName)
471+
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
457472
if err != nil {
458473
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
459474
return
@@ -471,13 +486,13 @@ func RenameBranch(ctx *context.APIContext) {
471486
return
472487
}
473488

474-
br, err := convert.ToBranch(ctx, repo, opt.NewName, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
489+
br, err := convert.ToBranch(ctx, repo, branch.Name, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
475490
if err != nil {
476491
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
477492
return
478493
}
479494

480-
ctx.JSON(http.StatusCreated, br)
495+
ctx.JSON(http.StatusOK, br)
481496
}
482497

483498
// GetBranchProtection gets a branch protection

routers/api/v1/swagger/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type swaggerParameterBodies struct {
9090
// in:body
9191
EditRepoOption api.EditRepoOption
9292
// in:body
93-
RenameBranchReopOption api.RenameBranchRepoOption
93+
UpdateBranchRepoOption api.UpdateBranchRepoOption
9494
// in:body
9595
TransferRepoOption api.TransferRepoOption
9696
// in:body

templates/swagger/v1_json.tmpl

Lines changed: 56 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)