Skip to content

Commit bcb0d8b

Browse files
committed
fix
1 parent ae0bc02 commit bcb0d8b

10 files changed

Lines changed: 81 additions & 296 deletions

File tree

custom/conf/app.example.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,9 +1153,6 @@ LEVEL = Info
11531153
;; Add co-authored-by and co-committed-by trailers if committer does not match author
11541154
;ADD_CO_COMMITTER_TRAILERS = true
11551155
;;
1156-
;; In addition to testing patches using the three-way merge method, re-test conflicting patches with git apply
1157-
;TEST_CONFLICTING_PATCHES_WITH_GIT_APPLY = false
1158-
;;
11591156
;; Retarget child pull requests to the parent pull request branch target on merge of parent pull request. It only works on merged PRs where the head and base branch target the same repo.
11601157
;RETARGET_CHILDREN_ON_MERGE = true
11611158
;;

models/auth/source.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/db"
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/optional"
15+
"code.gitea.io/gitea/modules/setting"
1516
"code.gitea.io/gitea/modules/timeutil"
1617
"code.gitea.io/gitea/modules/util"
1718

@@ -139,7 +140,10 @@ func init() {
139140
// BeforeSet is invoked from XORM before setting the value of a field of this object.
140141
func (source *Source) BeforeSet(colName string, val xorm.Cell) {
141142
if colName == "type" {
142-
typ := Type(db.Cell2Int64(val))
143+
typ, _, err := db.CellToInt(val, NoType)
144+
if err != nil {
145+
setting.PanicInDevOrTesting("Unable to convert login source (id=%d) type: %v", source.ID, err)
146+
}
143147
constructor, ok := registeredConfigs[typ]
144148
if !ok {
145149
return

models/auth/source_test.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ import (
1717
)
1818

1919
type TestSource struct {
20-
auth_model.ConfigBase
20+
auth_model.ConfigBase `json:"-"`
2121

22-
Provider string
23-
ClientID string
24-
ClientSecret string
25-
OpenIDConnectAutoDiscoveryURL string
26-
IconURL string
22+
TestField string
2723
}
2824

2925
// FromDB fills up a LDAPConfig from serialized format.
@@ -37,27 +33,23 @@ func (source *TestSource) ToDB() ([]byte, error) {
3733
}
3834

3935
func TestDumpAuthSource(t *testing.T) {
40-
assert.NoError(t, unittest.PrepareTestDatabase())
36+
require.NoError(t, unittest.PrepareTestDatabase())
4137

4238
authSourceSchema, err := unittest.GetXORMEngine().TableInfo(new(auth_model.Source))
43-
assert.NoError(t, err)
39+
require.NoError(t, err)
4440

4541
auth_model.RegisterTypeConfig(auth_model.OAuth2, new(TestSource))
46-
47-
auth_model.CreateSource(t.Context(), &auth_model.Source{
48-
Type: auth_model.OAuth2,
49-
Name: "TestSource",
50-
IsActive: false,
51-
Cfg: &TestSource{
52-
Provider: "ConvertibleSourceName",
53-
ClientID: "42",
54-
},
55-
})
56-
57-
sb := new(strings.Builder)
58-
59-
// TODO: this test is quite hacky, it should use a low-level "select" (without model processors) but not a database dump
60-
engine := unittest.GetXORMEngine()
61-
require.NoError(t, engine.DumpTables([]*schemas.Table{authSourceSchema}, sb))
62-
assert.Contains(t, sb.String(), `"Provider":"ConvertibleSourceName"`)
42+
source := &auth_model.Source{
43+
Type: auth_model.OAuth2,
44+
Name: "TestSource",
45+
Cfg: &TestSource{TestField: "TestValue"},
46+
}
47+
require.NoError(t, auth_model.CreateSource(t.Context(), source))
48+
49+
// intentionally test the "dump" to make sure the dumped JSON is correct: https://github.com/go-gitea/gitea/pull/16847
50+
sb := &strings.Builder{}
51+
require.NoError(t, unittest.GetXORMEngine().DumpTables([]*schemas.Table{authSourceSchema}, sb))
52+
// the dumped SQL is something like:
53+
// INSERT INTO `login_source` (`id`, `type`, `name`, `is_active`, `is_sync_enabled`, `two_factor_policy`, `cfg`, `created_unix`, `updated_unix`) VALUES (1,6,'TestSource',0,0,'','{"TestField":"TestValue"}',1774179784,1774179784);
54+
assert.Contains(t, sb.String(), `'{"TestField":"TestValue"}'`)
6355
}

models/db/convert.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ package db
55

66
import (
77
"fmt"
8-
"strconv"
98

10-
"code.gitea.io/gitea/modules/log"
119
"code.gitea.io/gitea/modules/setting"
1210

1311
"xorm.io/xorm"
12+
"xorm.io/xorm/convert"
1413
"xorm.io/xorm/schemas"
1514
)
1615

@@ -74,15 +73,14 @@ WHERE ST.name ='varchar'`)
7473
return err
7574
}
7675

77-
// Cell2Int64 converts a xorm.Cell type to int64,
78-
// and handles possible irregular cases.
79-
func Cell2Int64(val xorm.Cell) int64 {
80-
switch (*val).(type) {
81-
case []uint8:
82-
log.Trace("Cell2Int64 ([]uint8): %v", *val)
83-
84-
v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64)
85-
return v
76+
// CellToInt converts a xorm.Cell field value to an int value
77+
func CellToInt[T ~int | int64](cell xorm.Cell, def T) (ret T, has bool, err error) {
78+
if *cell == nil {
79+
return def, false, nil
80+
}
81+
val, err := convert.AsInt64(*cell)
82+
if err != nil {
83+
return def, false, err
8684
}
87-
return (*val).(int64)
85+
return T(val), true, err
8886
}

models/repo/repo_unit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ func (cfg *ProjectsConfig) IsProjectsAllowed(m ProjectsMode) bool {
226226
func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
227227
switch colName {
228228
case "type":
229-
r.Type = unit.Type(db.Cell2Int64(val))
229+
var err error
230+
r.Type, _, err = db.CellToInt(val, unit.TypeInvalid)
231+
if err != nil {
232+
setting.PanicInDevOrTesting("Unable to convert repo unit (id=%d) type: %v", r.ID, err)
233+
}
230234
switch r.Type {
231235
case unit.TypeExternalWiki:
232236
r.Config = new(ExternalWikiConfig)

modules/setting/repository.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ var (
8686
DefaultMergeMessageOfficialApproversOnly bool
8787
PopulateSquashCommentWithCommitMessages bool
8888
AddCoCommitterTrailers bool
89-
TestConflictingPatchesWithGitApply bool
9089
RetargetChildrenOnMerge bool
9190
DelayCheckForInactiveDays int
9291
} `ini:"repository.pull-request"`
@@ -210,7 +209,6 @@ var (
210209
DefaultMergeMessageOfficialApproversOnly bool
211210
PopulateSquashCommentWithCommitMessages bool
212211
AddCoCommitterTrailers bool
213-
TestConflictingPatchesWithGitApply bool
214212
RetargetChildrenOnMerge bool
215213
DelayCheckForInactiveDays int
216214
}{

modules/translation/mock.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package translation
55

66
import (
77
"fmt"
8+
"html"
89
"html/template"
9-
"strings"
1010
)
1111

1212
// MockLocale provides a mocked locale without any translations
@@ -20,25 +20,40 @@ func (l MockLocale) Language() string {
2020
return "en"
2121
}
2222

23-
func (l MockLocale) TrString(s string, args ...any) string {
24-
return sprintAny(s, args...)
23+
func (l MockLocale) TrString(format string, args ...any) (ret string) {
24+
ret = format + ":"
25+
for _, arg := range args {
26+
// usually there is no arg or at most 1-2 args, so a simple string concatenation is more efficient
27+
switch v := arg.(type) {
28+
case string:
29+
ret += v + ","
30+
default:
31+
ret += fmt.Sprint(v) + ","
32+
}
33+
}
34+
return ret[:len(ret)-1]
2535
}
2636

27-
func (l MockLocale) Tr(s string, args ...any) template.HTML {
28-
return template.HTML(sprintAny(s, args...))
37+
func (l MockLocale) Tr(format string, args ...any) (ret template.HTML) {
38+
ret = template.HTML(html.EscapeString(format)) + ":"
39+
for _, arg := range args {
40+
// usually there is no arg or at most 1-2 args, so a simple string concatenation is more efficient
41+
switch v := arg.(type) {
42+
case template.HTML:
43+
ret += v + ","
44+
case string:
45+
ret += template.HTML(html.EscapeString(v)) + ","
46+
default:
47+
ret += template.HTML(html.EscapeString(fmt.Sprint(v))) + ","
48+
}
49+
}
50+
return ret[:len(ret)-1]
2951
}
3052

3153
func (l MockLocale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
32-
return template.HTML(sprintAny(key1, args...))
54+
return l.Tr(key1, args...)
3355
}
3456

3557
func (l MockLocale) PrettyNumber(v any) string {
3658
return fmt.Sprint(v)
3759
}
38-
39-
func sprintAny(s string, args ...any) string {
40-
if len(args) == 0 {
41-
return s
42-
}
43-
return s + ":" + fmt.Sprintf(strings.Repeat(",%v", len(args))[1:], args...)
44-
}

routers/common/maintenancemode.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func isMaintenanceModeAllowedRequest(req *http.Request) bool {
1515
// URLs like "/-/admin", "/-/fetch-redirect" and "/-/markup" are still accessible in maintenance mode
1616
return true
1717
}
18+
if strings.HasPrefix(req.URL.Path, "/.well-known/") {
19+
return true
20+
}
1821
if strings.HasPrefix(req.URL.Path, "/api/internal/") {
1922
// internal APIs should be allowed
2023
return true
@@ -26,6 +29,15 @@ func isMaintenanceModeAllowedRequest(req *http.Request) bool {
2629
if strings.HasPrefix(req.URL.Path, "/assets/") {
2730
return true
2831
}
32+
if strings.HasPrefix(req.URL.Path, "/avatars/") {
33+
return true
34+
}
35+
if strings.HasPrefix(req.URL.Path, "/captcha/") {
36+
return true
37+
}
38+
if req.URL.Path == "/api/healthz" {
39+
return true
40+
}
2941
return false
3042
}
3143

0 commit comments

Comments
 (0)