-
Notifications
You must be signed in to change notification settings - Fork 133
224 lines (197 loc) · 7.9 KB
/
pr-comment-processing.yml
File metadata and controls
224 lines (197 loc) · 7.9 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
name: PR Comment Processing
on:
pull_request_review:
types: [submitted]
issue_comment:
types: [created]
permissions:
contents: write
pull-requests: write
issues: write
jobs:
check_comment:
runs-on: ubuntu-latest
if: github.event_name == 'issue_comment'
outputs:
should_process: ${{ steps.check_comment.outputs.should_process }}
steps:
- name: Check if comment starts with packagefox
id: check_comment
uses: actions/github-script@v7
with:
script: |
const comment = context.payload.comment.body.trim();
if (!comment.toLowerCase().startsWith('packagefox:')) {
core.setOutput('should_process', 'false');
return;
}
// Check if commenter is a maintainer
const { data: collaborators } = await github.rest.repos.listCollaborators({
owner: context.repo.owner,
repo: context.repo.repo
});
const commenter = context.payload.comment.user.login;
const isMaintainer = collaborators.some(collab =>
collab.login === commenter &&
(collab.permissions.admin || collab.permissions.maintain || collab.permissions.write)
);
if (!isMaintainer) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: '❌ Only maintainers can process packagefox commands. Your request has been ignored.'
});
core.setOutput('should_process', 'false');
return;
}
// Add approved label
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
labels: ['approved']
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: '✅ Packagefox command approved by maintainer. Processing will begin shortly.'
});
core.setOutput('should_process', 'true');
process_approved_comment:
needs: check_comment
if: needs.check_comment.outputs.should_process == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: scripts/package-lock.json
- name: Install Script Dependencies
run: cd scripts && npm install
- name: Get changed files in packages/
id: changed-files
uses: tj-actions/changed-files@v46.0.1
with:
files: packages/**
files_ignore: |
packages/*/node_modules/**
packages/*/dist/**
packages/*/build/**
packages/*/.turbo/**
packages/*/.next/**
packages/*/coverage/**
- name: Extract package name from changed files
id: extract-package
if: steps.changed-files.outputs.any_changed == 'true'
run: |
# Get the first changed package directory
PACKAGE_NAME=$(echo "${{ steps.changed-files.outputs.all_changed_files }}" | grep -oE 'packages/([^/]+)' | head -n 1 | sed 's|packages/||')
if [ -n "$PACKAGE_NAME" ]; then
echo "package_name=@microfox/$PACKAGE_NAME" >> $GITHUB_OUTPUT
fi
- name: Process Comment
id: comment_data
run: cd scripts && npm run process-comment
env:
COMMENT_BODY: ${{ github.event.comment.body }}
PR_NUMBER: ${{ github.event.issue.number }}
PR_URL: ${{ github.event.issue.html_url }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGE_NAME: ${{ steps.extract-package.outputs.package_name }}
- name: Prepare packagefox-build.json
id: prepare_file
run: |
set -e
mkdir -p .microfox
EXISTING_JSON=$(cat .microfox/packagefox-build.json 2>/dev/null || echo '{"requests":[]}')
NEW_REQUEST_PAYLOAD='${{ steps.comment_data.outputs.json_payload }}'
if ! command -v jq &> /dev/null; then
echo "jq not found, installing..."
sudo apt-get update && sudo apt-get install -y jq
fi
echo "Existing JSON: $EXISTING_JSON"
echo "New Payload: $NEW_REQUEST_PAYLOAD"
UPDATED_JSON=$(echo "$EXISTING_JSON" | jq --argjson payload "$NEW_REQUEST_PAYLOAD" '
if type == "object" and has("requests") and (.requests | type == "array") then
.requests += [$payload]
else
{"requests": [$payload]}
end
' || echo "{"requests": [$NEW_REQUEST_PAYLOAD]}")
echo "Updated JSON: $UPDATED_JSON"
echo "$UPDATED_JSON" | jq '.' > .microfox/packagefox-build.json
echo "File .microfox/packagefox-build.json prepared successfully."
shell: bash
- name: Generate GitHub App Token
id: generate_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.PACKAGEFOX_APP_ID }}
private_key: ${{ secrets.PACKAGEFOX_PRIVATE_KEY }}
- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
token: ${{ steps.generate_token.outputs.token }}
commit-message: ${{ steps.comment_data.outputs.commit_message }}
branch: ${{ steps.comment_data.outputs.branch_name }}
delete-branch: true
title: ${{ steps.comment_data.outputs.pr_title }}
body: ${{ steps.comment_data.outputs.pr_body }}
add-paths: .microfox/packagefox-build.json
labels: |
automated-pr
from-pr-comments
approved
assignees: ${{ github.actor }}
process_approval:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request_review'
steps:
- name: Check if review is approval
id: check_approval
uses: actions/github-script@v7
with:
script: |
const review = context.payload.review;
if (review.state !== 'approved') {
return;
}
// Check if reviewer is a maintainer
const { data: collaborators } = await github.rest.repos.listCollaborators({
owner: context.repo.owner,
repo: context.repo.repo
});
const reviewer = review.user.login;
const isMaintainer = collaborators.some(collab =>
collab.login === reviewer &&
(collab.permissions.admin || collab.permissions.maintain || collab.permissions.write)
);
if (!isMaintainer) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: '❌ Only maintainers can approve PRs. Your approval has been ignored.'
});
return;
}
// Add approved label
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
labels: ['approved']
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: '✅ PR approved by maintainer. Processing will begin shortly.'
});