Skip to content

Commit

Permalink
Move old branch name to request body
Browse files Browse the repository at this point in the history
  • Loading branch information
kemzeb committed Nov 17, 2024
1 parent a2c26d0 commit 36829ec
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 55 deletions.
5 changes: 5 additions & 0 deletions modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
92 changes: 46 additions & 46 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/integration/api_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 36829ec

Please sign in to comment.