name: Check for Generated Files

on:
  # Triggers the workflow on push or pull request events but only for the dev branch
  pull_request:
    branches: [dev]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  checkFiles:
    name: Check Files
    runs-on: ubuntu-latest
    steps:
      - name: Checkout files
        uses: actions/checkout@v2

      - name: Check bundle files
        id: changed-bundle-files
        uses: tj-actions/changed-files@v18.4
        with:
          files: |
            dist/*
            index.html
            main.bundle.js
            main.bundle.js.map

      - name: Check documentation changes
        id: changed-markdown-doc-files
        uses: tj-actions/changed-files@v18.4
        with:
          files: |
            markdown/*.md

      - name: Warn when bundle files were changed
        id: warn-bundles-changed
        if: steps.changed-bundle-files.outputs.any_changed == 'true'
        run: |
          echo "One or more files in the bundle files were changed." >> warnings.txt

      - name: Warn when documentation markdown files were changed
        id: warn-markdown-changed
        if: steps.changed-markdown-doc-files.outputs.any_changed == 'true'
        run: |
          echo "One or more files in the markdown documentation were changed." >> warnings.txt

      - name: Print Warnings
        id: get-warnings
        run: |
          if [ -f warnings.txt ]
          then
            echo "::set-output name=has_warnings::true"
          else
            echo "::set-output name=has_warnings::false"
            touch warnings.txt
          fi

      - name: Get Comment Body
        id: get-comment-body
        if : steps.get-warnings.outputs.has_warnings == 'true'
        run: |
          cat warnings.txt > comment.txt
          echo "" >> comment.txt
          echo "Please do not commit files generated by webpack or generated markdown" >> comment.txt
          echo "" >> comment.txt
          echo "See [CONTRIBUTING.md](https://github.com/danielyxie/bitburner/blob/dev/doc/CONTRIBUTING.md) for details." >> comment.txt

          body=$(cat comment.txt)
          body="${body//'%'/'%25'}"
          body="${body//$'\n'/'%0A'}"
          body="${body//$'\r'/'%0D'}"

          echo ::set-output name=body::$body
      - name: Add github comment on problem
        if : steps.get-warnings.outputs.has_warnings == 'true'
        uses: peter-evans/commit-comment@v1
        with:
          body: ${{ steps.get-comment-body.outputs.body }}
      - name: Flag as error
        if : steps.get-warnings.outputs.has_warnings == 'true'
        run: |
          COMMIT_WARNINGS=$(cat warnings.txt)
          echo "::warning:: $COMMIT_WARNINGS"
          exit 1