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