Skip to content

Commit 80953de

Browse files
style: add .golangci-lint.yaml
1 parent 47b6742 commit 80953de

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

.golangci.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
linters:
2+
enable:
3+
- errorlint
4+
- forbidigo
5+
- gochecknoinits
6+
- gocritic
7+
- goconst
8+
- gocyclo
9+
- gofumpt
10+
- goimports
11+
- misspell
12+
- revive
13+
- unconvert
14+
- unparam
15+
- wastedassign
16+
17+
linters-settings:
18+
gocyclo:
19+
min-complexity: 12
20+
gofumpt:
21+
extra-rules: true
22+
govet:
23+
enable-all: true
24+
disable:
25+
- fieldalignment

pkg/provider/github.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (repo *GitHubRepository) Init(config map[string]string) error {
5353

5454
oauthClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))
5555
if gheHost != "" {
56-
gheUrl := fmt.Sprintf("https://%s/api/v3/", gheHost)
57-
rClient, err := github.NewEnterpriseClient(gheUrl, gheUrl, oauthClient)
56+
gheURL := fmt.Sprintf("https://%s/api/v3/", gheHost)
57+
rClient, err := github.NewEnterpriseClient(gheURL, gheURL, oauthClient)
5858
if err != nil {
5959
return err
6060
}
@@ -131,6 +131,7 @@ func (repo *GitHubRepository) GetCommits(fromSha, toSha string) ([]*semrel.RawCo
131131
return allCommits, nil
132132
}
133133

134+
//gocyclo:ignore
134135
func (repo *GitHubRepository) GetReleases(rawRe string) ([]*semrel.Release, error) {
135136
re := regexp.MustCompile(rawRe)
136137
allReleases := make([]*semrel.Release, 0)

pkg/provider/github_test.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ func createGithubCommit(sha, message string) *github.RepositoryCommit {
4545
return &github.RepositoryCommit{SHA: &sha, Commit: &github.Commit{Message: &message}}
4646
}
4747

48-
var commitType = "commit"
49-
var tagType = "tag"
48+
var (
49+
commitType = "commit"
50+
tagType = "tag"
51+
)
5052

5153
func createGithubRef(ref, sha string) *github.Reference {
5254
return &github.Reference{Ref: &ref, Object: &github.GitObject{SHA: &sha, Type: &commitType}}
@@ -57,19 +59,19 @@ func createGithubRefWithTag(ref, sha string) *github.Reference {
5759
}
5860

5961
var (
60-
GITHUB_REPO_PRIVATE = true
61-
GITHUB_DEFAULTBRANCH = "master"
62-
GITHUB_REPO_NAME = "test-repo"
63-
GITHUB_OWNER_LOGIN = "owner"
64-
GITHUB_REPO = github.Repository{
65-
DefaultBranch: &GITHUB_DEFAULTBRANCH,
66-
Private: &GITHUB_REPO_PRIVATE,
62+
githubRepoPrivate = true
63+
githubDefaultBranch = "master"
64+
githubRepoName = "test-repo"
65+
githubOwnerLogin = "owner"
66+
githubRepo = github.Repository{
67+
DefaultBranch: &githubDefaultBranch,
68+
Private: &githubRepoPrivate,
6769
Owner: &github.User{
68-
Login: &GITHUB_OWNER_LOGIN,
70+
Login: &githubOwnerLogin,
6971
},
70-
Name: &GITHUB_REPO_NAME,
72+
Name: &githubRepoName,
7173
}
72-
GITHUB_COMMITS = []*github.RepositoryCommit{
74+
githubCommits = []*github.RepositoryCommit{
7375
createGithubCommit("abcd", "feat(app): new new feature"),
7476
createGithubCommit("1111", "feat: to"),
7577
createGithubCommit("abcd", "feat(app): new feature"),
@@ -79,62 +81,64 @@ var (
7981
createGithubCommit("2222", "feat: from"),
8082
createGithubCommit("beef", "fix: test"),
8183
}
82-
GITHUB_TAGS = []*github.Reference{
84+
githubTags = []*github.Reference{
8385
createGithubRef("refs/tags/test-tag", "deadbeef"),
84-
createGithubRef("refs/tags/v1.0.0", "deadbeef"),
86+
createGithubRef("refs/tags/v1.0.0", "beefdead"),
8587
createGithubRef("refs/tags/v2.0.0", "deadbeef"),
86-
createGithubRef("refs/tags/v2.1.0-beta", "deadbeef"),
88+
createGithubRef("refs/tags/v2.1.0-beta", "beefdead"),
8789
createGithubRef("refs/tags/v3.0.0-beta.2", "deadbeef"),
88-
createGithubRef("refs/tags/v3.0.0-beta.1", "deadbeef"),
90+
createGithubRef("refs/tags/v3.0.0-beta.1", "beefdead"),
8991
createGithubRef("refs/tags/2020.04.19", "deadbeef"),
9092
createGithubRefWithTag("refs/tags/v1.1.1", "12345678"),
9193
}
9294
)
9395

9496
//nolint:errcheck
97+
//gocyclo:ignore
9598
func githubHandler(w http.ResponseWriter, r *http.Request) {
9699
if r.Header.Get("Authorization") != "Bearer token" {
97100
http.Error(w, "unauthorized", http.StatusUnauthorized)
98101
return
99102
}
100-
if r.Method == "GET" && r.URL.Path == "/repos/owner/test-repo" {
101-
json.NewEncoder(w).Encode(GITHUB_REPO)
103+
if r.Method == http.MethodGet && r.URL.Path == "/repos/owner/test-repo" {
104+
json.NewEncoder(w).Encode(githubRepo)
102105
return
103106
}
104-
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, "/repos/owner/test-repo/compare/") {
107+
108+
if r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/owner/test-repo/compare/") {
105109
li := strings.LastIndex(r.URL.Path, "/")
106110
shaRange := strings.Split(r.URL.Path[li+1:], "...")
107111
fromSha := shaRange[0]
108112
toSha := shaRange[1]
109113
start := 0
110114
end := 0
111-
for i, commit := range GITHUB_COMMITS {
115+
for i, commit := range githubCommits {
112116
if commit.GetSHA() == toSha {
113117
start = i
114118
} else if commit.GetSHA() == fromSha {
115119
end = i
116120
}
117121
}
118-
json.NewEncoder(w).Encode(github.CommitsComparison{Commits: GITHUB_COMMITS[start:end]})
122+
json.NewEncoder(w).Encode(github.CommitsComparison{Commits: githubCommits[start:end]})
119123
return
120124
}
121-
if r.Method == "GET" && r.URL.Path == "/repos/owner/test-repo/commits" {
125+
if r.Method == http.MethodGet && r.URL.Path == "/repos/owner/test-repo/commits" {
122126
toSha := r.URL.Query().Get("sha")
123127
skip := 0
124-
for i, commit := range GITHUB_COMMITS {
128+
for i, commit := range githubCommits {
125129
if commit.GetSHA() == toSha {
126130
skip = i
127131
break
128132
}
129133
}
130-
json.NewEncoder(w).Encode(GITHUB_COMMITS[skip:])
134+
json.NewEncoder(w).Encode(githubCommits[skip:])
131135
return
132136
}
133-
if r.Method == "GET" && r.URL.Path == "/repos/owner/test-repo/git/matching-refs/tags" {
134-
json.NewEncoder(w).Encode(GITHUB_TAGS)
137+
if r.Method == http.MethodGet && r.URL.Path == "/repos/owner/test-repo/git/matching-refs/tags" {
138+
json.NewEncoder(w).Encode(githubTags)
135139
return
136140
}
137-
if r.Method == "POST" && r.URL.Path == "/repos/owner/test-repo/git/refs" {
141+
if r.Method == http.MethodPost && r.URL.Path == "/repos/owner/test-repo/git/refs" {
138142
var data map[string]string
139143
json.NewDecoder(r.Body).Decode(&data)
140144
r.Body.Close()
@@ -145,7 +149,7 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
145149
fmt.Fprint(w, "{}")
146150
return
147151
}
148-
if r.Method == "POST" && r.URL.Path == "/repos/owner/test-repo/releases" {
152+
if r.Method == http.MethodPost && r.URL.Path == "/repos/owner/test-repo/releases" {
149153
var data map[string]string
150154
json.NewDecoder(r.Body).Decode(&data)
151155
r.Body.Close()
@@ -156,7 +160,7 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
156160
fmt.Fprint(w, "{}")
157161
return
158162
}
159-
if r.Method == "GET" && r.URL.Path == "/repos/owner/test-repo/git/tags/12345678" {
163+
if r.Method == http.MethodGet && r.URL.Path == "/repos/owner/test-repo/git/tags/12345678" {
160164
sha := "deadbeef"
161165
json.NewEncoder(w).Encode(github.Tag{
162166
Object: &github.GitObject{SHA: &sha, Type: &commitType},
@@ -183,9 +187,9 @@ func TestGithubGetInfo(t *testing.T) {
183187
defer ts.Close()
184188
repoInfo, err := repo.GetInfo()
185189
require.NoError(t, err)
186-
require.Equal(t, GITHUB_DEFAULTBRANCH, repoInfo.DefaultBranch)
187-
require.Equal(t, GITHUB_OWNER_LOGIN, repoInfo.Owner)
188-
require.Equal(t, GITHUB_REPO_NAME, repoInfo.Repo)
190+
require.Equal(t, githubDefaultBranch, repoInfo.DefaultBranch)
191+
require.Equal(t, githubOwnerLogin, repoInfo.Owner)
192+
require.Equal(t, githubRepoName, repoInfo.Repo)
189193
require.True(t, repoInfo.Private)
190194
}
191195

@@ -198,8 +202,8 @@ func TestGithubGetCommits(t *testing.T) {
198202

199203
for i, c := range commits {
200204
idxOff := i + 1
201-
require.Equal(t, c.SHA, GITHUB_COMMITS[idxOff].GetSHA())
202-
require.Equal(t, c.RawMessage, GITHUB_COMMITS[idxOff].Commit.GetMessage())
205+
require.Equal(t, c.SHA, githubCommits[idxOff].GetSHA())
206+
require.Equal(t, c.RawMessage, githubCommits[idxOff].Commit.GetMessage())
203207
}
204208
}
205209

@@ -213,8 +217,8 @@ func TestGithubGetCommitsWithCompare(t *testing.T) {
213217

214218
for i, c := range commits {
215219
idxOff := i + 1
216-
require.Equal(t, c.SHA, GITHUB_COMMITS[idxOff].GetSHA())
217-
require.Equal(t, c.RawMessage, GITHUB_COMMITS[idxOff].Commit.GetMessage())
220+
require.Equal(t, c.SHA, githubCommits[idxOff].GetSHA())
221+
require.Equal(t, c.RawMessage, githubCommits[idxOff].Commit.GetMessage())
218222
}
219223
}
220224

0 commit comments

Comments
 (0)