Skip to content

Commit aa6b3d8

Browse files
committed
chore: Add fmt/lint/vet/sec checks
Signed-off-by: Andrew Bayer <[email protected]>
1 parent a1c44fa commit aa6b3d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+507
-213
lines changed

Makefile

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
1+
# Make does not offer a recursive wildcard function, so here's one:
2+
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
3+
4+
GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,scm/,*.go)
5+
GO := GO111MODULE=on go
6+
GO_NOMOD := GO111MODULE=off go
7+
18
build: test
29

310
test:
411
go test ./...
512

6-
linux: build
13+
linux: build
14+
15+
.PHONY: check
16+
check: fmt lint sec ## Runs Go format check as well as security checks
17+
18+
get-fmt-deps:
19+
$(GO_NOMOD) get golang.org/x/tools/cmd/goimports
20+
21+
.PHONY: importfmt
22+
importfmt: get-fmt-deps ## Checks the import format of the Go source files
23+
@echo "FORMATTING IMPORTS"
24+
@goimports -w $(GO_DEPENDENCIES)
25+
26+
.PHONY: fmt ## Checks Go source files are formatted properly
27+
fmt: importfmt
28+
@echo "FORMATTING SOURCE"
29+
FORMATTED=`$(GO) fmt ./...`
30+
@([[ ! -z "$(FORMATTED)" ]] && printf "Fixed un-formatted files:\n$(FORMATTED)") || true
31+
32+
GOLINT := $(GOPATH)/bin/golint
33+
$(GOLINT):
34+
$(GO_NOMOD) get -u golang.org/x/lint/golint
35+
36+
.PHONY: lint
37+
lint: $(GOLINT) ## Runs 'go vet' anf 'go lint'
38+
@echo "VETTING"
39+
$(GO) vet ./...
40+
@echo "LINTING"
41+
$(GOLINT) -set_exit_status ./...
42+
43+
GOSEC := $(GOPATH)/bin/gosec
44+
$(GOSEC):
45+
$(GO_NOMOD) get -u github.com/securego/gosec/cmd/gosec
46+
47+
.PHONY: sec
48+
sec: $(GOSEC) ## Runs gosec to check for potential security issues in the Go source
49+
@echo "SECURITY SCANNING"
50+
$(GOSEC) -quiet -fmt=csv ./...

jenkins-x.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ pipelineConfig:
3333
- name: test
3434
command: make
3535
args:
36-
- test
36+
- check test
3737
dir: /workspace/source

pkg/hmac/hmac.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package hmac
66

