-
Notifications
You must be signed in to change notification settings - Fork 81
159 lines (145 loc) · 7.82 KB
/
code_samples.yaml
File metadata and controls
159 lines (145 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
name: "Check code samples"
on:
pull_request: ~
jobs:
code-samples-validation:
name: Validate code samples
runs-on: "ubuntu-22.04"
strategy:
matrix:
php:
- "8.3"
steps:
- uses: actions/checkout@v4
- name: Setup PHP Action
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
extensions: "pdo_sqlite, gd"
tools: cs2pr
- name: Generate token
id: generate_token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ secrets.AUTOMATION_CLIENT_ID }}
private-key: ${{ secrets.AUTOMATION_CLIENT_SECRET }}
owner: ${{ github.repository_owner }}
- name: Add composer keys for private packagist
run: |
composer config --global http-basic.updates.ibexa.co $SATIS_NETWORK_KEY $SATIS_NETWORK_TOKEN
composer config --global github-oauth.github.com $GITHUB_TOKEN
env:
SATIS_NETWORK_KEY: ${{ secrets.SATIS_NETWORK_KEY }}
SATIS_NETWORK_TOKEN: ${{ secrets.SATIS_NETWORK_TOKEN }}
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
- uses: ramsey/composer-install@v3
with:
dependency-versions: highest
- name: Run PHPStan analysis
run: composer phpstan
- name: Deptrac
run: composer deptrac
- name: Run rector
run: vendor/bin/rector process --dry-run --ansi
code-samples-inclusion-check:
name: Check code samples inclusion
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
# Needed to manage the comment
pull-requests: write
steps:
- name: List modified files
id: list
run: |
URL="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files"
echo 'CODE_SAMPLES_CHANGE<<CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
curl -s -X GET -G $URL | jq -r '.[] | .filename,.previous_filename' | grep '^code_samples/' | tr '\n' ' ' >> "$GITHUB_OUTPUT"
echo '' >> "$GITHUB_OUTPUT"
echo 'CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
- name: Checkout target branch (base_ref)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
uses: actions/checkout@v3
with:
ref: ${{ github.base_ref }}
- name: Log target branch code_samples usage
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
run: |
git fetch origin
git checkout origin/${{ github.head_ref }} -- tools/code_samples/code_samples_usage.php
php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_target.txt
- name: Checkout source branch (head_ref)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Log source branch code_samples usage
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
run: php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_source.txt
- name: Compare code_samples usages (diff --unified)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
# diff returns 1 if there is a difference, this is normal but seen as an error by the job.
continue-on-error: true
run: |
source_length=`wc -l < $HOME/code_samples_usage_source.txt`
target_length=`wc -l < $HOME/code_samples_usage_target.txt`
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
- name: Check for differences
id: diff
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
run: |
echo "CODE_SAMPLES_DIFF=$(wc -l < $HOME/code_samples_usage.diff | xargs)" >> "$GITHUB_OUTPUT"
- name: Convert code_samples usages differences (diff2html)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
run: |
npm install -g diff2html-cli
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
- name: Upload code_samples usages differences artifact
id: artifact
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
uses: actions/upload-artifact@v4
with:
name: code_samples_usage.diff.html
path: ~/code_samples_usage.diff.html
overwrite: true
- name: Convert code_samples usages for comment
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
run: |
title='# code_samples/ change report'
link='<a href="${{ steps.artifact.outputs.artifact-url }}">Download colorized diff</a>'
echo "$title" > code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
php tools/code_samples/code_samples_usage_diff2html.php $HOME/code_samples_usage.diff >> code_samples_usage.diff.md
echo "$link" >> code_samples_usage.diff.md
if [[ `wc -m < code_samples_usage.diff.md | xargs` -ge $((2**16)) ]]; then
echo "$title" > code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
echo "Report's diff is too long to be displayed in a comment." >> code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
echo "$link" >> code_samples_usage.diff.md
fi
- name: Find Comment
id: find-comment
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'code_samples/ change report'
- name: Delete comment
if: steps.find-comment.outputs.comment-id != ''
uses: actions/github-script@v6
with:
script: |
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.find-comment.outputs.comment-id }}
})
- name: Create comment
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: code_samples_usage.diff.md
edit-mode: replace