leaf-markdown-viewer 1.26.2

Terminal Markdown previewer with a GUI-like experience
name: Contrib Refresh

on:
  workflow_dispatch:
    inputs:
      timestamp:
        description: "Timestamp (optional)"
        required: false
        type: string
        default: "717807600"

permissions:
  contents: write

jobs:
  refresh:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.WORKFLOW_TOKEN }}

      - name: Import GPG key
        env:
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
        run: echo "$GPG_PRIVATE_KEY" | gpg --batch --import

      - name: Configure GPG agent
        run: |
          echo "allow-loopback-pinentry" >> ~/.gnupg/gpg-agent.conf
          echo "allow-preset-passphrase" >> ~/.gnupg/gpg-agent.conf
          gpgconf --kill gpg-agent

      - name: Preset GPG passphrase
        env:
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
        run: |
          KEYGRIP=$(gpg --list-secret-keys --with-keygrip --with-colons \
            | grep '^grp' | head -1 | cut -d: -f10)
          if [ -z "$KEYGRIP" ]; then
            echo "Failed to extract GPG keygrip" >&2
            exit 1
          fi
          gpg-connect-agent "PRESET_PASSPHRASE $KEYGRIP -1 \
            $(echo -n "$GPG_PASSPHRASE" | xxd -p -c 256)" /bye

      - name: Configure git signing
        run: |
          KEY_ID=$(gpg --list-secret-keys --keyid-format long \
            | grep '^sec' | head -1 | awk '{print $2}' \
            | cut -d'/' -f2)
          if [ -z "$KEY_ID" ]; then
            echo "Failed to extract GPG key ID" >&2
            exit 1
          fi
          git config user.name "RivoLink"
          git config user.email "rivo.link@gmail.com"
          git config user.signingkey "$KEY_ID"
          git config commit.gpgsign true

      - name: Resolve timestamp
        env:
          INPUT_TS: ${{ inputs.timestamp }}
        run: |
          if [ -z "$INPUT_TS" ]; then
            echo "TS=$(date +%s)" >> "$GITHUB_ENV"
          else
            echo "TS=${INPUT_TS}" >> "$GITHUB_ENV"
          fi

      - name: Refresh contrib image
        env:
          REPO: ${{ github.repository }}
        run: |
          URL="https://contrib.rocks/image?repo=${REPO}&t=${TS}"

          echo "Repo: ${REPO}"
          echo "URL: ${URL}"
          echo ""

          curl -s -o /dev/null \
            -w "HTTP status: %{http_code}\n" \
            "$URL"

          sed -i "s|contrib.rocks/image?repo=${REPO}[^)]*)|contrib.rocks/image?repo=${REPO}\&t=${TS})|" README.md

          echo "Done"

      - name: Create pull request
        env:
          GH_TOKEN: ${{ secrets.WORKFLOW_TOKEN }}
        run: |
          git diff --quiet README.md && echo "No changes" && exit 0

          BRANCH="chore/contrib-refresh-${TS}"
          git push origin --delete "$BRANCH" 2>/dev/null || true
          git checkout -b "$BRANCH"
          git add README.md
          git commit -m "chore: refresh contrib image"
          git push -u origin "$BRANCH"

          gh pr create \
            --title "chore: refresh contrib image" \
            --body "Refresh contrib.rocks image cache via timestamp update." \
            --base main