aboutsummaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-21 13:39:32 +0800
committerGitHub <noreply@github.com>2020-03-21 13:39:32 +0800
commit5843038a0812cc133c1895b7410aeda7153e8708 (patch)
tree3515998242443e1a82217ee4702603819c5315c8 /internal/db
parent958d8b6bb4c2da66859325695b91d871e567a4fa (diff)
workflows: enable golangci-lint (#5998)
* Create golint.yml * Update golint.yml * Update golint.yml * Fix errcheck * Fix gosimple * Fix staticcheck
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/comment.go2
-rw-r--r--internal/db/issue.go2
-rw-r--r--internal/db/migrations/migrations.go2
-rw-r--r--internal/db/migrations/v16.go2
-rw-r--r--internal/db/models.go15
-rw-r--r--internal/db/org.go4
-rw-r--r--internal/db/org_team.go2
-rw-r--r--internal/db/pull.go10
-rw-r--r--internal/db/repo.go12
-rw-r--r--internal/db/repo_editor.go12
-rw-r--r--internal/db/user.go9
-rw-r--r--internal/db/webhook.go4
12 files changed, 45 insertions, 31 deletions
diff --git a/internal/db/comment.go b/internal/db/comment.go
index 03f95eea..50976357 100644
--- a/internal/db/comment.go
+++ b/internal/db/comment.go
@@ -239,7 +239,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
attachments[i].IssueID = opts.Issue.ID
attachments[i].CommentID = comment.ID
// No assign value could be 0, so ignore AllCols().
- if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
+ if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
}
}
diff --git a/internal/db/issue.go b/internal/db/issue.go
index 377e77b4..b153e6a1 100644
--- a/internal/db/issue.go
+++ b/internal/db/issue.go
@@ -738,7 +738,7 @@ func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
for i := 0; i < len(attachments); i++ {
attachments[i].IssueID = opts.Issue.ID
- if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
+ if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
return fmt.Errorf("update attachment [id: %d]: %v", attachments[i].ID, err)
}
}
diff --git a/internal/db/migrations/migrations.go b/internal/db/migrations/migrations.go
index a4f47441..dce1476d 100644
--- a/internal/db/migrations/migrations.go
+++ b/internal/db/migrations/migrations.go
@@ -337,7 +337,7 @@ func convertDateToUnix(x *xorm.Engine) (err error) {
offset := 0
for {
beans := make([]*Bean, 0, 100)
- if err = x.Sql(fmt.Sprintf("SELECT * FROM `%s` ORDER BY id ASC LIMIT 100 OFFSET %d",
+ if err = x.SQL(fmt.Sprintf("SELECT * FROM `%s` ORDER BY id ASC LIMIT 100 OFFSET %d",
table.name, offset)).Find(&beans); err != nil {
return fmt.Errorf("select beans [table: %s, offset: %d]: %v", table.name, offset, err)
}
diff --git a/internal/db/migrations/v16.go b/internal/db/migrations/v16.go
index f60f9d15..a17185e0 100644
--- a/internal/db/migrations/v16.go
+++ b/internal/db/migrations/v16.go
@@ -37,7 +37,7 @@ func updateRepositorySizes(x *xorm.Engine) (err error) {
offset := 0
for {
repos := make([]*Repository, 0, 10)
- if err = x.Sql(fmt.Sprintf("SELECT * FROM `repository` ORDER BY id ASC LIMIT 10 OFFSET %d", offset)).
+ if err = x.SQL(fmt.Sprintf("SELECT * FROM `repository` ORDER BY id ASC LIMIT 10 OFFSET %d", offset)).
Find(&repos); err != nil {
return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
}
diff --git a/internal/db/models.go b/internal/db/models.go
index 212a1f60..cf00727e 100644
--- a/internal/db/models.go
+++ b/internal/db/models.go
@@ -151,8 +151,8 @@ func getEngine() (*xorm.Engine, error) {
return xorm.NewEngine(conf.Database.Type, connStr)
}
-func NewTestEngine(x *xorm.Engine) (err error) {
- x, err = getEngine()
+func NewTestEngine() error {
+ x, err := getEngine()
if err != nil {
return fmt.Errorf("connect to database: %v", err)
}
@@ -260,8 +260,11 @@ type Version struct {
}
// DumpDatabase dumps all data from database to file system in JSON format.
-func DumpDatabase(dirPath string) (err error) {
- os.MkdirAll(dirPath, os.ModePerm)
+func DumpDatabase(dirPath string) error {
+ if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
+ return err
+ }
+
// Purposely create a local variable to not modify global variable
tables := append(tables, new(Version))
for _, table := range tables {
@@ -275,10 +278,10 @@ func DumpDatabase(dirPath string) (err error) {
if err = x.Asc("id").Iterate(table, func(idx int, bean interface{}) (err error) {
return jsoniter.NewEncoder(f).Encode(bean)
}); err != nil {
- f.Close()
+ _ = f.Close()
return fmt.Errorf("dump table '%s': %v", tableName, err)
}
- f.Close()
+ _ = f.Close()
}
return nil
}
diff --git a/internal/db/org.go b/internal/db/org.go
index e07480bf..bcf307a7 100644
--- a/internal/db/org.go
+++ b/internal/db/org.go
@@ -131,7 +131,7 @@ func CreateOrganization(org, owner *User) (err error) {
if _, err = sess.Insert(org); err != nil {
return fmt.Errorf("insert organization: %v", err)
}
- org.GenerateRandomAvatar()
+ _ = org.GenerateRandomAvatar()
// Add initial creator to organization and owner team.
if _, err = sess.Insert(&OrgUser{
@@ -356,10 +356,8 @@ func AddOrgUser(orgID, uid int64) error {
}
if _, err := sess.Insert(ou); err != nil {
- sess.Rollback()
return err
} else if _, err = sess.Exec("UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgID); err != nil {
- sess.Rollback()
return err
}
diff --git a/internal/db/org_team.go b/internal/db/org_team.go
index 418d4f6b..8ed587db 100644
--- a/internal/db/org_team.go
+++ b/internal/db/org_team.go
@@ -255,13 +255,11 @@ func NewTeam(t *Team) error {
}
if _, err = sess.Insert(t); err != nil {
- sess.Rollback()
return err
}
// Update organization number of teams.
if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
- sess.Rollback()
return err
}
return sess.Commit()
diff --git a/internal/db/pull.go b/internal/db/pull.go
index 331bc3dd..b1046715 100644
--- a/internal/db/pull.go
+++ b/internal/db/pull.go
@@ -219,8 +219,12 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
// Create temporary directory to store temporary copy of the base repository,
// and clean it up when operation finished regardless of succeed or not.
tmpBasePath := filepath.Join(conf.Server.AppDataPath, "tmp", "repos", com.ToStr(time.Now().Nanosecond())+".git")
- os.MkdirAll(filepath.Dir(tmpBasePath), os.ModePerm)
- defer os.RemoveAll(filepath.Dir(tmpBasePath))
+ if err = os.MkdirAll(filepath.Dir(tmpBasePath), os.ModePerm); err != nil {
+ return err
+ }
+ defer func() {
+ _ = os.RemoveAll(filepath.Dir(tmpBasePath))
+ }()
// Clone the base repository to the defined temporary directory,
// and checks out to base branch directly.
@@ -845,7 +849,7 @@ func (pr *PullRequest) checkAndUpdateStatus() {
// TODO: test more pull requests at same time.
func TestPullRequests() {
prs := make([]*PullRequest, 0, 10)
- x.Iterate(PullRequest{
+ _ = x.Iterate(PullRequest{
Status: PULL_REQUEST_STATUS_CHECKING,
},
func(idx int, bean interface{}) error {
diff --git a/internal/db/repo.go b/internal/db/repo.go
index 1a27a34a..8f797e9b 100644
--- a/internal/db/repo.go
+++ b/internal/db/repo.go
@@ -660,7 +660,9 @@ func (repo *Repository) SavePatch(index int64, patch []byte) error {
return fmt.Errorf("PatchPath: %v", err)
}
- os.MkdirAll(filepath.Dir(patchPath), os.ModePerm)
+ if err = os.MkdirAll(filepath.Dir(patchPath), os.ModePerm); err != nil {
+ return err
+ }
if err = ioutil.WriteFile(patchPath, patch, 0644); err != nil {
return fmt.Errorf("WriteFile: %v", err)
}
@@ -1017,7 +1019,9 @@ func initRepository(e Engine, repoPath string, doer *User, repo *Repository, opt
// Initialize repository according to user's choice.
if opts.AutoInit {
- os.MkdirAll(tmpDir, os.ModePerm)
+ if err = os.MkdirAll(tmpDir, os.ModePerm); err != nil {
+ return err
+ }
defer RemoveAllWithNotice("Delete repository for auto-initialization", tmpDir)
if err = prepareRepoCommit(repo, tmpDir, repoPath, opts); err != nil {
@@ -1349,7 +1353,9 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
}
// Rename remote repository to new path and delete local copy.
- os.MkdirAll(UserPath(newOwner.Name), os.ModePerm)
+ if err = os.MkdirAll(UserPath(newOwner.Name), os.ModePerm); err != nil {
+ return err
+ }
if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
return fmt.Errorf("rename repository directory: %v", err)
}
diff --git a/internal/db/repo_editor.go b/internal/db/repo_editor.go
index cf711557..c525bcd6 100644
--- a/internal/db/repo_editor.go
+++ b/internal/db/repo_editor.go
@@ -152,7 +152,9 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
oldFilePath := path.Join(localPath, opts.OldTreeName)
filePath := path.Join(localPath, opts.NewTreeName)
- os.MkdirAll(path.Dir(filePath), os.ModePerm)
+ if err = os.MkdirAll(path.Dir(filePath), os.ModePerm); err != nil {
+ return err
+ }
// If it's meant to be a new file, make sure it doesn't exist.
if opts.IsNewFile {
@@ -206,7 +208,9 @@ func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *
localPath := repo.LocalCopyPath()
filePath := path.Join(localPath, treePath)
- os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
+ if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
+ return nil, err
+ }
if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
return nil, fmt.Errorf("write file: %v", err)
}
@@ -471,7 +475,9 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
localPath := repo.LocalCopyPath()
dirPath := path.Join(localPath, opts.TreePath)
- os.MkdirAll(dirPath, os.ModePerm)
+ if err = os.MkdirAll(dirPath, os.ModePerm); err != nil {
+ return err
+ }
// Copy uploaded files into repository
for _, upload := range uploads {
diff --git a/internal/db/user.go b/internal/db/user.go
index 8678c1c5..4764db4a 100644
--- a/internal/db/user.go
+++ b/internal/db/user.go
@@ -819,7 +819,7 @@ func deleteUser(e *xorm.Session, u *User) error {
return fmt.Errorf("clear assignee: %v", err)
}
- if _, err = e.Id(u.ID).Delete(new(User)); err != nil {
+ if _, err = e.ID(u.ID).Delete(new(User)); err != nil {
return fmt.Errorf("Delete: %v", err)
}
@@ -827,8 +827,8 @@ func deleteUser(e *xorm.Session, u *User) error {
// Note: There are something just cannot be roll back,
// so just keep error logs of those operations.
- os.RemoveAll(UserPath(u.Name))
- os.Remove(u.CustomAvatarPath())
+ _ = os.RemoveAll(UserPath(u.Name))
+ _ = os.Remove(u.CustomAvatarPath())
return nil
}
@@ -1076,8 +1076,7 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
Or("LOWER(full_name) LIKE ?", searchQuery).
And("type = ?", opts.Type)
- var countSess xorm.Session
- countSess = *sess
+ countSess := *sess
count, err := countSess.Count(new(User))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
diff --git a/internal/db/webhook.go b/internal/db/webhook.go
index 1547a276..cce7e3e6 100644
--- a/internal/db/webhook.go
+++ b/internal/db/webhook.go
@@ -629,7 +629,7 @@ func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Pay
log.Error("prepareWebhooks.JSONPayload: %v", err)
}
sig := hmac.New(sha256.New, []byte(w.Secret))
- sig.Write(data)
+ _, _ = sig.Write(data)
signature = hex.EncodeToString(sig.Sum(nil))
}
@@ -769,7 +769,7 @@ func (t *HookTask) deliver() {
// TODO: shoot more hooks at same time.
func DeliverHooks() {
tasks := make([]*HookTask, 0, 10)
- x.Where("is_delivered = ?", false).Iterate(new(HookTask),
+ _ = x.Where("is_delivered = ?", false).Iterate(new(HookTask),
func(idx int, bean interface{}) error {
t := bean.(*HookTask)
t.deliver()