Skip to content

Commit de9f6d8

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: [skip ci] Updated translations via Crowdin Let `curl` write the content to file (go-gitea#28427) Improve doctor cli behavior (go-gitea#28422) Second part of refactor `db.Find` (go-gitea#28194)
2 parents e465c79 + baea205 commit de9f6d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+303
-576
lines changed

cmd/doctor.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/models/db"
1515
"code.gitea.io/gitea/models/migrations"
1616
migrate_base "code.gitea.io/gitea/models/migrations/base"
17+
"code.gitea.io/gitea/modules/container"
1718
"code.gitea.io/gitea/modules/doctor"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/setting"
@@ -22,6 +23,19 @@ import (
2223
"xorm.io/xorm"
2324
)
2425

26+
// CmdDoctor represents the available doctor sub-command.
27+
var CmdDoctor = &cli.Command{
28+
Name: "doctor",
29+
Usage: "Diagnose and optionally fix problems",
30+
Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.",
31+
32+
Subcommands: []*cli.Command{
33+
cmdDoctorCheck,
34+
cmdRecreateTable,
35+
cmdDoctorConvert,
36+
},
37+
}
38+
2539
var cmdDoctorCheck = &cli.Command{
2640
Name: "check",
2741
Usage: "Diagnose and optionally fix problems",
@@ -60,19 +74,6 @@ var cmdDoctorCheck = &cli.Command{
6074
},
6175
}
6276

63-
// CmdDoctor represents the available doctor sub-command.
64-
var CmdDoctor = &cli.Command{
65-
Name: "doctor",
66-
Usage: "Diagnose and optionally fix problems",
67-
Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.",
68-
69-
Subcommands: []*cli.Command{
70-
cmdDoctorCheck,
71-
cmdRecreateTable,
72-
cmdDoctorConvert,
73-
},
74-
}
75-
7677
var cmdRecreateTable = &cli.Command{
7778
Name: "recreate-table",
7879
Usage: "Recreate tables from XORM definitions and copy the data.",
@@ -177,6 +178,7 @@ func runDoctorCheck(ctx *cli.Context) error {
177178
if ctx.IsSet("list") {
178179
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
179180
_, _ = w.Write([]byte("Default\tName\tTitle\n"))
181+
doctor.SortChecks(doctor.Checks)
180182
for _, check := range doctor.Checks {
181183
if check.IsDefault {
182184
_, _ = w.Write([]byte{'*'})
@@ -192,33 +194,26 @@ func runDoctorCheck(ctx *cli.Context) error {
192194

193195
var checks []*doctor.Check
194196
if ctx.Bool("all") {
195-
checks = doctor.Checks
197+
checks = make([]*doctor.Check, len(doctor.Checks))
198+
copy(checks, doctor.Checks)
196199
} else if ctx.IsSet("run") {
197200
addDefault := ctx.Bool("default")
198-
names := ctx.StringSlice("run")
199-
for i, name := range names {
200-
names[i] = strings.ToLower(strings.TrimSpace(name))
201-
}
202-
201+
runNamesSet := container.SetOf(ctx.StringSlice("run")...)
203202
for _, check := range doctor.Checks {
204-
if addDefault && check.IsDefault {
203+
if (addDefault && check.IsDefault) || runNamesSet.Contains(check.Name) {
205204
checks = append(checks, check)
206-
continue
207-
}
208-
for _, name := range names {
209-
if name == check.Name {
210-
checks = append(checks, check)
211-
break
212-
}
205+
runNamesSet.Remove(check.Name)
213206
}
214207
}
208+
if len(runNamesSet) > 0 {
209+
return fmt.Errorf("unknown checks: %q", strings.Join(runNamesSet.Values(), ","))
210+
}
215211
} else {
216212
for _, check := range doctor.Checks {
217213
if check.IsDefault {
218214
checks = append(checks, check)
219215
}
220216
}
221217
}
222-
223218
return doctor.RunChecks(stdCtx, colorize, ctx.Bool("fix"), checks)
224219
}

cmd/doctor_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cmd
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/doctor"
11+
"code.gitea.io/gitea/modules/log"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/urfave/cli/v2"
15+
)
16+
17+
func TestDoctorRun(t *testing.T) {
18+
doctor.Register(&doctor.Check{
19+
Title: "Test Check",
20+
Name: "test-check",
21+
Run: func(ctx context.Context, logger log.Logger, autofix bool) error { return nil },
22+
23+
SkipDatabaseInitialization: true,
24+
})
25+
app := cli.NewApp()
26+
app.Commands = []*cli.Command{cmdDoctorCheck}
27+
err := app.Run([]string{"./gitea", "check", "--run", "test-check"})
28+
assert.NoError(t, err)
29+
err = app.Run([]string{"./gitea", "check", "--run", "no-such"})
30+
assert.ErrorContains(t, err, `unknown checks: "no-such"`)
31+
err = app.Run([]string{"./gitea", "check", "--run", "test-check,no-such"})
32+
assert.ErrorContains(t, err, `unknown checks: "no-such"`)
33+
}

models/git/branch_list.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/util"
1313

1414
"xorm.io/builder"
15-
"xorm.io/xorm"
1615
)
1716

1817
type BranchList []*Branch
@@ -91,41 +90,30 @@ func (opts FindBranchOptions) ToConds() builder.Cond {
9190
return cond
9291
}
9392

94-
func CountBranches(ctx context.Context, opts FindBranchOptions) (int64, error) {
95-
return db.GetEngine(ctx).Where(opts.ToConds()).Count(&Branch{})
96-
}
97-
98-
func orderByBranches(sess *xorm.Session, opts FindBranchOptions) *xorm.Session {
93+
func (opts FindBranchOptions) ToOrders() string {
94+
orderBy := opts.OrderBy
9995
if !opts.IsDeletedBranch.IsFalse() { // if deleted branch included, put them at the end
100-
sess = sess.OrderBy("is_deleted ASC")
96+
if orderBy != "" {
97+
orderBy += ", "
98+
}
99+
orderBy += "is_deleted ASC"
101100
}
102-
103-
if opts.OrderBy == "" {
101+
if orderBy == "" {
104102
// the commit_time might be the same, so add the "name" to make sure the order is stable
105-
opts.OrderBy = "commit_time DESC, name ASC"
103+
return "commit_time DESC, name ASC"
106104
}
107-
return sess.OrderBy(opts.OrderBy)
108-
}
109105

110-
func FindBranches(ctx context.Context, opts FindBranchOptions) (BranchList, error) {
111-
sess := db.GetEngine(ctx).Where(opts.ToConds())
112-
if opts.PageSize > 0 && !opts.IsListAll() {
113-
sess = db.SetSessionPagination(sess, &opts.ListOptions)
114-
}
115-
sess = orderByBranches(sess, opts)
116-
117-
var branches []*Branch
118-
return branches, sess.Find(&branches)
106+
return orderBy
119107
}
120108

121109
func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, error) {
122110
sess := db.GetEngine(ctx).Select("name").Where(opts.ToConds())
123111
if opts.PageSize > 0 && !opts.IsListAll() {
124112
sess = db.SetSessionPagination(sess, &opts.ListOptions)
125113
}
126-
sess = orderByBranches(sess, opts)
114+
127115
var branches []string
128-
if err := sess.Table("branch").Find(&branches); err != nil {
116+
if err := sess.Table("branch").OrderBy(opts.ToOrders()).Find(&branches); err != nil {
129117
return nil, err
130118
}
131119
return branches, nil

models/git/branch_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ func TestGetDeletedBranches(t *testing.T) {
4545
assert.NoError(t, unittest.PrepareTestDatabase())
4646
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
4747

48-
branches, err := git_model.FindBranches(db.DefaultContext, git_model.FindBranchOptions{
49-
ListOptions: db.ListOptions{
50-
ListAll: true,
51-
},
48+
branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
49+
ListOptions: db.ListOptionsAll,
5250
RepoID: repo.ID,
5351
IsDeletedBranch: util.OptionalBoolTrue,
5452
})

models/issues/milestone.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,15 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error {
295295
return err
296296
}
297297

298-
numMilestones, err := CountMilestones(ctx, GetMilestonesOption{
298+
numMilestones, err := db.Count[Milestone](ctx, FindMilestoneOptions{
299299
RepoID: repo.ID,
300-
State: api.StateAll,
301300
})
302301
if err != nil {
303302
return err
304303
}
305-
numClosedMilestones, err := CountMilestones(ctx, GetMilestonesOption{
306-
RepoID: repo.ID,
307-
State: api.StateClosed,
304+
numClosedMilestones, err := db.Count[Milestone](ctx, FindMilestoneOptions{
305+
RepoID: repo.ID,
306+
IsClosed: util.OptionalBoolTrue,
308307
})
309308
if err != nil {
310309
return err

0 commit comments

Comments
 (0)