---
# yamllint disable rule:line-length
name: Tag, release, and publish role based on CHANGELOG.md push
on:  # yamllint disable-line rule:truthy
  push:
    branches:
      - main
    paths:
      - CHANGELOG.md
permissions:
  contents: read
jobs:
  tag_release_publish:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Update pip, git
        run: |
          set -euxo pipefail
          sudo apt update
          sudo apt install -y git

      - name: checkout PR
        uses: actions/checkout@v6

      - name: Get tag and message from the latest CHANGELOG.md commit
        id: tag
        run: |
          set -euxo pipefail
          print=false
          while read -r line; do
              if [[ "$line" =~ ^\[([0-9]+\.[0-9]+\.[0-9]+)\]\ -\ [0-9-]+ ]]; then
                  if [ "$print" = false ]; then
                      _tagname="${BASH_REMATCH[1]}"
                      echo "$line"
                      print=true
                  else
                      break
                  fi
              elif [ "$print" = true ]; then
                  echo "$line"
              fi
          done < CHANGELOG.md > ./.tagmsg.txt
          git fetch --all --tags
          for t in $( git tag -l ); do
              if [ "$t" = "$_tagname" ]; then
                  echo INFO: tag "$t" already exists
                  exit 1
              fi
          done
          # Get name of the branch that the change was pushed to
          _branch="${GITHUB_REF_NAME:-}"
          if [ "$_branch" = master ] || [ "$_branch" = main ]; then
              echo Using branch name ["$_branch"] as push branch
          else
              echo WARNING: GITHUB_REF_NAME ["$_branch"] is not main or master
              _branch=$( git branch -r | grep -o 'origin/HEAD -> origin/.*$' | \
                         awk -F'/' '{print $3}' || : )
          fi
          if [ -z "$_branch" ]; then
              _branch=$( git branch --points-at HEAD --no-color --format='%(refname:short)' )
          fi
          if [ -z "$_branch" ]; then
              echo ERROR: unable to determine push branch
              git branch -a
              exit 1
          fi
          echo "tagname=$_tagname" >> "$GITHUB_OUTPUT"
          echo "branch=$_branch" >> "$GITHUB_OUTPUT"
      - name: Create tag
        uses: mathieudutour/github-tag-action@v6.2
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          custom_tag: ${{ steps.tag.outputs.tagname }}
          tag_prefix: ''

      - name: Create Release
        id: create_release
        uses: ncipollo/release-action@v1
        with:
          tag: ${{ steps.tag.outputs.tagname }}
          name: Version ${{ steps.tag.outputs.tagname }}
          bodyFile: ./.tagmsg.txt
          makeLatest: true

      - name: Publish role to Galaxy
        uses: robertdebock/galaxy-action@1.2.1
        with:
          galaxy_api_key: ${{ secrets.galaxy_api_key }}
          git_branch: ${{ steps.tag.outputs.branch }}
