diff --git a/.drone.yml b/.drone.yml index db568fa..94b01fe 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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: diff --git a/cache/cache_test.go b/cache/cache_test.go index e984da2..6fa963a 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -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() diff --git a/cache/helper.go b/cache/helper.go index 0a04f0b..0efe09f 100644 --- a/cache/helper.go +++ b/cache/helper.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/cache/helper_test.go b/cache/helper_test.go index 295d865..f251f0d 100644 --- a/cache/helper_test.go +++ b/cache/helper_test.go @@ -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" @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/model/config.go b/model/config.go index 06ed6af..5968178 100644 --- a/model/config.go +++ b/model/config.go @@ -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 } diff --git a/model/maintainer.go b/model/maintainer.go index 340e64d..4ad3ff4 100644 --- a/model/maintainer.go +++ b/model/maintainer.go @@ -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. diff --git a/model/util.go b/model/util.go index ff27d60..669c98f 100644 --- a/model/util.go +++ b/model/util.go @@ -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. diff --git a/remote/github/client.go b/remote/github/client.go index ef1a7d5..77bafd1 100644 --- a/remote/github/client.go +++ b/remote/github/client.go @@ -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 @@ -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} } @@ -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) @@ -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 diff --git a/remote/github/github.go b/remote/github/github.go index fa5bf87..e92b975 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -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) } @@ -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) diff --git a/remote/github/utils.go b/remote/github/utils.go index 632a793..cb4befd 100644 --- a/remote/github/utils.go +++ b/remote/github/utils.go @@ -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 diff --git a/store/datastore/repos.go b/store/datastore/repos.go index 90c0b2b..224ec76 100644 --- a/store/datastore/repos.go +++ b/store/datastore/repos.go @@ -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 diff --git a/store/datastore/repos_test.go b/store/datastore/repos_test.go index 22f7fdc..e2116b2 100644 --- a/store/datastore/repos_test.go +++ b/store/datastore/repos_test.go @@ -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() { @@ -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) @@ -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) @@ -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() @@ -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) diff --git a/store/datastore/users.go b/store/datastore/users.go index 02cf5c0..adf9132 100644 --- a/store/datastore/users.go +++ b/store/datastore/users.go @@ -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 = ? diff --git a/store/datastore/users_test.go b/store/datastore/users_test.go index 42280ef..ef4edcb 100644 --- a/store/datastore/users_test.go +++ b/store/datastore/users_test.go @@ -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() { @@ -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) @@ -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) @@ -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) diff --git a/web/login.go b/web/login.go index 336d076..42a9ab9 100644 --- a/web/login.go +++ b/web/login.go @@ -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{