77
import (
88
"crypto/hmac"
9-
"crypto/sha1"
9+
"crypto/sha1" // #nosec
1010
"crypto/sha256"
1111
"encoding/hex"
1212
"hash"
@@ -42,7 +42,7 @@ func ValidatePrefix(message, key []byte, signature string) bool {
4242

4343
func validate(h func() hash.Hash, message, key, signature []byte) bool {
4444
mac := hmac.New(h, key)
45-
mac.Write(message)
45+
mac.Write(message) // #nosec
4646
sum := mac.Sum(nil)
4747
return hmac.Equal(signature, sum)
4848
}

scm/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
type (
9+
// InstallationToken is the token used for interacting with the app
910
InstallationToken struct {
1011
Token string
1112
ExpiresAt *time.Time

scm/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ func (c *Client) Do(ctx context.Context, in *Request) (*Response, error) {
174174

175175
// dumps the response for debugging purposes.
176176
if c.DumpResponse != nil {
177-
c.DumpResponse(res, true)
177+
_, err = c.DumpResponse(res, true)
178178
}
179-
return newResponse(res), nil
179+
return newResponse(res), err
180180
}
181181

182182
// newResponse creates a new Response for the provided

scm/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ func ToState(s string) State {
7171
}
7272
}
7373

74+
// MarshalJSON marshals State to JSON
7475
func (s State) MarshalJSON() ([]byte, error) {
7576
return []byte(fmt.Sprintf(`"%s"`, s.String())), nil
7677
}
7778

79+
// UnmarshalJSON unmarshals JSON to State
7880
func (s *State) UnmarshalJSON(b []byte) error {
7981
*s = ToState(strings.Trim(string(b), `"`))
8082
return nil

scm/driver/bitbucket/bitbucket.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/jenkins-x/go-scm/scm"
1717
)
1818

19+
// NewWebHookService creates a new instance of the webhook service without the rest of the client
1920
func NewWebHookService() scm.WebhookService {
2021
return &webhookService{nil}
2122
}
@@ -70,7 +71,7 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
7071
// write it to the body of the request.
7172
if in != nil {
7273
buf := new(bytes.Buffer)
73-
json.NewEncoder(buf).Encode(in)
74+
json.NewEncoder(buf).Encode(in) // #nosec
7475
req.Header = map[string][]string{
7576
"Content-Type": {"application/json"},
7677
}
@@ -90,7 +91,7 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
9091
return res, scm.ErrNotAuthorized
9192
} else if res.Status > 300 {
9293
err := new(Error)
93-
json.NewDecoder(res.Body).Decode(err)
94+
json.NewDecoder(res.Body).Decode(err) // #nosec
9495
return res, err
9596
}
9697

@@ -101,8 +102,8 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
101102
// if raw output is expected, copy to the provided
102103
// buffer and exit.
103104
if w, ok := out.(io.Writer); ok {
104-
io.Copy(w, res.Body)
105-
return res, nil
105+
_, err := io.Copy(w, res.Body)
106+
return res, err
106107
}
107108

108109
// if a json response is expected, parse and return

scm/driver/bitbucket/git.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,43 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.Lis
8484
path := fmt.Sprintf("2.0/repositories/%s/refs/branches?%s", repo, encodeListOptions(opts))
8585
out := new(branches)
8686
res, err := s.client.do(ctx, "GET", path, nil, out)
87-
copyPagination(out.pagination, res)
87+
if err != nil {
88+
return nil, res, err
89+
}
90+
err = copyPagination(out.pagination, res)
8891
return convertBranchList(out), res, err
8992
}
9093

9194
func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
9295
path := fmt.Sprintf("2.0/repositories/%s/commits/%s?%s", repo, opts.Ref, encodeCommitListOptions(opts))
9396
out := new(commits)
9497
res, err := s.client.do(ctx, "GET", path, nil, out)
95-
copyPagination(out.pagination, res)
98+
if err != nil {
99+
return nil, res, err
100+
}
101+
err = copyPagination(out.pagination, res)
96102
return convertCommitList(out), res, err
97103
}
98104

99105
func (s *gitService) ListTags(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
100106
path := fmt.Sprintf("2.0/repositories/%s/refs/tags?%s", repo, encodeListOptions(opts))
101107
out := new(branches)
102108
res, err := s.client.do(ctx, "GET", path, nil, &out)
103-
copyPagination(out.pagination, res)
109+
if err != nil {
110+
return nil, res, err
111+
}
112+
err = copyPagination(out.pagination, res)
104113
return convertTagList(out), res, err
105114
}
106115

107116
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
108117
path := fmt.Sprintf("2.0/repositories/%s/diffstat/%s?%s", repo, ref, encodeListOptions(opts))
109118
out := new(diffstats)
110119
res, err := s.client.do(ctx, "GET", path, nil, &out)
111-
copyPagination(out.pagination, res)
120+
if err != nil {
121+
return nil, res, err
122+
}
123+
err = copyPagination(out.pagination, res)
112124
return convertDiffstats(out), res, err
113125
}
114126

scm/driver/bitbucket/org.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([
5454
path := fmt.Sprintf("2.0/teams?%s", encodeListRoleOptions(opts))
5555
out := new(organizationList)
5656
res, err := s.client.do(ctx, "GET", path, nil, out)
57-
copyPagination(out.pagination, res)
57+
if err != nil {
58+
return nil, res, err
59+
}
60+
err = copyPagination(out.pagination, res)
5861
return convertOrganizationList(out), res, err
5962
}
6063

scm/driver/bitbucket/pr.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,21 @@ func (s *pullService) List(ctx context.Context, repo string, opts scm.PullReques
3737
return nil, res, err
3838
}
3939
res, err := s.client.do(ctx, "GET", path, nil, out)
40-
copyPagination(out.pagination, res)
40+
if err != nil {
41+
return nil, res, err
42+
}
43+
err = copyPagination(out.pagination, res)
4144
return convertPullRequests(out), res, err
4245
}
4346

4447
func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
4548
path := fmt.Sprintf("2.0/repositories/%s/pullrequests/%d/diffstat?%s", repo, number, encodeListOptions(opts))
4649
out := new(diffstats)
4750
res, err := s.client.do(ctx, "GET", path, nil, out)
48-
copyPagination(out.pagination, res)
51+
if err != nil {
52+
return nil, res, err
53+
}
54+
err = copyPagination(out.pagination, res)
4955
return convertDiffstats(out), res, err
5056
}
5157

scm/driver/bitbucket/repo.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*
127127
}
128128
out := new(repositories)
129129
res, err := s.client.do(ctx, "GET", path, nil, &out)
130-
copyPagination(out.pagination, res)
130+
if err != nil {
131+
return nil, res, err
132+
}
133+
err = copyPagination(out.pagination, res)
131134
return convertRepositoryList(out), res, err
132135
}
133136

