|
| 1 | +name: Zulip emoji CI status |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["continuous integration", "continuous integration (mathlib forks)"] |
| 6 | + types: [requested, completed] |
| 7 | + |
| 8 | +# Limit permissions for GITHUB_TOKEN for the entire workflow |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: read |
| 12 | + # All other permissions are implicitly 'none' |
| 13 | + |
| 14 | +jobs: |
| 15 | + update_ci_emoji: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + if: github.repository == 'leanprover-community/mathlib4' |
| 18 | + steps: |
| 19 | + - name: Determine PR number |
| 20 | + id: pr |
| 21 | + env: |
| 22 | + GH_TOKEN: ${{ github.token }} |
| 23 | + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 24 | + HEAD_REPO_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }} |
| 25 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 26 | + run: | |
| 27 | + # Try to get PR number from the workflow_run event |
| 28 | + PR_NUMBER=$(echo '${{ toJSON(github.event.workflow_run.pull_requests) }}' | jq -r '.[0].number // empty') |
| 29 | + # For push-triggered CI (non-fork PRs), pull_requests may be empty; |
| 30 | + # fall back to looking up the PR by branch name. |
| 31 | + # Use owner:branch format to avoid matching the wrong PR when |
| 32 | + # multiple forks use the same branch name. |
| 33 | + if [ -z "$PR_NUMBER" ]; then |
| 34 | + PR_NUMBER=$(gh pr list --repo "${{ github.repository }}" --head "$HEAD_REPO_OWNER:$HEAD_BRANCH" --state open --json number,headRefOid --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number" 2>/dev/null || true) |
| 35 | + fi |
| 36 | + # If owner-qualified lookup failed, try without owner (for same-repo branches) |
| 37 | + if [ -z "$PR_NUMBER" ]; then |
| 38 | + PR_NUMBER=$(gh pr list --repo "${{ github.repository }}" --head "$HEAD_BRANCH" --state open --json number,headRefOid --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number" 2>/dev/null || true) |
| 39 | + fi |
| 40 | + if [ -z "$PR_NUMBER" ]; then |
| 41 | + echo "No PR found for branch $HEAD_REPO_OWNER:$HEAD_BRANCH at $HEAD_SHA, skipping" |
| 42 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 43 | + else |
| 44 | + echo "Found PR #$PR_NUMBER" |
| 45 | + echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" |
| 46 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Determine CI action |
| 50 | + id: action |
| 51 | + if: steps.pr.outputs.skip != 'true' |
| 52 | + run: | |
| 53 | + EVENT_ACTION="${{ github.event.action }}" |
| 54 | + CONCLUSION="${{ github.event.workflow_run.conclusion }}" |
| 55 | + echo "Event action: $EVENT_ACTION, conclusion: $CONCLUSION" |
| 56 | + if [ "$EVENT_ACTION" = "requested" ]; then |
| 57 | + echo "ci_action=ci-running" >> "$GITHUB_OUTPUT" |
| 58 | + elif [ "$EVENT_ACTION" = "completed" ]; then |
| 59 | + case "$CONCLUSION" in |
| 60 | + success) |
| 61 | + echo "ci_action=ci-success" >> "$GITHUB_OUTPUT" |
| 62 | + ;; |
| 63 | + cancelled) |
| 64 | + # Clear the running emoji. A new run may or may not follow |
| 65 | + # (manual cancel, branch deleted, etc.), so don't leave stale 🟡. |
| 66 | + echo "ci_action=ci-cancelled" >> "$GITHUB_OUTPUT" |
| 67 | + ;; |
| 68 | + *) |
| 69 | + echo "ci_action=ci-failure" >> "$GITHUB_OUTPUT" |
| 70 | + ;; |
| 71 | + esac |
| 72 | + fi |
| 73 | +
|
| 74 | + - name: Checkout mathlib repository |
| 75 | + if: steps.pr.outputs.skip != 'true' && steps.action.outputs.ci_action != 'skip' |
| 76 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 77 | + with: |
| 78 | + ref: master |
| 79 | + sparse-checkout: | |
| 80 | + scripts/zulip_emoji_reactions.py |
| 81 | +
|
| 82 | + - name: Set up Python |
| 83 | + if: steps.pr.outputs.skip != 'true' && steps.action.outputs.ci_action != 'skip' |
| 84 | + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 |
| 85 | + with: |
| 86 | + python-version: '3.x' |
| 87 | + |
| 88 | + - name: Install dependencies |
| 89 | + if: steps.pr.outputs.skip != 'true' && steps.action.outputs.ci_action != 'skip' |
| 90 | + run: | |
| 91 | + python -m pip install --upgrade pip |
| 92 | + pip install zulip |
| 93 | +
|
| 94 | + - name: Update CI emoji |
| 95 | + if: steps.pr.outputs.skip != 'true' && steps.action.outputs.ci_action != 'skip' |
| 96 | + env: |
| 97 | + ZULIP_API_KEY: ${{ secrets.ZULIP_API_KEY }} |
| 98 | + ZULIP_EMAIL: github-mathlib4-bot@leanprover.zulipchat.com |
| 99 | + ZULIP_SITE: https://leanprover.zulipchat.com |
| 100 | + run: | |
| 101 | + python scripts/zulip_emoji_reactions.py "$ZULIP_API_KEY" "$ZULIP_EMAIL" "$ZULIP_SITE" "${{ steps.action.outputs.ci_action }}" "none" "${{ steps.pr.outputs.pr_number }}" |
0 commit comments