Make the `name` PATCH field required

This commit is contained in:
Kemal Zebari 2024-11-18 22:07:59 -08:00
parent e4f7307511
commit 4445c70698
4 changed files with 19 additions and 44 deletions

View File

@ -283,8 +283,9 @@ type CreateBranchRepoOption struct {
type UpdateBranchRepoOption struct { type UpdateBranchRepoOption struct {
// New branch name // New branch name
// //
// required: true
// unique: true // unique: true
Name string `json:"name" binding:"GitRefName;MaxSize(100)"` Name string `json:"name" binding:"Required;GitRefName;MaxSize(100)"`
} }
// TransferRepoOption options when transfer a repository's ownership // TransferRepoOption options when transfer a repository's ownership

View File

@ -450,33 +450,22 @@ func UpdateBranch(ctx *context.APIContext) {
return return
} }
branchName := opt.Name msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, oldName, opt.Name)
if branchName != "" { if err != nil {
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, oldName, branchName) ctx.Error(http.StatusInternalServerError, "RenameBranch", err)
if err != nil { return
ctx.Error(http.StatusInternalServerError, "RenameBranch", err) }
return if msg == "target_exist" {
} ctx.Error(http.StatusUnprocessableEntity, "", "Cannot rename a branch using the same name or rename to a branch that already exists.")
if msg == "target_exist" { return
ctx.Error(http.StatusUnprocessableEntity, "", "Cannot rename a branch using the same name or rename to a branch that already exists.") }
return if msg == "from_not_exist" {
} ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
if msg == "from_not_exist" { return
ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
return
}
} else {
branchName = oldName
} }
branch, err := ctx.Repo.GitRepo.GetBranch(branchName) branch, err := ctx.Repo.GitRepo.GetBranch(opt.Name)
if err != nil { if err != nil {
if git.IsErrBranchNotExist(err) {
// This could occur if the client passes a non-existent branch and we
// skip executing the branch that contains the RenameBranch() call.
ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
return
}
ctx.Error(http.StatusInternalServerError, "GetBranch", err) ctx.Error(http.StatusInternalServerError, "GetBranch", err)
return return
} }
@ -495,7 +484,7 @@ func UpdateBranch(ctx *context.APIContext) {
br, err := convert.ToBranch(ctx, repo, branch.Name, commit, pb, ctx.Doer, ctx.Repo.IsAdmin()) br, err := convert.ToBranch(ctx, repo, branch.Name, commit, pb, ctx.Doer, ctx.Repo.IsAdmin())
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) ctx.Error(http.StatusInternalServerError, "ToBranch", err)
return return
} }

View File

@ -24946,6 +24946,9 @@
"UpdateBranchRepoOption": { "UpdateBranchRepoOption": {
"description": "UpdateBranchRepoOption options when updating a branch in a repository", "description": "UpdateBranchRepoOption options when updating a branch in a repository",
"type": "object", "type": "object",
"required": [
"name"
],
"properties": { "properties": {
"name": { "name": {
"description": "New branch name", "description": "New branch name",

View File

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"slices"
"testing" "testing"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
@ -204,23 +203,6 @@ func TestAPIUpdateBranch(t *testing.T) {
resp := testAPIUpdateBranch(t, "user2", "repo1", "i-dont-exist", "new-branch-name", http.StatusNotFound) resp := testAPIUpdateBranch(t, "user2", "repo1", "i-dont-exist", "new-branch-name", http.StatusNotFound)
assert.Contains(t, resp.Body.String(), "Branch doesn't exist.") assert.Contains(t, resp.Body.String(), "Branch doesn't exist.")
}) })
t.Run("UpdateBranchWithEmptyStringAsNewName", func(t *testing.T) {
resp := testAPIUpdateBranch(t, "user13", "repo11", "master", "", http.StatusOK)
var branch api.Branch
DecodeJSON(t, resp, &branch)
assert.EqualValues(t, "master", branch.Name)
// Make sure the branch name did not change in the db.
branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
RepoID: 11,
})
assert.NoError(t, err)
branchWasUnchanged := slices.ContainsFunc(branches, func(b *git_model.Branch) bool { return b.Name == "master" })
assert.True(t, branchWasUnchanged, "master branch shouldn't have been renamed")
})
t.Run("UpdateBranchWithNonExistentBranchAndNewNameIsTheEmptyString", func(t *testing.T) {
testAPIUpdateBranch(t, "user2", "repo1", "i-dont-exist", "", http.StatusNotFound)
})
t.Run("RenameBranchNormalScenario", func(t *testing.T) { t.Run("RenameBranchNormalScenario", func(t *testing.T) {
resp := testAPIUpdateBranch(t, "user2", "repo1", "branch2", "new-branch-name", http.StatusOK) resp := testAPIUpdateBranch(t, "user2", "repo1", "branch2", "new-branch-name", http.StatusOK)
var branch api.Branch var branch api.Branch