|
| 1 | +name: Format PR icons |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + pr_number: |
| 7 | + description: Pull request number to format |
| 8 | + required: true |
| 9 | + type: number |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + issues: read |
| 14 | + pull-requests: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + format: |
| 18 | + name: Format added icons |
| 19 | + runs-on: ubuntu-latest |
| 20 | + env: |
| 21 | + PR_NUMBER: ${{ inputs.pr_number }} |
| 22 | + GH_TOKEN: ${{ github.token }} |
| 23 | + steps: |
| 24 | + - name: Check out trusted formatter |
| 25 | + uses: actions/checkout@v7 |
| 26 | + with: |
| 27 | + path: base |
| 28 | + ref: ${{ github.event.repository.default_branch }} |
| 29 | + fetch-depth: 1 |
| 30 | + |
| 31 | + - name: Get pull request head |
| 32 | + id: pr |
| 33 | + run: | |
| 34 | + gh pr view "$PR_NUMBER" --repo "${{ github.repository }}" --json \ |
| 35 | + headRefName,headRepository,headRefOid,maintainerCanModify \ |
| 36 | + --jq '"head_ref=\(.headRefName)\nhead_repository=\(.headRepository.nameWithOwner)\nhead_sha=\(.headRefOid)\nmaintainer_can_modify=\(.maintainerCanModify)"' >> "$GITHUB_OUTPUT" |
| 37 | +
|
| 38 | + - name: Require maintainer edits |
| 39 | + if: >- |
| 40 | + steps.pr.outputs.head_repository != github.repository && |
| 41 | + steps.pr.outputs.maintainer_can_modify != 'true' |
| 42 | + run: | |
| 43 | + echo "This PR does not allow edits from maintainers." >&2 |
| 44 | + exit 1 |
| 45 | +
|
| 46 | + - name: Check out pull request |
| 47 | + uses: actions/checkout@v7 |
| 48 | + with: |
| 49 | + repository: ${{ steps.pr.outputs.head_repository }} |
| 50 | + ref: ${{ steps.pr.outputs.head_sha }} |
| 51 | + path: pr |
| 52 | + token: ${{ github.token }} |
| 53 | + |
| 54 | + - name: Set up uv |
| 55 | + uses: astral-sh/setup-uv@v7 |
| 56 | + with: |
| 57 | + enable-cache: true |
| 58 | + |
| 59 | + - name: Set up Python |
| 60 | + uses: actions/setup-python@v7 |
| 61 | + with: |
| 62 | + python-version: "3.13" |
| 63 | + |
| 64 | + - name: Format added icons |
| 65 | + id: format |
| 66 | + run: | |
| 67 | + set -euo pipefail |
| 68 | + mkdir -p format/icons |
| 69 | +
|
| 70 | + mapfile -t added_icons < <( |
| 71 | + gh api --paginate "repos/${{ github.repository }}/pulls/$PR_NUMBER/files" \ |
| 72 | + --jq '.[] | select(.status == "added" and (.filename | startswith("icons/"))) | .filename' |
| 73 | + ) |
| 74 | +
|
| 75 | + if ((${#added_icons[@]} == 0)); then |
| 76 | + echo "No added icons found in PR #$PR_NUMBER." |
| 77 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | +
|
| 81 | + for path in "${added_icons[@]}"; do |
| 82 | + relative_path="${path#icons/}" |
| 83 | + if [[ "$relative_path" == */* ]]; then |
| 84 | + echo "Icon path must be directly under icons/: $path" >&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + cp "pr/$path" "format/icons/$relative_path" |
| 88 | + done |
| 89 | +
|
| 90 | + ( |
| 91 | + cd format |
| 92 | + uv run --project ../base --locked python ../base/format_icons.py |
| 93 | + ) |
| 94 | +
|
| 95 | + changed=false |
| 96 | + for path in "${added_icons[@]}"; do |
| 97 | + relative_path="${path#icons/}" |
| 98 | + if ! cmp -s "format/icons/$relative_path" "pr/$path"; then |
| 99 | + cp "format/icons/$relative_path" "pr/$path" |
| 100 | + changed=true |
| 101 | + fi |
| 102 | + done |
| 103 | + echo "changed=$changed" >> "$GITHUB_OUTPUT" |
| 104 | +
|
| 105 | + - name: Commit formatted icons to pull request |
| 106 | + if: steps.format.outputs.changed == 'true' |
| 107 | + working-directory: pr |
| 108 | + env: |
| 109 | + HEAD_REF: ${{ steps.pr.outputs.head_ref }} |
| 110 | + PUSH_TOKEN: >- |
| 111 | + ${{ steps.pr.outputs.head_repository == github.repository && |
| 112 | + github.token || |
| 113 | + secrets.MAINTAINER_TOKEN }} |
| 114 | + run: | |
| 115 | + set -euo pipefail |
| 116 | + if [[ -z "$PUSH_TOKEN" ]]; then |
| 117 | + echo "MAINTAINER_TOKEN is required to push to fork PR branches." >&2 |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + git config user.name "github-actions[bot]" |
| 121 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 122 | + git add -- icons |
| 123 | + git commit -m "Format icons" |
| 124 | + git -c "http.extraheader=AUTHORIZATION: bearer $PUSH_TOKEN" \ |
| 125 | + push origin "HEAD:$HEAD_REF" |
0 commit comments