2017-10-15 21:59:24 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-15 21:59:24 +02:00
|
|
|
|
2022-09-02 21:18:23 +02:00
|
|
|
package integration
|
2017-10-15 21:59:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-05-11 17:29:17 +02:00
|
|
|
"net/url"
|
2017-10-15 21:59:24 +02:00
|
|
|
"path"
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
"strconv"
|
2017-10-15 21:59:24 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-12-17 22:49:58 +01:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-12-17 22:49:58 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-07-08 15:57:24 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-12-15 22:11:02 +01:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-06-26 16:19:22 +02:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2022-09-02 21:18:23 +02:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-12-15 22:11:02 +01:00
|
|
|
|
2017-10-15 21:59:24 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:27 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-10-15 21:59:24 +02:00
|
|
|
)
|
|
|
|
|
2017-12-24 01:33:34 +01:00
|
|
|
func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
|
2017-10-15 21:59:24 +02:00
|
|
|
var csrf string
|
|
|
|
if expectedStatus == http.StatusNotFound {
|
2017-10-30 03:04:25 +01:00
|
|
|
csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))
|
2017-10-15 21:59:24 +02:00
|
|
|
} else {
|
2017-10-30 03:04:25 +01:00
|
|
|
csrf = GetCSRF(t, session, path.Join(user, repo, "src", oldRefSubURL))
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|
2017-10-30 03:04:25 +01:00
|
|
|
req := NewRequestWithValues(t, "POST", path.Join(user, repo, "branches/_new", oldRefSubURL), map[string]string{
|
2017-10-15 21:59:24 +02:00
|
|
|
"_csrf": csrf,
|
|
|
|
"new_branch_name": newBranchName,
|
|
|
|
})
|
|
|
|
resp := session.MakeRequest(t, req, expectedStatus)
|
2022-03-23 05:54:07 +01:00
|
|
|
if expectedStatus != http.StatusSeeOther {
|
2017-10-15 21:59:24 +02:00
|
|
|
return ""
|
|
|
|
}
|
2017-12-15 22:11:02 +01:00
|
|
|
return test.RedirectURL(resp)
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBranch(t *testing.T) {
|
2019-05-11 17:29:17 +02:00
|
|
|
onGiteaRun(t, testCreateBranches)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateBranches(t *testing.T, giteaURL *url.URL) {
|
2017-10-15 21:59:24 +02:00
|
|
|
tests := []struct {
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL string
|
|
|
|
NewBranch string
|
|
|
|
CreateRelease string
|
|
|
|
FlashMessage string
|
|
|
|
ExpectedStatus int
|
2023-12-17 22:49:58 +01:00
|
|
|
CheckBranch bool
|
2017-10-15 21:59:24 +02:00
|
|
|
}{
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "feature/test1",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.create_success", "feature/test1"),
|
2023-12-17 22:49:58 +01:00
|
|
|
CheckBranch: true,
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("form.NewBranchName") + translation.NewLocale("en-US").TrString("form.require_error"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "feature=test1",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.create_success", "feature=test1"),
|
2023-12-17 22:49:58 +01:00
|
|
|
CheckBranch: true,
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: strings.Repeat("b", 101),
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("form.NewBranchName") + translation.NewLocale("en-US").TrString("form.max_size_error", "100"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "master",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.branch_already_exists", "master"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "master/test",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.branch_name_conflict", "master/test", "master"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0",
|
|
|
|
NewBranch: "feature/test2",
|
|
|
|
ExpectedStatus: http.StatusNotFound,
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
NewBranch: "feature/test3",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.create_success", "feature/test3"),
|
2023-12-17 22:49:58 +01:00
|
|
|
CheckBranch: true,
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "v1.0.0",
|
|
|
|
CreateRelease: "v1.0.0",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.tag_collision", "v1.0.0"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "tag/v1.0.0",
|
|
|
|
NewBranch: "feature/test4",
|
2020-11-08 22:24:54 +01:00
|
|
|
CreateRelease: "v1.0.1",
|
2022-03-23 05:54:07 +01:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2024-02-14 22:48:45 +01:00
|
|
|
FlashMessage: translation.NewLocale("en-US").TrString("repo.branch.create_success", "feature/test4"),
|
2023-12-17 22:49:58 +01:00
|
|
|
CheckBranch: true,
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
}
|
2023-12-17 22:49:58 +01:00
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
2017-10-15 21:59:24 +02:00
|
|
|
for _, test := range tests {
|
2023-12-17 22:49:58 +01:00
|
|
|
if test.CheckBranch {
|
|
|
|
unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: 1, Name: test.NewBranch})
|
|
|
|
}
|
2017-10-15 21:59:24 +02:00
|
|
|
if test.CreateRelease != "" {
|
|
|
|
createNewRelease(t, session, "/user2/repo1", test.CreateRelease, test.CreateRelease, false, false)
|
|
|
|
}
|
2017-10-30 03:04:25 +01:00
|
|
|
redirectURL := testCreateBranch(t, session, "user2", "repo1", test.OldRefSubURL, test.NewBranch, test.ExpectedStatus)
|
2022-03-23 05:54:07 +01:00
|
|
|
if test.ExpectedStatus == http.StatusSeeOther {
|
2017-10-15 21:59:24 +02:00
|
|
|
req := NewRequest(t, "GET", redirectURL)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2023-07-08 05:19:00 +02:00
|
|
|
assert.Contains(t,
|
2017-10-15 21:59:24 +02:00
|
|
|
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
|
2023-07-08 05:19:00 +02:00
|
|
|
test.FlashMessage,
|
2017-10-15 21:59:24 +02:00
|
|
|
)
|
|
|
|
}
|
2023-12-17 22:49:58 +01:00
|
|
|
if test.CheckBranch {
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: 1, Name: test.NewBranch})
|
|
|
|
}
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBranchInvalidCSRF(t *testing.T) {
|
2022-09-02 21:18:23 +02:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2017-10-15 21:59:24 +02:00
|
|
|
session := loginUser(t, "user2")
|
2017-10-30 03:04:25 +01:00
|
|
|
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master", map[string]string{
|
2017-10-15 21:59:24 +02:00
|
|
|
"_csrf": "fake_csrf",
|
|
|
|
"new_branch_name": "test",
|
|
|
|
})
|
2022-03-23 05:54:07 +01:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-07-08 15:57:24 +02:00
|
|
|
loc := resp.Header().Get("Location")
|
|
|
|
assert.Equal(t, setting.AppSubURL+"/", loc)
|
|
|
|
resp = session.MakeRequest(t, NewRequest(t, "GET", loc), http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
assert.Equal(t,
|
2021-12-28 14:28:27 +01:00
|
|
|
"Bad Request: invalid CSRF token",
|
2021-07-08 15:57:24 +02:00
|
|
|
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
|
|
|
|
)
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
|
|
|
|
func TestDatabaseMissingABranch(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, URL *url.URL) {
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
|
|
|
// Create two branches
|
|
|
|
testCreateBranch(t, session, "user2", "repo1", "branch/master", "will-be-present", http.StatusSeeOther)
|
|
|
|
testCreateBranch(t, session, "user2", "repo1", "branch/master", "will-be-missing", http.StatusSeeOther)
|
|
|
|
|
|
|
|
// Run the repo branch sync, to ensure the db and git agree.
|
2024-04-29 10:47:56 +02:00
|
|
|
err2 := repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext())
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err2)
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
|
|
|
|
// Delete one branch from git only, leaving it in the database
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
cmd := git.NewCommand(db.DefaultContext, "branch", "-D").AddDynamicArguments("will-be-missing")
|
|
|
|
_, _, err := cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
|
|
|
|
// Verify that loading the repo's branches page works still, and that it
|
|
|
|
// reports at least three branches (master, will-be-present, and
|
|
|
|
// will-be-missing).
|
|
|
|
req := NewRequest(t, "GET", "/user2/repo1/branches")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
|
|
firstBranchCount, _ := strconv.Atoi(doc.Find(".repository-menu a[href*='/branches'] b").Text())
|
|
|
|
assert.GreaterOrEqual(t, firstBranchCount, 3)
|
|
|
|
|
|
|
|
// Run the repo branch sync again
|
2024-04-29 10:47:56 +02:00
|
|
|
err2 = repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext())
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err2)
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 21:02:49 +01:00
|
|
|
|
|
|
|
// Verify that loading the repo's branches page works still, and that it
|
|
|
|
// reports one branch less than the first time.
|
|
|
|
//
|
|
|
|
// NOTE: This assumes that the branch counter on the web UI is out of
|
|
|
|
// date before the sync. If that problem gets resolved, we'll have to
|
|
|
|
// find another way to test that the syncing works.
|
|
|
|
req = NewRequest(t, "GET", "/user2/repo1/branches")
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc = NewHTMLParser(t, resp.Body)
|
|
|
|
secondBranchCount, _ := strconv.Atoi(doc.Find(".repository-menu a[href*='/branches'] b").Text())
|
|
|
|
assert.Equal(t, firstBranchCount-1, secondBranchCount)
|
|
|
|
})
|
|
|
|
}
|