Skip to content

Commit 4445c70

Browse files
committed
Make the name PATCH field required
1 parent e4f7307 commit 4445c70

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
@@ -450,33 +450,22 @@ func UpdateBranch(ctx *context.APIContext) {
450450
return
451451
}
452452

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

472-
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
467+
branch, err := ctx.Repo.GitRepo.GetBranch(opt.Name)
473468
if err != nil {
474-
if git.IsErrBranchNotExist(err) {
475-
// This could occur if the client passes a non-existent branch and we
476-
// skip executing the branch that contains the RenameBranch() call.
477-
ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
478-
return
479-
}
480469
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
481470
return
482471
}
@@ -495,7 +484,7 @@ func UpdateBranch(ctx *context.APIContext) {
495484

496485
br, err := convert.ToBranch(ctx, repo, branch.Name, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
497486
if err != nil {
498-
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
487+
ctx.Error(http.StatusInternalServerError, "ToBranch", err)
499488
return
500489
}
501490

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

0 commit comments

Comments
 (0)