Skip to content

Commit 8dccb2b

Browse files
authored
chore(lint): linting the codebase (runatlantis#4668)
Signed-off-by: Rui Chen <[email protected]>
1 parent ca113f1 commit 8dccb2b

File tree

14 files changed

+59
-40
lines changed

14 files changed

+59
-40
lines changed

.gitattributes

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
11
# Set the default behavior, in case people don't have core.autocrlf set.
2-
* text=auto
3-
4-
# Explicitly declare text files you want to always be normalized and converted
5-
# to native line endings on checkout.
6-
*.go text
7-
*.json text
8-
*.yml text
9-
*.yaml text
10-
*.sh text
11-
*.tf text
2+
* text=auto eol=lf

.github/renovate.json5

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@
104104
{
105105
customType: 'regex',
106106
fileMatch: [
107-
'.circleci/config.yml$',
108107
'^\\.github/workflows/[^/]+\\.ya?ml$',
108+
'Makefile$',
109109
],
110110
matchStrings: [
111111
'renovate: datasource=(?<datasource>.*?) depName=(?<depName>.*?)( versioning=(?<versioning>.*?))?\\s.*?_VERSION: (?<currentValue>.*)\\s',

.github/workflows/lint.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ concurrency:
1515
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
1616
cancel-in-progress: true
1717

18+
permissions:
19+
# Required: allow read access to the content for analysis.
20+
contents: read
21+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
22+
pull-requests: read
23+
# Optional: Allow write access to checks to allow the action to annotate code in the PR.
24+
checks: write
25+
1826
jobs:
1927
changes:
2028
outputs:
@@ -47,9 +55,10 @@ jobs:
4755
go-version-file: go.mod
4856

4957
- name: golangci-lint
50-
uses: reviewdog/action-golangci-lint@7708105983c614f7a2725e2172908b7709d1c3e4 # v2
58+
uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 # v6
5159
with:
52-
tool_name: golangci-lint
60+
# renovate: datasource=github-releases depName=golangci/golangci-lint
61+
version: v1.59.1
5362

5463
skip-lint:
5564
needs: [changes]

.golangci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ linters:
1919
- gofmt
2020
- gosec
2121
- gosimple
22+
- govet
2223
- ineffassign
2324
- misspell
2425
- revive
2526
- staticcheck
27+
- testifylint
2628
- typecheck
2729
- unconvert
2830
- unused
29-
- vet
30-
- vetshadow
3131
run:
3232
timeout: 10m

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ IMAGE_NAME := runatlantis/atlantis
66

77
.DEFAULT_GOAL := help
88

9+
# renovate: datasource=github-releases depName=golangci/golangci-lint
10+
GOLANGCI_LINT_VERSION := v1.59.1
11+
912
.PHONY: help
1013
help: ## List targets & descriptions
1114
@cat Makefile* | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@@ -93,7 +96,7 @@ lint: ## Run linter locally
9396

9497
.PHONY: check-lint
9598
check-lint: ## Run linter in CI/CD. If running locally use 'lint'
96-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.49.0
99+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin $(GOLANGCI_LINT_VERSION)
97100
./bin/golangci-lint run -j 4 --timeout 5m
98101

99102
.PHONY: check-fmt

server/controllers/events/events_controller_test.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,10 @@ func TestPost_GitlabCommentNotAllowlisted(t *testing.T) {
246246
w := httptest.NewRecorder()
247247
e.Post(w, req)
248248

249-
Equals(t, http.StatusForbidden, w.Result().StatusCode)
250-
body, _ := io.ReadAll(w.Result().Body)
249+
resp := w.Result()
250+
defer resp.Body.Close()
251+
Equals(t, http.StatusForbidden, resp.StatusCode)
252+
body, _ := io.ReadAll(resp.Body)
251253
exp := "Repo not allowlisted"
252254
Assert(t, strings.Contains(string(body), exp), "exp %q to be contained in %q", exp, string(body))
253255
expRepo, _ := models.NewRepo(models.Gitlab, "gitlabhq/gitlab-test", "https://example.com/gitlabhq/gitlab-test.git", "", "")
@@ -279,8 +281,10 @@ func TestPost_GitlabCommentNotAllowlistedWithSilenceErrors(t *testing.T) {
279281
w := httptest.NewRecorder()
280282
e.Post(w, req)
281283

282-
Equals(t, http.StatusForbidden, w.Result().StatusCode)
283-
body, _ := io.ReadAll(w.Result().Body)
284+
resp := w.Result()
285+
defer resp.Body.Close()
286+
Equals(t, http.StatusForbidden, resp.StatusCode)
287+
body, _ := io.ReadAll(resp.Body)
284288
exp := "Repo not allowlisted"
285289
Assert(t, strings.Contains(string(body), exp), "exp %q to be contained in %q", exp, string(body))
286290
vcsClient.VerifyWasCalled(Never()).CreateComment(Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string]())
@@ -311,8 +315,10 @@ func TestPost_GithubCommentNotAllowlisted(t *testing.T) {
311315
w := httptest.NewRecorder()
312316
e.Post(w, req)
313317

314-
Equals(t, http.StatusForbidden, w.Result().StatusCode)
315-
body, _ := io.ReadAll(w.Result().Body)
318+
resp := w.Result()
319+
defer resp.Body.Close()
320+
Equals(t, http.StatusForbidden, resp.StatusCode)
321+
body, _ := io.ReadAll(resp.Body)
316322
exp := "Repo not allowlisted"
317323
Assert(t, strings.Contains(string(body), exp), "exp %q to be contained in %q", exp, string(body))
318324
expRepo, _ := models.NewRepo(models.Github, "baxterthehacker/public-repo", "https://github.com/baxterthehacker/public-repo.git", "", "")
@@ -345,8 +351,10 @@ func TestPost_GithubCommentNotAllowlistedWithSilenceErrors(t *testing.T) {
345351
w := httptest.NewRecorder()
346352
e.Post(w, req)
347353

348-
Equals(t, http.StatusForbidden, w.Result().StatusCode)
349-
body, _ := io.ReadAll(w.Result().Body)
354+
resp := w.Result()
355+
defer resp.Body.Close()
356+
Equals(t, http.StatusForbidden, resp.StatusCode)
357+
body, _ := io.ReadAll(resp.Body)
350358
exp := "Repo not allowlisted"
351359
Assert(t, strings.Contains(string(body), exp), "exp %q to be contained in %q", exp, string(body))
352360
vcsClient.VerifyWasCalled(Never()).CreateComment(Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string]())

server/core/runtime/show_step_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (p *showStepRunner) Run(ctx command.ProjectContext, _ []string, path string
4949
return "", errors.Wrap(err, "running terraform show")
5050
}
5151

52-
if err := os.WriteFile(showResultFile, []byte(output), os.ModePerm); err != nil {
52+
if err := os.WriteFile(showResultFile, []byte(output), 0600); err != nil {
5353
return "", errors.Wrap(err, "writing terraform show result")
5454
}
5555

server/events/command/name_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/runatlantis/atlantis/server/events/command"
1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestName_TitleString(t *testing.T) {
@@ -182,7 +183,7 @@ func TestParseCommandName(t *testing.T) {
182183
for _, tt := range tests {
183184
t.Run(tt.name, func(t *testing.T) {
184185
got, err := command.ParseCommandName(tt.name)
185-
assert.NoError(t, err)
186+
require.NoError(t, err)
186187
assert.Equal(t, tt.exp, got)
187188
})
188189
}

server/events/modules_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
//go:embed testdata/fs
@@ -20,9 +21,9 @@ func Test_findModuleDependants(t *testing.T) {
2021
autoplanModuleDependants string
2122
}
2223
a, err := fs.Sub(repos, "testdata/fs/repoA")
23-
assert.NoError(t, err)
24+
require.NoError(t, err)
2425
b, err := fs.Sub(repos, "testdata/fs/repoB")
25-
assert.NoError(t, err)
26+
require.NoError(t, err)
2627

2728
tests := []struct {
2829
name string

server/events/repo_branch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ projects:
8080
repo, err := parser.ParseRepoCfg(tmp, global, "github.com/foo/bar", "main")
8181
require.NoError(t, err)
8282

83-
require.Equal(t, 1, len(repo.Projects))
83+
require.Len(t, repo.Projects, 1)
8484

8585
t.Logf("Projects: %+v", repo.Projects)
8686
}

server/events/vcs/bitbucketcloud/client_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
. "github.com/runatlantis/atlantis/testing"
1515
)
1616

17+
const diffstatURL = "/2.0/repositories/owner/repo/pullrequests/1/diffstat"
18+
1719
// Should follow pagination properly.
1820
func TestClient_GetModifiedFilesPagination(t *testing.T) {
1921
logger := logging.NewNoopLogger(t)
@@ -56,12 +58,12 @@ func TestClient_GetModifiedFilesPagination(t *testing.T) {
5658
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5759
switch r.RequestURI {
5860
// The first request should hit this URL.
59-
case "/2.0/repositories/owner/repo/pullrequests/1/diffstat":
60-
resp := firstResp + fmt.Sprintf(`,"next": "%s/2.0/repositories/owner/repo/pullrequests/1/diffstat?page=2"}`, serverURL)
61+
case diffstatURL:
62+
resp := firstResp + fmt.Sprintf(`,"next": "%s%s?page=2"}`, serverURL, diffstatURL)
6163
w.Write([]byte(resp)) // nolint: errcheck
6264
return
6365
// The second should hit this URL.
64-
case "/2.0/repositories/owner/repo/pullrequests/1/diffstat?page=2":
66+
case fmt.Sprintf("%s?page=2", diffstatURL):
6567
w.Write([]byte(secondResp + "}")) // nolint: errcheck
6668
default:
6769
t.Errorf("got unexpected request at %q", r.RequestURI)
@@ -125,7 +127,7 @@ func TestClient_GetModifiedFilesOldNil(t *testing.T) {
125127
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126128
switch r.RequestURI {
127129
// The first request should hit this URL.
128-
case "/2.0/repositories/owner/repo/pullrequests/1/diffstat":
130+
case diffstatURL:
129131
w.Write([]byte(resp)) // nolint: errcheck
130132
return
131133
default:
@@ -322,7 +324,7 @@ func TestClient_PullIsMergeable(t *testing.T) {
322324
t.Run(name, func(t *testing.T) {
323325
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
324326
switch r.RequestURI {
325-
case "/2.0/repositories/owner/repo/pullrequests/1/diffstat":
327+
case diffstatURL:
326328
w.Write([]byte(c.DiffStat)) // nolint: errcheck
327329
return
328330
default:

server/router_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/runatlantis/atlantis/server/events/command"
1212
"github.com/runatlantis/atlantis/server/events/models"
1313
. "github.com/runatlantis/atlantis/testing"
14-
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1515
)
1616

1717
func TestRouter_GenerateLockURL(t *testing.T) {
@@ -107,6 +107,6 @@ func TestGenerateProjectJobURL_ShouldReturnErrorWhenJobIDNotSpecified(t *testing
107107
}
108108
expectedErrString := "no job id in ctx"
109109
gotURL, err := router.GenerateProjectJobURL(ctx)
110-
assert.EqualError(t, err, expectedErrString)
110+
require.EqualError(t, err, expectedErrString)
111111
Equals(t, "", gotURL)
112112
}

server/server_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ func TestHealthz(t *testing.T) {
139139
req, _ := http.NewRequest("GET", "/healthz", bytes.NewBuffer(nil))
140140
w := httptest.NewRecorder()
141141
s.Healthz(w, req)
142-
Equals(t, http.StatusOK, w.Result().StatusCode)
143-
body, _ := io.ReadAll(w.Result().Body)
144-
Equals(t, "application/json", w.Result().Header["Content-Type"][0])
142+
143+
resp := w.Result()
144+
defer resp.Body.Close()
145+
Equals(t, http.StatusOK, resp.StatusCode)
146+
body, _ := io.ReadAll(resp.Body)
147+
Equals(t, "application/json", resp.Header["Content-Type"][0])
145148
Equals(t,
146149
`{
147150
"status": "ok"

server/user_config_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/runatlantis/atlantis/server/logging"
99
. "github.com/runatlantis/atlantis/testing"
1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestUserConfig_ToAllowCommandNames(t *testing.T) {
@@ -61,7 +62,7 @@ func TestUserConfig_ToAllowCommandNames(t *testing.T) {
6162
}
6263
got, err := u.ToAllowCommandNames()
6364
if err != nil {
64-
assert.ErrorContains(t, err, tt.wantErr, "ToAllowCommandNames()")
65+
require.ErrorContains(t, err, tt.wantErr, "ToAllowCommandNames()")
6566
}
6667
assert.Equalf(t, tt.want, got, "ToAllowCommandNames()")
6768
})

0 commit comments

Comments
 (0)