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:
      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: HuggingFaceDocBuilder

      - 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: |
          cd doc-build-dev && git pull
          cd ../doc-builder
          doc-builder build transformers ../transformers/docs/source --build_dir ../doc-build-dev --notebook_dir ../notebooks/transformers_doc --clean --version pr_$PR_NUMBER --html

      - name: Push to repositories
        run: |
          cd doc-build-dev
          ls
          git add .
          git commit -m "Updated with commit ${{ github.sha }} See: https://github.com/huggingface/transformers/commit/${{ github.sha }}"
          git push origin main
