Skip to content

Commit 4c4a16c

Browse files
committed
Merge branch 'main' into lunny/project_workflow
2 parents 984d4b0 + de70cd3 commit 4c4a16c

File tree

34 files changed

+661
-117
lines changed

34 files changed

+661
-117
lines changed

cmd/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func globalBool(c *cli.Command, name string) bool {
121121
// Any log appears in git stdout pipe will break the git protocol, eg: client can't push and hangs forever.
122122
func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cli.Command) (context.Context, error) {
123123
return func(ctx context.Context, c *cli.Command) (context.Context, error) {
124+
if setting.InstallLock {
125+
// During config loading, there might also be logs (for example: deprecation warnings).
126+
// It must make sure that console logger is set up before config is loaded.
127+
log.Error("Config is loaded before console logger is setup, it will cause bugs. Please fix it.")
128+
return nil, errors.New("console logger must be setup before config is loaded")
129+
}
124130
level := defaultLevel
125131
if globalBool(c, "quiet") {
126132
level = log.FATAL

cmd/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
var CmdKeys = &cli.Command{
2020
Name: "keys",
2121
Usage: "(internal) Should only be called by SSH server",
22-
Hidden: true, // internal commands shouldn't not be visible
22+
Hidden: true, // internal commands shouldn't be visible
2323
Description: "Queries the Gitea database to get the authorized command for a given ssh key fingerprint",
2424
Before: PrepareConsoleLoggerLevel(log.FATAL),
2525
Action: runKeys,

cmd/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ DEFAULT CONFIGURATION:
5050

5151
func prepareSubcommandWithGlobalFlags(originCmd *cli.Command) {
5252
originBefore := originCmd.Before
53-
originCmd.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
54-
prepareWorkPathAndCustomConf(cmd)
53+
originCmd.Before = func(ctxOrig context.Context, cmd *cli.Command) (ctx context.Context, err error) {
54+
ctx = ctxOrig
5555
if originBefore != nil {
56-
return originBefore(ctx, cmd)
56+
ctx, err = originBefore(ctx, cmd)
57+
if err != nil {
58+
return ctx, err
59+
}
5760
}
61+
prepareWorkPathAndCustomConf(cmd)
5862
return ctx, nil
5963
}
6064
}

cmd/main_test.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/models/unittest"
1616
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/modules/test"
18+
"code.gitea.io/gitea/modules/util"
1819

1920
"github.com/stretchr/testify/assert"
2021
"github.com/urfave/cli/v3"
@@ -28,11 +29,11 @@ func makePathOutput(workPath, customPath, customConf string) string {
2829
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
2930
}
3031

31-
func newTestApp(testCmdAction cli.ActionFunc) *cli.Command {
32+
func newTestApp(testCmd cli.Command) *cli.Command {
3233
app := NewMainApp(AppVersion{})
33-
testCmd := &cli.Command{Name: "test-cmd", Action: testCmdAction}
34-
prepareSubcommandWithGlobalFlags(testCmd)
35-
app.Commands = append(app.Commands, testCmd)
34+
testCmd.Name = util.IfZero(testCmd.Name, "test-cmd")
35+
prepareSubcommandWithGlobalFlags(&testCmd)
36+
app.Commands = append(app.Commands, &testCmd)
3637
app.DefaultCommand = testCmd.Name
3738
return app
3839
}
@@ -156,9 +157,11 @@ func TestCliCmd(t *testing.T) {
156157

157158
for _, c := range cases {
158159
t.Run(c.cmd, func(t *testing.T) {
159-
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error {
160-
_, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
161-
return nil
160+
app := newTestApp(cli.Command{
161+
Action: func(ctx context.Context, cmd *cli.Command) error {
162+
_, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
163+
return nil
164+
},
162165
})
163166
for k, v := range c.env {
164167
t.Setenv(k, v)
@@ -173,31 +176,54 @@ func TestCliCmd(t *testing.T) {
173176
}
174177

175178
func TestCliCmdError(t *testing.T) {
176-
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error { return errors.New("normal error") })
179+
app := newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return errors.New("normal error") }})
177180
r, err := runTestApp(app, "./gitea", "test-cmd")
178181
assert.Error(t, err)
179182
assert.Equal(t, 1, r.ExitCode)
180183
assert.Empty(t, r.Stdout)
181184
assert.Equal(t, "Command error: normal error\n", r.Stderr)
182185

183-
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return cli.Exit("exit error", 2) })
186+
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return cli.Exit("exit error", 2) }})
184187
r, err = runTestApp(app, "./gitea", "test-cmd")
185188
assert.Error(t, err)
186189
assert.Equal(t, 2, r.ExitCode)
187190
assert.Empty(t, r.Stdout)
188191
assert.Equal(t, "exit error\n", r.Stderr)
189192

190-
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
193+
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }})
191194
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
192195
assert.Error(t, err)
193196
assert.Equal(t, 1, r.ExitCode)
194197
assert.Empty(t, r.Stdout)
195198
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stderr)
196199

