name: Auto Label PR on: pull_request: types: [opened, synchronize, reopened] jobs: add-automerge-label: runs-on: [macos, host] steps: - name: Add 'automerge' label to PR env: CI_TOKEN: ${{ secrets.CI_TOKEN }} GITEA_BASE_URL: https://gitea.jaybe.dev PR_NUMBER: ${{ github.event.pull_request.number }} REPO: ${{ github.repository }} run: | set -euo pipefail if [ -z "${CI_TOKEN:-}" ]; then echo "CI_TOKEN not set; skipping labeling"; exit 0; fi OWNER=$(echo "$REPO" | cut -d'/' -f1) NAME=$(echo "$REPO" | cut -d'/' -f2) echo "Resolving existing label id for 'automerge' in $OWNER/$NAME (no creation)" curl -sS -o /tmp/labels.json -H "Authorization: token ${CI_TOKEN}" \ "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/labels?limit=1000" || true # Extract all ids whose name == automerge, prefer the smallest id (oldest) LID=$(tr '\n' ' ' < /tmp/labels.json | \ grep -o '"id":[0-9][0-9]*[^\}]*"name":"automerge"' | \ sed -E 's/.*"id":([0-9]+).*/\1/' | sort -n | head -n1 || true) if [ -z "${LID:-}" ]; then echo "No existing 'automerge' label found; skipping labeling"; exit 0; fi echo "Labeling PR #$PR_NUMBER with id $LID" curl -sS -X POST \ -H "Content-Type: application/json" \ -H "Authorization: token ${CI_TOKEN}" \ "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/issues/${PR_NUMBER}/labels" \ -d '{"labels":['"${LID}"']}' || true