@@ -144,7 +147,10 @@ func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm
144147
path := fmt.Sprintf("2.0/repositories/%s/hooks?%s", repo, encodeListOptions(opts))
145148
out := new(hooks)
146149
res, err := s.client.do(ctx, "GET", path, nil, out)
147-
copyPagination(out.pagination, res)
150+
if err != nil {
151+
return nil, res, err
152+
}
153+
err = copyPagination(out.pagination, res)
148154
return convertHookList(out), res, err
149155
}
150156

@@ -153,7 +159,10 @@ func (s *repositoryService) ListStatus(ctx context.Context, repo, ref string, op
153159
path := fmt.Sprintf("2.0/repositories/%s/commit/%s/statuses?%s", repo, ref, encodeListOptions(opts))
154160
out := new(statuses)
155161
res, err := s.client.do(ctx, "GET", path, nil, out)
156-
copyPagination(out.pagination, res)
162+
if err != nil {
163+
return nil, res, err
164+
}
165+
err = copyPagination(out.pagination, res)
157166
return convertStatusList(out), res, err
158167
}
159168

scm/driver/fake/content.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c contentService) Find(_ context.Context, repo, path, ref string) (*scm.Co
3232
Status: 404,
3333
}, errors.Wrapf(err, "file %s does not exist", f)
3434
}
35-
data, err := ioutil.ReadFile(f)
35+
data, err := ioutil.ReadFile(f) // #nosec
3636
if err != nil {
3737
return nil, nil, errors.Wrapf(err, "failed to read file %s", f)
3838
}
@@ -129,7 +129,7 @@ func (c contentService) path(repo string, path string, ref string) (string, erro
129129
return filepath.Join(repoDir, path), nil
130130
}
131131

132-
/// DirExists checks if path exists and is a directory
132+
// DirExists checks if path exists and is a directory
133133
func DirExists(path string) (bool, error) {
134134
info, err := os.Stat(path)
135135
if err == nil {

scm/driver/fake/data.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fake
22

33
import "github.com/jenkins-x/go-scm/scm"
44

5+
// Data is used to store/represent test data for the fake client
56
type Data struct {
67
Issues map[int][]*scm.Issue
78
OrgMembers map[string][]string
@@ -74,6 +75,7 @@ type Data struct {
7475
ContentDir string
7576
}
7677

78+
// DeletedRef represents a ref that has been deleted
7779
type DeletedRef struct {
7880
Org, Repo, Ref string
7981
}

scm/driver/fake/org.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ func (s *organizationService) List(context.Context, scm.ListOptions) ([]*scm.Org
6262
Name: fmt.Sprintf("organisation%d", i),
6363
Avatar: fmt.Sprintf("https://github.com/organisation%d.png", i),
6464
Permissions: scm.Permissions{
65-
true,
66-
true,
67-
true,
65+
MembersCreatePrivate: true,
66+
MembersCreatePublic: true,
67+
MembersCreateInternal: true,
6868
},
6969
}
7070
orgs = append(orgs, &org)

scm/driver/fake/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func (s *repositoryService) ListHooks(ctx context.Context, fullName string, opts
175175
}
176176

177177
func (s *repositoryService) CreateHook(ctx context.Context, fullName string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
178+
/* #nosec */
178179
hook := &scm.Hook{
179180
ID: fmt.Sprintf("%d", rand.Int()),
180181
Name: input.Name,

scm/driver/fake/user.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ func (s *userService) DeleteToken(context.Context, int64) (*scm.Response, error)
1919
return nil, scm.ErrNotSupported
2020
}
2121

22-
func (u *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
23-
return &u.data.CurrentUser, nil, nil
22+
func (s *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
23+
return &s.data.CurrentUser, nil, nil
2424
}
2525

26-
func (u *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
27-
return u.data.CurrentUser.Email, nil, nil
26+
func (s *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
27+
return s.data.CurrentUser.Email, nil, nil
2828
}
2929

30-
func (u *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
31-
for _, user := range u.data.Users {
30+
func (s *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
31+
for _, user := range s.data.Users {
3232
if user.Login == login {
3333
return user, nil, nil
3434
}

scm/driver/gitea/gitea.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/jenkins-x/go-scm/scm"
2020
)
2121

22+
// NewWebHookService creates a new instance of the webhook service without the rest of the client
2223
func NewWebHookService() scm.WebhookService {
2324
return &webhookService{nil}
2425
}

0 commit comments

Comments
 (0)