|
| 1 | +name: Auto Fix Issue |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 */12 * * *' # every 12 hours |
| 6 | + workflow_dispatch: # allow manual trigger |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + pull-requests: write |
| 11 | + issues: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + pick-and-fix: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + timeout-minutes: 30 |
| 17 | + |
| 18 | + steps: |
| 19 | + # 1. Checkout |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + # 2. Install Claude Code |
| 26 | + - name: Install Claude Code |
| 27 | + run: npm install -g @anthropic-ai/claude-code |
| 28 | + |
| 29 | + # 3. Install PHP & Composer dependencies |
| 30 | + - name: Set up PHP |
| 31 | + uses: shivammathur/setup-php@v2 |
| 32 | + with: |
| 33 | + php-version: '8.3' |
| 34 | + tools: composer:v2 |
| 35 | + coverage: none |
| 36 | + |
| 37 | + - name: Install Composer dependencies |
| 38 | + uses: ramsey/composer-install@v3 |
| 39 | + |
| 40 | + # 4. Pick a random open issue from rectorphp/rector (skip any already covered by a PR) |
| 41 | + - name: Pick a fixable issue |
| 42 | + id: pick |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + run: | |
| 46 | + set -euo pipefail |
| 47 | +
|
| 48 | + ISSUES_REPO="rectorphp/rector" |
| 49 | +
|
| 50 | + # Fetch open issues (exclude PRs) |
| 51 | + issues=$(gh api "repos/$ISSUES_REPO/issues?state=open&per_page=100" \ |
| 52 | + --jq '[.[] | select(.pull_request == null) | {number, title, body}]') |
| 53 | +
|
| 54 | + # Collect issue numbers already covered by an open PR in this repo |
| 55 | + covered=$(gh api "repos/${{ github.repository }}/pulls?state=open&per_page=100" \ |
| 56 | + --jq '[.[].head.ref]' | grep -oP '(?<=issue-)\d+' | tr '\n' ' ' || true) |
| 57 | +
|
| 58 | + echo "Issues covered by open PRs: ${covered:-none}" |
| 59 | +
|
| 60 | + # Exclude covered issues and pick one at random |
| 61 | + candidates=$(echo "$issues" | jq -c \ |
| 62 | + --argjson covered "$(echo "$covered" | jq -Rsc 'split(" ") | map(select(. != ""))')" \ |
| 63 | + '[.[] | select((.number | tostring) as $n | $covered | index($n) == null)]') |
| 64 | +
|
| 65 | + count=$(echo "$candidates" | jq 'length') |
| 66 | + echo "Candidates: $count" |
| 67 | +
|
| 68 | + if [ "$count" -eq 0 ]; then |
| 69 | + echo "no_issue=true" >> "$GITHUB_OUTPUT" |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | +
|
| 73 | + issue=$(echo "$candidates" | jq -c ".[$(( RANDOM % count ))]") |
| 74 | + issue_number=$(echo "$issue" | jq -r '.number') |
| 75 | + issue_title=$(echo "$issue" | jq -r '.title') |
| 76 | + issue_body=$(echo "$issue" | jq -r '.body // ""') |
| 77 | +
|
| 78 | + echo "Picked issue #$issue_number — $issue_title" |
| 79 | +
|
| 80 | + branch="fix/issue-${issue_number}-$(echo "$issue_title" | \ |
| 81 | + tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | cut -c1-50 | sed 's/-$//')" |
| 82 | +
|
| 83 | + echo "issue_number=$issue_number" >> "$GITHUB_OUTPUT" |
| 84 | + echo "issue_title=$issue_title" >> "$GITHUB_OUTPUT" |
| 85 | + echo "issue_body<<EOF" >> "$GITHUB_OUTPUT" |
| 86 | + echo "$issue_body" >> "$GITHUB_OUTPUT" |
| 87 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 88 | + echo "branch=$branch" >> "$GITHUB_OUTPUT" |
| 89 | + echo "no_issue=false" >> "$GITHUB_OUTPUT" |
| 90 | +
|
| 91 | + # 5. Early exit if no suitable issue was found |
| 92 | + - name: No suitable issue found |
| 93 | + if: steps.pick.outputs.no_issue == 'true' |
| 94 | + run: echo "::notice::No fixable open issues found — skipping this run." |
| 95 | + |
| 96 | + # 6. Configure git |
| 97 | + - name: Configure git identity |
| 98 | + if: steps.pick.outputs.no_issue == 'false' |
| 99 | + run: | |
| 100 | + git config user.name "github-actions[bot]" |
| 101 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 102 | +
|
| 103 | + # 7. Create fix branch |
| 104 | + - name: Create fix branch |
| 105 | + if: steps.pick.outputs.no_issue == 'false' |
| 106 | + run: git checkout -b "${{ steps.pick.outputs.branch }}" |
| 107 | + |
| 108 | + # 8. Ask Claude Code to fix the issue |
| 109 | + - name: Run Claude Code to fix the issue |
| 110 | + if: steps.pick.outputs.no_issue == 'false' |
| 111 | + env: |
| 112 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 113 | + CLAUDE_ISSUE_NUMBER: ${{ steps.pick.outputs.issue_number }} |
| 114 | + CLAUDE_ISSUE_TITLE: ${{ steps.pick.outputs.issue_title }} |
| 115 | + CLAUDE_ISSUE_BODY: ${{ steps.pick.outputs.issue_body }} |
| 116 | + run: | |
| 117 | + claude --print --dangerously-skip-permissions \ |
| 118 | + "You are an expert software engineer. Fix the following GitHub issue in this repository. |
| 119 | +
|
| 120 | + Issue #${CLAUDE_ISSUE_NUMBER}: ${CLAUDE_ISSUE_TITLE} |
| 121 | +
|
| 122 | + Issue description: |
| 123 | + ${CLAUDE_ISSUE_BODY} |
| 124 | +
|
| 125 | + Instructions: |
| 126 | + 1. Understand the root cause of the issue deeply before making any changes. |
| 127 | + 2. Make the minimal change that fixes the issue — no unrelated refactoring. |
| 128 | + 3. Follow the existing code style, naming conventions, and formatting. |
| 129 | + 4. If the project has a test suite, add or update a test that covers this fix. |
| 130 | + 5. Run the test suite and make sure all tests pass. |
| 131 | + 6. Stage all changed files with 'git add'. |
| 132 | + 7. Commit with the message: fix: <short description> (fixes #${CLAUDE_ISSUE_NUMBER}) |
| 133 | + 8. Do NOT push — only commit locally. |
| 134 | + 9. If you cannot confidently fix this issue without breaking anything, output exactly: |
| 135 | + CANNOT_FIX: <reason> |
| 136 | + and make no changes." |
| 137 | +
|
| 138 | + # 9. Verify something was committed |
| 139 | + - name: Check for new commits |
| 140 | + if: steps.pick.outputs.no_issue == 'false' |
| 141 | + id: check_commit |
| 142 | + run: | |
| 143 | + ahead=$(git rev-list --count origin/HEAD..HEAD 2>/dev/null || echo 0) |
| 144 | + echo "commits_ahead=$ahead" >> "$GITHUB_OUTPUT" |
| 145 | + if [ "$ahead" -eq 0 ]; then |
| 146 | + echo "::warning::Claude Code made no commits — issue may not be fixable automatically." |
| 147 | + fi |
| 148 | +
|
| 149 | + # 10. Push branch and open PR |
| 150 | + - name: Push branch and open PR |
| 151 | + if: > |
| 152 | + steps.pick.outputs.no_issue == 'false' && |
| 153 | + steps.check_commit.outputs.commits_ahead != '0' |
| 154 | + env: |
| 155 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 156 | + BRANCH: ${{ steps.pick.outputs.branch }} |
| 157 | + ISSUE_NUMBER: ${{ steps.pick.outputs.issue_number }} |
| 158 | + ISSUE_TITLE: ${{ steps.pick.outputs.issue_title }} |
| 159 | + run: | |
| 160 | + git push origin "$BRANCH" |
| 161 | +
|
| 162 | + gh pr create \ |
| 163 | + --title "fix: $ISSUE_TITLE (fixes rectorphp/rector#$ISSUE_NUMBER)" \ |
| 164 | + --body "## Summary |
| 165 | + Fixes rectorphp/rector#${ISSUE_NUMBER} — ${ISSUE_TITLE} |
| 166 | +
|
| 167 | + ## Root cause |
| 168 | + See the commit message and diff for details of what was changed and why. |
| 169 | +
|
| 170 | + ## Testing |
| 171 | + - Test suite run inside the workflow — see CI logs for details. |
| 172 | +
|
| 173 | + > Opened automatically by the [Auto Fix Issue](.github/workflows/auto-fix-issue.yml) workflow. Please review carefully before merging." \ |
| 174 | + --head "$BRANCH" \ |
| 175 | + --label "auto-fix" |
| 176 | +
|
| 177 | + # 11. Summary |
| 178 | + - name: Write job summary |
| 179 | + if: always() |
| 180 | + env: |
| 181 | + NO_ISSUE: ${{ steps.pick.outputs.no_issue }} |
| 182 | + ISSUE_NUMBER: ${{ steps.pick.outputs.issue_number }} |
| 183 | + ISSUE_TITLE: ${{ steps.pick.outputs.issue_title }} |
| 184 | + BRANCH: ${{ steps.pick.outputs.branch }} |
| 185 | + COMMITS_AHEAD: ${{ steps.check_commit.outputs.commits_ahead }} |
| 186 | + run: | |
| 187 | + if [ "$NO_ISSUE" = "true" ]; then |
| 188 | + echo "## ✅ No candidates found" >> "$GITHUB_STEP_SUMMARY" |
| 189 | + echo "No open issues without an existing PR were found." >> "$GITHUB_STEP_SUMMARY" |
| 190 | + elif [ "${COMMITS_AHEAD:-0}" = "0" ]; then |
| 191 | + echo "## ⚠️ Could not fix issue #${ISSUE_NUMBER}" >> "$GITHUB_STEP_SUMMARY" |
| 192 | + echo "**${ISSUE_TITLE}** — Claude Code was unable to produce a commit." >> "$GITHUB_STEP_SUMMARY" |
| 193 | + else |
| 194 | + echo "## 🚀 PR opened for issue #${ISSUE_NUMBER}" >> "$GITHUB_STEP_SUMMARY" |
| 195 | + echo "**${ISSUE_TITLE}** — branch: \`${BRANCH}\`" >> "$GITHUB_STEP_SUMMARY" |
| 196 | + fi |
0 commit comments