feat: add golangci-lint and restore megalinter config (#22)

* feat: add golangci-lint and restore megalinter config

* chore: fix linting, go mod tidy

* fix(ci): add golangci-lint action setup to test.yml

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore: tweaks

* fix(ci): tweaks

* fix(ci): disable go_golangci_lint in megalinter

* chore: yamllint rules

* chore(ci): tweak yml

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
2025-07-15 17:49:15 +03:00
committed by GitHub
parent a09f3e358b
commit ffb5da2a8e
12 changed files with 95 additions and 98 deletions

8
.github/README.md vendored
View File

@@ -73,6 +73,7 @@ Clone the repository and build the project:
git clone https://github.com/ivuorinen/go-test-sarif-action.git git clone https://github.com/ivuorinen/go-test-sarif-action.git
cd go-test-sarif-action cd go-test-sarif-action
go build -o go-test-sarif ./cmd/main.go go build -o go-test-sarif ./cmd/main.go
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
``` ```
Run tests: Run tests:
@@ -81,6 +82,13 @@ Run tests:
go test ./... go test ./...
``` ```
Run linting:
```sh
go vet ./...
golangci-lint run
```
## 📄 License ## 📄 License
This project is licensed under the **MIT License**. This project is licensed under the **MIT License**.

View File

@@ -1,3 +1,4 @@
---
name: Run Go Tests and Generate SARIF name: Run Go Tests and Generate SARIF
on: on:
@@ -23,6 +24,15 @@ jobs:
with: with:
go-version-file: "go.mod" go-version-file: "go.mod"
- name: Install golangci-lint
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
with:
version: latest
- name: Run Linters
run: |
go vet ./...
golangci-lint run
- name: Run Go Tests - name: Run Go Tests
shell: bash shell: bash
run: go test -json ./... > go-test-results.json run: go test -json ./... > go-test-results.json

8
.golangci.yml Normal file
View File

@@ -0,0 +1,8 @@
version: "2"
run:
timeout: 5m
linters:
enable:
- errcheck
- govet
- staticcheck

View File

@@ -17,4 +17,5 @@ SHOW_SKIPPED_LINTERS: false # Show skipped linters in MegaLinter log
DISABLE_LINTERS: DISABLE_LINTERS:
- REPOSITORY_DEVSKIM - REPOSITORY_DEVSKIM
- GO_GOLANGCI_LINT # old go version, fails always

11
.yamlfmt.yml Normal file
View File

@@ -0,0 +1,11 @@
---
# yamlfmt configuration file
# Schema: https://raw.githubusercontent.com/google/yamlfmt/main/schema.json
formatter:
type: basic
include_document_start: true
gitignore_excludes: true
retain_line_breaks_single: true
eof_newline: true
max_line_length: 120
indent: 2

0
.yamlignore Normal file
View File

20
.yamllint.yml Normal file
View File

@@ -0,0 +1,20 @@
---
extends: relaxed
yaml-files:
- '*.yaml'
- '*.yml'
- '.yamllint'
ignore-from-file: .gitignore
rules:
empty-lines:
max: 1
max-start: 0
max-end: 1
line-length:
max: 100
allow-non-breakable-words: true
allow-non-breakable-inline-mappings: false

View File

@@ -7,10 +7,11 @@ src := "./cmd/main.go"
default: default:
just build just build
# Lint with megalinter # Lint Go code
lint: lint:
echo "Linting..." echo "Linting..."
npx --yes mega-linter-runner go vet ./...
golangci-lint run
# Build the Go binary # Build the Go binary
build: build:

7
go.mod
View File

@@ -2,12 +2,9 @@ module github.com/ivuorinen/go-test-sarif-action
go 1.24.1 go 1.24.1
require ( require github.com/owenrumney/go-sarif/v2 v2.3.3
github.com/owenrumney/go-sarif/v2 v2.3.3
github.com/owenrumney/go-sarif/v3 v3.2.1
)
require gopkg.in/yaml.v3 v3.0.1 // indirect require github.com/stretchr/testify v1.10.0 // indirect
replace golang.org/x/crypto => golang.org/x/crypto v0.40.0 replace golang.org/x/crypto => golang.org/x/crypto v0.40.0

5
go.sum
View File

@@ -4,6 +4,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -11,12 +12,12 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/owenrumney/go-sarif v1.1.1/go.mod h1:dNDiPlF04ESR/6fHlPyq7gHKmrM0sHUvAGjsoh8ZH0U= github.com/owenrumney/go-sarif v1.1.1/go.mod h1:dNDiPlF04ESR/6fHlPyq7gHKmrM0sHUvAGjsoh8ZH0U=
github.com/owenrumney/go-sarif/v2 v2.3.3 h1:ubWDJcF5i3L/EIOER+ZyQ03IfplbSU1BLOE26uKQIIU= github.com/owenrumney/go-sarif/v2 v2.3.3 h1:ubWDJcF5i3L/EIOER+ZyQ03IfplbSU1BLOE26uKQIIU=
github.com/owenrumney/go-sarif/v2 v2.3.3/go.mod h1:MSqMMx9WqlBSY7pXoOZWgEsVB4FDNfhcaXDA1j6Sr+w= github.com/owenrumney/go-sarif/v2 v2.3.3/go.mod h1:MSqMMx9WqlBSY7pXoOZWgEsVB4FDNfhcaXDA1j6Sr+w=
github.com/owenrumney/go-sarif/v3 v3.2.1/go.mod h1:S2sdyDnv0sxN5x+M8iFZIzZE2+uTX/1uXlwTRx0efT0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4= github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

View File

@@ -24,7 +24,11 @@ func ConvertToSARIF(inputFile, outputFile string) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to read input file: %w", err) return fmt.Errorf("failed to read input file: %w", err)
} }
defer f.Close() defer func() {
if cerr := f.Close(); cerr != nil {
fmt.Fprintf(os.Stderr, "failed to close input file: %v\n", cerr)
}
}()
report, err := sarif.New(sarif.Version210) report, err := sarif.New(sarif.Version210)
if err != nil { if err != nil {

View File

@@ -2,147 +2,83 @@ package internal
import ( import (
"os" "os"
"path/filepath"
"testing" "testing"
) )
// TestConvertToSARIF_Success tests the successful conversion of a valid Go test JSON output to SARIF format. // TestConvertToSARIF_Success tests the successful conversion of a valid Go test JSON output to SARIF format.
func TestConvertToSARIF_Success(t *testing.T) { func TestConvertToSARIF_Success(t *testing.T) {
// Create a temporary JSON input file with valid test data dir := t.TempDir()
inputFile, err := os.CreateTemp("", "test_input_*.json")
if err != nil {
t.Fatalf("Failed to create temp input file: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp input file: %v", err)
}
}(inputFile.Name())
inputPath := filepath.Join(dir, "input.json")
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":"Test failed"}` + "\n" inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":"Test failed"}` + "\n"
if _, err := inputFile.WriteString(inputContent); err != nil { if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err) t.Fatalf("Failed to write input file: %v", err)
} }
// Create a temporary SARIF output file outputPath := filepath.Join(dir, "output.sarif")
outputFile, err := os.CreateTemp("", "test_output_*.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp output file: %v", err)
}
}(outputFile.Name())
// Run the ConvertToSARIF function if err := ConvertToSARIF(inputPath, outputPath); err != nil {
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
if err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err) t.Errorf("ConvertToSARIF returned an error: %v", err)
} }
// Read and validate the SARIF output outputContent, err := os.ReadFile(outputPath)
outputContent, err := os.ReadFile(outputFile.Name())
if err != nil { if err != nil {
t.Fatalf("Failed to read SARIF output file: %v", err) t.Fatalf("Failed to read SARIF output file: %v", err)
} }
// Perform basic validation on the SARIF output
if len(outputContent) == 0 { if len(outputContent) == 0 {
t.Errorf("SARIF output is empty") t.Errorf("SARIF output is empty")
} }
// Additional validations can be added here to verify the correctness of the SARIF content
} }
// TestConvertToSARIF_InvalidInput tests the function's behavior when provided with invalid JSON input. // TestConvertToSARIF_InvalidInput tests the function's behavior when provided with invalid JSON input.
func TestConvertToSARIF_InvalidInput(t *testing.T) { func TestConvertToSARIF_InvalidInput(t *testing.T) {
// Create a temporary JSON input file with invalid test data dir := t.TempDir()
inputFile, err := os.CreateTemp("", "test_input_invalid_*.json")
if err != nil {
t.Fatalf("Failed to create temp input file: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp input file: %v", err)
}
}(inputFile.Name())
inputPath := filepath.Join(dir, "invalid.json")
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":` + inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif/internal","Test":"TestExample","Output":` +
`Test failed}` + "\n" // Missing quotes around 'Test failed' `Test failed}` + "\n" // Missing quotes around 'Test failed'
if _, err := inputFile.WriteString(inputContent); err != nil { if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err) t.Fatalf("Failed to write input file: %v", err)
} }
// Create a temporary SARIF output file outputPath := filepath.Join(dir, "output.sarif")
outputFile, err := os.CreateTemp("", "test_output_invalid_*.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp output file: %v", err)
}
}(outputFile.Name())
// Run the ConvertToSARIF function if err := ConvertToSARIF(inputPath, outputPath); err == nil {
err = ConvertToSARIF(inputFile.Name(), outputFile.Name())
if err == nil {
t.Errorf("Expected an error for invalid JSON input, but got none") t.Errorf("Expected an error for invalid JSON input, but got none")
} }
} }
// TestConvertToSARIF_FileNotFound tests the function's behavior when the input file does not exist. // TestConvertToSARIF_FileNotFound tests the function's behavior when the input file does not exist.
func TestConvertToSARIF_FileNotFound(t *testing.T) { func TestConvertToSARIF_FileNotFound(t *testing.T) {
// Define a non-existent input file path
inputFile := "non_existent_file.json" inputFile := "non_existent_file.json"
// Create a temporary SARIF output file dir := t.TempDir()
outputFile, err := os.CreateTemp("", "test_output_notfound_*.sarif") outputPath := filepath.Join(dir, "output.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatalf("Failed to remove temp output file: %v", err)
}
}(outputFile.Name())
// Run the ConvertToSARIF function if err := ConvertToSARIF(inputFile, outputPath); err == nil {
err = ConvertToSARIF(inputFile, outputFile.Name())
if err == nil {
t.Errorf("Expected an error for non-existent input file, but got none") t.Errorf("Expected an error for non-existent input file, but got none")
} }
} }
// TestConvertToSARIF_PackageFailure ensures package-level failures are included in the SARIF output. // TestConvertToSARIF_PackageFailure ensures package-level failures are included in the SARIF output.
func TestConvertToSARIF_PackageFailure(t *testing.T) { func TestConvertToSARIF_PackageFailure(t *testing.T) {
inputFile, err := os.CreateTemp("", "test_input_pkgfail_*.json") dir := t.TempDir()
if err != nil {
t.Fatalf("Failed to create temp input file: %v", err)
}
defer os.Remove(inputFile.Name())
inputPath := filepath.Join(dir, "input.json")
inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif-action","Output":"FAIL"}` + "\n" inputContent := `{"Action":"fail","Package":"github.com/ivuorinen/go-test-sarif-action","Output":"FAIL"}` + "\n"
if _, err := inputFile.WriteString(inputContent); err != nil { if err := os.WriteFile(inputPath, []byte(inputContent), 0o600); err != nil {
t.Fatalf("Failed to write to temp input file: %v", err) t.Fatalf("Failed to write input file: %v", err)
} }
outputFile, err := os.CreateTemp("", "test_output_pkgfail_*.sarif") outputPath := filepath.Join(dir, "output.sarif")
if err != nil {
t.Fatalf("Failed to create temp output file: %v", err)
}
defer os.Remove(outputFile.Name())
if err := ConvertToSARIF(inputFile.Name(), outputFile.Name()); err != nil { if err := ConvertToSARIF(inputPath, outputPath); err != nil {
t.Errorf("ConvertToSARIF returned an error: %v", err) t.Errorf("ConvertToSARIF returned an error: %v", err)
} }
data, err := os.ReadFile(outputFile.Name()) data, err := os.ReadFile(outputPath)
if err != nil { if err != nil {
t.Fatalf("Failed to read SARIF output file: %v", err) t.Fatalf("Failed to read SARIF output file: %v", err)
} }