Skip to content

Commit

Permalink
Make the name PATCH field required
Browse files Browse the repository at this point in the history
  • Loading branch information
kemzeb committed Nov 19, 2024
1 parent e4f7307 commit 4445c70
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
3 changes: 2 additions & 1 deletion modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 14 additions & 25 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions tests/integration/api_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"slices"
"testing"

auth_model "code.gitea.io/gitea/models/auth"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4445c70

Please sign in to comment.