Skip to content

Commit 13beca8

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Improve text in Security settings (go-gitea#28393) Fix Docker meta action for releases (go-gitea#28232) Make gogit Repository.GetBranchNames consistent (go-gitea#28348) Remove GetByBean method because sometimes it's danger when query condition parameter is zero and also introduce new generic methods (go-gitea#28220) Include public repos in doer's dashboard for issue search (go-gitea#28304) Issue fixes for RSS feed improvements (go-gitea#28380) Fix margin in server signed signature verification view (go-gitea#28379) [skip ci] Updated translations via Crowdin Fix incorrect run order of action jobs (go-gitea#28367) Improve RSS feed icons (go-gitea#28368) Use `filepath` instead of `path` to create SQLite3 database file (go-gitea#28374) Fix incorrect default value of `[attachment].MAX_SIZE` (go-gitea#28373) Fix object does not exist error when checking citation file (go-gitea#28314)
2 parents 0d2c650 + 18ba1c6 commit 13beca8

Some content is hidden

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

51 files changed

+310
-447
lines changed

.github/workflows/release-tag-rc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ jobs:
7878
id: meta
7979
with:
8080
images: gitea/gitea
81+
flavor: |
82+
latest=false
8183
# 1.2.3-rc0
8284
tags: |
8385
type=semver,pattern={{version}}
@@ -109,6 +111,7 @@ jobs:
109111
images: gitea/gitea
110112
# each tag below will have the suffix of -rootless
111113
flavor: |
114+
latest=false
112115
suffix=-rootless
113116
# 1.2.3-rc0
114117
tags: |

.github/workflows/release-tag-version.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ jobs:
8686
# 1.2
8787
# 1.2.3
8888
tags: |
89-
type=raw,value=latest
9089
type=semver,pattern={{major}}
9190
type=semver,pattern={{major}}.{{minor}}
9291
type=semver,pattern={{version}}
@@ -118,14 +117,13 @@ jobs:
118117
images: gitea/gitea
119118
# each tag below will have the suffix of -rootless
120119
flavor: |
121-
suffix=-rootless
120+
suffix=-rootless,onlatest=true
122121
# this will generate tags in the following format (with -rootless suffix added):
123122
# latest
124123
# 1
125124
# 1.2
126125
# 1.2.3
127126
tags: |
128-
type=raw,value=latest
129127
type=semver,pattern={{major}}
130128
type=semver,pattern={{major}}.{{minor}}
131129
type=semver,pattern={{version}}

models/actions/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
234234
}
235235

