name: Release New ISA When Merging a PR

on:
  workflow_run:
    workflows: ["RISC-V ISA Build"]
    branches:
      - main
    types:
      - completed

jobs:
  on-success:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - run: |
        echo The PR was successfully merged.

    - name: Set short SHA
      run: echo "SHORT_SHA=$(echo ${GITHUB_SHA::7})" >> $GITHUB_ENV

    - name: Get current date
      run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

    - name: Pull Container
      id: pull_container_image
      run: |
          docker pull riscvintl/riscv-docs-base-container-image:latest

    - name: Build Files
      id: build_files
      if: steps.pull_container_image.outcome == 'success'
      run: |
          docker run --rm -v ${{ github.workspace }}:/build riscvintl/riscv-docs-base-container-image:latest \
          /bin/sh -c "make -j$(nproc)"

    # Upload the riscv-privileged PDF file
    - name: Upload riscv-privileged.pdf
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-privileged-${{ env.SHORT_SHA }}.pdf
        path: ${{ github.workspace }}/build/riscv-privileged.pdf

    # Upload the riscv-privileged HTML file
    - name: Upload riscv-privileged.html
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-privileged-${{ env.SHORT_SHA }}.html
        path: ${{ github.workspace }}/build/riscv-privileged.html

    # Upload the riscv-privileged EPUB file
    - name: Upload riscv-privileged.epub
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-privileged-${{ env.SHORT_SHA }}.epub
        path: ${{ github.workspace }}/build/riscv-privileged.epub

    # Upload the riscv-unprivileged PDF file
    - name: Upload riscv-unprivileged.pdf
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-unprivileged-${{ env.SHORT_SHA }}.pdf
        path: ${{ github.workspace }}/build/riscv-unprivileged.pdf

    # Upload the riscv-unprivileged HTML file
    - name: Upload riscv-unprivileged.html
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-unprivileged-${{ env.SHORT_SHA }}.html
        path: ${{ github.workspace }}/build/riscv-unprivileged.html

    # Upload the riscv-unprivileged EPUB file
    - name: Upload riscv-unprivileged.epub
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-unprivileged-${{ env.SHORT_SHA }}.epub
        path: ${{ github.workspace }}/build/riscv-unprivileged.epub

    - name: Create Release
      uses: softprops/action-gh-release@v2
      env:
        GITHUB_TOKEN: ${{ secrets.GHTOKEN }}
      with:
        tag_name: riscv-isa-release-${{ env.SHORT_SHA }}-${{ env.CURRENT_DATE }}
        name: Release riscv-isa-release-${{ env.SHORT_SHA }}-${{ env.CURRENT_DATE }}
        draft: false
        prerelease: false
        make_latest: true
        generate_release_notes: true
        body: |
              This release was created by: ${{ github.event.sender.login }}
              Release of RISC-V ISA, built from commit ${{ env.SHORT_SHA }}, is now available.
        files: |
          ${{ github.workspace }}/build/riscv-privileged.pdf
          ${{ github.workspace }}/build/riscv-privileged.html
          ${{ github.workspace }}/build/riscv-privileged.epub
          ${{ github.workspace }}/build/riscv-unprivileged.pdf
          ${{ github.workspace }}/build/riscv-unprivileged.html
          ${{ github.workspace }}/build/riscv-unprivileged.epub
