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