Skip to content

Commit 577b3dd

Browse files
kemzebwxiaoguang
andcommitted
Reimplement as PATCH /branches/* endpoint
Co-authored-by: wxiaoguang <[email protected]>
1 parent 591580a commit 577b3dd

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
@@ -386,11 +386,11 @@ func ListBranches(ctx *context.APIContext) {
386386
ctx.JSON(http.StatusOK, apiBranches)
387387
}
388388

389-
// RenameBranch renames a repository's branch.
390-
func RenameBranch(ctx *context.APIContext) {
391-
// swagger:operation POST /repos/{owner}/{repo}/branches/rename repository repoRenameBranch
389+
// UpdateBranch renames a repository's branch.
390+
func UpdateBranch(ctx *context.APIContext) {
391+
// swagger:operation PATCH /repos/{owner}/{repo}/branches/{branch} repository repoUpdateBranch
392392
// ---
393-
// summary: Rename a branch
393+
// summary: Update a branch
394394
// consumes:
395395
// - application/json
396396
// produces:
@@ -406,12 +406,16 @@ func RenameBranch(ctx *context.APIContext) {
406406
// description: name of the repo
407407
// type: string
408408
// required: true
409+
// - name: branch
410+
// in: path
411+
// description: name of the branch
412+
// type: string
409413
// - name: body
410414
// in: body
411415
// schema:
412-
// "$ref": "#/definitions/RenameBranchRepoOption"
416+
// "$ref": "#/definitions/UpdateBranchRepoOption"
413417
// responses:
414-
// "201":
418+
// "200":
415419
// "$ref": "#/responses/Branch"
416420
// "403":
417421
// "$ref": "#/responses/forbidden"
@@ -420,7 +424,9 @@ func RenameBranch(ctx *context.APIContext) {
420424
// "422":
421425
// "$ref": "#/responses/validationError"
422426

423-
opt := web.GetForm(ctx).(*api.RenameBranchRepoOption)
427+
opt := web.GetForm(ctx).(*api.UpdateBranchRepoOption)
428+
429+
oldName := ctx.PathParam("*")
424430
repo := ctx.Repo.Repository
425431

426432
if repo.IsEmpty {
@@ -433,17 +439,26 @@ func RenameBranch(ctx *context.APIContext) {
433439
return
434440
}
435441

436-
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, opt.OldName, opt.NewName)
437-
if err != nil {
438-
ctx.Error(http.StatusInternalServerError, "RenameBranch", err)
439-
return
440-
}
441-
if msg != "" {
442-
ctx.Error(http.StatusUnprocessableEntity, "", msg)
443-
return
442+
branchName := opt.Name
443+
if branchName != "" {
444+
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, oldName, branchName)
445+
if err != nil {
446+
ctx.Error(http.StatusInternalServerError, "RenameBranch", err)
447+
return
448+
}
449+
if msg == "target_exist" {
450+
ctx.Error(http.StatusUnprocessableEntity, "", "Cannot rename a branch using the same name or rename to a branch that already exists.")
451+
return
452+
}
453+
if msg == "from_not_exist" {
454+
ctx.Error(http.StatusUnprocessableEntity, "", "Branch doesn't exist.")
455+
return
456+
}
457+
} else {
458+
branchName = oldName
444459
}
445460

446-
branch, err := ctx.Repo.GitRepo.GetBranch(opt.NewName)
461+
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
447462
if err != nil {
448463
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
449464
return
@@ -461,13 +476,13 @@ func RenameBranch(ctx *context.APIContext) {
461476
return
462477
}
463478

464-
br, err := convert.ToBranch(ctx, repo, opt.NewName, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
479+
br, err := convert.ToBranch(ctx, repo, branch.Name, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
465480
if err != nil {
466481
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
467482
return
468483
}
469484

470-
ctx.JSON(http.StatusCreated, br)
485+
ctx.JSON(http.StatusOK, br)
471486
}
472487

473488
// 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)