From 03a0ddd4d9013fb54899e08d9d11ca68a8a0c069 Mon Sep 17 00:00:00 2001 From: Gellipapa Date: Thu, 6 Jul 2023 02:33:40 +0200 Subject: [PATCH] Add automatic formatter workflows --- .github/workflows/format-lua.yml | 56 ++++++++++++++++++++++++ .github/workflows/format-prettier.yml | 62 +++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 .github/workflows/format-lua.yml create mode 100644 .github/workflows/format-prettier.yml diff --git a/.github/workflows/format-lua.yml b/.github/workflows/format-lua.yml new file mode 100644 index 0000000..745af2a --- /dev/null +++ b/.github/workflows/format-lua.yml @@ -0,0 +1,56 @@ +name: formatter-lua + +on: + push: + branches: [ main ] + pull_request: + types: [ labeled ] + +jobs: + formatter: + name: formatter + runs-on: ubuntu-latest + permissions: + contents: write + if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.label.name == 'format') }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + fetch-depth: 0 + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v37 + with: + files: | + **/*.lua + !**/*.yml + - name: List all changed files + run: | + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do + echo "$file was changed" + done + - name: Install stylua + if: steps.changed-files.outputs.any_changed == 'true' + run: | + wget https://github.com/JohnnyMorganz/StyLua/releases/download/v0.18.0/stylua-linux-x86_64.zip + unzip stylua-linux-x86_64.zip + chmod +x stylua + - name: Format changed files with StyLua + if: steps.changed-files.outputs.any_changed == 'true' + run: | + ./stylua ${{ steps.changed-files.outputs.all_changed_files }} + - name: Update repo before push + run: | + git pull + - name: Commit changes and push current branch + if: steps.changed-files.outputs.any_changed == 'true' + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_user_name: ESX GITHUB ACTIONS BOT + commit_user_email: esx-github-actions-bot@users.noreply.github.com + commit_message: :art:Code formatted in *.lua files + file_pattern: '*.lua' + status_options: '--untracked-files=no' diff --git a/.github/workflows/format-prettier.yml b/.github/workflows/format-prettier.yml new file mode 100644 index 0000000..3494e7c --- /dev/null +++ b/.github/workflows/format-prettier.yml @@ -0,0 +1,62 @@ +name: formatter-prettier + +on: + push: + branches: [ main ] + pull_request: + types: [ labeled ] + +jobs: + formatter: + name: formatter + runs-on: ubuntu-latest + if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.label.name == 'format') }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + fetch-depth: 0 + - uses: actions/setup-node@v3 + with: + node-version: "16" + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v37 + with: + files: | + **/*.{js,html,css} + !**/*.yml + !**/*.min.js + - name: List all changed files + run: | + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do + echo "$file was changed" + done + - name: Get file extensions + if: steps.changed-files.outputs.any_changed == 'true' + id: getext + run: | + import os + files = '${{ steps.changed-files.outputs.all_changed_files }}'.split() + extensions = set('*' + os.path.splitext(file)[1] for file in files) + with open(os.getenv('GITHUB_ENV'), 'a') as f: + f.write(f"CHANGED_EXTENSIONS={' '.join(extensions)}\n") + shell: python + - name: Format changed files with Prettier + if: steps.changed-files.outputs.any_changed == 'true' + run: | + npx prettier --write ${{ steps.changed-files.outputs.all_changed_files }} + - name: Update repo before push + run: | + git pull + - name: Commit changed files and push current branch + if: steps.changed-files.outputs.any_changed == 'true' + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_user_name: ESX GITHUB ACTIONS BOT + commit_user_email: esx-github-actions-bot@users.noreply.github.com + commit_message: :art:Code formatted in ${{ env.CHANGED_EXTENSIONS }} files + file_pattern: ${{ env.CHANGED_EXTENSIONS }} + status_options: '--untracked-files=no'