Skip to content

Commit d7cb433

Browse files
committed
Add Linting
1 parent aa9b0e7 commit d7cb433

File tree

5 files changed

+84
-31
lines changed

5 files changed

+84
-31
lines changed

.editorconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.go]
12+
indent_style = tab
13+
indent_size = 4
14+
15+
[Makefile]
16+
indent_style = tab
17+
indent_size = 4
18+
19+
[*.mod]
20+
indent_style = tab
21+
indent_size = 4

.github/workflows/build.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ jobs:
2626
with:
2727
go-version-file: "go.mod"
2828

29+
- name: Lint
30+
uses: golangci/golangci-lint-action@v7
31+
with:
32+
version: latest
33+
2934
- name: Test
3035
run: go test ./...
3136

@@ -59,6 +64,7 @@ jobs:
5964
uses: docker/setup-buildx-action@v3
6065

6166
- name: Login to DockerHub
67+
if: github.event_name == 'release' && github.event.action == 'published'
6268
uses: docker/login-action@v3
6369
with:
6470
username: ${{ vars.DOCKERHUB_USERNAME }}

.golangci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: "2"
2+
3+
run:
4+
timeout: 10m
5+
modules-download-mode: readonly
6+
7+
formatters:
8+
enable:
9+
- gofmt
10+
- goimports
11+
settings:
12+
goimports:
13+
local-prefixes:
14+
- github.com/Staffbase/yamllint-action
15+
16+
linters:
17+
default: none
18+
enable:
19+
- bodyclose
20+
- gosec
21+
- govet
22+
- ineffassign
23+
- noctx
24+
- staticcheck
25+
- unused
26+
- whitespace

yamllint-action.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func parseInput(r io.Reader) Report {
108108
Severity: severity,
109109
}
110110

111-
if _, exist := files[fileName]; exist == false {
111+
if _, exist := files[fileName]; !exist {
112112
files[fileName] = &LinterResult{FilePath: fileName}
113113
}
114114

@@ -156,15 +156,15 @@ func handlePush(ctx context.Context, client *github.Client, report Report) error
156156
// find the action's checkrun
157157
checkName := os.Getenv("ACTION_NAME")
158158
result, _, err := client.Checks.ListCheckRunsForRef(ctx, owner, repoName, head, &github.ListCheckRunsOptions{
159-
CheckName: github.String(checkName),
160-
Status: github.String("in_progress"),
159+
CheckName: github.Ptr(checkName),
160+
Status: github.Ptr("in_progress"),
161161
})
162162
if err != nil {
163163
return err
164164
}
165165

166166
if len(result.CheckRuns) == 0 {
167-
return fmt.Errorf("Unable to find check run for action: %s", checkName)
167+
return fmt.Errorf("unable to find check run for action: %s", checkName)
168168
}
169169
checkRun := result.CheckRuns[0]
170170

@@ -177,12 +177,12 @@ func handlePush(ctx context.Context, client *github.Client, report Report) error
177177
if len(t.AssertionResults) > 0 {
178178
for _, a := range t.AssertionResults {
179179
annotations = append(annotations, &github.CheckRunAnnotation{
180-
Path: github.String(path),
181-
StartLine: github.Int(a.Line),
182-
EndLine: github.Int(a.Line),
183-
AnnotationLevel: github.String(a.Severity),
184-
Title: github.String(""),
185-
Message: github.String(a.Message),
180+
Path: github.Ptr(path),
181+
StartLine: github.Ptr(a.Line),
182+
EndLine: github.Ptr(a.Line),
183+
AnnotationLevel: github.Ptr(a.Severity),
184+
Title: github.Ptr(""),
185+
Message: github.Ptr(a.Message),
186186
})
187187
}
188188
}
@@ -202,8 +202,8 @@ func handlePush(ctx context.Context, client *github.Client, report Report) error
202202
}
203203

204204
output := &github.CheckRunOutput{
205-
Title: github.String("Result"),
206-
Summary: github.String(summary),
205+
Title: github.Ptr("Result"),
206+
Summary: github.Ptr(summary),
207207
Annotations: annotations[i:end],
208208
}
209209

yamllint-action_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ limitations under the License.
1414
package main
1515

1616
import (
17-
"strings"
18-
"testing"
17+
"strings"
18+
"testing"
1919
)
2020

2121
func TestParseInput(t *testing.T) {
22-
t.Run("standard case", func(t *testing.T) {
23-
testData := `sources/prod/testapp-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation)
22+
t.Run("standard case", func(t *testing.T) {
23+
testData := `sources/prod/testapp-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation)
2424
sources/prod/testapp2-deploy.yaml:16:11: [warning] wrong indentation: expected 12 but found 10 (indentation)
2525
sources/prod/testapp3-deploy.yaml:16:11: [warning] wrong indentation: expected 12 but found 10 (indentation)
2626
sources/prod/testapp4-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation)
@@ -31,21 +31,21 @@ sources/stage/testapp4-deploy.yaml:17:11: [warning] wrong indentation: expected
3131
sources/dev/testapp-deploy.yaml:17:11: [warning] wrong indentation: expected 12 but found 10 (indentation)
3232
sources/dev/testapp2-deploy.yaml:16:11: [warning] wrong indentation: expected 12 but found 10 (indentation)`
3333

34-
report := parseInput(strings.NewReader(testData))
35-
if report.NumFailedLines != 10 {
36-
t.Errorf("unexpected amount of failed lines: got %d, wanted %d", report.NumFailedLines, 10)
37-
}
34+
report := parseInput(strings.NewReader(testData))
35+
if report.NumFailedLines != 10 {
36+
t.Errorf("unexpected amount of failed lines: got %d, wanted %d", report.NumFailedLines, 10)
37+
}
3838

39-
if report.LinterResults[0].AssertionResults[0].Severity != "warning" {
40-
t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[0].AssertionResults[0].Severity, "warning")
41-
}
42-
43-
if report.LinterResults[6].AssertionResults[0].Severity != "failure" {
44-
t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Severity, "failure")
45-
}
39+
if report.LinterResults[0].AssertionResults[0].Severity != "warning" {
40+
t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[0].AssertionResults[0].Severity, "warning")
41+
}
4642

47-
if report.LinterResults[6].AssertionResults[0].Message != "wrong indentation: expected 12 but found 10 (indentation)" {
48-
t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Message, "wrong indentation: expected 12 but found 10 (indentation)")
49-
}
50-
})
43+
if report.LinterResults[6].AssertionResults[0].Severity != "failure" {
44+
t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Severity, "failure")
45+
}
46+
47+
if report.LinterResults[6].AssertionResults[0].Message != "wrong indentation: expected 12 but found 10 (indentation)" {
48+
t.Errorf("unexpected severity found: got %s, wanted %s", report.LinterResults[4].AssertionResults[0].Message, "wrong indentation: expected 12 but found 10 (indentation)")
49+
}
50+
})
5151
}

0 commit comments

Comments
 (0)