Skip to content

Commit 83e3528

Browse files
committed
Make the name PATCH field required
1 parent 6a82204 commit 83e3528

File tree

4 files changed

+19
-44
lines changed

4 files changed

+19
-44
lines changed

modules/structs/repo.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ type CreateBranchRepoOption struct {
283283
type UpdateBranchRepoOption struct {
284284
// New branch name
285285
//
286+
// required: true
286287
// unique: true
287-
Name string `json:"name" binding:"GitRefName;MaxSize(100)"`
288+
Name string `json:"name" binding:"Required;GitRefName;MaxSize(100)"`
288289
}
289290

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

routers/api/v1/repo/branch.go

+14-25
Original file line numberDiff line numberDiff line change
@@ -440,33 +440,22 @@ func UpdateBranch(ctx *context.APIContext) {
440440
return
441441
}
442442

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

462-
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
457+
branch, err := ctx.Repo.GitRepo.GetBranch(opt.Name)
463458
if err != nil {
464-
if git.IsErrBranchNotExist(err) {
465-
// This could occur if the client passes a non-existent branch and we
466-
// skip executing the branch that contains the RenameBranch() call.
467-
ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
468-
return
469-
}
470459
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
471460
return
472461
}
@@ -485,7 +474,7 @@ func UpdateBranch(ctx *context.APIContext) {
485474

486475
br, err := convert.ToBranch(ctx, repo, branch.Name, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
487476
if err != nil {
488-
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
477+
ctx.Error(http.StatusInternalServerError, "ToBranch", err)
489478
return
490479
}
491480

templates/swagger/v1_json.tmpl

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/api_branch_test.go

-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"net/http/httptest"
99
"net/url"
10-
"slices"
1110
"testing"
1211

1312
auth_model "code.gitea.io/gitea/models/auth"
@@ -205,23 +204,6 @@ func TestAPIUpdateBranch(t *testing.T) {
205204
resp := testAPIUpdateBranch(t, "user2", "repo1", "i-dont-exist", "new-branch-name", http.StatusNotFound)
206205
assert.Contains(t, resp.Body.String(), "Branch doesn't exist.")
207206
})
208-
t.Run("UpdateBranchWithEmptyStringAsNewName", func(t *testing.T) {
209-
resp := testAPIUpdateBranch(t, "user13", "repo11", "master", "", http.StatusOK)
210-
var branch api.Branch
211-
DecodeJSON(t, resp, &branch)
212-
assert.EqualValues(t, "master", branch.Name)
213-
214-
// Make sure the branch name did not change in the db.
215-
branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
216-
RepoID: 11,
217-
})
218-
assert.NoError(t, err)
219-
branchWasUnchanged := slices.ContainsFunc(branches, func(b *git_model.Branch) bool { return b.Name == "master" })
220-
assert.True(t, branchWasUnchanged, "master branch shouldn't have been renamed")
221-
})
222-
t.Run("UpdateBranchWithNonExistentBranchAndNewNameIsTheEmptyString", func(t *testing.T) {
223-
testAPIUpdateBranch(t, "user2", "repo1", "i-dont-exist", "", http.StatusNotFound)
224-
})
225207
t.Run("RenameBranchNormalScenario", func(t *testing.T) {
226208
resp := testAPIUpdateBranch(t, "user2", "repo1", "branch2", "new-branch-name", http.StatusOK)
227209
var branch api.Branch

0 commit comments

Comments
 (0)