name: Upload API Doc

on:
  workflow_run:
    workflows: ["Build API Doc"]
    types:
      - completed

jobs:
  upload:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.workflow_run.head_sha }}
          token: ${{ secrets.GH_TOKEN }}

      - uses: dawidd6/action-download-artifact@v2
        with:
          workflow: ${{ github.event.workflow_run.workflow_id }}
          name: docs
          path: docs/

      - env:
          REF: ${{ github.event.workflow_run.head_branch }}
          PR_REPO: ${{ github.event.workflow_run.head_repository.full_name }}
          PR_USER: ${{ github.event.workflow_run.head_repository.owner.login }}
        run: |
            git config user.name $PR_USER
            git config user.email $PR_USER@users.noreply.github.com
            git add .
            git diff-index --quiet HEAD || git commit -m ":memo: update api docs"
            git remote add target https://github.com/$PR_REPO.git
            git push target HEAD:$REF