Skip to content

Commit ea329a3

Browse files
committed
CI
1 parent f135fad commit ea329a3

File tree

3 files changed

+295
-3
lines changed

3 files changed

+295
-3
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
name: Fix Code Style
2+
3+
on:
4+
workflow_dispatch:
5+
6+
permissions:
7+
contents: write
8+
pull-requests: write
9+
issues: write
10+
11+
jobs:
12+
fix-code-style:
13+
name: Fix Code Style
14+
runs-on: ubuntu-latest
15+
16+
# Only run if the PR is from a branch in the same repository (not from a fork)
17+
if: github.event.pull_request.head.repo.full_name == github.repository
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
with:
23+
token: ${{ secrets.GITHUB_TOKEN }}
24+
ref: ${{ github.head_ref }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: '8.4'
30+
extensions: mbstring, hash, sodium
31+
32+
- name: Install dependencies
33+
uses: ramsey/composer-install@v3
34+
35+
- name: Check if code style fixes are needed
36+
id: check
37+
run: composer lint
38+
continue-on-error: true
39+
40+
- name: Set fixes needed flag
41+
id: fixes
42+
run: |
43+
if [ "${{ steps.check.outcome }}" = "success" ]; then
44+
echo "fixes_needed=false" >> $GITHUB_OUTPUT
45+
echo "✅ No code style fixes needed"
46+
else
47+
echo "fixes_needed=true" >> $GITHUB_OUTPUT
48+
echo "🔧 Code style fixes needed"
49+
fi
50+
51+
- name: Fix code style
52+
if: steps.fixes.outputs.fixes_needed == 'true'
53+
run: composer fix
54+
55+
- name: Check for changes
56+
if: steps.fixes.outputs.fixes_needed == 'true'
57+
id: changes
58+
run: |
59+
if [ -n "$(git diff --name-only)" ]; then
60+
echo "changes_made=true" >> $GITHUB_OUTPUT
61+
echo "📝 Code style changes made:"
62+
git diff --name-only
63+
else
64+
echo "changes_made=false" >> $GITHUB_OUTPUT
65+
echo "ℹ️ No changes made after running Pint"
66+
fi
67+
68+
- name: Commit and push fixes
69+
if: steps.fixes.outputs.fixes_needed == 'true' && steps.changes.outputs.changes_made == 'true'
70+
run: |
71+
git config --local user.email "action@github.com"
72+
git config --local user.name "GitHub Action"
73+
git add .
74+
git commit -m "🎨 Fix code style with Laravel Pint
75+
76+
Automatically fixed code style violations using Laravel Pint.
77+
78+
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
79+
git push
80+
81+
- name: Comment on PR
82+
if: steps.fixes.outputs.fixes_needed == 'true' && steps.changes.outputs.changes_made == 'true'
83+
uses: actions/github-script@v7
84+
with:
85+
script: |
86+
const message = `## 🎨 Code Style Auto-Fix
87+
88+
I've automatically fixed code style violations in this PR using Laravel Pint.
89+
90+
**Changes made:**
91+
- Applied PSR-12 code style standards
92+
- Fixed formatting and spacing issues
93+
- Organized imports
94+
95+
The fixes have been committed to this PR. You can review the changes in the latest commit.
96+
97+
---
98+
*This comment was automatically generated by the code style fix workflow.*`;
99+
100+
// Check if we already commented on this PR
101+
const comments = await github.rest.issues.listComments({
102+
owner: context.repo.owner,
103+
repo: context.repo.repo,
104+
issue_number: context.issue.number,
105+
});
106+
107+
const botComment = comments.data.find(comment =>
108+
comment.user.login === 'github-actions[bot]' &&
109+
comment.body.includes('Code Style Auto-Fix')
110+
);
111+
112+
if (botComment) {
113+
// Update existing comment
114+
await github.rest.issues.updateComment({
115+
owner: context.repo.owner,
116+
repo: context.repo.repo,
117+
comment_id: botComment.id,
118+
body: message
119+
});
120+
} else {
121+
// Create new comment
122+
await github.rest.issues.createComment({
123+
owner: context.repo.owner,
124+
repo: context.repo.repo,
125+
issue_number: context.issue.number,
126+
body: message
127+
});
128+
}

.github/workflows/tests.yml

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ "main", "develop" ]
6+
pull_request:
7+
branches: [ "main", "develop" ]
8+
9+
permissions:
10+
contents: read
11+
pull-requests: write
12+
issues: write
13+
14+
jobs:
15+
tests:
16+
name: Tests (PHP ${{ matrix.php }})
17+
runs-on: ubuntu-latest
18+
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
php: ['8.4']
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
28+
- name: Setup PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: ${{ matrix.php }}
32+
extensions: mbstring, hash, sodium
33+
coverage: xdebug
34+
35+
- name: Install dependencies
36+
uses: ramsey/composer-install@v3
37+
38+
- name: Check code style
39+
run: composer lint
40+
continue-on-error: true
41+
id: lint
42+
43+
- name: Run tests with coverage
44+
run: ./vendor/bin/phpunit --coverage-clover=coverage/clover.xml --coverage-html=coverage/html --coverage-text
45+
continue-on-error: true
46+
id: tests
47+
48+
- name: Check coverage percentage
49+
id: coverage
50+
run: |
51+
if [ -f coverage/clover.xml ]; then
52+
COVERAGE=$(php -r "
53+
\$xml = simplexml_load_file('coverage/clover.xml');
54+
\$metrics = \$xml->project->metrics;
55+
\$statements = (float)\$metrics['statements'];
56+
\$coveredstatements = (float)\$metrics['coveredstatements'];
57+
if (\$statements > 0) {
58+
\$percentage = (\$coveredstatements / \$statements) * 100;
59+
echo round(\$percentage, 2);
60+
} else {
61+
echo '0';
62+
}
63+
")
64+
echo "percentage=$COVERAGE" >> $GITHUB_OUTPUT
65+
echo "Coverage: $COVERAGE%"
66+
67+
if (( $(echo "$COVERAGE < 100" | bc -l) )); then
68+
echo "coverage_failed=true" >> $GITHUB_OUTPUT
69+
echo "❌ Coverage requirement not met: $COVERAGE% (required: 100%)"
70+
exit 1
71+
else
72+
echo "coverage_failed=false" >> $GITHUB_OUTPUT
73+
echo "✅ Coverage requirement met: $COVERAGE%"
74+
fi
75+
else
76+
echo "percentage=0" >> $GITHUB_OUTPUT
77+
echo "coverage_failed=true" >> $GITHUB_OUTPUT
78+
echo "❌ Coverage report not found"
79+
exit 1
80+
fi
81+
82+
- name: Comment on PR (Failure)
83+
if: failure() && github.event_name == 'pull_request'
84+
uses: actions/github-script@v7
85+
with:
86+
script: |
87+
const fs = require('fs');
88+
89+
let lintStatus = '${{ steps.lint.outcome }}';
90+
let testStatus = '${{ steps.tests.outcome }}';
91+
let coveragePercentage = '${{ steps.coverage.outputs.percentage }}';
92+
let coverageFailed = '${{ steps.coverage.outputs.coverage_failed }}';
93+
94+
let message = `## 🔍 Test Results - PHP ${{ matrix.php }}\n\n`;
95+
96+
if (lintStatus === 'failure') {
97+
message += `❌ **Code Style Check Failed**\n\n`;
98+
message += `Code style violations found. Please run \`composer fix\` to automatically fix them.\n\n`;
99+
} else {
100+
message += `✅ **Code Style Check Passed**\n\n`;
101+
}
102+
103+
if (testStatus === 'failure') {
104+
message += `❌ **Tests Failed**\n\n`;
105+
message += `Some tests are failing. Please check the [workflow run](${context.payload.pull_request.html_url}/checks) for details.\n\n`;
106+
} else {
107+
message += `✅ **Tests Passed**\n\n`;
108+
}
109+
110+
if (coverageFailed === 'true') {
111+
message += `❌ **Code Coverage: ${coveragePercentage}%** (Required: 100%)\n\n`;
112+
message += `Code coverage requirement not met. Please add tests to achieve 100% coverage.\n\n`;
113+
} else {
114+
message += `✅ **Code Coverage: ${coveragePercentage}%**\n\n`;
115+
}
116+
117+
message += `---\n*This comment was automatically generated by the test workflow.*`;
118+
119+
// Check if we already commented on this PR
120+
const comments = await github.rest.issues.listComments({
121+
owner: context.repo.owner,
122+
repo: context.repo.repo,
123+
issue_number: context.issue.number,
124+
});
125+
126+
const botComment = comments.data.find(comment =>
127+
comment.user.login === 'github-actions[bot]' &&
128+
comment.body.includes('Test Results - PHP ${{ matrix.php }}')
129+
);
130+
131+
if (botComment) {
132+
// Update existing comment
133+
await github.rest.issues.updateComment({
134+
owner: context.repo.owner,
135+
repo: context.repo.repo,
136+
comment_id: botComment.id,
137+
body: message
138+
});
139+
} else {
140+
// Create new comment
141+
await github.rest.issues.createComment({
142+
owner: context.repo.owner,
143+
repo: context.repo.repo,
144+
issue_number: context.issue.number,
145+
body: message
146+
});
147+
}
148+
149+
- name: Upload coverage reports
150+
if: always()
151+
uses: actions/upload-artifact@v4
152+
with:
153+
name: coverage-reports-php${{ matrix.php }}
154+
path: coverage/
155+
retention-days: 30
156+
157+
- name: Fail on lint errors
158+
if: steps.lint.outcome == 'failure'
159+
run: |
160+
echo "❌ Build failed due to code style violations"
161+
exit 1
162+
163+
- name: Fail on coverage < 100%
164+
if: steps.coverage.outputs.coverage_failed == 'true'
165+
run: |
166+
echo "❌ Build failed due to insufficient code coverage: ${{ steps.coverage.outputs.percentage }}%"
167+
exit 1

phpunit.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
<include>
1010
<directory suffix=".php">src</directory>
1111
</include>
12-
<exclude>
13-
<directory>binary-tools</directory>
14-
</exclude>
1512
</source>
1613
<coverage>
1714
<report>

0 commit comments

Comments
 (0)