Skip to content

Commit 6ed1b26

Browse files
authored
Do not show 500 error when default branch doesn't exist (#34096)
Fix #34090
1 parent 88352e0 commit 6ed1b26

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

Diff for: options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,7 @@ branch.restore_success = Branch "%s" has been restored.
27332733
branch.restore_failed = Failed to restore branch "%s".
27342734
branch.protected_deletion_failed = Branch "%s" is protected. It cannot be deleted.
27352735
branch.default_deletion_failed = Branch "%s" is the default branch. It cannot be deleted.
2736+
branch.default_branch_not_exist = Default branch "%s" does not exist.
27362737
branch.restore = Restore Branch "%s"
27372738
branch.download = Download Branch "%s"
27382739
branch.rename = Rename Branch "%s"

Diff for: routers/web/repo/actions/actions.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"bytes"
88
stdCtx "context"
9+
"errors"
910
"net/http"
1011
"slices"
1112
"strings"
@@ -67,7 +68,11 @@ func List(ctx *context.Context) {
6768
ctx.Data["PageIsActions"] = true
6869

6970
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
70-
if err != nil {
71+
if errors.Is(err, util.ErrNotExist) {
72+
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
73+
ctx.NotFound(nil)
74+
return
75+
} else if err != nil {
7176
ctx.ServerError("GetBranchCommit", err)
7277
return
7378
}

Diff for: routers/web/repo/activity.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
activities_model "code.gitea.io/gitea/models/activities"
11+
"code.gitea.io/gitea/models/git"
1112
"code.gitea.io/gitea/models/unit"
1213
"code.gitea.io/gitea/modules/templates"
1314
"code.gitea.io/gitea/services/context"
@@ -52,12 +53,26 @@ func Activity(ctx *context.Context) {
5253
ctx.Data["DateUntil"] = timeUntil
5354
ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))
5455

56+
canReadCode := ctx.Repo.CanRead(unit.TypeCode)
57+
if canReadCode {
58+
// GetActivityStats needs to read the default branch to get some information
59+
branchExist, _ := git.IsBranchExist(ctx, ctx.Repo.Repository.ID, ctx.Repo.Repository.DefaultBranch)
60+
if !branchExist {
61+
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
62+
ctx.NotFound(nil)
63+
return
64+
}
65+
}
66+
5567
var err error
56-
if ctx.Data["Activity"], err = activities_model.GetActivityStats(ctx, ctx.Repo.Repository, timeFrom,
68+
// TODO: refactor these arguments to a struct
69+
ctx.Data["Activity"], err = activities_model.GetActivityStats(ctx, ctx.Repo.Repository, timeFrom,
5770
ctx.Repo.CanRead(unit.TypeReleases),
5871
ctx.Repo.CanRead(unit.TypeIssues),
5972
ctx.Repo.CanRead(unit.TypePullRequests),
60-
ctx.Repo.CanRead(unit.TypeCode)); err != nil {
73+
canReadCode,
74+
)
75+
if err != nil {
6176
ctx.ServerError("GetActivityStats", err)
6277
return
6378
}

Diff for: services/context/context_response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (ctx *Context) notFoundInternal(logMsg string, logErr error) {
150150

151151
ctx.Data["IsRepo"] = ctx.Repo.Repository != nil
152152
ctx.Data["Title"] = "Page Not Found"
153-
ctx.HTML(http.StatusNotFound, templates.TplName("status/404"))
153+
ctx.HTML(http.StatusNotFound, "status/404")
154154
}
155155

156156
// ServerError displays a 500 (Internal Server Error) page and prints the given error, if any.

Diff for: tests/integration/repo_activity_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"strings"
1010
"testing"
1111

12+
"code.gitea.io/gitea/models/db"
1213
repo_model "code.gitea.io/gitea/models/repo"
14+
"code.gitea.io/gitea/models/unittest"
1315
"code.gitea.io/gitea/modules/test"
1416

1517
"github.com/stretchr/testify/assert"
@@ -61,5 +63,14 @@ func TestRepoActivity(t *testing.T) {
6163
// Should be 3 new issues
6264
list = htmlDoc.doc.Find("#new-issues").Next().Find("p.desc")
6365
assert.Len(t, list.Nodes, 3)
66+
67+
// Non-existing default branch
68+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1"})
69+
repo1.DefaultBranch = "no-such-branch"
70+
_, _ = db.GetEngine(t.Context()).Cols("default_branch").Update(repo1)
71+
req = NewRequest(t, "GET", "/user2/repo1/activity")
72+
req.Header.Add("Accept", "text/html")
73+
resp = session.MakeRequest(t, req, http.StatusNotFound)
74+
assert.Contains(t, resp.Body.String(), `Default branch "no-such-branch" does not exist.`)
6475
})
6576
}

0 commit comments

Comments
 (0)