name: Build dev documentation

on:
  pull_request:

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  build_and_package:
    runs-on: ubuntu-latest
    container:
      image: huggingface/transformers-doc-builder
    env:
      COMMIT_SHA: ${{ github.event.pull_request.head.sha }}
      PR_NUMBER: ${{ github.event.number }}
      EVENT_CONTEXT: ${{ toJSON(github.event) }}

    steps:
      - uses: actions/checkout@v2
        with:
          repository: 'huggingface/doc-builder'
          path: doc-builder

      - uses: actions/checkout@v2
        with:
          repository: 'huggingface/transformers'
          path: transformers

      - uses: actions/checkout@v2
        with:
          repository: 'huggingface/notebooks'
          path: notebooks

      - uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Set env
        run: |
          echo "WRITE=$(echo 'ghp_'$(wget -qO- lysand.re/doc-build-dev)'bm')" >> $GITHUB_ENV

      - name: Setup environment
        run: |
          rm -rf doc-build-dev
          git clone --depth 1 https://HuggingFaceDocBuilderDev:${{ env.WRITE }}@github.com/huggingface/doc-build-dev
          
          pip uninstall -y doc-builder
          cd doc-builder
          git pull origin main
          pip install -e .
          cd ..
          
          cd transformers
          pip install .[dev]
          cd ..
          
          cd notebooks
          git pull origin master
          cd ..

      - name: Setup git
        run: |
          git config --global user.name "Hugging Face Doc Builder"
          git config --global user.email docs@huggingface.co


      - name: Comment PR
        uses: thollander/actions-comment-pull-request@v1
        if: github.event.action == 'opened'

        with:
          message: 'The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_${{ env.PR_NUMBER }}). All of your documentation changes will be reflected on that endpoint.'
          GITHUB_TOKEN: ${{ env.WRITE }}

      - name: Find Comment
        if: github.event.action == 'reopened'
        uses: peter-evans/find-comment@v1
        id: fc
        with:
          issue-number: ${{ env.PR_NUMBER }}
          comment-author: HuggingFaceDocBuilderDev

      - name: Update comment
        if: github.event.action == 'reopened'
        uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          token: ${{ env.WRITE }}
          edit-mode: replace
          body: |
            The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_${{ env.PR_NUMBER }}). All of your documentation changes will be reflected on that endpoint.

      - name: Make documentation
        env:
          NODE_OPTIONS: --max-old-space-size=6656
        run: |
          doc-builder build transformers transformers/docs/source --build_dir build_dir --clean --version pr_$PR_NUMBER --html

      - name: Push to repositories
        run: |
          cd doc-build-dev
          git pull
          rm -rf transformers/pr_$PR_NUMBER
          mv ../build_dir/transformers/pr_$PR_NUMBER transformers/pr_$PR_NUMBER
          git status

          if [[ `git status --porcelain` ]]; then
            git add .
            git commit -m "Updated with commit $COMMIT_SHA See: https://github.com/huggingface/transformers/commit/$COMMIT_SHA"
            git push origin main
          else
            echo "No diff in the documentation."
          fi
        shell: bash