diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index eb080e4367..a17f0ad40d 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -420,6 +420,7 @@ func UpdateBranch(ctx *context.APIContext) { // in: path // description: name of the branch // type: string + // required: true // - name: body // in: body // schema: @@ -470,6 +471,12 @@ func UpdateBranch(ctx *context.APIContext) { branch, err := ctx.Repo.GitRepo.GetBranch(branchName) 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.NotFound(err) + return + } ctx.Error(http.StatusInternalServerError, "GetBranch", err) return } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 0cd377b2e6..b8006258c4 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -5025,7 +5025,8 @@ "type": "string", "description": "name of the branch", "name": "branch", - "in": "path" + "in": "path", + "required": true }, { "name": "body", diff --git a/tests/integration/api_branch_test.go b/tests/integration/api_branch_test.go index 80a9a66301..e2b3dc86fb 100644 --- a/tests/integration/api_branch_test.go +++ b/tests/integration/api_branch_test.go @@ -218,6 +218,9 @@ func TestAPIUpdateBranch(t *testing.T) { 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