Skip to content

Commit e646b0d

Browse files
vprashrexAyush8923
andauthored
fix(pr-formatter): Add GitHub Action for PR formatter (#234)
Co-authored-by: Ayush <80516839+Ayush8923@users.noreply.github.com>
1 parent 0f840d0 commit e646b0d

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

‎.github/workflows/pr-formatter.yml‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
name: Format PR with OpenAI
2+
3+
on:
4+
pull_request:
5+
types: [opened]
6+
7+
permissions:
8+
pull-requests: write
9+
10+
jobs:
11+
format-pr:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Rewrite PR body
15+
id: body
16+
uses: actions/github-script@v8
17+
env:
18+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
19+
with:
20+
script: |
21+
const pr = context.payload.pull_request;
22+
23+
const response = await fetch('https://api.openai.com/v1/responses', {
24+
method: 'POST',
25+
headers: {
26+
'Content-Type': 'application/json',
27+
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
28+
},
29+
body: JSON.stringify({
30+
model: 'gpt-4o-mini',
31+
input: `Rewrite this GitHub pull request body using the exact format below. Keep it short, crisp and easy to understand. Do NOT add any information that wasn't in the original. Output ONLY the formatted markdown, nothing else.
32+
33+
Format:
34+
## Issue
35+
Closes <issue url or number — copy it exactly from the original body. If none, write "N/A">
36+
37+
## Summary
38+
(What this PR does. Use bullet points. Nest sub-bullets where it adds clarity.)
39+
40+
## Checklist
41+
42+
Before submitting a pull request, please ensure that you mark these task.
43+
44+
(Copy the checklist items from the original body exactly, preserving each [ ] or [x] state. If the original has none, output the two default items below unchecked.)
45+
- [ ] Ran \`npm run dev\` and \`npm run build\` in the repository root and test.
46+
- [ ] If you've fixed a bug or added code that is tested
47+
48+
Here is an example of a well-formatted body:
49+
50+
## Issue
51+
52+
Closes https://github.com/ProjectTech4DevAI/kaapi-backend/issues/903
53+
54+
## Summary
55+
* Implement edit and delete functionality for Organizations and Projects.
56+
* Support two types of deletion:
57+
- **Soft Delete**
58+
* Mark the organization/project as inactive instead of permanently removing it.
59+
* Retain all associated data for future recovery or auditing purposes.
60+
- **Hard Delete**
61+
* Permanently remove the organization/project and all related data from the system.
62+
* Handle authentication-related edge cases.
63+
64+
## Checklist
65+
66+
Before submitting a pull request, please ensure that you mark these task.
67+
68+
- [x] Ran \`npm run dev\` and \`npm run build\` in the repository root and test.
69+
- [x] If you've fixed a bug or added code that is tested
70+
71+
Original PR title: ${pr.title}
72+
Original PR body:
73+
${pr.body || '(empty)'}`
74+
})
75+
});
76+
77+
const data = await response.json();
78+
79+
const text = data.output
80+
?.find(o => o.type === 'message')
81+
?.content?.find(c => c.type === 'output_text')
82+
?.text;
83+
84+
if (!text) {
85+
core.setFailed(`OpenAI API error: ${JSON.stringify(data)}`);
86+
return;
87+
}
88+
89+
return text;
90+
91+
- name: Rewrite PR title
92+
id: title
93+
uses: actions/github-script@v8
94+
env:
95+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
96+
with:
97+
script: |
98+
const formattedBody = ${{ steps.body.outputs.result }};
99+
100+
const response = await fetch('https://api.openai.com/v1/responses', {
101+
method: 'POST',
102+
headers: {
103+
'Content-Type': 'application/json',
104+
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
105+
},
106+
body: JSON.stringify({
107+
model: 'gpt-4o-mini',
108+
input: `Based on the PR body below, generate a semantic-release style title in this exact format:
109+
"type(scope): 4-6 word summary"
110+
111+
Pick the type based on these release rules:
112+
- feat -> new feature (minor release)
113+
- fix -> bug fix (patch release)
114+
- chore -> maintenance / tooling (patch release)
115+
- docs -> documentation only (patch release)
116+
- refactor -> code restructuring, no behavior change (patch release)
117+
118+
"scope" is the affected module/area in lowercase (e.g. collection, auth, assessment). Summary is capitalized, imperative or descriptive, no trailing period.
119+
120+
Examples:
121+
- feat(collection): Added new fields
122+
- fix(auth): Prevent login for deleted org
123+
- refactor(assessment): Simplify schema builder
124+
- docs(readme): Update setup steps
125+
126+
Output ONLY the title, nothing else.
127+
128+
PR body:
129+
${formattedBody}`
130+
})
131+
});
132+
133+
const data = await response.json();
134+
135+
const text = data.output
136+
?.find(o => o.type === 'message')
137+
?.content?.find(c => c.type === 'output_text')
138+
?.text;
139+
140+
if (!text) {
141+
core.setFailed(`OpenAI API error: ${JSON.stringify(data)}`);
142+
return;
143+
}
144+
145+
return text.replace(/^["']|["']$/g, '');
146+
147+
- name: Update PR
148+
uses: actions/github-script@v8
149+
with:
150+
script: |
151+
const formattedBody = ${{ steps.body.outputs.result }};
152+
const formattedTitle = ${{ steps.title.outputs.result }};
153+
const original = context.payload.pull_request.body || '(empty)';
154+
155+
await github.rest.pulls.update({
156+
owner: context.repo.owner,
157+
repo: context.repo.repo,
158+
pull_number: context.payload.pull_request.number,
159+
title: formattedTitle,
160+
body: `${formattedBody}\n\n<details><summary>Original PR description</summary>\n\n${original}\n\n</details>`
161+
});

0 commit comments

Comments
 (0)