Skip to content

Commit 8f9c825

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: [skip ci] Updated translations via Crowdin Refactor deletion (go-gitea#28610) Refactor CORS handler (go-gitea#28587)
2 parents a548fe7 + ff7383e commit 8f9c825

Some content is hidden

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

67 files changed

+228
-292
lines changed

custom/conf/app.example.ini

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,9 @@ LEVEL = Info
11581158
;; enable cors headers (disabled by default)
11591159
;ENABLED = false
11601160
;;
1161-
;; scheme of allowed requests
1162-
;SCHEME = http
1163-
;;
1164-
;; list of requesting domains that are allowed
1161+
;; list of requesting origins that are allowed, eg: "https://*.example.com"
11651162
;ALLOW_DOMAIN = *
11661163
;;
1167-
;; allow subdomains of headers listed above to request
1168-
;ALLOW_SUBDOMAIN = false
1169-
;;
11701164
;; list of methods allowed to request
11711165
;METHODS = GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS
11721166
;;

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
196196
## CORS (`cors`)
197197

198198
- `ENABLED`: **false**: enable cors headers (disabled by default)
199-
- `SCHEME`: **http**: scheme of allowed requests
200-
- `ALLOW_DOMAIN`: **\***: list of requesting domains that are allowed
201-
- `ALLOW_SUBDOMAIN`: **false**: allow subdomains of headers listed above to request
199+
- `ALLOW_DOMAIN`: **\***: list of requesting origins that are allowed, eg: "https://*.example.com"
202200
- `METHODS`: **GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS**: list of methods allowed to request
203201
- `MAX_AGE`: **10m**: max time to cache response
204202
- `ALLOW_CREDENTIALS`: **false**: allow request with credentials

docs/content/administration/config-cheat-sheet.zh-cn.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ menu:
195195
## 跨域 (`cors`)
196196

197197
- `ENABLED`: **false**: 启用 CORS 头部(默认禁用)
198-
- `SCHEME`: **http**: 允许请求的协议
199198
- `ALLOW_DOMAIN`: **\***: 允许请求的域名列表
200-
- `ALLOW_SUBDOMAIN`: **false**: 允许上述列出的头部的子域名发出请求。
201199
- `METHODS`: **GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS**: 允许发起的请求方式列表
202200
- `MAX_AGE`: **10m**: 缓存响应的最大时间
203201
- `ALLOW_CREDENTIALS`: **false**: 允许带有凭据的请求

models/actions/artifact.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ func getArtifactByNameAndPath(ctx context.Context, runID int64, name, fpath stri
9090
return &art, nil
9191
}
9292

93-
// GetArtifactByID returns an artifact by id
94-
func GetArtifactByID(ctx context.Context, id int64) (*ActionArtifact, error) {
95-
var art ActionArtifact
96-
has, err := db.GetEngine(ctx).ID(id).Get(&art)
97-
if err != nil {
98-
return nil, err
99-
} else if !has {
100-
return nil, util.ErrNotExist
101-
}
102-
103-
return &art, nil
104-
}
105-
10693
// UpdateArtifactByID updates an artifact by id
10794
func UpdateArtifactByID(ctx context.Context, id int64, art *ActionArtifact) error {
10895
art.ID = id

models/actions/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func DeleteRunner(ctx context.Context, id int64) error {
254254
return err
255255
}
256256

257-
_, err := db.GetEngine(ctx).Delete(&ActionRunner{ID: id})
257+
_, err := db.DeleteByID[ActionRunner](ctx, id)
258258
return err
259259
}
260260

models/asymkey/ssh_key.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,6 @@ func UpdatePublicKeyUpdated(ctx context.Context, id int64) error {
227227
return nil
228228
}
229229

230-
// DeletePublicKeys does the actual key deletion but does not update authorized_keys file.
231-
func DeletePublicKeys(ctx context.Context, keyIDs ...int64) error {
232-
if len(keyIDs) == 0 {
233-
return nil
234-
}
235-
236-
_, err := db.GetEngine(ctx).In("id", keyIDs).Delete(new(PublicKey))
237-
return err
238-
}
239-
240230
// PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key
241231
func PublicKeysAreExternallyManaged(ctx context.Context, keys []*PublicKey) ([]bool, error) {
242232
sources := make([]*auth.Source, 0, 5)
@@ -322,8 +312,8 @@ func deleteKeysMarkedForDeletion(ctx context.Context, keys []string) (bool, erro
322312
log.Error("SearchPublicKeyByContent: %v", err)
323313
continue
324314
}
325-
if err = DeletePublicKeys(ctx, key.ID); err != nil {
326-
log.Error("deletePublicKeys: %v", err)
315+
if _, err = db.DeleteByID[PublicKey](ctx, key.ID); err != nil {
316+
log.Error("DeleteByID[PublicKey]: %v", err)
327317
continue
328318
}
329319
sshKeysNeedUpdate = true

models/db/context.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func Exec(ctx context.Context, sqlAndArgs ...any) (sql.Result, error) {
175175

176176
func Get[T any](ctx context.Context, cond builder.Cond) (object *T, exist bool, err error) {
177177
if !cond.IsValid() {
178-
return nil, false, ErrConditionRequired{}
178+
panic("cond is invalid in db.Get(ctx, cond). This should not be possible.")
179179
}
180180

181181
var bean T
@@ -201,7 +201,7 @@ func GetByID[T any](ctx context.Context, id int64) (object *T, exist bool, err e
201201

202202
func Exist[T any](ctx context.Context, cond builder.Cond) (bool, error) {
203203
if !cond.IsValid() {
204-
return false, ErrConditionRequired{}
204+
panic("cond is invalid in db.Exist(ctx, cond). This should not be possible.")
205205
}
206206

207207
var bean T
@@ -213,16 +213,36 @@ func ExistByID[T any](ctx context.Context, id int64) (bool, error) {
213213
return GetEngine(ctx).ID(id).NoAutoCondition().Exist(&bean)
214214
}
215215

216+
// DeleteByID deletes the given bean with the given ID
217+
func DeleteByID[T any](ctx context.Context, id int64) (int64, error) {
218+
var bean T
219+
return GetEngine(ctx).ID(id).NoAutoCondition().NoAutoTime().Delete(&bean)
220+
}
221+
222+
func DeleteByIDs[T any](ctx context.Context, ids ...int64) error {
223+
if len(ids) == 0 {
224+
return nil
225+
}
226+
227+
var bean T
228+
_, err := GetEngine(ctx).In("id", ids).NoAutoCondition().NoAutoTime().Delete(&bean)
229+
return err
230+
}
231+
232+
func Delete[T any](ctx context.Context, opts FindOptions) (int64, error) {
233+
if opts == nil || !opts.ToConds().IsValid() {
234+
panic("opts are empty or invalid in db.Delete(ctx, opts). This should not be possible.")
235+
}
236+
237+
var bean T
238+
return GetEngine(ctx).Where(opts.ToConds()).NoAutoCondition().NoAutoTime().Delete(&bean)
239+
}
240+
216241
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
217242
func DeleteByBean(ctx context.Context, bean any) (int64, error) {
218243
return GetEngine(ctx).Delete(bean)
219244
}
220245

221-
// DeleteByID deletes the given bean with the given ID
222-
func DeleteByID(ctx context.Context, id int64, bean any) (int64, error) {
223-
return GetEngine(ctx).ID(id).NoAutoCondition().NoAutoTime().Delete(bean)
224-
}
225-
226246
// FindIDs finds the IDs for the given table name satisfying the given condition
227247
// By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition
228248
func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([]int64, error) {

models/db/error.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,3 @@ 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/issues/issue_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ func TestIssue_InsertIssue(t *testing.T) {
249249

250250
// there are 5 issues and max index is 5 on repository 1, so this one should 6
251251
issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6)
252-
_, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue))
252+
_, err := db.DeleteByID[issues_model.Issue](db.DefaultContext, issue.ID)
253253
assert.NoError(t, err)
254254

255255
issue = testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?", 7)
256-
_, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue))
256+
_, err = db.DeleteByID[issues_model.Issue](db.DefaultContext, issue.ID)
257257
assert.NoError(t, err)
258258
}
259259

models/issues/label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func DeleteLabel(ctx context.Context, id, labelID int64) error {
256256
return nil
257257
}
258258

259-
if _, err = sess.ID(labelID).Delete(new(Label)); err != nil {
259+
if _, err = db.DeleteByID[Label](ctx, labelID); err != nil {
260260
return err
261261
} else if _, err = sess.
262262
Where("label_id = ?", labelID).

models/issues/milestone.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error {
289289
}
290290
defer committer.Close()
291291

292-
sess := db.GetEngine(ctx)
293-
294-
if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
292+
if _, err = db.DeleteByID[Milestone](ctx, m.ID); err != nil {
295293
return err
296294
}
297295

@@ -311,7 +309,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error {
311309
repo.NumMilestones = int(numMilestones)
312310
repo.NumClosedMilestones = int(numClosedMilestones)
313311

314-
if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
312+
if _, err = db.GetEngine(ctx).ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
315313
return err
316314
}
317315

models/issues/review.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, issue *Issue, revi
446446
continue
447447
}
448448

449-
if _, err := sess.ID(teamReviewRequest.ID).NoAutoCondition().Delete(teamReviewRequest); err != nil {
449+
if _, err := db.DeleteByID[Review](ctx, teamReviewRequest.ID); err != nil {
450450
return nil, nil, err
451451
}
452452
}
@@ -869,7 +869,6 @@ func DeleteReview(ctx context.Context, r *Review) error {
869869
return err
870870
}
871871
defer committer.Close()
872-
sess := db.GetEngine(ctx)
873872

874873
if r.ID == 0 {
875874
return fmt.Errorf("review is not allowed to be 0")
@@ -885,7 +884,7 @@ func DeleteReview(ctx context.Context, r *Review) error {
885884
ReviewID: r.ID,
886885
}
887886

888-
if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil {
887+
if _, err := db.Delete[Comment](ctx, opts); err != nil {
889888
return err
890889
}
891890

@@ -895,7 +894,7 @@ func DeleteReview(ctx context.Context, r *Review) error {
895894
ReviewID: r.ID,
896895
}
897896

898-
if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil {
897+
if _, err := db.Delete[Comment](ctx, opts); err != nil {
899898
return err
900899
}
901900

@@ -905,11 +904,11 @@ func DeleteReview(ctx context.Context, r *Review) error {
905904
ReviewID: r.ID,
906905
}
907906

908-
if _, err := sess.Where(opts.ToConds()).Delete(new(Comment)); err != nil {
907+
if _, err := db.Delete[Comment](ctx, opts); err != nil {
909908
return err
910909
}
911910

912-
if _, err := sess.ID(r.ID).Delete(new(Review)); err != nil {
911+
if _, err := db.DeleteByID[Review](ctx, r.ID); err != nil {
913912
return err
914913
}
915914

models/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func RemoveOrgUser(ctx context.Context, orgID, userID int64) error {
5757
}
5858
defer committer.Close()
5959

60-
if _, err := db.GetEngine(ctx).ID(ou.ID).Delete(ou); err != nil {
60+
if _, err := db.DeleteByID[organization.OrgUser](ctx, ou.ID); err != nil {
6161
return err
6262
} else if _, err = db.Exec(ctx, "UPDATE `user` SET num_members=num_members-1 WHERE id=?", orgID); err != nil {
6363
return err

models/repo.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ func DeleteDeployKey(ctx context.Context, doer *user_model.User, id int64) error
344344
}
345345
}
346346

347-
if _, err := db.DeleteByBean(ctx, &asymkey_model.DeployKey{
348-
ID: key.ID,
349-
}); err != nil {
347+
if _, err := db.DeleteByID[asymkey_model.DeployKey](ctx, key.ID); err != nil {
350348
return fmt.Errorf("delete deploy key [%d]: %w", key.ID, err)
351349
}
352350

@@ -355,7 +353,7 @@ func DeleteDeployKey(ctx context.Context, doer *user_model.User, id int64) error
355353
if err != nil {
356354
return err
357355
} else if !has {
358-
if err = asymkey_model.DeletePublicKeys(ctx, key.KeyID); err != nil {
356+
if _, err = db.DeleteByID[asymkey_model.PublicKey](ctx, key.KeyID); err != nil {
359357
return err
360358
}
361359
}

models/repo/archiver.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
6868
}, nil
6969
}
7070

71-
var delRepoArchiver = new(RepoArchiver)
72-
73-
// DeleteRepoArchiver delete archiver
74-
func DeleteRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
75-
_, err := db.GetEngine(ctx).ID(archiver.ID).Delete(delRepoArchiver)
76-
return err
77-
}
78-
7971
// GetRepoArchiver get an archiver
8072
func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
8173
var archiver RepoArchiver
@@ -100,12 +92,6 @@ func ExistsRepoArchiverWithStoragePath(ctx context.Context, storagePath string)
10092
return db.GetEngine(ctx).Exist(archiver)
10193
}
10294

103-
// AddRepoArchiver adds an archiver
104-
func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
105-
_, err := db.GetEngine(ctx).Insert(archiver)
106-
return err
107-
}
108-
10995
// UpdateRepoArchiverStatus updates archiver's status
11096
func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error {
11197
_, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver)
@@ -114,6 +100,7 @@ func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error
114100

115101
// DeleteAllRepoArchives deletes all repo archives records
116102
func DeleteAllRepoArchives(ctx context.Context) error {
103+
// 1=1 to enforce delete all data, otherwise it will delete nothing
117104
_, err := db.GetEngine(ctx).Where("1=1").Delete(new(RepoArchiver))
118105
return err
119106
}

models/repo/pushmirror.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ type PushMirror struct {
3434
}
3535

3636
type PushMirrorOptions struct {
37+
db.ListOptions
3738
ID int64
3839
RepoID int64
3940
RemoteName string
4041
}
4142

42-
func (opts *PushMirrorOptions) toConds() builder.Cond {
43+
func (opts PushMirrorOptions) ToConds() builder.Cond {
4344
cond := builder.NewCond()
4445
if opts.RepoID > 0 {
4546
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
@@ -75,12 +76,6 @@ func (m *PushMirror) GetRemoteName() string {
7576
return m.RemoteName
7677
}
7778

78-
// InsertPushMirror inserts a push-mirror to database
79-
func InsertPushMirror(ctx context.Context, m *PushMirror) error {
80-
_, err := db.GetEngine(ctx).Insert(m)
81-
return err
82-
}
83-
8479
// UpdatePushMirror updates the push-mirror
8580
func UpdatePushMirror(ctx context.Context, m *PushMirror) error {
8681
_, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
@@ -95,23 +90,12 @@ func UpdatePushMirrorInterval(ctx context.Context, m *PushMirror) error {
9590

9691
func DeletePushMirrors(ctx context.Context, opts PushMirrorOptions) error {
9792
if opts.RepoID > 0 {
98-
_, err := db.GetEngine(ctx).Where(opts.toConds()).Delete(&PushMirror{})
93+
_, err := db.Delete[PushMirror](ctx, opts)
9994
return err
10095
}
10196
return util.NewInvalidArgumentErrorf("repoID required and must be set")
10297
}
10398

104-
func GetPushMirror(ctx context.Context, opts PushMirrorOptions) (*PushMirror, error) {
105-
mirror := &PushMirror{}
106-
exist, err := db.GetEngine(ctx).Where(opts.toConds()).Get(mirror)
107-
if err != nil {
108-
return nil, err
109-
} else if !exist {
110-
return nil, ErrPushMirrorNotExist
111-
}
112-
return mirror, nil
113-
}
114-
11599
// GetPushMirrorsByRepoID returns push-mirror information of a repository.
116100
func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*PushMirror, int64, error) {
117101
sess := db.GetEngine(ctx).Where("repo_id = ?", repoID)

0 commit comments

Comments
 (0)