| 1 | name: Auto Fix Failed Tests |
| 2 | |
| 3 | on: |
| 4 | workflow_run: |
| 5 | workflows: ["PR Checks", "CLI PR Checks"] |
| 6 | types: |
| 7 | - completed |
| 8 | |
| 9 | permissions: |
| 10 | contents: write |
| 11 | pull-requests: write |
| 12 | issues: write |
| 13 | actions: read |
| 14 | |
| 15 | jobs: |
| 16 | fix-failed-tests: |
| 17 | # Only run if the workflow failed |
| 18 | # DISABLED: Remove 'false && ' on the next line to enable auto-fixing |
| 19 | if: false && github.event.workflow_run.conclusion == 'failure' |
| 20 | runs-on: ubuntu-latest |
| 21 | |
| 22 | steps: |
| 23 | - name: Get workflow run details |
| 24 | id: workflow-details |
| 25 | uses: actions/github-script@v8 |
| 26 | with: |
| 27 | script: | |
| 28 | const workflowRun = context.payload.workflow_run; |
| 29 | const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({ |
| 30 | owner: context.repo.owner, |
| 31 | repo: context.repo.repo, |
| 32 | run_id: workflowRun.id |
| 33 | }); |
| 34 | |
| 35 | // Find all failed jobs since we're only monitoring specific test workflows |
| 36 | const failedJobs = jobs.jobs.filter(job => job.conclusion === 'failure'); |
| 37 | |
| 38 | if (failedJobs.length === 0) { |
| 39 | core.info('No failed jobs found'); |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | core.setOutput('has_failed_tests', 'true'); |
| 44 | core.setOutput('workflow_name', workflowRun.name); |
| 45 | core.setOutput('workflow_run_id', workflowRun.id); |
| 46 | core.setOutput('head_branch', workflowRun.head_branch); |
| 47 | core.setOutput('head_sha', workflowRun.head_sha); |
| 48 | core.setOutput('failed_jobs', JSON.stringify(failedJobs.map(j => j.name))); |
| 49 | |
| 50 | return failedJobs; |
| 51 | |
| 52 | - name: Get job logs for failed tests |
| 53 | if: steps.workflow-details.outputs.has_failed_tests == 'true' |
| 54 | id: get-logs |
| 55 | uses: actions/github-script@v8 |
| 56 | with: |
| 57 | script: | |
| 58 | const workflowRunId = ${{ github.event.workflow_run.id }}; |
| 59 | const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({ |
| 60 | owner: context.repo.owner, |
| 61 | repo: context.repo.repo, |
| 62 | run_id: workflowRunId |
| 63 | }); |
| 64 | |
| 65 | let errorLogs = ''; |
| 66 | |
| 67 | for (const job of jobs.jobs) { |
| 68 | if (job.conclusion === 'failure') { |
| 69 | try { |
| 70 | const { data: logData } = await github.rest.actions.downloadJobLogsForWorkflowRun({ |
| 71 | owner: context.repo.owner, |
| 72 | repo: context.repo.repo, |
| 73 | job_id: job.id |
| 74 | }); |
| 75 | |
| 76 | errorLogs += `\n\n=== Job: ${job.name} ===\n`; |
| 77 | errorLogs += logData; |
| 78 | } catch (error) { |
| 79 | core.warning(`Could not fetch logs for job ${job.name}: ${error.message}`); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Store logs in environment file for next step |
| 85 | const fs = require('fs'); |
| 86 | fs.writeFileSync('/tmp/test_failure_logs.txt', errorLogs); |
| 87 | |
| 88 | core.setOutput('has_logs', errorLogs.length > 0 ? 'true' : 'false'); |
| 89 | |
| 90 | - name: Checkout repository |
| 91 | if: steps.workflow-details.outputs.has_failed_tests == 'true' |
| 92 | uses: actions/checkout@v5 |
| 93 | with: |
| 94 | token: ${{ secrets.GITHUB_TOKEN }} |
| 95 | fetch-depth: 0 |
| 96 | ref: ${{ steps.workflow-details.outputs.head_sha }} |
| 97 | |
| 98 | - name: Setup Node.js |
| 99 | if: steps.workflow-details.outputs.has_failed_tests == 'true' |
| 100 | uses: actions/setup-node@v5 |
| 101 | with: |
| 102 | node-version: "20" |
| 103 | |
| 104 | - name: Install Continue CLI globally |
| 105 | if: steps.workflow-details.outputs.has_failed_tests == 'true' |
| 106 | run: npm i -g @continuedev/cli |
| 107 | |
| 108 | - name: Start remote session to fix failed tests |
| 109 | if: steps.workflow-details.outputs.has_failed_tests == 'true' |
| 110 | id: remote-session |
| 111 | env: |
| 112 | CONTINUE_API_KEY: ${{ secrets.CONTINUE_API_KEY }} |
| 113 | run: | |
| 114 | # Create a detailed prompt for fixing the failed tests |
| 115 | cat > /tmp/fix_tests_prompt.txt << 'PROMPT_EOF' |
| 116 | 🔧 **Auto Test Fix Request** |
| 117 | |
| 118 | The following tests failed in workflow "${{ steps.workflow-details.outputs.workflow_name }}" (Run ID: ${{ steps.workflow-details.outputs.workflow_run_id }}): |
| 119 | |
| 120 | **Failed Jobs:** ${{ steps.workflow-details.outputs.failed_jobs }} |
| 121 | **Branch:** ${{ steps.workflow-details.outputs.head_branch }} |
| 122 | **Commit:** ${{ steps.workflow-details.outputs.head_sha }} |
| 123 | |
| 124 | **Your Task:** |
| 125 | 1. Analyze the test failure logs and error messages |
| 126 | 2. Identify the root cause of the test failures |
| 127 | 3. Fix the failing tests by updating the test code or the underlying implementation |
| 128 | 4. Ensure all tests pass after your changes |
| 129 | 5. Commit your fixes with a descriptive message |
| 130 | |
| 131 | **Test Failure Context:** |
| 132 | Please examine the repository structure, run the failing tests locally to understand the errors, and implement appropriate fixes. |
| 133 | |
| 134 | Focus on: |
| 135 | - Understanding what the tests are trying to validate |
| 136 | - Identifying why they're failing (code changes, environment issues, test logic errors) |
| 137 | - Making minimal, targeted fixes that address the root cause |
| 138 | - Ensuring the fixes don't break other functionality |
| 139 | |
| 140 | Please start by examining the failing tests and their error messages, then proceed with the necessary fixes. |
| 141 | PROMPT_EOF |
| 142 | |
| 143 | echo "Starting Continue CLI remote session for test fixes..." |
| 144 | echo "Prompt content:" |
| 145 | cat /tmp/fix_tests_prompt.txt |
| 146 | |
| 147 | # Start remote session and capture JSON output |
| 148 | SESSION_OUTPUT=$(cat /tmp/fix_tests_prompt.txt | cn remote -s --branch ${{ steps.workflow-details.outputs.head_branch }}) |
| 149 | echo "Raw session output: $SESSION_OUTPUT" |
| 150 | |
| 151 | # Extract URL from JSON output |
| 152 | SESSION_URL=$(echo "$SESSION_OUTPUT" | jq -r '.url // empty') |
| 153 | |
| 154 | if [ -z "$SESSION_URL" ] || [ "$SESSION_URL" = "null" ]; then |
| 155 | echo "Failed to extract session URL from output: $SESSION_OUTPUT" |
| 156 | exit 1 |
| 157 | fi |
| 158 | |
| 159 | echo "session_url=$SESSION_URL" >> $GITHUB_OUTPUT |
| 160 | echo "✅ Started remote session: $SESSION_URL" |
| 161 | |
| 162 | - name: Log session details |
| 163 | if: steps.workflow-details.outputs.has_failed_tests == 'true' |
| 164 | run: | |
| 165 | echo "✅ Successfully started auto-fix session for failed tests" |
| 166 | echo "Workflow: ${{ steps.workflow-details.outputs.workflow_name }}" |
| 167 | echo "Run ID: ${{ steps.workflow-details.outputs.workflow_run_id }}" |
| 168 | echo "Branch: ${{ steps.workflow-details.outputs.head_branch }}" |
| 169 | echo "Session URL: ${{ steps.remote-session.outputs.session_url }}" |
| 170 | echo "Failed jobs: ${{ steps.workflow-details.outputs.failed_jobs }}" |