---
# This action is a candidate to centrally manage in https://github.com/<organization>/.github/
# If more pacman packages are developed, consider moving this action to the organization's .github repository,
# using the `pacman-pkg` repository label to identify repositories that should trigger have this workflow.

# Update pacman repo on release events.

name: Update pacman repo

on:
  release:
    types: [released]

concurrency:
  group: "${{ github.workflow }}-${{ github.event.release.tag_name }}"
  cancel-in-progress: true

jobs:
  update-homebrew-release:
    if: >-
      github.repository_owner == 'LizardByte'
    runs-on: ubuntu-latest
    steps:
      - name: Check if pacman repo
        env:
          TOPIC: pacman-pkg
        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 pacman-repo
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        uses: actions/checkout@v4
        with:
          repository: ${{ github.repository_owner }}/pacman-repo

      - name: Prep
        id: prep
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        run: |
          echo "pkg_name=$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

      - name: Download release asset
        id: download
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true'
        uses: robinraju/release-downloader@v1.11
        with:
          repository: "${{ github.repository }}"
          tag: "${{ github.event.release.tag_name }}"
          fileName: "*.pkg.tar.gz"
          tarBall: false
          zipBall: false
          out-file-path: "pkgbuilds/${{ steps.prep.outputs.pkg_name }}"
          extract: true

      - name: Create/Update Pull Request
        if: >-
          steps.check-label.outputs.hasTopic == 'true' &&
          steps.check-release.outputs.isLatestRelease == 'true' &&
          fromJson(steps.download.outputs.downloaded_files)[0]
        uses: peter-evans/create-pull-request@v7
        with:
          add-paths: |
            pkgbuilds/*
          token: ${{ secrets.GH_BOT_TOKEN }}
          commit-message: "chore: Update ${{ github.repository }} to ${{ github.event.release.tag_name }}"
          branch: bot/bump-${{ github.repository }}-${{ github.event.release.tag_name }}
          delete-branch: true
          title: "chore: Update ${{ github.repository }} to ${{ github.event.release.tag_name }}"
          body: ${{ github.event.release.body }}
          labels: |
            auto-approve
            auto-merge
