Skip to content

Commit 2b4e2b4

Browse files
committed
Do not show 500 error when default branch doesn't exist (go-gitea#34096)
Fix go-gitea#34090 # Conflicts: # routers/web/repo/actions/actions.go # services/context/context_response.go
1 parent 25e409e commit 2b4e2b4

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,7 @@ branch.restore_success = Branch "%s" has been restored.
26992699
branch.restore_failed = Failed to restore branch "%s".
27002700
branch.protected_deletion_failed = Branch "%s" is protected. It cannot be deleted.
27012701
branch.default_deletion_failed = Branch "%s" is the default branch. It cannot be deleted.
2702+
branch.default_branch_not_exist = Default branch "%s" does not exist.
27022703
branch.restore = Restore Branch "%s"
27032704
branch.download = Download Branch "%s"
27042705
branch.rename = Rename Branch "%s"

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
"fmt"
1011
"net/http"
1112
"slices"
@@ -77,7 +78,11 @@ func List(ctx *context.Context) {
7778
return
7879
} else if !empty {
7980
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
80-
if err != nil {
81+
if errors.Is(err, util.ErrNotExist) {
82+
ctx.Data["NotFoundPrompt"] = ctx.Tr("repo.branch.default_branch_not_exist", ctx.Repo.Repository.DefaultBranch)
83+
ctx.NotFound("GetBranchCommit", err)
84+
return
85+
} else if err != nil {
8186
ctx.ServerError("GetBranchCommit", err)
8287
return
8388
}

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/base"
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
}

services/context/context_response.go

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

152152
ctx.Data["IsRepo"] = ctx.Repo.Repository != nil
153153
ctx.Data["Title"] = "Page Not Found"
154-
ctx.HTML(http.StatusNotFound, base.TplName("status/404"))
154+
ctx.HTML(http.StatusNotFound, "status/404")
155155
}
156156

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

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)