diff --git a/.github/workflows/pr-formatter.yml b/.github/workflows/pr-formatter.yml new file mode 100644 index 0000000..8d13ce9 --- /dev/null +++ b/.github/workflows/pr-formatter.yml @@ -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 + + ## 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 + + 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 + + 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
Original PR description\n\n${original}\n\n
` + });