Skip to content

Commit c1bbcdb

Browse files
authored
Enable golangci-lint checks which were not running (#431)
* Bump reviewdog golangci-lint action major version This is a necessary precursor to bumping the Go version from 1.21 to 1.22, which will be done in separate PR. * Remove deprecated deadcode linter See golangci/golangci-lint#3125 * Add missing period at end of comment As warned by golangci-lint. * Remove unnecessary trailing whitespace As per golangci-lint warning. * Change case of variable to mixed not snake As per golangci-lint warning. * Fix cuddled statements warned by golangci-lint * Fix return with no blank line before lint warnings * Fix unchecked error return value linting warnings * Exclude line from line length linter warning * Format files with gofumpt to fix linting warnings * Exclude file permissions line from gosec linting The [gosec linter][1] warns by default on [file permissions above 0600][2]. We need the permissions to be 0644 for this line (because it has to be written to), so we exclude it from linting. [1]: https://github.com/securego/gosec [2]: securego/gosec#107 * Remove depguard from Go linting Created #430 to consider reinstating it in the future. * Fix magic number warning by extracting constant * Fix cuddling linter warnings * Add required comment to exported function * Do not use deprecated ioutil package * Remove unused function
1 parent f889af1 commit c1bbcdb

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

.github/workflows/reviewdog.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ubuntu-22.04
1717
steps:
1818
- uses: actions/checkout@v4
19-
- uses: reviewdog/action-golangci-lint@v1
19+
- uses: reviewdog/action-golangci-lint@v2
2020
with:
2121
github_token: ${{ secrets.github_token }}
2222
golangci_lint_flags: "-c .golangci.yml"

.golangci.yml

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ linters:
1414
# enable-all is deprecated, so enable linters individually
1515
enable:
1616
- bodyclose
17-
- deadcode
18-
- depguard
1917
- dogsled
2018
- dupl
2119
- errcheck

internal/github/action.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
"os"
1312
"path/filepath"
1413
"strings"
@@ -17,7 +16,7 @@ import (
1716
"github.com/agilepathway/label-checker/internal/github/pullrequest"
1817
)
1918

20-
// Action encapsulates the Label Checker GitHub Action
19+
// Action encapsulates the Label Checker GitHub Action.
2120
type Action struct {
2221
Stdout io.Writer // writer to write stdout messages to
2322
Stderr io.Writer // writer to write stderr messages to
@@ -55,6 +54,7 @@ func (a *Action) CheckLabels(stdout, stderr io.Writer) int {
5554
}
5655

5756
a.outputResult("success")
57+
5858
return 0
5959
}
6060

@@ -64,9 +64,11 @@ func (a *Action) handleFailure() int {
6464
a.outputResult("failure")
6565
err := errors.New(a.trimTrailingNewLine(a.failMsg))
6666
fmt.Fprintln(a.Stderr, "::error::", err)
67+
6768
if a.allowFailure() {
6869
return 0
6970
}
71+
7072
return 1
7173
}
7274

@@ -76,23 +78,24 @@ func (a *Action) trimTrailingNewLine(input string) string {
7678

7779
type check func([]string, bool) (bool, string)
7880

79-
type specified func() []string
80-
8181
func (a *Action) runCheck(chk check, specified []string, prefixMode bool) {
8282
if len(specified) == 0 {
8383
return
8484
}
85+
8586
if prefixMode && len(specified) > 1 {
8687
a.failMsg += "Currently the label checker only supports checking with one prefix, not multiple."
88+
8789
return
8890
}
91+
8992
valid, message := chk(specified, prefixMode)
93+
9094
if valid {
9195
a.successMsg += message + "\n"
9296
} else {
9397
a.failMsg += message + "\n"
9498
}
95-
9699
}
97100

98101
func (a *Action) repositoryOwner() string {
@@ -112,23 +115,30 @@ func (a *Action) pullRequestNumber() int {
112115
githubEventJSONFile, err := os.Open(filepath.Clean(os.Getenv("GITHUB_EVENT_PATH")))
113116
panic.IfError(err)
114117
defer githubEventJSONFile.Close() //nolint
115-
byteValue, _ := ioutil.ReadAll(githubEventJSONFile)
118+
byteValue, _ := io.ReadAll(githubEventJSONFile)
116119
panic.IfError(json.Unmarshal(byteValue, &event))
117120

118121
return event.PullRequest.Number
119122
}
120123

121124
func (a *Action) outputResult(result string) {
122-
label_check_output := fmt.Sprintf("label_check=%s", result)
125+
const UserReadWriteFilePermission = 0o644
126+
127+
labelCheckOutput := fmt.Sprintf("label_check=%s", result)
123128
gitHubOutputFileName := filepath.Clean(os.Getenv("GITHUB_OUTPUT"))
124-
githubOutputFile, err := os.OpenFile(gitHubOutputFileName, os.O_APPEND|os.O_WRONLY, 0644)
129+
githubOutputFile, err := os.OpenFile(gitHubOutputFileName, os.O_APPEND|os.O_WRONLY, UserReadWriteFilePermission) //nolint:gosec,lll
125130
panic.IfError(err)
126-
_, err = githubOutputFile.WriteString(label_check_output)
131+
_, err = githubOutputFile.WriteString(labelCheckOutput)
132+
127133
if err != nil {
128-
githubOutputFile.Close()
134+
closingErr := githubOutputFile.Close()
135+
129136
panic.IfError(err)
137+
panic.IfError(closingErr)
130138
}
131-
githubOutputFile.Close()
139+
140+
err = githubOutputFile.Close()
141+
panic.IfError(err)
132142
}
133143

134144
func (a *Action) token() string {

internal/github/action_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ import (
1414
"github.com/agilepathway/label-checker/internal/error/panic"
1515
)
1616

17-
//nolint: gochecknoglobals
17+
//nolint:gochecknoglobals
1818
var integration = flag.Bool(
1919
"integration",
2020
false,
2121
"Make calls to real external services. Requires INPUT_REPO_TOKEN environment variable.")
2222

23-
//nolint: gochecknoglobals
23+
//nolint:gochecknoglobals
2424
var enterpriseCloud = flag.Bool(
2525
"enterprise-cloud",
2626
false,
2727
"Run the label checker against GitHub Enterprise Cloud instead of standard GitHub")
2828

29-
//nolint: gochecknoglobals
29+
//nolint:gochecknoglobals
3030
var enterpriseServer = flag.Bool(
3131
"enterprise-server",
3232
false,
3333
"Run the label checker against GitHub Enterprise Server instead of standard GitHub")
3434

35-
// nolint: lll
35+
//nolint:lll
3636
const (
3737
EnvGitHubRepository = "GITHUB_REPOSITORY"
3838
EnvGitHubEventPath = "GITHUB_EVENT_PATH"
@@ -91,7 +91,7 @@ const (
9191

9292
type specifyChecks func()
9393

94-
// nolint: lll, funlen, dupl
94+
//nolint:lll,funlen,dupl
9595
func TestLabelChecks(t *testing.T) {
9696
tests := map[string]struct {
9797
prNumber int
@@ -155,6 +155,7 @@ func TestLabelChecks(t *testing.T) {
155155
if len(tc.expectedStderr) > 0 {
156156
tc.expectedStderr = "::error:: " + tc.expectedStderr
157157
}
158+
158159
setPullRequestNumber(tc.prNumber)
159160
setPrefixMode(tc.prefixMode)
160161
tc.specifyChecks()
@@ -269,6 +270,7 @@ func checkLabels() (int, *bytes.Buffer, *bytes.Buffer) {
269270
stdout := &bytes.Buffer{}
270271
stderr := &bytes.Buffer{}
271272
a := Action{}
273+
272274
return a.CheckLabels(stdout, stderr), stdout, stderr
273275
}
274276

internal/github/pullrequest/labels.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ func (l Labels) HasNoneOf(specified []string, prefixMode bool) (bool, string) {
2727
// all of the specified labels, along with a report describing the result.
2828
func (l Labels) HasAllOf(specified []string, prefixMode bool) (bool, string) {
2929
if prefixMode {
30-
return false, "The label checker does not support prefix checking with `all_of`, as that is not a logical combination."
30+
return false, "The label checker does not support prefix checking with `all_of`, as that is not a logical combination." //nolint:lll
3131
}
32+
3233
return l.hasXof(specified, "all", prefixMode)
3334
}
3435

internal/slice/slice.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func Contains(slice []string, val string) bool {
1818
return false
1919
}
2020

21+
// StartsWithAnyOf returns true if the given slice starts with any of the given prefixes
2122
func StartsWithAnyOf(prefixes []string, candidate string) bool {
2223
for _, prefix := range prefixes {
2324
if strings.HasPrefix(candidate, prefix) {

0 commit comments

Comments
 (0)