-
Notifications
You must be signed in to change notification settings - Fork 16
311 lines (260 loc) · 10.2 KB
/
pr-bot.yaml
File metadata and controls
311 lines (260 loc) · 10.2 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
name: PR Bot
on:
issue_comment:
types: [created]
permissions:
contents: write
pull-requests: write
issues: write
actions: write
attestations: write
id-token: write
packages: write
jobs:
handle-command:
# Only run on PR comments (not issue comments) and from authorized users
if: |
github.event.issue.pull_request &&
startsWith(github.event.comment.body, '/') &&
(
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'COLLABORATOR'
)
runs-on: ubuntu-latest
steps:
- name: Get PR info
id: pr-info
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_DATA=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.issue.number }})
echo "head_sha=$(echo "$PR_DATA" | jq -r '.head.sha')" >> $GITHUB_OUTPUT
echo "head_ref=$(echo "$PR_DATA" | jq -r '.head.ref')" >> $GITHUB_OUTPUT
echo "base_ref=$(echo "$PR_DATA" | jq -r '.base.ref')" >> $GITHUB_OUTPUT
- name: Parse command
id: parse
run: |
COMMENT="${{ github.event.comment.body }}"
COMMAND=$(echo "$COMMENT" | head -1 | awk '{print $1}' | tr -d '/')
ARGS=$(echo "$COMMENT" | head -1 | cut -d' ' -f2- -s)
echo "command=$COMMAND" >> $GITHUB_OUTPUT
echo "args=$ARGS" >> $GITHUB_OUTPUT
echo "Parsed command: $COMMAND, args: $ARGS"
- name: React to comment
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='eyes' || true
- name: Handle /help
if: steps.parse.outputs.command == 'help'
env:
GH_TOKEN: ${{ github.token }}
run: |
HELP_MSG=$(cat << 'EOF'
## 🤖 PR Bot Commands
| Command | Description |
|---------|-------------|
| `/help` | Show this help message |
| `/build` | Build all changed recipes in this PR |
| `/build x86_64` | Build for specific host (x86_64, aarch64) |
| `/build <recipe>` | Build a specific recipe |
| `/lint` | Run linter on changed recipes |
| `/check` | Check upstream versions |
| `/approve` | Approve and merge (maintainers only) |
<sub>Commands are only available to repository collaborators.</sub>
EOF
)
gh pr comment ${{ github.event.issue.number }} \
--repo ${{ github.repository }} \
--body "$HELP_MSG"
- name: Handle /build
if: steps.parse.outputs.command == 'build'
env:
GH_TOKEN: ${{ github.token }}
run: |
ARGS="${{ steps.parse.outputs.args }}"
PR_NUM="${{ github.event.issue.number }}"
# Parse args: each word is either a host or a package
HOST="ALL"
PACKAGE=""
for arg in $ARGS; do
case "$arg" in
x86_64|X86_64|x86_64-linux) HOST="x86_64-linux" ;;
aarch64|AARCH64|arm64|aarch64-linux) HOST="aarch64-linux" ;;
all|ALL) HOST="ALL" ;;
*) PACKAGE="$arg" ;;
esac
done
# Build info for comment
PKG_INFO=""
if [ -n "$PACKAGE" ]; then
PKG_INFO="| **Package** | \`${PACKAGE}\` |"
fi
# Post acknowledgment
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "🚀 **Build triggered!**
| | |
|---|---|
| **PR** | #${PR_NUM} |
| **Host** | \`${HOST}\` |
${PKG_INFO}
| **Triggered by** | @${{ github.event.comment.user.login }} |
Build will start shortly. I'll post results when complete.
[View workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})"
# Trigger the build workflow
gh workflow run pr-build-test.yaml \
--repo ${{ github.repository }} \
-f pr_number="$PR_NUM" \
-f host="$HOST" \
-f package="$PACKAGE"
- name: Checkout for lint
if: steps.parse.outputs.command == 'lint'
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Handle /lint
if: steps.parse.outputs.command == 'lint'
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUM="${{ github.event.issue.number }}"
HEAD_SHA="${{ steps.pr-info.outputs.head_sha }}"
BASE_REF="${{ steps.pr-info.outputs.base_ref }}"
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "🔍 **Running linter...**"
# Checkout PR branch
gh pr checkout "$PR_NUM"
# Download sbuild
curl -fsSL "https://github.com/pkgforge/sbuilder/releases/download/nightly/sbuild-x86_64-linux" \
-o /tmp/sbuild && chmod +x /tmp/sbuild
# Get changed files
CHANGED=$(git diff --name-only "origin/${BASE_REF}" HEAD -- 'binaries/**/*.yaml' 'packages/**/*.yaml' 2>/dev/null || true)
if [ -z "$CHANGED" ]; then
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "✅ **Lint complete** - No recipe files changed"
exit 0
fi
# Run linter on each file
RESULTS="| Recipe | Status | Details |
|--------|--------|---------|"
HAS_ERRORS=false
for file in $CHANGED; do
if [ -f "$file" ]; then
if /tmp/sbuild lint "$file" > /tmp/lint-output.txt 2>&1; then
RESULTS="${RESULTS}
| \`${file}\` | ✅ Valid | - |"
else
ERROR_MSG=$(cat /tmp/lint-output.txt | head -3 | tr '\n' ' ' | head -c 100)
RESULTS="${RESULTS}
| \`${file}\` | ❌ Invalid | ${ERROR_MSG:-Unknown error} |"
HAS_ERRORS=true
fi
fi
done
if [ "$HAS_ERRORS" = true ]; then
EMOJI="❌"
STATUS="Lint Failed"
else
EMOJI="✅"
STATUS="Lint Passed"
fi
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "## ${EMOJI} ${STATUS}
${RESULTS}
<sub>Triggered by @${{ github.event.comment.user.login }}</sub>"
- name: Checkout for check
if: steps.parse.outputs.command == 'check'
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Handle /check
if: steps.parse.outputs.command == 'check'
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUM="${{ github.event.issue.number }}"
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "🔍 **Checking upstream versions...**"
# Checkout PR branch
gh pr checkout "$PR_NUM"
# Download sbuild
curl -fsSL "https://github.com/pkgforge/sbuilder/releases/download/nightly/sbuild-x86_64-linux" \
-o /tmp/sbuild && chmod +x /tmp/sbuild || {
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "⚠️ Failed to download sbuild"
exit 1
}
# Run version check
/tmp/sbuild meta check-updates \
--recipes ./binaries ./packages \
--output /tmp/updates.json \
--parallel 5 \
--timeout 30 || true
if [ ! -f /tmp/updates.json ] || [ "$(jq 'length' /tmp/updates.json)" = "0" ]; then
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "✅ **Version check complete** - All packages are up to date!"
exit 0
fi
# Format results
RESULTS="| Package | Current | Upstream |
|---------|---------|----------|"
while read -r line; do
pkg=$(echo "$line" | jq -r '.pkg')
current=$(echo "$line" | jq -r '.current_version')
upstream=$(echo "$line" | jq -r '.upstream_version')
RESULTS="${RESULTS}
| \`${pkg}\` | ${current} | **${upstream}** |"
done < <(jq -c '.[]' /tmp/updates.json)
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "## 📦 Version Check Results
${RESULTS}
<sub>Triggered by @${{ github.event.comment.user.login }}</sub>"
- name: Handle /approve
if: steps.parse.outputs.command == 'approve'
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUM="${{ github.event.issue.number }}"
USER="${{ github.event.comment.user.login }}"
ASSOC="${{ github.event.comment.author_association }}"
# Only owners/admins can approve
if [[ "$ASSOC" != "OWNER" && "$ASSOC" != "MEMBER" ]]; then
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "⚠️ @${USER} Only repository owners/members can use \`/approve\`"
exit 0
fi
# Approve the PR
gh pr review "$PR_NUM" \
--repo ${{ github.repository }} \
--approve \
--body "✅ Approved via \`/approve\` command by @${USER}"
gh pr comment "$PR_NUM" \
--repo ${{ github.repository }} \
--body "✅ **PR Approved** by @${USER}
Ready to merge when checks pass."
- name: Handle unknown command
if: |
steps.parse.outputs.command != 'help' &&
steps.parse.outputs.command != 'build' &&
steps.parse.outputs.command != 'lint' &&
steps.parse.outputs.command != 'check' &&
steps.parse.outputs.command != 'approve'
env:
GH_TOKEN: ${{ github.token }}
run: |
COMMAND="${{ steps.parse.outputs.command }}"
gh pr comment ${{ github.event.issue.number }} \
--repo ${{ github.repository }} \
--body "❓ Unknown command: \`/${COMMAND}\`
Use \`/help\` to see available commands."