|
| 1 | +name: Auto Issue Triage |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [labeled] |
| 6 | + |
| 7 | +# Elevated permissions needed for the fix+PR path (commit, push, create PR). |
| 8 | +# The needs-info path only uses issues: write, but splitting into separate |
| 9 | +# jobs would add complexity without meaningful security benefit since this |
| 10 | +# only triggers on maintainer-applied labels. |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + issues: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: issue-triage-${{ github.event.issue.number }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + triage: |
| 22 | + if: github.event.label.name == 'kind/bug' |
| 23 | + runs-on: ubuntu-latest |
| 24 | + timeout-minutes: 15 |
| 25 | + env: |
| 26 | + HAS_APP_SECRETS: ${{ secrets.CAGENT_REVIEWER_APP_ID != '' }} |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout repository |
| 30 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 |
| 31 | + with: |
| 32 | + fetch-depth: 1 |
| 33 | + |
| 34 | + - name: Generate GitHub App token |
| 35 | + if: env.HAS_APP_SECRETS == 'true' |
| 36 | + id: app-token |
| 37 | + continue-on-error: true |
| 38 | + uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a # v2 |
| 39 | + with: |
| 40 | + app_id: ${{ secrets.CAGENT_REVIEWER_APP_ID }} |
| 41 | + private_key: ${{ secrets.CAGENT_REVIEWER_APP_PRIVATE_KEY }} |
| 42 | + |
| 43 | + - name: Construct prompt |
| 44 | + id: prompt |
| 45 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 |
| 46 | + with: |
| 47 | + script: | |
| 48 | + const issue = context.payload.issue; |
| 49 | + const labels = issue.labels.map(l => l.name).join(', '); |
| 50 | +
|
| 51 | + const bugTemplate = [ |
| 52 | + '## Bug Report Template (for reference)', |
| 53 | + '- Describe the bug', |
| 54 | + '- Version affected', |
| 55 | + '- How To Reproduce (steps)', |
| 56 | + '- Expected behavior', |
| 57 | + '- Screenshots (optional)', |
| 58 | + '- OS and Terminal type', |
| 59 | + '- Additional context', |
| 60 | + ].join('\n'); |
| 61 | +
|
| 62 | + const prompt = [ |
| 63 | + `## Issue #${issue.number}: ${issue.title}`, |
| 64 | + '', |
| 65 | + `**Labels:** ${labels}`, |
| 66 | + `**Author:** ${issue.user.login}`, |
| 67 | + `**Created:** ${issue.created_at}`, |
| 68 | + '', |
| 69 | + '### Issue Body', |
| 70 | + '', |
| 71 | + issue.body || '(empty)', |
| 72 | + '', |
| 73 | + '---', |
| 74 | + '', |
| 75 | + bugTemplate, |
| 76 | + '', |
| 77 | + '---', |
| 78 | + '', |
| 79 | + `Triage this bug report. The issue number for gh CLI commands is ${issue.number}.`, |
| 80 | + ].join('\n'); |
| 81 | +
|
| 82 | + core.setOutput('text', prompt); |
| 83 | +
|
| 84 | + - name: Run triage agent |
| 85 | + id: agent |
| 86 | + uses: docker/cagent-action@latest |
| 87 | + env: |
| 88 | + GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }} |
| 89 | + with: |
| 90 | + agent: ${{ github.workspace }}/.github/agents/issue-triager.yaml |
| 91 | + prompt: ${{ steps.prompt.outputs.text }} |
| 92 | + anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }} |
| 93 | + github-token: ${{ steps.app-token.outputs.token || github.token }} |
| 94 | + timeout: 600 |
| 95 | + |
| 96 | + - name: Parse agent result |
| 97 | + id: result |
| 98 | + shell: bash |
| 99 | + run: | |
| 100 | + OUTPUT_FILE="${{ steps.agent.outputs.output-file }}" |
| 101 | + if [ ! -f "$OUTPUT_FILE" ]; then |
| 102 | + echo "No output file found" |
| 103 | + echo "action=none" >> "$GITHUB_OUTPUT" |
| 104 | + exit 0 |
| 105 | + fi |
| 106 | +
|
| 107 | + CONTENT=$(cat "$OUTPUT_FILE") |
| 108 | + echo "--- Agent output ---" |
| 109 | + echo "$CONTENT" |
| 110 | + echo "--------------------" |
| 111 | +
|
| 112 | + # Check for result markers (search from the end of output) |
| 113 | + if echo "$CONTENT" | grep -q "RESULT:NEEDS_INFO"; then |
| 114 | + echo "action=needs_info" >> "$GITHUB_OUTPUT" |
| 115 | + elif echo "$CONTENT" | grep -q "RESULT:FIXED"; then |
| 116 | + echo "action=fixed" >> "$GITHUB_OUTPUT" |
| 117 | + elif echo "$CONTENT" | grep -q "RESULT:NO_CHANGES"; then |
| 118 | + echo "action=none" >> "$GITHUB_OUTPUT" |
| 119 | + else |
| 120 | + echo "No recognized result marker found" |
| 121 | + echo "action=none" >> "$GITHUB_OUTPUT" |
| 122 | + fi |
| 123 | +
|
| 124 | + - name: Check for changes |
| 125 | + if: steps.result.outputs.action == 'fixed' |
| 126 | + id: changes |
| 127 | + shell: bash |
| 128 | + run: | |
| 129 | + if [ -n "$(git status --porcelain)" ]; then |
| 130 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 131 | + else |
| 132 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 133 | + fi |
| 134 | +
|
| 135 | + - name: Commit and push fix |
| 136 | + if: steps.result.outputs.action == 'fixed' && steps.changes.outputs.has_changes == 'true' |
| 137 | + id: push |
| 138 | + shell: bash |
| 139 | + env: |
| 140 | + GITHUB_TOKEN: ${{ steps.app-token.outputs.token || github.token }} |
| 141 | + run: | |
| 142 | + ISSUE_NUMBER=${{ github.event.issue.number }} |
| 143 | + BRANCH_NAME="fix/issue-${ISSUE_NUMBER}" |
| 144 | +
|
| 145 | + git config user.name "github-actions[bot]" |
| 146 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 147 | +
|
| 148 | + git checkout -b "$BRANCH_NAME" |
| 149 | + git add -A |
| 150 | + git commit -m "fix: auto-triage fix for #${ISSUE_NUMBER} |
| 151 | +
|
| 152 | + Automated fix generated by issue triage agent. |
| 153 | + Resolves #${ISSUE_NUMBER}" |
| 154 | +
|
| 155 | + git push origin "$BRANCH_NAME" || { |
| 156 | + echo "::error::Failed to push branch $BRANCH_NAME" |
| 157 | + exit 1 |
| 158 | + } |
| 159 | + echo "branch=$BRANCH_NAME" >> "$GITHUB_OUTPUT" |
| 160 | +
|
| 161 | + - name: Create draft PR and comment on issue |
| 162 | + if: steps.push.outputs.branch != '' |
| 163 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 |
| 164 | + env: |
| 165 | + BRANCH_NAME: ${{ steps.push.outputs.branch }} |
| 166 | + with: |
| 167 | + github-token: ${{ steps.app-token.outputs.token || github.token }} |
| 168 | + script: | |
| 169 | + const issue = context.payload.issue; |
| 170 | + const branch = process.env.BRANCH_NAME; |
| 171 | +
|
| 172 | + // Create draft PR |
| 173 | + const pr = await github.rest.pulls.create({ |
| 174 | + owner: context.repo.owner, |
| 175 | + repo: context.repo.repo, |
| 176 | + title: `fix: auto-triage fix for #${issue.number}`, |
| 177 | + body: [ |
| 178 | + `## Summary`, |
| 179 | + ``, |
| 180 | + `Automated fix for #${issue.number}.`, |
| 181 | + ``, |
| 182 | + `> **${issue.title}**`, |
| 183 | + ``, |
| 184 | + `This PR was generated by the issue triage agent. Please review carefully before merging.`, |
| 185 | + ``, |
| 186 | + `## Test plan`, |
| 187 | + ``, |
| 188 | + `- [ ] Review the changes for correctness`, |
| 189 | + `- [ ] Verify tests pass in CI`, |
| 190 | + `- [ ] Manual testing if applicable`, |
| 191 | + ].join('\n'), |
| 192 | + head: branch, |
| 193 | + base: 'main', |
| 194 | + draft: true, |
| 195 | + }); |
| 196 | +
|
| 197 | + // Comment on the issue with the PR link |
| 198 | + await github.rest.issues.createComment({ |
| 199 | + owner: context.repo.owner, |
| 200 | + repo: context.repo.repo, |
| 201 | + issue_number: issue.number, |
| 202 | + body: [ |
| 203 | + `I've analyzed this bug report and created an automated fix:`, |
| 204 | + ``, |
| 205 | + `**Draft PR:** ${pr.data.html_url}`, |
| 206 | + ``, |
| 207 | + `Please review the changes — this was generated automatically and may need adjustments.`, |
| 208 | + ].join('\n'), |
| 209 | + }); |
| 210 | +
|
| 211 | + core.info(`Created draft PR #${pr.data.number}: ${pr.data.html_url}`); |
0 commit comments