|
| 1 | +name: "DocuCraft — PR Description Generator" |
| 2 | +description: "Automatically generates structured PR descriptions from pull request diffs." |
| 3 | +author: "DocuCraft" |
| 4 | +branding: |
| 5 | + icon: "file-text" |
| 6 | + color: "blue" |
| 7 | + |
| 8 | +inputs: |
| 9 | + github-token: |
| 10 | + description: "GitHub token (usually secrets.GITHUB_TOKEN)" |
| 11 | + required: true |
| 12 | + openai-api-key: |
| 13 | + description: "Optional OpenAI API key for AI-powered descriptions" |
| 14 | + required: false |
| 15 | + openai-model: |
| 16 | + description: "OpenAI model to use (default: gpt-4o-mini)" |
| 17 | + required: false |
| 18 | + default: "gpt-4o-mini" |
| 19 | + mode: |
| 20 | + description: "Generation mode: template (default) or ai" |
| 21 | + required: false |
| 22 | + default: "template" |
| 23 | + update-title: |
| 24 | + description: "Whether to update the PR title with a generated one (true/false)" |
| 25 | + required: false |
| 26 | + default: "false" |
| 27 | + |
| 28 | +outputs: |
| 29 | + description: |
| 30 | + description: "The generated PR description" |
| 31 | + value: ${{ steps.generate.outputs.description }} |
| 32 | + |
| 33 | +runs: |
| 34 | + using: "composite" |
| 35 | + steps: |
| 36 | + - name: Get PR diff |
| 37 | + id: diff |
| 38 | + shell: bash |
| 39 | + env: |
| 40 | + GH_TOKEN: ${{ inputs.github-token }} |
| 41 | + run: | |
| 42 | + PR_NUMBER=${{ github.event.pull_request.number }} |
| 43 | + if [ -z "$PR_NUMBER" ]; then |
| 44 | + echo "error=Not a pull request event" >> $GITHUB_OUTPUT |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 48 | + echo "title<<EOF" >> $GITHUB_OUTPUT |
| 49 | + echo "${{ github.event.pull_request.title }}" >> $GITHUB_OUTPUT |
| 50 | + echo "EOF" >> $GITHUB_OUTPUT |
| 51 | + echo "body<<EOF" >> $GITHUB_OUTPUT |
| 52 | + echo "${{ github.event.pull_request.body }}" >> $GITHUB_OUTPUT |
| 53 | + echo "EOF" >> $GITHUB_OUTPUT |
| 54 | +
|
| 55 | + gh pr diff $PR_NUMBER --color=never > /tmp/pr_diff.txt 2>&1 || true |
| 56 | + DIFF_SIZE=$(wc -c < /tmp/pr_diff.txt) |
| 57 | + if [ "$DIFF_SIZE" -gt 32000 ]; then |
| 58 | + head -c 32000 /tmp/pr_diff.txt > /tmp/pr_diff_truncated.txt |
| 59 | + mv /tmp/pr_diff_truncated.txt /tmp/pr_diff.txt |
| 60 | + fi |
| 61 | + echo "diff_size=$DIFF_SIZE" >> $GITHUB_OUTPUT |
| 62 | + gh pr view $PR_NUMBER --json files --jq '.files[].path' > /tmp/pr_files.txt 2>&1 || true |
| 63 | +
|
| 64 | + - name: Generate description (template mode) |
| 65 | + id: generate |
| 66 | + shell: bash |
| 67 | + if: ${{ inputs.mode != 'ai' || inputs.openai-api-key == '' }} |
| 68 | + env: |
| 69 | + GH_TOKEN: ${{ inputs.github-token }} |
| 70 | + run: | |
| 71 | + PR_NUMBER="${{ steps.diff.outputs.pr_number }}" |
| 72 | + PR_TITLE="${{ steps.diff.outputs.title }}" |
| 73 | +
|
| 74 | + FILES_LIST="" |
| 75 | + while IFS= read -r file; do |
| 76 | + FILES_LIST="$FILES_LIST- $file"$'\n' |
| 77 | + done < /tmp/pr_files.txt |
| 78 | +
|
| 79 | + DIFF_PREVIEW="" |
| 80 | + if [ -f /tmp/pr_diff.txt ]; then |
| 81 | + DIFF_PREVIEW=$(head -c 2000 /tmp/pr_diff.txt) |
| 82 | + fi |
| 83 | +
|
| 84 | + DESCRIPTION=$(cat <<DESC_EOF |
| 85 | +## Summary |
| 86 | + |
| 87 | +<!-- Generated by DocuCraft --> |
| 88 | + |
| 89 | +This pull request introduces changes to the following files: |
| 90 | + |
| 91 | +$FILES_LIST |
| 92 | + |
| 93 | +### Changes |
| 94 | + |
| 95 | +- $(echo "$FILES_LIST" | head -5 | tr '\n' ' ' | sed 's/- //g' | awk '{print "Modified: "$0}') |
| 96 | +
|
| 97 | +### Why |
| 98 | +
|
| 99 | +Automated pull request description generated by DocuCraft. Review the diff for detailed changes. |
| 100 | +
|
| 101 | +--- |
| 102 | +
|
| 103 | +> 🤖 This description was automatically generated by [DocuCraft](https://github.com/${{ github.repository }}/actions). |
| 104 | +DESC_EOF |
| 105 | +) |
| 106 | + echo "description<<EOF" >> $GITHUB_OUTPUT |
| 107 | + echo "$DESCRIPTION" >> $GITHUB_OUTPUT |
| 108 | + echo "EOF" >> $GITHUB_OUTPUT |
| 109 | + |
| 110 | + - name: Generate description (AI mode) |
| 111 | + id: generate-ai |
| 112 | + shell: bash |
| 113 | + if: ${{ inputs.mode == 'ai' && inputs.openai-api-key != '' }} |
| 114 | + env: |
| 115 | + OPENAI_API_KEY: ${{ inputs.openai-api-key }} |
| 116 | + run: | |
| 117 | + PR_TITLE="${{ steps.diff.outputs.title }}" |
| 118 | + FILES_LIST=$(cat /tmp/pr_files.txt 2>/dev/null || echo "No files detected") |
| 119 | + DIFF=$(cat /tmp/pr_diff.txt 2>/dev/null || echo "No diff available") |
| 120 | +
|
| 121 | + PROMPT=$(cat <<PROMPT_EOF |
| 122 | +You are DocuCraft, an expert code reviewer. Given this pull request, write a clear and concise PR description. |
| 123 | + |
| 124 | +PR Title: $PR_TITLE |
| 125 | + |
| 126 | +Files Changed: |
| 127 | +$FILES_LIST |
| 128 | + |
| 129 | +Diff (truncated): |
| 130 | +\`\`\`diff |
| 131 | +${DIFF:0:8000} |
| 132 | +\`\`\` |
| 133 | + |
| 134 | +Generate a PR description with markdown: |
| 135 | +1. **Summary**: 1-2 sentence overview |
| 136 | +2. **Changes**: Key changes organized by area |
| 137 | +3. **Why**: Motivation behind the changes |
| 138 | +PROMPT_EOF |
| 139 | +) |
| 140 | + JSON_ESCAPED=$(echo "$PROMPT" | jq -Rs .) |
| 141 | + RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \ |
| 142 | + -H "Authorization: Bearer $OPENAI_API_KEY" \ |
| 143 | + -H "Content-Type: application/json" \ |
| 144 | + -d "{\"model\":\"${{ inputs.openai-model }}\",\"messages\":[{\"role\":\"user\",\"content\":$JSON_ESCAPED}],\"temperature\":0.3,\"max_tokens\":1000}") |
| 145 | + DESCRIPTION=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // "Failed to generate AI description."') |
| 146 | + echo "description<<EOF" >> $GITHUB_OUTPUT |
| 147 | + echo "$DESCRIPTION" >> $GITHUB_OUTPUT |
| 148 | + echo "EOF" >> $GITHUB_OUTPUT |
| 149 | + |
| 150 | + - name: Update PR body |
| 151 | + shell: bash |
| 152 | + env: |
| 153 | + GH_TOKEN: ${{ inputs.github-token }} |
| 154 | + run: | |
| 155 | + PR_NUMBER="${{ steps.diff.outputs.pr_number }}" |
| 156 | + DESCRIPTION="${{ steps.generate.outputs.description }}" |
| 157 | + if [ -z "$DESCRIPTION" ]; then |
| 158 | + DESCRIPTION="${{ steps.generate-ai.outputs.description }}" |
| 159 | + fi |
| 160 | + if [ -n "$DESCRIPTION" ]; then |
| 161 | + echo "$DESCRIPTION" | gh pr review $PR_NUMBER --body-file - 2>&1 || true |
| 162 | + fi |
0 commit comments