name: Automatically format the code when requested

on: issue_comment

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        deno: ["v1.x"]
    steps:
      - uses: actions/checkout@v2
      - run: git submodule update --init --recursive
      - uses: denoland/setup-deno@main
        with:
          deno-version: ${{ matrix.deno }}
      - name: Cache dependencies
        run: deno cache mod.ts
      - name: Cache Templates
        run: deno cache template/beginner/mod.ts template/minimal/mod.ts
      - name: Format code
        if: ${{ github.event.issue.pull_request && github.event.comment.body == 'run-fmt' && (github.actor == 'Skillz4Killz' || github.actor == 'itohatweb') }}
        run: deno fmt
      - name: Push changes
        if: ${{ github.event.issue.pull_request && github.event.comment.body == 'run-fmt' && (github.actor == 'Skillz4Killz' || github.actor == 'itohatweb') }}
        continue-on-error: true # if code is unchanged it will error
        run: |
          git config user.name Github Actions
          git config user.email github-actions@github.com
          git add .
          git commit -m "chore: Format code"
          git push
