Skip to content

Commit e0a28dd

Browse files
ci(GitHub): auto-commit changes from jobs
* Added composite .github/actions/auto-commit * Using stefanzweifel/[email protected] * Extended w/ comment step explaining the auto-commit. * Added auto-commit to linting/lint job. * Configured golangci-lint to enable all linters. * Wrapped golangci-lint w/ reviewdog on pull requests. * & addressed all linting problems. Closes #8 Signed-off-by: Stefan Zimmermann <[email protected]>
1 parent 1d9ec21 commit e0a28dd

File tree

6 files changed

+133
-33
lines changed

6 files changed

+133
-33
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: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
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) & reviewdog' || '' }}
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+
golangci_lint_flags: --default all --fix
45+
46+
reporter: github-pr-review
47+
github_token: ${{ secrets.GITHUB_TOKEN }}
48+
filter_mode: file
49+
level: error
50+
fail_level: any
51+
cache: false
52+
53+
- name: Auto-commit lint fixes
54+
if: github.event_name == 'pull_request' && (success() || failure()) # (golangci-lint succeeds when all is auto-fixed)
55+
uses: ./.github/actions/auto-commit

.github/workflows/testing.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
name: Test & benchmark
2-
1+
name: Testing
32
on:
43
push:
54
pull_request:
65

6+
permissions:
7+
contents: write
8+
pull-requests: write
9+
710
jobs:
811
test:
12+
name: Test w/ go test
913
runs-on: ubuntu-latest
10-
1114
steps:
1215
- uses: actions/checkout@v5
1316
with:
1417
fetch-depth: 0
15-
18+
1619
- name: Setup Go environment
1720
uses: actions/setup-go@v6
1821
with:
1922
go-version: 1.25.1
20-
23+
2124
- name: Run tests
2225
run: |
2326
go mod tidy
2427
go test -v ./...
2528
2629
benchmark:
30+
name: Benchmark w/ go test & benchstat
2731
runs-on: ubuntu-latest
28-
2932
steps:
3033
- uses: actions/checkout@v5
3134
with:
@@ -37,7 +40,8 @@ jobs:
3740
go-version: 1.25.1
3841

3942
- name: Install benchstat
40-
run: go install golang.org/x/perf/cmd/benchstat@latest
43+
run: |
44+
go install golang.org/x/perf/cmd/benchstat@latest
4145
4246
- name: Run benchmark on base branch
4347
if: github.event_name == 'pull_request'

.golangci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
15+
- github.com/zimmermanncode/go-require

pointer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18+
// Package require provides runtime Go assertions w/ descriptive panics & a fluent API.
1819
package require
1920

2021
import (
@@ -30,6 +31,7 @@ import (
3031
// // if not nil => panic: assertion failed: My value should be a nil pointer
3132
func NilPtr[T any](name string, value *T) *T {
3233
lo.Assertf(value == nil, "%s should be a nil pointer", name)
34+
3335
return value
3436
}
3537

@@ -42,5 +44,6 @@ func NilPtr[T any](name string, value *T) *T {
4244
// // if nil => panic: assertion failed: My value should not be a nil pointer
4345
func NotNilPtr[T any](name string, value *T) *T {
4446
lo.Assertf(value != nil, "%s should not be a nil pointer", name)
47+
4548
return value
4649
}

pointer_test.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,72 @@
1-
package require
1+
package require_test
22

33
import (
44
"testing"
55

66
assert "github.com/stretchr/testify/assert"
7+
"github.com/zimmermanncode/go-require"
78
)
89

910
func TestNilPtr(t *testing.T) {
11+
t.Parallel()
1012
t.Run("should accept nil pointer & return nil", func(t *testing.T) {
11-
var nilPtr *int
12-
assert.Nil(t, NilPtr("Test pointer", nilPtr))
13+
t.Parallel()
14+
assert.Nil(t, require.NilPtr("Test pointer", (*int)(nil)))
1315
})
1416
t.Run("should panic when pointer is not nil", func(t *testing.T) {
17+
value := "test"
18+
19+
t.Parallel()
1520
assert.PanicsWithValue(t, "assertion failed: Test pointer should be a nil pointer", func() {
16-
value := "test"
17-
NilPtr("Test pointer", &value)
21+
require.NilPtr("Test pointer", &value)
1822
})
1923
})
2024
t.Run("should use given name in panic message", func(t *testing.T) {
21-
assert.PanicsWithValue(t, "assertion failed: Other pointer should be a nil pointer", func() {
22-
value := 42
23-
NilPtr("Other pointer", &value)
25+
value := 42
26+
27+
t.Parallel()
28+
assert.PanicsWithValue(t, "assertion failed: Other value should be a nil pointer", func() {
29+
require.NilPtr("Other value", &value)
2430
})
2531
})
2632
}
2733

2834
func TestNotNilPtr(t *testing.T) {
35+
t.Parallel()
2936
t.Run("should accept not-nil pointer & return same pointer", func(t *testing.T) {
3037
value := 42
31-
assert.Same(t, &value, NotNilPtr("Test pointer", &value))
38+
39+
t.Parallel()
40+
assert.Same(t, &value, require.NotNilPtr("Test pointer", &value))
3241
})
3342
t.Run("should panic when pointer is nil", func(t *testing.T) {
43+
t.Parallel()
3444
assert.PanicsWithValue(t, "assertion failed: Test pointer should not be a nil pointer", func() {
35-
var nilPtr *string
36-
NotNilPtr("Test pointer", nilPtr)
45+
require.NotNilPtr("Test pointer", (*string)(nil))
3746
})
3847
})
3948
t.Run("should use given name in panic message", func(t *testing.T) {
40-
assert.PanicsWithValue(t, "assertion failed: Other pointer should not be a nil pointer", func() {
41-
var nilPtr *float64
42-
NotNilPtr("Other pointer", nilPtr)
49+
t.Parallel()
50+
assert.PanicsWithValue(t, "assertion failed: Other value should not be a nil pointer", func() {
51+
require.NotNilPtr("Other value", (*float64)(nil))
4352
})
4453
})
4554
}
4655

4756
func BenchmarkNilPtr(b *testing.B) {
48-
var nilPtr *int
4957
b.ResetTimer()
50-
for i := 0; i < b.N; i++ {
51-
NilPtr("Benchmark pointer", nilPtr)
58+
59+
for range b.N {
60+
require.NilPtr("Benchmark pointer", (*int)(nil))
5261
}
5362
}
5463

5564
func BenchmarkNotNilPtr(b *testing.B) {
5665
value := "benchmark"
66+
5767
b.ResetTimer()
58-
for i := 0; i < b.N; i++ {
59-
NotNilPtr("Benchmark pointer", &value)
68+
69+
for range b.N {
70+
require.NotNilPtr("Benchmark pointer", &value)
6071
}
6172
}

0 commit comments

Comments
 (0)