197-
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
200+
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }})
198201
r, err = runTestApp(app, "./gitea", "test-cmd")
199202
assert.NoError(t, err)
200203
assert.Equal(t, -1, r.ExitCode) // the cli.OsExiter is not called
201204
assert.Empty(t, r.Stdout)
202205
assert.Empty(t, r.Stderr)
203206
}
207+
208+
func TestCliCmdBefore(t *testing.T) {
209+
ctxNew := context.WithValue(context.Background(), any("key"), "value")
210+
configValues := map[string]string{}
211+
setting.CustomConf = "/tmp/any.ini"
212+
var actionCtx context.Context
213+
app := newTestApp(cli.Command{
214+
Before: func(context.Context, *cli.Command) (context.Context, error) {
215+
configValues["before"] = setting.CustomConf
216+
return ctxNew, nil
217+
},
218+
Action: func(ctx context.Context, cmd *cli.Command) error {
219+
configValues["action"] = setting.CustomConf
220+
actionCtx = ctx
221+
return nil
222+
},
223+
})
224+
_, err := runTestApp(app, "./gitea", "--config", "/dev/null", "test-cmd")
225+
assert.NoError(t, err)
226+
assert.Equal(t, ctxNew, actionCtx)
227+
assert.Equal(t, "/tmp/any.ini", configValues["before"], "BeforeFunc must be called before preparing config")
228+
assert.Equal(t, "/dev/null", configValues["action"])
229+
}

eslint.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ export default defineConfig([
924924
'vue/html-closing-bracket-spacing': [2, {startTag: 'never', endTag: 'never', selfClosingTag: 'never'}],
925925
'vue/max-attributes-per-line': [0],
926926
'vue/singleline-html-element-content-newline': [0],
927+
'vue/require-typed-ref': [2],
927928
},
928929
},
929930
{

models/migrations/base/tests.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/stretchr/testify/require"
2121
"xorm.io/xorm"
22+
"xorm.io/xorm/schemas"
2223
)
2324

2425
// FIXME: this file shouldn't be in a normal package, it should only be compiled for tests
@@ -88,6 +89,16 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
8889
return x, deferFn
8990
}
9091

