Skip to content

Commit 2a36934

Browse files
committed
ci: add GitHub Actions workflow to format PR icons
1 parent 16c7b66 commit 2a36934

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)