Skip to content

Commit b599d8a

Browse files
ci(GitHub): auto-commit changes from jobs
* Added composite .github/actions/auto-commit * Using stefanzweifel/git-auto-commit-action@v6.0.1 * Extended w/ comment step explaining the auto-commit. * Added auto-commit to linting/lint job. * Further configured golangci-lint to enable all linters. * & addressed all linting problems. Closes #8 Signed-off-by: Stefan Zimmermann <user@zimmermann.co>
1 parent 1d9ec21 commit b599d8a

File tree

5 files changed

+93
-10
lines changed

5 files changed

+93
-10
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Auto-commit changes
2+
description: Composite action for auto-committing changes made by jobs
3+
outputs:
4+
changes-detected:
5+
description: Whether changes were detected & committed
6+
value: ${{ steps.auto_commit.outputs.changes_detected }}
7+
8+
commit-hash:
9+
description: SHA hash of the auto-commit
10+
value: ${{ steps.auto_commit.outputs.commit_hash }}
11+
12+
runs:
13+
using: composite
14+
steps:
15+
- name: Auto-commit changes made by job
16+
id: auto_commit
17+
if: github.event_name == 'pull_request'
18+
uses: stefanzweifel/git-auto-commit-action@778341af668090896ca464160c2def5d1d1a3eb0 # v6.0.1
19+
with:
20+
commit_message: Auto-fix from ${{ github.workflow }} / ${{ github.job }} job
21+
status_options: --untracked-files=no
22+
23+
- name: Comment on PR about the auto-commit
24+
if: github.event_name == 'pull_request' && steps.auto_commit.outputs.changes_detected == 'true'
25+
uses: actions/github-script@v8
26+
with:
27+
script: |
28+
const repositoryUrl = '${{ github.server_url }}/${{ github.repository }}'
29+
30+
await github.rest.issues.createComment({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
issue_number: context.issue.number,
34+
body: `
35+
**Auto-fix applied from ${{ github.workflow }}** / \`${{ github.job }}\` job
36+
37+
* Workflow run: ${repositoryUrl}/actions/runs/${{ github.run_id }}
38+
* Fix commit: ${repositoryUrl}/commit/${{ steps.auto_commit.outputs.commit_hash }}
39+
40+
@${{ github.event.pull_request.user.login }}: `
41+
+ 'Please check the auto-changes, pull them, squash commits, & force-push to continue the PR review ...'
42+
})

.github/workflows/linting.yml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
1-
name: Lint & format
1+
name: Linting
22
on:
33
push:
44
pull_request:
55

6+
permissions:
7+
contents: write
8+
pull-requests: write
9+
610
jobs:
711
lint:
12+
name: Lint w/ golangci-lint ${{ github.event_name == 'pull_request' && '(auto-fix)' || '' }}
813
runs-on: ubuntu-latest
9-
permissions:
10-
contents: write
11-
1214
steps:
1315
- uses: actions/checkout@v5
1416
with:
1517
fetch-depth: 0
18+
submodules: recursive
19+
20+
# Avoid merge commits when auto-committing fixes ...
21+
ref: ${{ github.head_ref || github.ref }}
1622

1723
- name: Setup Go environment
1824
uses: actions/setup-go@v6
1925
with:
2026
go-version: 1.25.1
2127

2228
- name: Run go mod tidy
23-
run: go mod tidy
29+
run: |
30+
go mod tidy
2431
2532
- name: Install & run golangci-lint
33+
if: github.event_name == 'push'
2634
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8
2735
with:
36+
install-mode: goinstall
2837
version: latest
29-
install-mode: "goinstall"
30-
args: --fix
38+
args: --default all # ${{ github.event_name == 'pull_request' && '--fix' || '' }}
39+
40+
- name: Install & run golangci-lint w/ reviewdog
41+
if: github.event_name == 'pull_request'
42+
uses: reviewdog/action-golangci-lint@f9bba13753278f6a73b27a56a3ffb1bfda90ed71 # v2.8.0
43+
with:
44+
cache: false
45+
reporter: github-pr-review
46+
golangci_lint_flags: --default all --fix
47+
level: error
48+
fail_level: any
49+
50+
- name: Auto-commit lint fixes
51+
if: github.event_name == 'pull_request' && (success() || failure()) # (golangci-lint succeeds when all is auto-fixed)
52+
uses: ./.github/actions/auto-commit

.golangci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '2'
2+
linters:
3+
disable:
4+
- wsl # <-- Auto-switches to wsl_v5.
5+
6+
settings:
7+
depguard:
8+
rules:
9+
main:
10+
list-mode: strict
11+
allow:
12+
- $gostd
13+
- github.com/samber/lo
14+
- github.com/stretchr/testify

pointer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ func NotNilPtr[T any](name string, value *T) *T {
4444
lo.Assertf(value != nil, "%s should not be a nil pointer", name)
4545
return value
4646
}
47+
48+
// bla
49+
func _() {
50+
}

pointer_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
)
88

99
func TestNilPtr(t *testing.T) {
10+
t.Parallel()
1011
t.Run("should accept nil pointer & return nil", func(t *testing.T) {
12+
t.Parallel()
1113
var nilPtr *int
1214
assert.Nil(t, NilPtr("Test pointer", nilPtr))
1315
})
@@ -45,10 +47,9 @@ func TestNotNilPtr(t *testing.T) {
4547
}
4648

4749
func BenchmarkNilPtr(b *testing.B) {
48-
var nilPtr *int
4950
b.ResetTimer()
50-
for i := 0; i < b.N; i++ {
51-
NilPtr("Benchmark pointer", nilPtr)
51+
for range b.N {
52+
NilPtr("Benchmark pointer", (*int)(nil))
5253
}
5354
}
5455

0 commit comments

Comments
 (0)