92+
func LoadTableSchemasMap(t *testing.T, x *xorm.Engine) map[string]*schemas.Table {
93+
tables, err := x.DBMetas()
94+
require.NoError(t, err)
95+
tableMap := make(map[string]*schemas.Table)
96+
for _, table := range tables {
97+
tableMap[table.Name] = table
98+
}
99+
return tableMap
100+
}
101+
91102
func MainTest(m *testing.M) {
92103
testlogger.Init()
93104

models/migrations/migrations.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ func prepareMigrationTasks() []*migration {
380380
newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices),
381381
newMigration(310, "Add Priority to ProtectedBranch", v1_23.AddPriorityToProtectedBranch),
382382
newMigration(311, "Add TimeEstimate to Issue table", v1_23.AddTimeEstimateColumnToIssueTable),
383-
384383
// Gitea 1.23.0-rc0 ends at migration ID number 311 (database version 312)
384+
385385
newMigration(312, "Add DeleteBranchAfterMerge to AutoMerge", v1_24.AddDeleteBranchAfterMergeForAutoMerge),
386386
newMigration(313, "Move PinOrder from issue table to a new table issue_pin", v1_24.MovePinOrderToTableIssuePin),
387387
newMigration(314, "Update OwnerID as zero for repository level action tables", v1_24.UpdateOwnerIDOfRepoLevelActionsTables),
@@ -391,12 +391,12 @@ func prepareMigrationTasks() []*migration {
391391
newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode),
392392
newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
393393
newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
394+
// Gitea 1.24.0 ends at migration ID number 320 (database version 321)
394395

395-
// Gitea 1.24.0 ends at database version 321
396396
newMigration(321, "Use LONGTEXT for some columns and fix review_state.updated_files column", v1_25.UseLongTextInSomeColumnsAndFixBugs),
397397
newMigration(322, "Extend comment tree_path length limit", v1_25.ExtendCommentTreePathLength),
398+
// Gitea 1.25.0 ends at migration ID number 322 (database version 323)
398399

399-
// Gitea 1.25.0 ends at database version 323
400400
newMigration(323, "Add support for actions concurrency", v1_26.AddActionsConcurrency),
401401
newMigration(324, "Add new table project_workflow", v1_26.AddProjectWorkflow),
402402
}

models/migrations/v1_25/v321_test.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/modules/timeutil"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func Test_UseLongTextInSomeColumnsAndFixBugs(t *testing.T) {
@@ -38,33 +39,26 @@ func Test_UseLongTextInSomeColumnsAndFixBugs(t *testing.T) {
3839
type Notice struct {
3940
ID int64 `xorm:"pk autoincr"`
4041
Type int
41-
Description string `xorm:"LONGTEXT"`
42+
Description string `xorm:"TEXT"`
4243
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
4344
}
4445

4546
// Prepare and load the testing database
46-
x, deferable := base.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice))
47-
defer deferable()
47+
x, deferrable := base.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice))
48+
defer deferrable()
4849

49-
assert.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x))
50+
require.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x))
5051

51-
tables, err := x.DBMetas()
52-
assert.NoError(t, err)
52+
tables := base.LoadTableSchemasMap(t, x)
53+
table := tables["review_state"]
54+
column := table.GetColumn("updated_files")
55+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
5356

54-
for _, table := range tables {
55-
switch table.Name {
56-
case "review_state":
57-
column := table.GetColumn("updated_files")
58-
assert.NotNil(t, column)
59-
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
60-
case "package_property":
61-
column := table.GetColumn("value")
62-
assert.NotNil(t, column)
63-
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
64-
case "notice":
65-
column := table.GetColumn("description")
66-
assert.NotNil(t, column)
67-
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
68-
}
69-
}
57+
table = tables["package_property"]
58+
column = table.GetColumn("value")
59+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
60+
61+
table = tables["notice"]
62+
column = table.GetColumn("description")
63+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
7064
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_25
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
"code.gitea.io/gitea/modules/setting"
11+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func Test_ExtendCommentTreePathLength(t *testing.T) {
17+
if setting.Database.Type.IsSQLite3() {
18+
t.Skip("For SQLITE, varchar or char will always be represented as TEXT")
19+
}
20+
21+
type Comment struct {
22+
ID int64 `xorm:"pk autoincr"`
23+
TreePath string `xorm:"VARCHAR(255)"`
24+
}
25+
26+
x, deferrable := base.PrepareTestEnv(t, 0, new(Comment))
27+
defer deferrable()
28+
29+
require.NoError(t, ExtendCommentTreePathLength(x))
30+
table := base.LoadTableSchemasMap(t, x)["comment"]
31+
column := table.GetColumn("tree_path")
32+
assert.Contains(t, []string{"NVARCHAR", "VARCHAR"}, column.SQLType.Name)
33+
assert.EqualValues(t, 4000, column.Length)
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_26
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
base.MainTest(m)
14+
}

0 commit comments

Comments
 (0)