-
Notifications
You must be signed in to change notification settings - Fork 0
CI: Add GitHub Action for PR formatter #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| name: Format PR with OpenAI | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened] | ||
|
|
||
| permissions: | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| format-pr: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Rewrite PR body | ||
| id: body | ||
| uses: actions/github-script@v8 | ||
| env: | ||
| OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | ||
| with: | ||
| script: | | ||
| const pr = context.payload.pull_request; | ||
|
|
||
| const response = await fetch('https://api.openai.com/v1/responses', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: 'gpt-4o-mini', | ||
| 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. | ||
|
|
||
| Format: | ||
| ## Issue | ||
| Closes <issue url or number — copy it exactly from the original body. If none, write "N/A"> | ||
|
|
||
| ## Summary | ||
| (What this PR does. Use bullet points. Nest sub-bullets where it adds clarity.) | ||
|
|
||
| ## Checklist | ||
|
|
||
| Before submitting a pull request, please ensure that you mark these task. | ||
|
|
||
| (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.) | ||
| - [ ] Ran \`npm run dev\` and \`npm run build\` in the repository root and test. | ||
| - [ ] If you've fixed a bug or added code that is tested | ||
|
Ayush8923 marked this conversation as resolved.
|
||
|
|
||
| Here is an example of a well-formatted body: | ||
|
|
||
| ## Issue | ||
|
|
||
| Closes https://github.com/ProjectTech4DevAI/kaapi-backend/issues/903 | ||
|
|
||
| ## Summary | ||
| * Implement edit and delete functionality for Organizations and Projects. | ||
| * Support two types of deletion: | ||
| - **Soft Delete** | ||
| * Mark the organization/project as inactive instead of permanently removing it. | ||
| * Retain all associated data for future recovery or auditing purposes. | ||
| - **Hard Delete** | ||
| * Permanently remove the organization/project and all related data from the system. | ||
| * Handle authentication-related edge cases. | ||
|
|
||
| ## Checklist | ||
|
|
||
| Before submitting a pull request, please ensure that you mark these task. | ||
|
|
||
| - [x] Ran \`npm run dev\` and \`npm run build\` in the repository root and test. | ||
| - [x] If you've fixed a bug or added code that is tested | ||
|
|
||
|
Ayush8923 marked this conversation as resolved.
|
||
| Original PR title: ${pr.title} | ||
| Original PR body: | ||
| ${pr.body || '(empty)'}` | ||
| }) | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| const text = data.output | ||
| ?.find(o => o.type === 'message') | ||
| ?.content?.find(c => c.type === 'output_text') | ||
| ?.text; | ||
|
|
||
| if (!text) { | ||
| core.setFailed(`OpenAI API error: ${JSON.stringify(data)}`); | ||
| return; | ||
| } | ||
|
|
||
| return text; | ||
|
|
||
| - name: Rewrite PR title | ||
| id: title | ||
| uses: actions/github-script@v8 | ||
| env: | ||
| OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | ||
| with: | ||
| script: | | ||
| const formattedBody = ${{ steps.body.outputs.result }}; | ||
|
|
||
| const response = await fetch('https://api.openai.com/v1/responses', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: 'gpt-4o-mini', | ||
| input: `Based on the PR body below, generate a semantic-release style title in this exact format: | ||
| "type(scope): 4-6 word summary" | ||
|
|
||
| Pick the type based on these release rules: | ||
| - feat -> new feature (minor release) | ||
| - fix -> bug fix (patch release) | ||
| - chore -> maintenance / tooling (patch release) | ||
| - docs -> documentation only (patch release) | ||
| - refactor -> code restructuring, no behavior change (patch release) | ||
|
|
||
| "scope" is the affected module/area in lowercase (e.g. collection, auth, assessment). Summary is capitalized, imperative or descriptive, no trailing period. | ||
|
|
||
| Examples: | ||
| - feat(collection): Added new fields | ||
| - fix(auth): Prevent login for deleted org | ||
| - refactor(assessment): Simplify schema builder | ||
| - docs(readme): Update setup steps | ||
|
|
||
| Output ONLY the title, nothing else. | ||
|
|
||
| PR body: | ||
| ${formattedBody}` | ||
| }) | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| const text = data.output | ||
| ?.find(o => o.type === 'message') | ||
| ?.content?.find(c => c.type === 'output_text') | ||
| ?.text; | ||
|
|
||
| if (!text) { | ||
| core.setFailed(`OpenAI API error: ${JSON.stringify(data)}`); | ||
| return; | ||
| } | ||
|
|
||
| return text.replace(/^["']|["']$/g, ''); | ||
|
|
||
| - name: Update PR | ||
| uses: actions/github-script@v8 | ||
| with: | ||
| script: | | ||
| const formattedBody = ${{ steps.body.outputs.result }}; | ||
| const formattedTitle = ${{ steps.title.outputs.result }}; | ||
| const original = context.payload.pull_request.body || '(empty)'; | ||
|
|
||
| await github.rest.pulls.update({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: context.payload.pull_request.number, | ||
| title: formattedTitle, | ||
| body: `${formattedBody}\n\n<details><summary>Original PR description</summary>\n\n${original}\n\n</details>` | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.