on:
  issue_comment:
    types: [created]
name: Commands
jobs:
  document:
    if: startsWith(github.event.comment.body, '/document')
    name: document
    # macos is actually better here due to native binary packages
    runs-on: macos-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - uses: r-lib/actions/pr-fetch@master
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: r-lib/actions/setup-r@master
        with:
          install-r: false
      - name: Configure Git identity
        run: |
          env | sort
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
        shell: bash
      - name: Install dependencies
        run: |
          install.packages(c("remotes", "roxygen2"), type = "binary")
          remotes::install_deps(dependencies = TRUE)
        shell: Rscript {0}
      - name: Document
        run: |
          roxygen2::roxygenise()
        shell: Rscript {0}
      - name: commit
        run: |
          if [ -n "$(git status --porcelain man/ NAMESPACE)" ]; then
            git add man/ NAMESPACE
            git commit -m 'Document'
          fi
      - uses: r-lib/actions/pr-push@master
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
  style:
    if: startsWith(github.event.comment.body, '/style')
    name: style
    # macos is actually better here due to native binary packages
    runs-on: macos-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - uses: r-lib/actions/pr-fetch@master
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: r-lib/actions/setup-r@master
        with:
          install-r: false
      - name: Configure Git identity
        run: |
          env | sort
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
        shell: bash
      - name: Install dependencies
        run: |
          install.packages(c("styler", "roxygen2"), type = "binary")
        shell: Rscript {0}
      - name: Style
        run: |
          styler::style_pkg(strict = FALSE)
        shell: Rscript {0}
      - name: commit
        run: |
          if [ -n "$(git status --porcelain '*.R' '*.Rmd')" ]; then
            git add '*.R' '*.Rmd'
            git commit -m 'Style'
          fi
      - uses: r-lib/actions/pr-push@master
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
  merge:
    if: startsWith(github.event.comment.body, '/merge')
    name: merge
    runs-on: ubuntu-22.04
    steps:
      - name: Create and merge pull request
        run: |
          set -exo pipefail
          PR_DETAILS=$( curl -s --header "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} )
          echo "$PR_DETAILS" | jq .
          PR_BASE=$(echo "$PR_DETAILS" | jq -r .base.ref)
          PR_HEAD=$(echo "$PR_DETAILS" | jq -r .head.ref)
          PR_URL=$(curl -s -X POST --header "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" --data '{ "head": "'$PR_BASE'", "base": "'$PR_HEAD'", "title": "Merge back PR target branch", "body": "Target: #${{ github.event.issue.number }}" }' https://api.github.com/repos/${{ github.repository }}/pulls | jq -r .url )
          echo $PR_URL
          # Merging here won't run CI/CD
          # curl -s -X PUT --header "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $PR_URL/merge
  # A mock job just to ensure we have a successful build status
  finish:
    runs-on: ubuntu-22.04
    steps:
      - run: true
