Skip to content

Commit

Permalink
Add automatic code style for new PR. (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jul 5, 2021
1 parent 9581962 commit 8d41c0f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
51 changes: 51 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: php-cs-fixer
on: [push, pull_request]
env:
PR_NUMBER: "${{ github.event.number }}"
SOURCE_BRANCH: "$GITHUB_HEAD_REF"
FIXER_BRANCH: "auto-fixed/$GITHUB_HEAD_REF"
TITLE: "Apply fixes from PHP-CS-Fixer"
DESCRIPTION: "This merge request applies PHP code style fixes from an analysis carried out through GitHub Actions."
jobs:
php-cs-fixer:
if: github.event_name == 'pull_request' && ! startsWith(github.ref, 'refs/heads/auto-fixed/')
runs-on: ubuntu-20.04
name: Run PHP CS Fixer
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: 7.4
extensions: json, dom, curl, libxml, mbstring
coverage: none
- name: Install PHP-CS-Fixer
run: |
curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.18.6/php-cs-fixer.phar -o .github/build/php-cs-fixer
chmod a+x .github/build/php-cs-fixer
- name: Prepare Git User
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "[email protected]"
git checkout -B "${{ env.FIXER_BRANCH }}"
- name: Apply auto-fixers
run: php .github/build/php-cs-fixer fix --config=.php_cs
- name: Create Fixer PR
run: |
if [[ -z $(git status --porcelain) ]]; then
echo "Nothing to fix.. Exiting."
exit 0
fi
OPEN_PRS=`curl --silent -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?state=open"`
OPEN_FIXER_PRS=`echo ${OPEN_PRS} | grep -o "\"ref\": \"${{ env.FIXER_BRANCH }}\"" | wc -l`
git commit -am "${{ env.TITLE }}"
git push origin "${{ env.FIXER_BRANCH }}" --force
if [ ${OPEN_FIXER_PRS} -eq "0" ]; then
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/pulls" \
-d "{ \"head\":\"${{ env.FIXER_BRANCH }}\", \"base\":\"${{ env.SOURCE_BRANCH }}\", \"title\":\"${{ env.TITLE }}\", \"body\":\"${{ env.DESCRIPTION }}\n\nTriggered by #${{ env.PR_NUMBER }}\" }"
fi
exit 1

0 comments on commit 8d41c0f

Please sign in to comment.