Skip to content
Permalink
Browse files

workflows: enable golangci-lint (#5998)

* Create golint.yml

* Update golint.yml

* Update golint.yml

* Fix errcheck

* Fix gosimple

* Fix staticcheck
  • Loading branch information
unknwon committed Mar 21, 2020
1 parent 958d8b6 commit 5843038a0812cc133c1895b7410aeda7153e8708
@@ -0,0 +1,9 @@
name: golint
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run golangci-lint
uses: actions-contrib/golangci-lint@v1
@@ -1,3 +1,4 @@
//nolint
package main

import (
@@ -1,3 +1,4 @@
//nolint
package main

import (
@@ -146,7 +146,9 @@ func runCreateUser(c *cli.Context) error {
return errors.Wrap(err, "init configuration")
}

db.SetEngine()
if err = db.SetEngine(); err != nil {
return errors.Wrap(err, "set engine")
}

if err := db.CreateUser(&db.User{
Name: c.String("name"),
@@ -169,7 +171,9 @@ func adminDashboardOperation(operation func() error, successMessage string) func
return errors.Wrap(err, "init configuration")
}

db.SetEngine()
if err = db.SetEngine(); err != nil {
return errors.Wrap(err, "set engine")
}

if err := operation(); err != nil {
functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
@@ -52,7 +52,9 @@ func runBackup(c *cli.Context) error {
return errors.Wrap(err, "init configuration")
}

db.SetEngine()
if err = db.SetEngine(); err != nil {
return errors.Wrap(err, "set engine")
}

tmpDir := c.String("tempdir")
if !com.IsExist(tmpDir) {
@@ -25,7 +25,7 @@ func boolFlag(name, usage string) cli.BoolFlag {
}
}

//nolint:deadcode
//nolint:deadcode,unused
func intFlag(name string, value int, usage string) cli.IntFlag {
return cli.IntFlag{
Name: name,
@@ -34,7 +34,7 @@ func intFlag(name string, value int, usage string) cli.IntFlag {
}
}

//nolint:deadcode
//nolint:deadcode,unused
func durationFlag(name string, value time.Duration, usage string) cli.DurationFlag {
return cli.DurationFlag{
Name: name,
@@ -61,7 +61,7 @@ func runImportLocale(c *cli.Context) error {

now := time.Now()

line := make([]byte, 0, 100)
var line []byte
badChars := []byte(`="`)
escapedQuotes := []byte(`\"`)
regularQuotes := []byte(`"`)
@@ -97,15 +97,15 @@ func runImportLocale(c *cli.Context) error {
line = append(line[:idx+1], line[idx+2:len(line)-1]...)
line = bytes.Replace(line, escapedQuotes, regularQuotes, -1)
}
tw.Write(line)
tw.WriteString("\n")
_, _ = tw.Write(line)
_, _ = tw.WriteString("\n")
}
sr.Close()
tw.Close()
_ = sr.Close()
_ = tw.Close()

// Modification time of files from Crowdin often ahead of current,
// so we need to set back to current.
os.Chtimes(target, now, now)
_ = os.Chtimes(target, now, now)
}

fmt.Println("Locale files has been successfully imported!")
@@ -99,7 +99,9 @@ func runRestore(c *cli.Context) error {
return errors.Wrap(err, "init configuration")
}

db.SetEngine()
if err = db.SetEngine(); err != nil {
return errors.Wrap(err, "set engine")
}

// Database
dbDir := path.Join(archivePath, "db")
@@ -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)
}
}
@@ -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)
}
}
@@ -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)
}
@@ -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)
}
@@ -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
}
@@ -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
}

@@ -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()
@@ -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 {
@@ -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)
}
@@ -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 {
@@ -819,16 +819,16 @@ 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)
}

// FIXME: system notice
// 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)
@@ -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()

0 comments on commit 5843038

Please sign in to comment.
You can’t perform that action at this time.