···11-// Copyright 2014 The Gogs Authors. All rights reserved.
22-// Copyright 2020 The Gitea Authors. All rights reserved.
11+// Copyright 2021 The Gitea Authors. All rights reserved.
32// Use of this source code is governed by a MIT-style
43// license that can be found in the LICENSE file.
5466-package models
55+package admin
7687import (
88+ "context"
99 "fmt"
10101111 "code.gitea.io/gitea/models/db"
···44444545// CreateNotice creates new system notice.
4646func CreateNotice(tp NoticeType, desc string, args ...interface{}) error {
4747- return createNotice(db.GetEngine(db.DefaultContext), tp, desc, args...)
4747+ return CreateNoticeCtx(db.DefaultContext, tp, desc, args...)
4848}
49495050-func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{}) error {
5050+// CreateNoticeCtx creates new system notice.
5151+func CreateNoticeCtx(ctx context.Context, tp NoticeType, desc string, args ...interface{}) error {
5152 if len(args) > 0 {
5253 desc = fmt.Sprintf(desc, args...)
5354 }
···5556 Type: tp,
5657 Description: desc,
5758 }
5858- _, err := e.Insert(n)
5959- return err
5959+ return db.Insert(ctx, n)
6060}
61616262// CreateRepositoryNotice creates new system notice with type NoticeRepository.
6363func CreateRepositoryNotice(desc string, args ...interface{}) error {
6464- return createNotice(db.GetEngine(db.DefaultContext), NoticeRepository, desc, args...)
6464+ return CreateNoticeCtx(db.DefaultContext, NoticeRepository, desc, args...)
6565}
66666767// RemoveAllWithNotice removes all directories in given path and
6868// creates a system notice when error occurs.
6969func RemoveAllWithNotice(title, path string) {
7070- removeAllWithNotice(db.GetEngine(db.DefaultContext), title, path)
7070+ RemoveAllWithNoticeCtx(db.DefaultContext, title, path)
7171}
72727373// RemoveStorageWithNotice removes a file from the storage and
7474// creates a system notice when error occurs.
7575func RemoveStorageWithNotice(bucket storage.ObjectStorage, title, path string) {
7676- removeStorageWithNotice(db.GetEngine(db.DefaultContext), bucket, title, path)
7676+ removeStorageWithNotice(db.DefaultContext, bucket, title, path)
7777}
78787979-func removeStorageWithNotice(e db.Engine, bucket storage.ObjectStorage, title, path string) {
7979+func removeStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, title, path string) {
8080 if err := bucket.Delete(path); err != nil {
8181 desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
8282 log.Warn(title+" [%s]: %v", path, err)
8383- if err = createNotice(e, NoticeRepository, desc); err != nil {
8383+ if err = CreateNoticeCtx(ctx, NoticeRepository, desc); err != nil {
8484 log.Error("CreateRepositoryNotice: %v", err)
8585 }
8686 }
8787}
88888989-func removeAllWithNotice(e db.Engine, title, path string) {
8989+// RemoveAllWithNoticeCtx removes all directories in given path and
9090+// creates a system notice when error occurs.
9191+func RemoveAllWithNoticeCtx(ctx context.Context, title, path string) {
9092 if err := util.RemoveAll(path); err != nil {
9193 desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
9294 log.Warn(title+" [%s]: %v", path, err)
9393- if err = createNotice(e, NoticeRepository, desc); err != nil {
9595+ if err = CreateNoticeCtx(ctx, NoticeRepository, desc); err != nil {
9496 log.Error("CreateRepositoryNotice: %v", err)
9597 }
9698 }
···142144 Delete(new(Notice))
143145 return err
144146}
145145-146146-// GetAdminUser returns the first administrator
147147-func GetAdminUser() (*User, error) {
148148- var admin User
149149- has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
150150- if err != nil {
151151- return nil, err
152152- } else if !has {
153153- return nil, ErrUserNotExist{}
154154- }
155155-156156- return &admin, nil
157157-}
+18
models/admin/main_test.go
···11+// Copyright 2020 The Gitea Authors. All rights reserved.
22+// Use of this source code is governed by a MIT-style
33+// license that can be found in the LICENSE file.
44+55+package admin
66+77+import (
88+ "path/filepath"
99+ "testing"
1010+1111+ "code.gitea.io/gitea/models/unittest"
1212+)
1313+1414+func TestMain(m *testing.M) {
1515+ unittest.MainTest(m, filepath.Join("..", ".."),
1616+ "notice.yml",
1717+ )
1818+}
+1-1
models/admin_test.go
models/admin/notice_test.go
···22// Use of this source code is governed by a MIT-style
33// license that can be found in the LICENSE file.
4455-package models
55+package admin
6677import (
88 "testing"