Skip to content

Commit 6e9a537

Browse files
committed
Added code samples testing with Deptrac
1 parent 65e8c85 commit 6e9a537

File tree

5 files changed

+191
-2
lines changed

5 files changed

+191
-2
lines changed

.github/workflows/build.yaml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,137 @@ jobs:
8686
with:
8787
reporter: github-check
8888
filter_mode: added
89+
90+
code-samples:
91+
name: Validate code samples
92+
runs-on: "ubuntu-22.04"
93+
strategy:
94+
matrix:
95+
php:
96+
- "8.3"
97+
steps:
98+
- uses: actions/checkout@v4
99+
100+
- name: Setup PHP Action
101+
uses: shivammathur/setup-php@v2
102+
with:
103+
php-version: ${{ matrix.php }}
104+
coverage: none
105+
extensions: "pdo_sqlite, gd"
106+
tools: cs2pr
107+
108+
- name: Add composer keys for private packagist
109+
run: |
110+
composer config http-basic.updates.ibexa.co $SATIS_NETWORK_KEY $SATIS_NETWORK_TOKEN
111+
composer config github-oauth.github.com $TRAVIS_GITHUB_TOKEN
112+
env:
113+
SATIS_NETWORK_KEY: ${{ secrets.SATIS_NETWORK_KEY }}
114+
SATIS_NETWORK_TOKEN: ${{ secrets.SATIS_NETWORK_TOKEN }}
115+
TRAVIS_GITHUB_TOKEN: ${{ secrets.TRAVIS_GITHUB_TOKEN }}
116+
117+
- uses: ramsey/composer-install@v3
118+
with:
119+
dependency-versions: highest
120+
121+
- name: Run PHPStan analysis
122+
run: composer phpstan
123+
124+
- name: Deptrac
125+
run: composer deptrac
126+
127+
code-samples-inclusion-check:
128+
name: Check code samples inclusion
129+
runs-on: ubuntu-latest
130+
if: github.event_name == 'pull_request'
131+
permissions:
132+
# Needed to manage the comment
133+
pull-requests: write
134+
135+
steps:
136+
- name: List modified files
137+
id: list
138+
run: |
139+
URL="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files"
140+
echo 'CODE_SAMPLES_CHANGE<<CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
141+
curl -s -X GET -G $URL | jq -r '.[] | .filename,.previous_filename' | grep '^code_samples/' | tr '\n' ' ' >> "$GITHUB_OUTPUT"
142+
echo '' >> "$GITHUB_OUTPUT"
143+
echo 'CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
144+
145+
- name: Checkout target branch (base_ref)
146+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
147+
uses: actions/checkout@v3
148+
with:
149+
ref: ${{ github.base_ref }}
150+
- name: Log target branch code_samples usage
151+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
152+
run: |
153+
git fetch origin
154+
git checkout origin/${{ github.head_ref }} -- tools/code_samples/code_samples_usage.php
155+
php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_target.txt
156+
157+
- name: Checkout source branch (head_ref)
158+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
159+
uses: actions/checkout@v3
160+
with:
161+
ref: ${{ github.head_ref }}
162+
- name: Log source branch code_samples usage
163+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
164+
run: php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_source.txt
165+
166+
- name: Compare code_samples usages (diff --unified)
167+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
168+
# diff returns 1 if there is a difference, this is normal but seen as an error by the job.
169+
continue-on-error: true
170+
run: |
171+
source_length=`wc -l < $HOME/code_samples_usage_source.txt`
172+
target_length=`wc -l < $HOME/code_samples_usage_target.txt`
173+
diff -U $(( source_length > target_length ? source_length : target_length )) $HOME/code_samples_usage_target.txt $HOME/code_samples_usage_source.txt > $HOME/code_samples_usage.diff
174+
- name: Check for differences
175+
id: diff
176+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
177+
run: |
178+
echo "CODE_SAMPLES_DIFF=$(wc -l < $HOME/code_samples_usage.diff | xargs)" >> "$GITHUB_OUTPUT"
179+
- name: Convert code_samples usages differences (diff2html)
180+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
181+
run: |
182+
npm install -g diff2html-cli
183+
diff2html -f html -s side -t 'code_samples/ changes report' --su hidden --fct false -o stdout -i file -- $HOME/code_samples_usage.diff > $HOME/code_samples_usage.diff.html
184+
- name: Upload code_samples usages differences artifact
185+
id: artifact
186+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
187+
uses: actions/upload-artifact@v4
188+
with:
189+
name: code_samples_usage.diff.html
190+
path: ~/code_samples_usage.diff.html
191+
overwrite: true
192+
- name: Convert code_samples usages for comment
193+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
194+
run: |
195+
echo '# code_samples/ change report' >> code_samples_usage.diff.md
196+
echo '' >> code_samples_usage.diff.md
197+
php tools/code_samples/code_samples_usage_diff2html.php $HOME/code_samples_usage.diff >> code_samples_usage.diff.md
198+
echo '<a href="${{ steps.artifact.outputs.artifact-url }}">Download colorized diff</a>' >> code_samples_usage.diff.md
199+
- name: Find Comment
200+
id: find-comment
201+
uses: peter-evans/find-comment@v3
202+
with:
203+
issue-number: ${{ github.event.pull_request.number }}
204+
comment-author: 'github-actions[bot]'
205+
body-includes: 'code_samples/ change report'
206+
- name: Delete comment
207+
if: steps.find-comment.outputs.comment-id != ''
208+
uses: actions/github-script@v6
209+
with:
210+
script: |
211+
github.rest.issues.deleteComment({
212+
owner: context.repo.owner,
213+
repo: context.repo.repo,
214+
comment_id: ${{ steps.find-comment.outputs.comment-id }}
215+
})
216+
- name: Create comment
217+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
218+
uses: peter-evans/create-or-update-comment@v4
219+
with:
220+
issue-number: ${{ github.event.pull_request.number }}
221+
body-path: code_samples_usage.diff.md
222+
edit-mode: replace

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ node_modules/
1010
.yarn
1111
yarn.lock
1212
docs/css/*.map
13+
.deptrac.cache

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,31 @@ of the command.
5454

5555
## Testing the code samples
5656

57+
### PHPStan
5758
This repository uses PHPStan to test the code samples. To run the tests locally execute the commands below:
5859
```bash
5960
composer update
6061
composer phpstan
6162
```
6263

64+
Regenerate the baseline by running:
65+
```bash
66+
composer phpstan -- --generate-baseline
67+
```
68+
69+
### Deptrac
70+
71+
This repository uses Deptrac to test the code samples. To run the tests locally execute the commands below:
72+
```bash
73+
composer update
74+
composer deptrac
75+
```
76+
77+
Regenerate the baseline by running:
78+
```bash
79+
vendor/bin/deptrac --formatter=baseline
80+
```
81+
6382
## Where to View
6483

6584
https://doc.ibexa.co

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@
7070
"ibexa/tree-builder": "~5.0.x-dev",
7171
"ibexa/discounts": "~5.0.x-dev",
7272
"ibexa/discounts-codes": "~5.0.x-dev",
73+
"ibexa/product-catalog-symbol-attribute": "~5.0.x-dev",
7374
"ibexa/core-search": "~5.0.x-dev",
74-
"ibexa/product-catalog-symbol-attribute": "~5.0.x-dev"
75+
"qossmic/deptrac": "^2.0"
7576
},
7677
"scripts": {
7778
"fix-cs": "php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots",
7879
"check-cs": "@fix-cs --dry-run",
79-
"phpstan": "phpstan analyse"
80+
"phpstan": "phpstan analyse",
81+
"deptrac": "deptrac analyse"
82+
},
83+
"scripts-descriptions": {
84+
"fix-cs": "Automatically fixes code style in all files",
85+
"check-cs": "Run code style checker for all files",
86+
"phpstan": "Run static code analysis",
87+
"deptrac": "Run Deptrac architecture testing"
8088
},
8189
"config": {
8290
"allow-plugins": false

deptrac.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
imports:
2+
- deptrac.baseline.yaml
3+
4+
deptrac:
5+
paths:
6+
- ./code_samples
7+
layers:
8+
- name: CodeSamples
9+
collectors:
10+
- type: directory
11+
value: code_samples
12+
- name: IbexaContracts
13+
collectors:
14+
- type: classLike
15+
value: .*Ibexa\\Contracts\\.*
16+
- name: IbexaNotContracts
17+
collectors:
18+
- type: bool
19+
must:
20+
- type: classLike
21+
value: .*Ibexa\\.*
22+
must_not:
23+
- type: classLike
24+
value: .*Ibexa\\Contracts.*
25+
ruleset:
26+
CodeSamples:
27+
- IbexaContracts

0 commit comments

Comments
 (0)