name: Check Changeset

on: pull_request

jobs:
  check_changeset:
    name: Check changeset vs changed files
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Repo
      uses: actions/checkout@master
      with:
        # This makes Actions fetch all Git history so check_changeset script can diff properly.
        fetch-depth: 0
    - name: Set up Node (10)
      uses: actions/setup-node@v2
      with:
        node-version: 10.x
    - name: Yarn install
      run: yarn
    - name: Run changeset script
      run: yarn ts-node-script scripts/check_changeset.ts
      id: check-changeset
    - name: Print changeset checker output
      run: echo "${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}"
    - name: Print blocking failure status
      run: echo "${{steps.check-changeset.outputs.BLOCKING_FAILURE}}"
    - name: Find Comment
      uses: peter-evans/find-comment@v1
      id: fc
      with:
        issue-number: ${{github.event.number}}
        body-includes: Changeset File Check
    - name: Create comment (missing packages)
      if: ${{!steps.fc.outputs.comment-id && steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
      uses: peter-evans/create-or-update-comment@v1
      with:
        issue-number: ${{github.event.number}}
        body: |
          ### Changeset File Check :warning:
          ${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
    - name: Update comment (missing packages)
      if: ${{steps.fc.outputs.comment-id}}
      uses: peter-evans/create-or-update-comment@v1
      with:
        comment-id: ${{steps.fc.outputs.comment-id}} && steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
        edit-mode: replace
        body: |
          ### Changeset File Check :warning:
          ${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
    - name: Update comment (no missing packages)
      if: ${{steps.fc.outputs.comment-id && !steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
      uses: peter-evans/create-or-update-comment@v1
      with:
        comment-id: ${{steps.fc.outputs.comment-id}}
        edit-mode: replace
        body: |
          ### Changeset File Check :white_check_mark:
          - No modified packages are missing from the changeset file.
          - No changeset formatting errors detected.
    # Don't want it to throw before editing the comment.
    - name: Fail if checker script logged a blocking failure
      if: ${{steps.check-changeset.outputs.BLOCKING_FAILURE == 'true'}}
      run: exit 1