Skip to content

Commit d0c04a7

Browse files
committed
API: The UpdateBranch API renames a branch
Change the description and naming accordingly Discussed in #35368, implemented in #32433
1 parent e19d0e4 commit d0c04a7

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

modules/structs/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ type CreateBranchRepoOption struct {
282282
OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"`
283283
}
284284

285-
// UpdateBranchRepoOption options when updating a branch in a repository
285+
// RenameBranchRepoOption options when renaming a branch in a repository
286286
// swagger:model
287-
type UpdateBranchRepoOption struct {
287+
type RenameBranchRepoOption struct {
288288
// New branch name
289289
//
290290
// required: true

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ func Routes() *web.Router {
12561256
m.Get("/*", repo.GetBranch)
12571257
m.Delete("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, repo.DeleteBranch)
12581258
m.Post("", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.CreateBranchRepoOption{}), repo.CreateBranch)
1259-
m.Patch("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.UpdateBranchRepoOption{}), repo.UpdateBranch)
1259+
m.Patch("/*", reqToken(), reqRepoWriter(unit.TypeCode), mustNotBeArchived, bind(api.RenameBranchRepoOption{}), repo.RenameBranch)
12601260
}, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode))
12611261
m.Group("/branch_protections", func() {
12621262
m.Get("", repo.ListBranchProtections)

routers/api/v1/repo/branch.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ func ListBranches(ctx *context.APIContext) {
380380
ctx.JSON(http.StatusOK, apiBranches)
381381
}
382382

383-
// UpdateBranch updates a repository's branch.
384-
func UpdateBranch(ctx *context.APIContext) {
385-
// swagger:operation PATCH /repos/{owner}/{repo}/branches/{branch} repository repoUpdateBranch
383+
// RenameBranch renames a repository's branch.
384+
func RenameBranch(ctx *context.APIContext) {
385+
// swagger:operation PATCH /repos/{owner}/{repo}/branches/{branch} repository repoRenameBranch
386386
// ---
387387
// summary: Update a branch
388388
// consumes:
@@ -408,7 +408,7 @@ func UpdateBranch(ctx *context.APIContext) {
408408
// - name: body
409409
// in: body
410410
// schema:
411-
// "$ref": "#/definitions/UpdateBranchRepoOption"
411+
// "$ref": "#/definitions/RenameBranchRepoOption"
412412
// responses:
413413
// "204":
414414
// "$ref": "#/responses/empty"
@@ -419,7 +419,7 @@ func UpdateBranch(ctx *context.APIContext) {
419419
// "422":
420420
// "$ref": "#/responses/validationError"
421421

422-
opt := web.GetForm(ctx).(*api.UpdateBranchRepoOption)
422+
opt := web.GetForm(ctx).(*api.RenameBranchRepoOption)
423423

424424
oldName := ctx.PathParam("*")
425425
repo := ctx.Repo.Repository

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-
UpdateBranchRepoOption api.UpdateBranchRepoOption
93+
RenameBranchRepoOption api.RenameBranchRepoOption
9494
// in:body
9595
TransferRepoOption api.TransferRepoOption
9696
// in:body

templates/swagger/v1_json.tmpl

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

tests/integration/api_branch_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -187,26 +187,26 @@ func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBran
187187
return resp.Result().StatusCode == status
188188
}
189189

190-
func TestAPIUpdateBranch(t *testing.T) {
190+
func TestAPIRenameBranch(t *testing.T) {
191191
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
192-
t.Run("UpdateBranchWithEmptyRepo", func(t *testing.T) {
193-
testAPIUpdateBranch(t, "user10", "user10", "repo6", "master", "test", http.StatusNotFound)
192+
t.Run("RenameBranchWithEmptyRepo", func(t *testing.T) {
193+
testAPIRenameBranch(t, "user10", "user10", "repo6", "master", "test", http.StatusNotFound)
194194
})
195-
t.Run("UpdateBranchWithSameBranchNames", func(t *testing.T) {
196-
resp := testAPIUpdateBranch(t, "user2", "user2", "repo1", "master", "master", http.StatusUnprocessableEntity)
195+
t.Run("RenameBranchWithSameBranchNames", func(t *testing.T) {
196+
resp := testAPIRenameBranch(t, "user2", "user2", "repo1", "master", "master", http.StatusUnprocessableEntity)
197197
assert.Contains(t, resp.Body.String(), "Cannot rename a branch using the same name or rename to a branch that already exists.")
198198
})
199-
t.Run("UpdateBranchThatAlreadyExists", func(t *testing.T) {
200-
resp := testAPIUpdateBranch(t, "user2", "user2", "repo1", "master", "branch2", http.StatusUnprocessableEntity)
199+
t.Run("RenameBranchThatAlreadyExists", func(t *testing.T) {
200+
resp := testAPIRenameBranch(t, "user2", "user2", "repo1", "master", "branch2", http.StatusUnprocessableEntity)
201201
assert.Contains(t, resp.Body.String(), "Cannot rename a branch using the same name or rename to a branch that already exists.")
202202
})
203-
t.Run("UpdateBranchWithNonExistentBranch", func(t *testing.T) {
204-
resp := testAPIUpdateBranch(t, "user2", "user2", "repo1", "i-dont-exist", "new-branch-name", http.StatusNotFound)
203+
t.Run("RenameBranchWithNonExistentBranch", func(t *testing.T) {
204+
resp := testAPIRenameBranch(t, "user2", "user2", "repo1", "i-dont-exist", "new-branch-name", http.StatusNotFound)
205205
assert.Contains(t, resp.Body.String(), "Branch doesn't exist.")
206206
})
207-
t.Run("UpdateBranchWithNonAdminDoer", func(t *testing.T) {
207+
t.Run("RenameBranchWithNonAdminDoer", func(t *testing.T) {
208208
// don't allow default branch renaming
209-
resp := testAPIUpdateBranch(t, "user40", "user2", "repo1", "master", "new-branch-name", http.StatusForbidden)
209+
resp := testAPIRenameBranch(t, "user40", "user2", "repo1", "master", "new-branch-name", http.StatusForbidden)
210210
assert.Contains(t, resp.Body.String(), "User must be a repo or site admin to rename default or protected branches.")
211211

212212
// don't allow protected branch renaming
@@ -216,10 +216,10 @@ func TestAPIUpdateBranch(t *testing.T) {
216216
}).AddTokenAuth(token)
217217
MakeRequest(t, req, http.StatusCreated)
218218
testAPICreateBranchProtection(t, "protected-branch", 1, http.StatusCreated)
219-
resp = testAPIUpdateBranch(t, "user40", "user2", "repo1", "protected-branch", "new-branch-name", http.StatusForbidden)
219+
resp = testAPIRenameBranch(t, "user40", "user2", "repo1", "protected-branch", "new-branch-name", http.StatusForbidden)
220220
assert.Contains(t, resp.Body.String(), "User must be a repo or site admin to rename default or protected branches.")
221221
})
222-
t.Run("UpdateBranchWithGlobedBasedProtectionRulesAndAdminAccess", func(t *testing.T) {
222+
t.Run("RenameBranchWithGlobedBasedProtectionRulesAndAdminAccess", func(t *testing.T) {
223223
// don't allow branch that falls under glob-based protection rules to be renamed
224224
token := getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)
225225
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/branch_protections", &api.BranchProtection{
@@ -234,18 +234,18 @@ func TestAPIUpdateBranch(t *testing.T) {
234234
}).AddTokenAuth(token)
235235
MakeRequest(t, req, http.StatusCreated)
236236

237-
resp := testAPIUpdateBranch(t, "user2", "user2", "repo1", from, "new-branch-name", http.StatusForbidden)
237+
resp := testAPIRenameBranch(t, "user2", "user2", "repo1", from, "new-branch-name", http.StatusForbidden)
238238
assert.Contains(t, resp.Body.String(), "Branch is protected by glob-based protection rules.")
239239
})
240-
t.Run("UpdateBranchNormalScenario", func(t *testing.T) {
241-
testAPIUpdateBranch(t, "user2", "user2", "repo1", "branch2", "new-branch-name", http.StatusNoContent)
240+
t.Run("RenameBranchNormalScenario", func(t *testing.T) {
241+
testAPIRenameBranch(t, "user2", "user2", "repo1", "branch2", "new-branch-name", http.StatusNoContent)
242242
})
243243
})
244244
}
245245

246-
func testAPIUpdateBranch(t *testing.T, doerName, ownerName, repoName, from, to string, expectedHTTPStatus int) *httptest.ResponseRecorder {
246+
func testAPIRenameBranch(t *testing.T, doerName, ownerName, repoName, from, to string, expectedHTTPStatus int) *httptest.ResponseRecorder {
247247
token := getUserToken(t, doerName, auth_model.AccessTokenScopeWriteRepository)
248-
req := NewRequestWithJSON(t, "PATCH", "api/v1/repos/"+ownerName+"/"+repoName+"/branches/"+from, &api.UpdateBranchRepoOption{
248+
req := NewRequestWithJSON(t, "PATCH", "api/v1/repos/"+ownerName+"/"+repoName+"/branches/"+from, &api.RenameBranchRepoOption{
249249
Name: to,
250250
}).AddTokenAuth(token)
251251
return MakeRequest(t, req, expectedHTTPStatus)

0 commit comments

Comments
 (0)