-
Notifications
You must be signed in to change notification settings - Fork 50
188 lines (159 loc) · 6.6 KB
/
Copy pathpr-summary-generate.yml
File metadata and controls
188 lines (159 loc) · 6.6 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
name: Generate PR Summary
on:
pull_request:
types: [opened]
issue_comment:
types: [created]
permissions:
contents: read
pull-requests: write
models: read
jobs:
generate-summary:
# Run when:
# 1. A PR is opened without a description, OR
# 2. Someone comments "/generate-summary" on a PR
if: |
(github.event_name == 'pull_request' && (github.event.pull_request.body == '' || github.event.pull_request.body == null)) ||
(github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '/generate-summary'))
runs-on: ubuntu-latest
outputs:
pr-number: ${{ steps.pr-number.outputs.number }}
steps:
- name: Determine PR number
id: pr-number
env:
EVENT_NAME: ${{ github.event_name }}
PR_NUMBER: ${{ github.event.pull_request.number }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
run: |
if [ "$EVENT_NAME" = "pull_request" ]; then
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
else
echo "number=$ISSUE_NUMBER" >> "$GITHUB_OUTPUT"
fi
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: React to comment
if: github.event_name == 'issue_comment'
uses: actions/github-script@v7
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket',
});
- name: Get PR diff summary
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ steps.pr-number.outputs.number }}
run: |
gh pr diff "$PR_NUMBER" --stat > /tmp/diff-stat.txt 2>/dev/null || echo "Unable to retrieve diff" > /tmp/diff-stat.txt
gh pr diff "$PR_NUMBER" --name-only > /tmp/changed-files.txt 2>/dev/null || echo "" > /tmp/changed-files.txt
- name: Read format instructions
run: |
if [ -f .github/copilot-instructions.md ]; then
cat .github/copilot-instructions.md > /tmp/format-instructions.txt
else
echo "No format instructions found" > /tmp/format-instructions.txt
fi
- name: Generate summary with GitHub Models
id: generate
uses: actions/github-script@v7
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
script: |
const fs = require('fs');
const diffStat = fs.readFileSync('/tmp/diff-stat.txt', 'utf8');
const changedFiles = fs.readFileSync('/tmp/changed-files.txt', 'utf8');
const formatInstructions = fs.readFileSync('/tmp/format-instructions.txt', 'utf8');
const prompt = `You are generating a pull request description for an automated sync from an internal development repository to the public GitHub repository.
Here are the format instructions to follow:
---
${formatInstructions}
---
Here are the changed files in this PR:
---
${changedFiles}
---
Here is the diff stat:
---
${diffStat}
---
Generate a PR description that follows the format instructions exactly. Categorize files correctly:
- Files under Samples/ that appear new → New samples
- Files under Samples/ that are modified → Updated samples (use the sample directory name)
- Files under Kits/ → Updated kits
- Everything else (Media/, configs, docs) → General
Also generate a concise PR title (not the H2 heading) that summarizes the changes in
a few words (e.g., "Add TriangleWave sample, update ATGTK helpers").
Output a JSON object with two fields:
- "title": the PR title string
- "body": the formatted PR description following the instructions
Output ONLY valid JSON, nothing else.`;
const response = await fetch('https://models.github.ai/inference/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'openai/gpt-4o-mini',
messages: [
{ role: 'user', content: prompt }
],
temperature: 0.3,
response_format: { type: 'json_object' },
}),
});
if (!response.ok) {
const error = await response.text();
core.setFailed(`GitHub Models API error: ${response.status} - ${error}`);
return;
}
const result = await response.json();
let content = result.choices[0].message.content.trim();
// Strip markdown code fences if present
content = content.replace(/^```(?:json)?\s*\n?/i, '').replace(/\n?```\s*$/i, '');
let parsed;
try {
parsed = JSON.parse(content);
} catch (e) {
// Fallback: treat entire response as body if JSON parsing fails
parsed = { title: '', body: content };
}
fs.writeFileSync('/tmp/pr-summary.md', parsed.body);
core.setOutput('summary', parsed.body);
core.setOutput('title', parsed.title);
- name: Update PR title and description
if: steps.generate.outputs.summary != ''
uses: actions/github-script@v7
env:
PR_TITLE: ${{ steps.generate.outputs.title }}
PR_NUMBER: ${{ steps.pr-number.outputs.number }}
with:
script: |
const fs = require('fs');
const summary = fs.readFileSync('/tmp/pr-summary.md', 'utf8');
const title = process.env.PR_TITLE || '';
const update = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: parseInt(process.env.PR_NUMBER, 10),
body: summary,
};
if (title) {
update.title = title;
}
await github.rest.pulls.update(update);
console.log('PR title and description updated successfully');
evaluate-summary:
needs: generate-summary
uses: ./.github/workflows/pr-summary-eval-reusable.yml
with:
pr-number: ${{ fromJSON(needs.generate-summary.outputs.pr-number) }}