0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-21 11:06:17 +01:00

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 {
// 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

View file

@ -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
}

View file

@ -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",

View file

@ -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