@@ -3,7 +3,6 @@ name: Go Lint
33 push :
44 branches :
55 - pf9-oidc
6- - private/harsh/soc2-scan
76 - private/soc2
87 pull_request :
98
@@ -19,10 +18,12 @@ jobs:
1918 - name : Export Go Version
2019 run : echo "go-version=1.21" >> $GITHUB_OUTPUT
2120
22- golangci-lint :
21+ golangci-lint-and-pr :
2322 name : GolangCI Lint & Auto PR
2423 needs : setup
2524 runs-on : ubuntu-latest
25+ outputs :
26+ golangci-issues-found : ${{ steps.scan.outputs.golangci_issues_found }}
2627 permissions :
2728 contents : write
2829 pull-requests : write
@@ -42,34 +43,51 @@ jobs:
4243 | sh -s -- -b $(go env GOPATH)/bin v1.59.0
4344 echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
4445
45- - name : Run golangci-lint
46- id : golangci
46+ - name : Sanitize branch name
47+ run : echo "SAFE_REF_NAME=${GITHUB_REF_NAME//\//-}" >> $GITHUB_ENV
48+
49+ - name : Run GolangCI Lint Scan
50+ id : scan
4751 run : |
48- echo "🔍 Running golangci-lint..."
49- golangci-lint run ./... > golangci_output.txt || true
50- cat golangci_output.txt
51- if [ -s golangci_output.txt ]; then
52- echo "lint_issues=true" >> "$GITHUB_OUTPUT"
52+ echo "Running GolangCI lint scan..."
53+ mkdir -p tmp
54+ golangci-lint run ./... --out-format=json > tmp/golangci_output.json || true
55+ echo -e "\nHuman-readable GolangCI output:\n"
56+ golangci-lint run ./... || true
57+ cat tmp/golangci_output.json || echo "{}"
58+
59+ issue_count=$(jq '.Issues | length' tmp/golangci_output.json 2>/dev/null || echo 0)
60+
61+ if [[ "$issue_count" -gt 0 ]]; then
62+ echo "golangci_issues_found=true" >> "$GITHUB_OUTPUT"
5363 else
54- echo "lint_issues =false" >> "$GITHUB_OUTPUT"
64+ echo "golangci_issues_found =false" >> "$GITHUB_OUTPUT"
5565 fi
5666
57- - name : Create PR if Issues Found
58- if : ${{ steps.golangci.outputs.lint_issues == 'true' }}
59- uses : peter-evans/create-pull-request@v5
67+ - name : Upload GolangCI Report
68+ uses : actions/upload-artifact@v4
6069 with :
61- commit-message : ' chore: fix golangci-lint issues'
62- title : ' chore: GolangCI Lint Issues Found'
63- body : |
64- ## ⚠️ GolangCI Lint Issues Found
65- See `golangci_output.txt` for full details.
66- branch : auto/golangci-lint-issues
67- base : pf9-oidc
68- add-paths : |
69- golangci_output.txt
70+ name : golangci-json-${{ env.SAFE_REF_NAME }}
71+ path : tmp/golangci_output.json
7072
71- - name : Fail job if issues found
72- if : ${{ steps.golangci .outputs.lint_issues == 'true' }}
73+ - name : Generate PR Body ( if issues found)
74+ if : ${{ steps.scan .outputs.golangci_issues_found == 'true' }}
7375 run : |
74- echo "❌ GolangCI lint issues found — failing job."
75- exit 1
76+ echo "# GolangCI Lint Report for branch \`${GITHUB_REF_NAME}\`" > tmp/pr-body.md
77+ jq -r '.Issues[] | "* File: \(.Pos.Filename)\n • Line: \(.Pos.Line)\n • Column: \(.Pos.Column)\n • Linter: \(.FromLinter)\n • Message: \(.Text)\n"' \
78+ tmp/golangci_output.json >> tmp/pr-body.md
79+
80+ - name : Create Pull Request (if issues found)
81+ if : ${{ github.event_name == 'push' && steps.scan.outputs.golangci_issues_found == 'true' }}
82+ uses : peter-evans/create-pull-request@v5
83+ with :
84+ commit-message : ' chore: GolangCI lint issues detected'
85+ title : ' GolangCI Lint Report for branch ${{ github.ref_name }}'
86+ body-path : tmp/pr-body.md
87+ branch : auto/golangci-lint/${{ env.SAFE_REF_NAME }}
88+ base : ${{ github.ref_name }}
89+ delete-branch : true
90+
91+ - name : Fail Job If Issues Found
92+ if : ${{ steps.scan.outputs.golangci_issues_found == 'true' }}
93+ run : exit 1
0 commit comments