---
# This action is centrally managed in https://github.com/<organization>/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in
# the above-mentioned repo.

# Create a blog post for a new release and open a PR to the blog repo

name: Release Notifications

on:
  release:
    types:
      - released  # this triggers when a release is published, but does not include pre-releases or drafts

jobs:
  update-blog:
    if: >-
      github.repository_owner == 'LizardByte'
    runs-on: ubuntu-latest
    steps:
      - name: Check topics
        env:
          TOPIC: replicator-release-notifications
        id: check-label
        uses: actions/github-script@v7
        with:
          script: |
            const topic = process.env.TOPIC;
            console.log(`Checking if repo has topic: ${topic}`);

            const repoTopics = await github.rest.repos.getAllTopics({
              owner: context.repo.owner,
              repo: context.repo.repo
            });
            console.log(`Repo topics: ${repoTopics.data.names}`);

            const hasTopic = repoTopics.data.names.includes(topic);
            console.log(`Has topic: ${hasTopic}`);

            core.setOutput('hasTopic', hasTopic);

      - name: Check if latest GitHub release
        id: check-release
        if: >-
          steps.check-label.outputs.hasTopic == 'true'
        uses: actions/github-script@v7
        with:
          script: |
            const latestRelease = await github.rest.repos.getLatestRelease({
              owner: context.repo.owner,
              repo: context.repo.repo
            });

            core.setOutput('isLatestRelease', latestRelease.data.tag_name === context.payload.release.tag_name);

      - name: Checkout blog
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        uses: actions/checkout@v4
        with:
          repository: "LizardByte/LizardByte.github.io"

      - name: Create blog post
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        run: |
          # setup variables
          tag_name="${{ github.event.release.tag_name }}"
          semver="${tag_name#v}"
          repo_lower="$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]')"

          # extract year, month, and day
          year="${semver%%.*}"
          month_day="${semver#*.}"
          month_day="${month_day%%.*}"

          # ensure month_day is 4 digits
          month_day=$(printf "%04d" "$month_day")

          # create the filename
          file_name="_posts/releases/${repo_lower}/${year}-${month_day:0:2}-${month_day:2:2}-v${semver}.md"
          mkdir -p "$(dirname "${file_name}")"

          # create jekyll blog post
          echo "---" > "${file_name}"
          echo "layout: release" >> "${file_name}"
          echo "title: ${{ github.event.repository.name }} ${tag_name} Released" >> "${file_name}"
          echo "release-tag: ${tag_name}" >> "${file_name}"
          echo "gh-repo: ${{ github.repository }}" >> "${file_name}"
          echo "gh-badge: [follow, fork, star]" >> "${file_name}"
          echo "tags: [release, ${repo_lower}]" >> "${file_name}"
          echo "comments: true" >> "${file_name}"
          echo "author: LizardByte-bot" >> "${file_name}"
          echo "---" >> "${file_name}"
          echo "" >> "${file_name}"

          release_body=$(cat <<EOF
          ${{ github.event.release.body }}
          EOF
          )

          echo "${release_body}" >> "${file_name}"

      - name: Create/Update Pull Request
        id: create-pr
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        uses: peter-evans/create-pull-request@v7
        with:
          token: ${{ secrets.GH_BOT_TOKEN }}
          commit-message: |
            chore: Add blog post for ${{ github.event.repository.name }} release ${{ github.event.release.tag_name }}
          branch: bot/add-${{ github.event.repository.name }}-${{ github.event.release.tag_name }}
          delete-branch: true
          title: |
            chore: Add blog post for ${{ github.event.repository.name }} release ${{ github.event.release.tag_name }}
          body: ${{ github.event.release.body }}
          labels:
            blog

      - name: Automerge PR
        env:
          GH_TOKEN: ${{ secrets.GH_BOT_TOKEN }}
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        run: |
          gh \
            pr \
            merge \
            --auto \
            --delete-branch \
            --repo "LizardByte/LizardByte.github.io" \
            --squash \
            "${{ steps.create-pr.outputs.pull-request-number }}"
