|
9 | 9 | jobs: |
10 | 10 | test-and-build: |
11 | 11 | runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
12 | 15 |
|
13 | 16 | steps: |
14 | | - - uses: actions/checkout@v3 |
| 17 | + - uses: actions/checkout@v4 |
15 | 18 |
|
16 | 19 | - name: Set up Go |
17 | | - uses: actions/setup-go@v4 |
| 20 | + uses: actions/setup-go@v5 |
18 | 21 | with: |
19 | 22 | go-version: "1.23" |
20 | 23 |
|
21 | 24 | - name: Install dependencies |
22 | 25 | run: go mod download |
23 | 26 |
|
24 | 27 | - name: Run golangci-lint |
25 | | - uses: golangci/golangci-lint-action@v3 |
| 28 | + uses: golangci/golangci-lint-action@v6 |
26 | 29 | with: |
27 | 30 | version: latest |
28 | 31 |
|
29 | | - - name: Run tests |
30 | | - run: go test -v ./... |
| 32 | + - name: Run tests with coverage |
| 33 | + run: | |
| 34 | + go test -v -race -coverprofile=coverage.out -covermode=atomic ./... |
| 35 | + go tool cover -html=coverage.out -o coverage.html |
| 36 | +
|
| 37 | + - name: Calculate coverage |
| 38 | + id: coverage |
| 39 | + run: | |
| 40 | + COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}') |
| 41 | + echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT |
| 42 | + echo "Coverage: $COVERAGE" |
| 43 | +
|
| 44 | + - name: Generate coverage summary |
| 45 | + id: coverage-summary |
| 46 | + run: | |
| 47 | + echo "## 📊 Test Coverage Report" > coverage-summary.md |
| 48 | + echo "" >> coverage-summary.md |
| 49 | + echo "**Overall Coverage:** \`${{ steps.coverage.outputs.coverage }}\`" >> coverage-summary.md |
| 50 | + echo "" >> coverage-summary.md |
| 51 | + echo "### Coverage by Package" >> coverage-summary.md |
| 52 | + echo "\`\`\`" >> coverage-summary.md |
| 53 | + go tool cover -func=coverage.out >> coverage-summary.md |
| 54 | + echo "\`\`\`" >> coverage-summary.md |
| 55 | + echo "" >> coverage-summary.md |
| 56 | + echo "- 🧪 All tests passed" >> coverage-summary.md |
| 57 | + echo "- 📈 Full coverage report available in workflow artifacts" >> coverage-summary.md |
| 58 | + echo "" >> coverage-summary.md |
| 59 | + echo "_Generated by GitHub Actions_" >> coverage-summary.md |
| 60 | +
|
| 61 | + - name: Comment PR with coverage |
| 62 | + if: github.event_name == 'pull_request' |
| 63 | + uses: actions/github-script@v7 |
| 64 | + with: |
| 65 | + script: | |
| 66 | + const fs = require('fs'); |
| 67 | + const coverageSummary = fs.readFileSync('coverage-summary.md', 'utf8'); |
| 68 | + |
| 69 | + github.rest.issues.createComment({ |
| 70 | + issue_number: context.issue.number, |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + body: coverageSummary |
| 74 | + }); |
| 75 | +
|
| 76 | + - name: Upload coverage artifact |
| 77 | + uses: actions/upload-artifact@v4 |
| 78 | + with: |
| 79 | + name: coverage-report |
| 80 | + path: | |
| 81 | + coverage.out |
| 82 | + coverage.html |
31 | 83 |
|
32 | 84 | - name: Build |
33 | 85 | run: go build -v ./... |
0 commit comments