|
| 1 | +name: Detect Invalid CI Commands |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +jobs: |
| 8 | + detect-invalid-commands: |
| 9 | + # Only run on PR comments that contain CI-related keywords but don't match valid commands |
| 10 | + if: | |
| 11 | + github.event.issue.pull_request && |
| 12 | + !contains(github.event.comment.body, '/run-skipped-ci') && |
| 13 | + !contains(github.event.comment.body, '/stop-run-skipped-ci') && |
| 14 | + ( |
| 15 | + contains(github.event.comment.body, '/run') || |
| 16 | + contains(github.event.comment.body, '/skip') || |
| 17 | + contains(github.event.comment.body, '/ci') || |
| 18 | + contains(github.event.comment.body, '/full-ci') || |
| 19 | + contains(github.event.comment.body, '/trigger') || |
| 20 | + contains(github.event.comment.body, '/enable') || |
| 21 | + contains(github.event.comment.body, '/disable') || |
| 22 | + contains(github.event.comment.body, '/test') |
| 23 | + ) |
| 24 | + runs-on: ubuntu-22.04 |
| 25 | + permissions: |
| 26 | + contents: read |
| 27 | + pull-requests: write |
| 28 | + steps: |
| 29 | + - name: Check for similar commands |
| 30 | + id: check_command |
| 31 | + uses: actions/github-script@v7 |
| 32 | + with: |
| 33 | + script: | |
| 34 | + const comment = context.payload.comment.body.toLowerCase(); |
| 35 | +
|
| 36 | + // Pattern to detect slash commands |
| 37 | + const slashCommandPattern = /(?:^|\n)\s*(\/[\w-]+)/g; |
| 38 | + const matches = [...comment.matchAll(slashCommandPattern)]; |
| 39 | +
|
| 40 | + if (matches.length === 0) { |
| 41 | + console.log('No slash commands found'); |
| 42 | + return { shouldRespond: false }; |
| 43 | + } |
| 44 | +
|
| 45 | + // Valid commands |
| 46 | + const validCommands = ['/run-skipped-ci', '/stop-run-skipped-ci']; |
| 47 | +
|
| 48 | + // CI-related keywords that might indicate an attempt to use a CI command |
| 49 | + const ciKeywords = [ |
| 50 | + 'run', 'skip', 'ci', 'full', 'trigger', 'enable', 'disable', |
| 51 | + 'test', 'suite', 'check', 'workflow' |
| 52 | + ]; |
| 53 | +
|
| 54 | + // Check if any slash command looks like it might be trying to trigger CI |
| 55 | + const potentialCICommands = matches.filter(match => { |
| 56 | + const cmd = match[1].toLowerCase(); |
| 57 | + return !validCommands.includes(cmd) && |
| 58 | + ciKeywords.some(keyword => cmd.includes(keyword)); |
| 59 | + }); |
| 60 | +
|
| 61 | + if (potentialCICommands.length > 0) { |
| 62 | + console.log('Found potential invalid CI commands:', potentialCICommands.map(m => m[1])); |
| 63 | + return { |
| 64 | + shouldRespond: true, |
| 65 | + invalidCommands: potentialCICommands.map(m => m[1]) |
| 66 | + }; |
| 67 | + } |
| 68 | +
|
| 69 | + return { shouldRespond: false }; |
| 70 | + result-encoding: string |
| 71 | + |
| 72 | + - name: Post helpful comment |
| 73 | + if: fromJSON(steps.check_command.outputs.result).shouldRespond |
| 74 | + uses: actions/github-script@v7 |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const result = ${{ steps.check_command.outputs.result }}; |
| 78 | + const invalidCommands = result.invalidCommands || []; |
| 79 | +
|
| 80 | + const invalidCmdsList = invalidCommands.length > 0 |
| 81 | + ? `\n\n**Invalid command(s) detected:** ${invalidCommands.map(cmd => \`\\\`${cmd}\\\`\`).join(', ')}` |
| 82 | + : ''; |
| 83 | +
|
| 84 | + const body = \`👋 It looks like you may have tried to use a CI command, but it doesn't match the available commands.${invalidCmdsList} |
| 85 | +
|
| 86 | + ## Available GitHub Comment Commands |
| 87 | +
|
| 88 | + ### 1. \\\`/run-skipped-ci\\\` - Enable Full CI Mode |
| 89 | + Triggers all CI workflows that were skipped due to unchanged code. |
| 90 | +
|
| 91 | + **What it does:** |
| 92 | + - Runs tests across all supported versions (Ruby 3.2-3.4, Node 20-22, Shakapacker 8-9, React 18-19) |
| 93 | + - Adds the \\\`full-ci\\\` label to persist full testing on future commits |
| 94 | + - Triggers: Main tests, Generator tests, and React on Rails Pro tests |
| 95 | +
|
| 96 | + **Requirements:** |
| 97 | + - Must have write access to the repository |
| 98 | + - Use at the start of a comment or after a newline |
| 99 | +
|
| 100 | + **Example:** |
| 101 | + \\\`\\\`\\\` |
| 102 | + /run-skipped-ci |
| 103 | + \\\`\\\`\\\` |
| 104 | +
|
| 105 | + --- |
| 106 | +
|
| 107 | + ### 2. \\\`/stop-run-skipped-ci\\\` - Disable Full CI Mode |
| 108 | + Returns to standard CI behavior (optimized suite, skips tests for unchanged code). |
| 109 | +
|
| 110 | + **What it does:** |
| 111 | + - Removes the \\\`full-ci\\\` label from the PR |
| 112 | + - Future commits will use the fast feedback CI suite |
| 113 | + - Does NOT stop currently running workflows |
| 114 | +
|
| 115 | + **Requirements:** |
| 116 | + - Must have write access to the repository |
| 117 | +
|
| 118 | + **Example:** |
| 119 | + \\\`\\\`\\\` |
| 120 | + /stop-run-skipped-ci |
| 121 | + \\\`\\\`\\\` |
| 122 | +
|
| 123 | + --- |
| 124 | +
|
| 125 | + ### 3. \\\`@claude\\\` - Claude Code AI Review |
| 126 | + Invokes Claude Code AI to perform code review or answer questions. |
| 127 | +
|
| 128 | + **What it does:** |
| 129 | + - Analyzes code changes and provides feedback |
| 130 | + - Follows instructions specified in the comment |
| 131 | + - Can read CI results on PRs |
| 132 | +
|
| 133 | + **Example:** |
| 134 | + \\\`\\\`\\\` |
| 135 | + @claude please review the error handling in this PR |
| 136 | + \\\`\\\`\\\` |
| 137 | +
|
| 138 | + --- |
| 139 | +
|
| 140 | + ## Local CI Debugging |
| 141 | +
|
| 142 | + Instead of using comment commands, you can replicate CI failures locally: |
| 143 | +
|
| 144 | + **Check CI status:** |
| 145 | + \\\`\\\`\\\`bash |
| 146 | + gh pr view --json statusCheckRollup |
| 147 | + \\\`\\\`\\\` |
| 148 | +
|
| 149 | + **Re-run only failed jobs:** |
| 150 | + \\\`\\\`\\\`bash |
| 151 | + bin/ci-rerun-failures |
| 152 | + \\\`\\\`\\\` |
| 153 | +
|
| 154 | + **Run specific failed specs:** |
| 155 | + \\\`\\\`\\\`bash |
| 156 | + pbpaste | bin/ci-run-failed-specs # macOS |
| 157 | + \\\`\\\`\\\` |
| 158 | +
|
| 159 | + **Switch between CI configurations:** |
| 160 | + \\\`\\\`\\\`bash |
| 161 | + bin/ci-switch-config minimum # Test with Ruby 3.2, Node 20, etc. |
| 162 | + bin/ci-switch-config latest # Return to Ruby 3.4, Node 22, etc. |
| 163 | + \\\`\\\`\\\` |
| 164 | +
|
| 165 | + --- |
| 166 | +
|
| 167 | + 📖 For more details, see the [Contributing Guide](https://github.com/shakacode/react_on_rails/blob/master/CONTRIBUTING.md#using-comment-commands-to-trigger-ci)\`; |
| 168 | +
|
| 169 | + await github.rest.issues.createComment({ |
| 170 | + owner: context.repo.owner, |
| 171 | + repo: context.repo.repo, |
| 172 | + issue_number: context.issue.number, |
| 173 | + body: body |
| 174 | + }); |
| 175 | +
|
| 176 | + // Add reaction to original comment |
| 177 | + await github.rest.reactions.createForIssueComment({ |
| 178 | + owner: context.repo.owner, |
| 179 | + repo: context.repo.repo, |
| 180 | + comment_id: context.payload.comment.id, |
| 181 | + content: 'eyes' |
| 182 | + }); |
0 commit comments