@@ -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
0 commit comments