236236
var jobs []*ActionRunJob
237-
if err := e.Where("task_id=? AND status=?", 0, StatusWaiting).And(jobCond).Asc("id").Find(&jobs); err != nil {
237+
if err := e.Where("task_id=? AND status=?", 0, StatusWaiting).And(jobCond).Asc("updated", "id").Find(&jobs); err != nil {
238238
return nil, false, err
239239
}
240240

models/asymkey/ssh_key_deploy.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,22 @@ func AddDeployKey(ctx context.Context, repoID int64, name, content string, readO
131131
}
132132
defer committer.Close()
133133

134-
pkey := &PublicKey{
135-
Fingerprint: fingerprint,
136-
}
137-
has, err := db.GetByBean(ctx, pkey)
134+
pkey, exist, err := db.Get[PublicKey](ctx, builder.Eq{"fingerprint": fingerprint})
138135
if err != nil {
139136
return nil, err
140-
}
141-
142-
if has {
137+
} else if exist {
143138
if pkey.Type != KeyTypeDeploy {
144139
return nil, ErrKeyAlreadyExist{0, fingerprint, ""}
145140
}
146141
} else {
147142
// First time use this deploy key.
148-
pkey.Mode = accessMode
149-
pkey.Type = KeyTypeDeploy
150-
pkey.Content = content
151-
pkey.Name = name
143+
pkey = &PublicKey{
144+
Fingerprint: fingerprint,
145+
Mode: accessMode,
146+
Type: KeyTypeDeploy,
147+
Content: content,
148+
Name: name,
149+
}
152150
if err = addKey(ctx, pkey); err != nil {
153151
return nil, fmt.Errorf("addKey: %w", err)
154152
}
@@ -164,26 +162,21 @@ func AddDeployKey(ctx context.Context, repoID int64, name, content string, readO
164162

165163
// GetDeployKeyByID returns deploy key by given ID.
166164
func GetDeployKeyByID(ctx context.Context, id int64) (*DeployKey, error) {
167-
key := new(DeployKey)
168-
has, err := db.GetEngine(ctx).ID(id).Get(key)
165+
key, exist, err := db.GetByID[DeployKey](ctx, id)
169166
if err != nil {
170167
return nil, err
171-
} else if !has {
168+
} else if !exist {
172169
return nil, ErrDeployKeyNotExist{id, 0, 0}
173170
}
174171
return key, nil
175172
}
176173

177174
// GetDeployKeyByRepo returns deploy key by given public key ID and repository ID.
178175
func GetDeployKeyByRepo(ctx context.Context, keyID, repoID int64) (*DeployKey, error) {
179-
key := &DeployKey{
180-
KeyID: keyID,
181-
RepoID: repoID,
182-
}
183-
has, err := db.GetByBean(ctx, key)
176+
key, exist, err := db.Get[DeployKey](ctx, builder.Eq{"key_id": keyID, "repo_id": repoID})
184177
if err != nil {
185178
return nil, err
186-
} else if !has {
179+
} else if !exist {
187180
return nil, ErrDeployKeyNotExist{0, keyID, repoID}
188181
}
189182
return key, nil

models/asymkey/ssh_key_fingerprint.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/util"
1616

1717
"golang.org/x/crypto/ssh"
18+
"xorm.io/builder"
1819
)
1920

2021
// ___________.__ .__ __
@@ -31,9 +32,7 @@ import (
3132
// checkKeyFingerprint only checks if key fingerprint has been used as public key,
3233
// it is OK to use same key as deploy key for multiple repositories/users.
3334
func checkKeyFingerprint(ctx context.Context, fingerprint string) error {
34-
has, err := db.GetByBean(ctx, &PublicKey{
35-
Fingerprint: fingerprint,
36-
})
35+
has, err := db.Exist[PublicKey](ctx, builder.Eq{"fingerprint": fingerprint})
3736
if err != nil {
3837
return err
3938
} else if has {

models/auth/session.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
"code.gitea.io/gitea/models/db"
1111
"code.gitea.io/gitea/modules/timeutil"
12+
13+
"xorm.io/builder"
1214
)
1315

1416
// Session represents a session compatible for go-chi session
@@ -33,34 +35,28 @@ func UpdateSession(ctx context.Context, key string, data []byte) error {
3335

3436
// ReadSession reads the data for the provided session
3537
func ReadSession(ctx context.Context, key string) (*Session, error) {
36-
session := Session{
37-
Key: key,
38-
}
39-
4038
ctx, committer, err := db.TxContext(ctx)
4139
if err != nil {
4240
return nil, err
4341
}
4442
defer committer.Close()
4543

46-
if has, err := db.GetByBean(ctx, &session); err != nil {
44+
session, exist, err := db.Get[Session](ctx, builder.Eq{"key": key})
45+
if err != nil {
4746
return nil, err
48-
} else if !has {
47+
} else if !exist {
4948
session.Expiry = timeutil.TimeStampNow()
5049
if err := db.Insert(ctx, &session); err != nil {
5150
return nil, err
5251
}
5352
}
5453

55-
return &session, committer.Commit()
54+
return session, committer.Commit()
5655
}
5756

5857
// ExistSession checks if a session exists
5958
func ExistSession(ctx context.Context, key string) (bool, error) {
60-
session := Session{
61-
Key: key,
62-
}
63-
return db.GetEngine(ctx).Get(&session)
59+
return db.Exist[Session](ctx, builder.Eq{"key": key})
6460
}
6561

6662
// DestroySession destroys a session
@@ -79,17 +75,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er
7975
}
8076
defer committer.Close()
8177

82-
if has, err := db.GetByBean(ctx, &Session{
83-
Key: newKey,
84-
}); err != nil {
78+
if has, err := db.Exist[Session](ctx, builder.Eq{"key": newKey}); err != nil {
8579
return nil, err
8680
} else if has {
8781
return nil, fmt.Errorf("session Key: %s already exists", newKey)
8882
}
8983

90-
if has, err := db.GetByBean(ctx, &Session{
91-
Key: oldKey,
92-
}); err != nil {
84+
if has, err := db.Exist[Session](ctx, builder.Eq{"key": oldKey}); err != nil {
9385
return nil, err
9486
} else if !has {
9587
if err := db.Insert(ctx, &Session{
@@ -104,14 +96,13 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er
10496
return nil, err
10597
}
10698

107-
s := Session{
108-
Key: newKey,
109-
}
110-
if _, err := db.GetByBean(ctx, &s); err != nil {
99+
s, _, err := db.Get[Session](ctx, builder.Eq{"key": newKey})
100+
if err != nil {
101+
// is not exist, it should be impossible
111102
return nil, err
112103
}
113104

114-
return &s, committer.Commit()
105+
return s, committer.Commit()
115106
}
116107

117108
// CountSessions returns the number of sessions

models/auth/source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ func IsSSPIEnabled(ctx context.Context) bool {
265265
return false
266266
}
267267

268-
exist, err := db.Exists[Source](ctx, FindSourcesOptions{
268+
exist, err := db.Exist[Source](ctx, FindSourcesOptions{
269269
IsActive: util.OptionalBoolTrue,
270270
LoginType: SSPI,
271-
})
271+
}.ToConds())
272272
if err != nil {
273273
log.Error("Active SSPI Sources: %v", err)
274274
return false

models/db/context.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,44 @@ func Exec(ctx context.Context, sqlAndArgs ...any) (sql.Result, error) {
173173
return GetEngine(ctx).Exec(sqlAndArgs...)
174174
}
175175

176-
// GetByBean filled empty fields of the bean according non-empty fields to query in database.
177-
func GetByBean(ctx context.Context, bean any) (bool, error) {
178-
return GetEngine(ctx).Get(bean)
176+
func Get[T any](ctx context.Context, cond builder.Cond) (object *T, exist bool, err error) {
177+
if !cond.IsValid() {
178+
return nil, false, ErrConditionRequired{}
179+
}
180+
181+
var bean T
182+
has, err := GetEngine(ctx).Where(cond).NoAutoCondition().Get(&bean)
183+
if err != nil {
184+
return nil, false, err
185+
} else if !has {
186+
return nil, false, nil
187+
}
188+
return &bean, true, nil
189+
}
190+
191+
func GetByID[T any](ctx context.Context, id int64) (object *T, exist bool, err error) {
192+
var bean T
193+
has, err := GetEngine(ctx).ID(id).NoAutoCondition().Get(&bean)
194+
if err != nil {
195+
return nil, false, err
196+
} else if !has {
197+
return nil, false, nil
198+
}
199+
return &bean, true, nil
200+
}
201+
202+
func Exist[T any](ctx context.Context, cond builder.Cond) (bool, error) {
203+
if !cond.IsValid() {
204+
return false, ErrConditionRequired{}
205+
}
206+
207+
var bean T
208+
return GetEngine(ctx).Where(cond).NoAutoCondition().Exist(&bean)
209+
}
210+
211+
func ExistByID[T any](ctx context.Context, id int64) (bool, error) {
212+
var bean T
213+
return GetEngine(ctx).ID(id).NoAutoCondition().Exist(&bean)
179214
}
180215

181216
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
@@ -264,8 +299,3 @@ func inTransaction(ctx context.Context) (*xorm.Session, bool) {
264299
return nil, false
265300
}
266301
}
267-
268-
func Exists[T any](ctx context.Context, opts FindOptions) (bool, error) {
269-
var bean T
270-
return GetEngine(ctx).Where(opts.ToConds()).Exist(&bean)
271-
}

models/db/error.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ func (err ErrNotExist) Error() string {
7272
func (err ErrNotExist) Unwrap() error {
7373
return util.ErrNotExist
7474
}
75+
76+
// ErrConditionRequired represents an error which require condition.
77+
type ErrConditionRequired struct{}
78+
79+
// IsErrConditionRequired checks if an error is an ErrConditionRequired
80+
func IsErrConditionRequired(err error) bool {
81+
_, ok := err.(ErrConditionRequired)
82+
return ok
83+
}
84+
85+
func (err ErrConditionRequired) Error() string {
86+
return "condition is required"
87+
}
88+
89+
// Unwrap unwraps this as a ErrNotExist err
90+
func (err ErrConditionRequired) Unwrap() error {
91+
return util.ErrInvalidArgument
92+
}

models/db/iterate_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func TestIterate(t *testing.T) {
3131
assert.EqualValues(t, cnt, repoUnitCnt)
3232

3333
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
34-
reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID}
35-
has, err := db.GetByBean(ctx, &reopUnit2)
34+
has, err := db.ExistByID[repo_model.RepoUnit](ctx, repoUnit.ID)
3635
if err != nil {
3736
return err
38-
} else if !has {
37+
}
38+
if !has {
3939
return db.ErrNotExist{Resource: "repo_unit", ID: repoUnit.ID}
4040
}
4141
assert.EqualValues(t, repoUnit.RepoID, repoUnit.RepoID)

0 commit comments

Comments
 (0)