Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Enable & fix lint (#60)
Browse files Browse the repository at this point in the history
* fix: lint

fix: lint

fix: lint

* Enable lint

* fix: add curl
  • Loading branch information
DonRenando committed Oct 6, 2021
1 parent 2bedbed commit 6fe8830
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ steps:
pull: always
image: golang:1.15-alpine
commands:
- apk add --no-cache make git gcc build-base
- apk add --no-cache make git gcc build-base curl
- make clean
- make vet
#- make lint
- make lint
- make test
- make build
settings:
Expand Down
3 changes: 2 additions & 1 deletion cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func TestCache(t *testing.T) {
})

g.It("Should set and get an item", func() {
Set(c, "foo", "bar")
err := Set(c, "foo", "bar")
g.Assert(err).IsNil()
v, e := Get(c, "foo")
g.Assert(v).Equal("bar")
g.Assert(e == nil).IsTrue()
Expand Down
23 changes: 19 additions & 4 deletions cache/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func GetRepos(c context.Context, user *model.User) ([]*model.Repo, error) {
if err != nil {
return nil, err
}
FromContext(c).Set(key, repos)
err = FromContext(c).Set(key, repos)
if err != nil {
return nil, err
}
return repos, nil
}

Expand All @@ -47,7 +50,12 @@ func GetTeams(c context.Context, user *model.User) ([]*model.Team, error) {
if err != nil {
return nil, err
}
FromContext(c).Set(key, teams)

err = FromContext(c).Set(key, teams)
if err != nil {
return nil, err
}

return teams, nil
}

Expand All @@ -70,7 +78,10 @@ func GetPerm(c context.Context, user *model.User, owner, name string) (*model.Pe
if err != nil {
return nil, err
}
FromContext(c).Set(key, perm)
err = FromContext(c).Set(key, perm)
if err != nil {
return nil, err
}
return perm, nil
}

Expand All @@ -90,6 +101,10 @@ func GetMembers(c context.Context, user *model.User, team string) ([]*model.Memb
if err != nil {
return nil, err
}
FromContext(c).Set(key, members)
err = FromContext(c).Set(key, members)
if err != nil {
return nil, err
}

return members, nil
}
14 changes: 9 additions & 5 deletions cache/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-gitea/lgtm/model"
"github.com/go-gitea/lgtm/remote"
"github.com/go-gitea/lgtm/remote/mock"
mocks "github.com/go-gitea/lgtm/remote/mock"

"github.com/franela/goblin"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -44,7 +44,8 @@ func TestHelper(t *testing.T) {
fakeRepo.Name,
)

Set(c, key, fakePerm)
err := Set(c, key, fakePerm)
g.Assert(err).IsNil()
r.On("GetPerm", c, fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, errFake).Once()
p, err := GetPerm(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
g.Assert(p).Equal(fakePerm)
Expand All @@ -71,7 +72,8 @@ func TestHelper(t *testing.T) {
fakeUser.Login,
)

Set(c, key, fakeRepos)
err := Set(c, key, fakeRepos)
g.Assert(err).IsNil()
r.On("GetRepos", c, fakeUser).Return(nil, errFake).Once()
p, err := GetRepos(c, fakeUser)
g.Assert(p).Equal(fakeRepos)
Expand All @@ -97,7 +99,8 @@ func TestHelper(t *testing.T) {
fakeUser.Login,
)

Set(c, key, fakeTeams)
err := Set(c, key, fakeTeams)
g.Assert(err).IsNil()
r.On("GetTeams", c, fakeUser).Return(nil, errFake).Once()
p, err := GetTeams(c, fakeUser)
g.Assert(p).Equal(fakeTeams)
Expand All @@ -121,7 +124,8 @@ func TestHelper(t *testing.T) {
g.It("Should get members", func() {
key := "members:drone"

Set(c, key, fakeMembers)
err := Set(c, key, fakeMembers)
g.Assert(err).IsNil()
r.On("GetMembers", c, fakeUser, "drone").Return(nil, errFake).Once()
p, err := GetMembers(c, fakeUser, "drone")
g.Assert(p).Equal(fakeMembers)
Expand Down
4 changes: 2 additions & 2 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func ParseConfigStr(data string) (*Config, error) {
if len(c.Team) == 0 {
c.Team = *team
}
if c.SelfApprovalOff == false {
if !c.SelfApprovalOff {
c.SelfApprovalOff = *selfApprovalOff
}
if c.IgnoreMaintainersFile == false {
if !c.IgnoreMaintainersFile {
c.IgnoreMaintainersFile = *ignoreMaintainersFile
}

Expand Down
4 changes: 2 additions & 2 deletions model/maintainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ func parseln(s string) string {

// regular expression determines if a line in the maintainers
// file only has the single GitHub username and no other metadata.
var reLogin = regexp.MustCompile("^\\w[\\w-]+$")
var reLogin = regexp.MustCompile(`^\w[\w-]+$`)

// regular expression determines if a line in the maintainers
// file has the username and metadata.
var reLoginMeta = regexp.MustCompile("(.+) <(.+)> \\(@(.+)\\)")
var reLoginMeta = regexp.MustCompile(`(.+) <(.+)> \(@(.+)\)`)

// regular expression determines if a line in the maintainers
// file has the username and email.
Expand Down
5 changes: 4 additions & 1 deletion model/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func Rand() string {
maxrb := byte(256 - (256 % len(chars)))
i := 0
for {
io.ReadFull(rand.Reader, r)
_, err := io.ReadFull(rand.Reader, r)
if err != nil {
return ""
}
for _, c := range r {
if c >= maxrb {
// Skip this number to avoid modulo bias.
Expand Down
18 changes: 2 additions & 16 deletions remote/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ import (
"net/http"
"net/url"

"golang.org/x/net/context"
"golang.org/x/oauth2"
)

const (
pathLogin = "%slogin?access_token=%s"
pathUser = "%sapi/user"
pathRepos = "%sapi/user/repos"
pathRepo = "%sapi/repos/%s"
pathConf = "%sapi/repos/%s/maintainers"
pathBranch = "%srepos/%s/%s/branches/%s/protection"

// protected branch
Expand All @@ -39,7 +35,7 @@ func NewClient(uri string) *Client {
// authenticates all outbound requests with the given token.
func NewClientToken(uri, token string) *Client {
config := new(oauth2.Config)
auther := config.Client(oauth2.NoContext, &oauth2.Token{AccessToken: token})
auther := config.Client(context.TODO(), &oauth2.Token{AccessToken: token})
return &Client{auther, uri}
}

Expand Down Expand Up @@ -79,11 +75,6 @@ func (c *Client) get(rawurl string, out interface{}) error {
return c.do(rawurl, "GET", nil, out)
}

// helper function for making an http POST request.
func (c *Client) post(rawurl string, in, out interface{}) error {
return c.do(rawurl, "POST", in, out)
}

// helper function for making an http PUT request.
func (c *Client) put(rawurl string, in, out interface{}) error {
return c.do(rawurl, "PUT", in, out)
Expand All @@ -94,11 +85,6 @@ func (c *Client) patch(rawurl string, in, out interface{}) error {
return c.do(rawurl, "PATCH", in, out)
}

// helper function for making an http DELETE request.
func (c *Client) delete(rawurl string) error {
return c.do(rawurl, "DELETE", nil, nil)
}

// helper function to make an http request
func (c *Client) do(rawurl, method string, in, out interface{}) error {
// executes the http request and returns the body as
Expand Down
7 changes: 5 additions & 2 deletions remote/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (g *Github) GetUser(c context.Context, res http.ResponseWriter, req *http.R
}

// exchanges the oauth2 code for an access token
token, err := config.Exchange(oauth2.NoContext, code)
token, err := config.Exchange(context.TODO(), code)
if err != nil {
return nil, fmt.Errorf("Error exchanging token. %s", err)
}
Expand Down Expand Up @@ -226,7 +226,10 @@ func (g *Github) SetHook(c context.Context, user *model.User, repo *model.Repo,

old, err := GetHook(c, client, repo.Owner, repo.Name, link)
if err == nil && old != nil {
client.Repositories.DeleteHook(c, repo.Owner, repo.Name, *old.ID)
_, err = client.Repositories.DeleteHook(c, repo.Owner, repo.Name, *old.ID)
if err != nil {
return err
}
}

_, err = CreateHook(c, client, repo.Owner, repo.Name, link)
Expand Down
2 changes: 1 addition & 1 deletion remote/github/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func setupClient(rawurl, accessToken string) *github.Client {
token := oauth2.Token{AccessToken: accessToken}
source := oauth2.StaticTokenSource(&token)
client := oauth2.NewClient(oauth2.NoContext, source)
client := oauth2.NewClient(context.TODO(), source)
github := github.NewClient(client)
github.BaseURL, _ = url.Parse(rawurl)
return github
Expand Down
4 changes: 2 additions & 2 deletions store/datastore/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func toList(items []string) (string, []interface{}) {
size = 990
items = items[:990]
}
var qs = make([]string, size, size)
var in = make([]interface{}, size, size)
var qs = make([]string, size)
var in = make([]interface{}, size)
for i, item := range items {
qs[i] = "?"
in[i] = item
Expand Down
24 changes: 16 additions & 8 deletions store/datastore/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ func Test_repostore(t *testing.T) {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
db.Exec("DELETE FROM repos")
db.Exec("DELETE FROM users")
_, err := db.Exec("DELETE FROM repos")
g.Assert(err).IsNil()
_, err = db.Exec("DELETE FROM users")
g.Assert(err).IsNil()
})

g.It("Should Set a Repo", func() {
Expand Down Expand Up @@ -59,7 +61,8 @@ func Test_repostore(t *testing.T) {
Link: "https://github.com/octocat/hello-world",
Private: true,
}
s.CreateRepo(&repo)
err := s.CreateRepo(&repo)
g.Assert(err).IsNil()
getrepo, err := s.GetRepo(repo.ID)
g.Assert(err == nil).IsTrue()
g.Assert(repo.ID).Equal(getrepo.ID)
Expand All @@ -77,7 +80,8 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
s.CreateRepo(&repo)
err := s.CreateRepo(&repo)
g.Assert(err).IsNil()
getrepo, err := s.GetRepoSlug(repo.Slug)
g.Assert(err == nil).IsTrue()
g.Assert(repo.ID).Equal(getrepo.ID)
Expand Down Expand Up @@ -105,9 +109,12 @@ func Test_repostore(t *testing.T) {
Name: "hello-world",
Slug: "octocat/hello-world",
}
s.CreateRepo(repo1)
s.CreateRepo(repo2)
s.CreateRepo(repo3)
err := s.CreateRepo(repo1)
g.Assert(err).IsNil()
err = s.CreateRepo(repo2)
g.Assert(err).IsNil()
err = s.CreateRepo(repo3)
g.Assert(err).IsNil()

repos, err := s.GetRepoMulti("octocat/fork-knife", "octocat/hello-world")
g.Assert(err == nil).IsTrue()
Expand All @@ -123,7 +130,8 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
s.CreateRepo(&repo)
err := s.CreateRepo(&repo)
g.Assert(err).IsNil()
_, err1 := s.GetRepo(repo.ID)
err2 := s.DeleteRepo(&repo)
_, err3 := s.GetRepo(repo.ID)
Expand Down
11 changes: 0 additions & 11 deletions store/datastore/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ WHERE user_login = ?
LIMIT 1
`

const userListQuery = `
SELECT *
FROM users
ORDER BY user_login ASC
`

const userCountQuery = `
SELECT count(1)
FROM users
`

const userDeleteStmt = `
DELETE FROM users
WHERE user_id = ?
Expand Down
12 changes: 8 additions & 4 deletions store/datastore/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func Test_userstore(t *testing.T) {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
db.Exec("DELETE FROM users")
_, err := db.Exec("DELETE FROM users")
g.Assert(err).IsNil()
})

g.It("Should Update a User", func() {
Expand Down Expand Up @@ -56,7 +57,8 @@ func Test_userstore(t *testing.T) {
Avatar: "b9015b0857e16ac4d94a0ffd9a0b79c8",
}

s.CreateUser(&user)
err := s.CreateUser(&user)
g.Assert(err).IsNil()
getuser, err := s.GetUser(user.ID)
g.Assert(err == nil).IsTrue()
g.Assert(user.ID).Equal(getuser.ID)
Expand All @@ -73,7 +75,8 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
s.CreateUser(&user)
err := s.CreateUser(&user)
g.Assert(err).IsNil()
getuser, err := s.GetUserLogin(user.Login)
g.Assert(err == nil).IsTrue()
g.Assert(user.ID).Equal(getuser.ID)
Expand Down Expand Up @@ -103,7 +106,8 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
s.CreateUser(&user)
err := s.CreateUser(&user)
g.Assert(err).IsNil()
_, err1 := s.GetUser(user.ID)
err2 := s.DeleteUser(&user)
_, err3 := s.GetUser(user.ID)
Expand Down
2 changes: 1 addition & 1 deletion web/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func LoginToken(c *gin.Context) {
token := token.New(token.UserToken, user.Login)
tokenstr, err := token.SignExpires(user.Secret, exp)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
c.AbortWithError(http.StatusInternalServerError, err) // nolint: errcheck
return
}
c.IndentedJSON(http.StatusOK, &tokenPayload{
Expand Down

0 comments on commit 6fe8830

Please sign in to comment.