name: Update Genuary

on:
  workflow_dispatch:
  schedule:
    # We want '0 9 * 1 *' in Pacific Time, but GitHub Actions doesn't support timezones.
    # So we use '0 17 * 1 *' in UTC, which is 9 AM in Pacific Time.
    - cron: '0 17 * * *'

jobs:
  update-genuary:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get pins
        run: | # TODO: Update CEL expression to filter by the current year.
          go run github.com/ethanthatonekid/pins@latest \
            get -o /tmp/pins $GUILD_ID "channel_parent_id == $CHANNEL_ID"
        env:
          GUILD_ID: ${{ secrets.GUILD_ID }}
          CHANNEL_ID: ${{ secrets.GENUARY_CHANNEL_ID }}
          DISCORD_TOKEN: ${{ secrets.GENUARY_DISCORD_TOKEN }}

      - name: Transform Genuary data
        id: transform-genuary
        run: |
          year=$(date +%Y)
          echo "year=$year" >> $GITHUB_OUTPUT

          # TODO: Remove $year as an argument once the CEL expression is updated.
          node scripts/transform-genuary/index.js /tmp/pins/*.json $year \
            > "src/routes/(site)/genuary/$year/data.json"

          if git diff --exit-code --quiet; then
            echo "updated=0" >> $GITHUB_OUTPUT
          else
            echo "updated=1" >> $GITHUB_OUTPUT
          fi

      - name: Create pull request
        if: steps.transform-genuary.outputs.updated == 1
        uses: peter-evans/create-pull-request@v4
        with:
          title: |-
            Update Genuary ${{ steps.transform-genuary.outputs.year }} :calendar: :sparkles: :art:
          body: |-
            This pull request was automatically generated by GitHub Actions.

            :sparkles: :sparkles: :sparkles:
          commit-message: |-
            Update Genuary ${{ steps.transform-genuary.outputs.year }} :calendar: :sparkles: :art:
          labels: genuary
          branch: genuary
