Make the `name` PATCH field required

pull/32433/head
Kemal Zebari 3 days ago
parent e4f7307511
commit 4445c70698
  1. 3
      modules/structs/repo.go
  2. 39
      routers/api/v1/repo/branch.go
  3. 3
      templates/swagger/v1_json.tmpl
  4. 18
      tests/integration/api_branch_test.go

@ -283,8 +283,9 @@ type CreateBranchRepoOption struct {
type UpdateBranchRepoOption struct {
// New branch name
//
// required: 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

@ -450,33 +450,22 @@ func UpdateBranch(ctx *context.APIContext) {
return
}
branchName := opt.Name
if branchName != "" {
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, oldName, branchName)
if err != nil {
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.")
return
}
if msg == "from_not_exist" {
ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
return
}
} else {
branchName = oldName
msg, err := repo_service.RenameBranch(ctx, repo, ctx.Doer, ctx.Repo.GitRepo, oldName, opt.Name)
if err != nil {
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.")
return
}
if msg == "from_not_exist" {
ctx.Error(http.StatusNotFound, "", "Branch doesn't exist.")
return
}
branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
branch, err := ctx.Repo.GitRepo.GetBranch(opt.Name)
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)
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())
if err != nil {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
ctx.Error(http.StatusInternalServerError, "ToBranch", err)
return
}

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

@ -7,7 +7,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"slices"
"testing"
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)
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) {
resp := testAPIUpdateBranch(t, "user2", "repo1", "branch2", "new-branch-name", http.StatusOK)
var branch api.Branch

Loading…
Cancel
Save