|
| 1 | +name: Issue Command Bot |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + issues: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + command-bot: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + if: github.event.issue && startsWith(github.event.comment.body, '/') |
| 15 | + steps: |
| 16 | + - name: Handle /assign command |
| 17 | + if: contains(github.event.comment.body, '/assign') |
| 18 | + uses: actions/github-script@v7 |
| 19 | + with: |
| 20 | + script: | |
| 21 | + const comment = context.payload.comment.body; |
| 22 | + const issueNumber = context.issue.number; |
| 23 | +
|
| 24 | + // Extract username from /assign @username or use commenter |
| 25 | + const assignMatch = comment.match(/\/assign\s+@?(\S+)/); |
| 26 | + const assignee = assignMatch ? assignMatch[1] : context.payload.comment.user.login; |
| 27 | +
|
| 28 | + try { |
| 29 | + await github.rest.issues.addAssignees({ |
| 30 | + owner: context.repo.owner, |
| 31 | + repo: context.repo.repo, |
| 32 | + issue_number: issueNumber, |
| 33 | + assignees: [assignee] |
| 34 | + }); |
| 35 | +
|
| 36 | + await github.rest.issues.createComment({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + issue_number: issueNumber, |
| 40 | + body: `Assigned @${assignee} to this issue.` |
| 41 | + }); |
| 42 | + } catch (error) { |
| 43 | + await github.rest.issues.createComment({ |
| 44 | + owner: context.repo.owner, |
| 45 | + repo: context.repo.repo, |
| 46 | + issue_number: issueNumber, |
| 47 | + body: `Failed to assign @${assignee}: ${error.message}` |
| 48 | + }); |
| 49 | + } |
| 50 | +
|
| 51 | + - name: Handle /close command |
| 52 | + if: contains(github.event.comment.body, '/close') |
| 53 | + uses: actions/github-script@v7 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + await github.rest.issues.update({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + issue_number: context.issue.number, |
| 60 | + state: 'closed' |
| 61 | + }); |
| 62 | +
|
| 63 | + await github.rest.issues.createComment({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + issue_number: context.issue.number, |
| 67 | + body: `Issue closed by @${context.payload.comment.user.login}` |
| 68 | + }); |
| 69 | +
|
| 70 | + - name: Handle /reopen command |
| 71 | + if: contains(github.event.comment.body, '/reopen') |
| 72 | + uses: actions/github-script@v7 |
| 73 | + with: |
| 74 | + script: | |
| 75 | + await github.rest.issues.update({ |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + issue_number: context.issue.number, |
| 79 | + state: 'open' |
| 80 | + }); |
| 81 | +
|
| 82 | + await github.rest.issues.createComment({ |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + issue_number: context.issue.number, |
| 86 | + body: `Issue reopened by @${context.payload.comment.user.login}` |
| 87 | + }); |
| 88 | +
|
| 89 | + - name: Handle /label command |
| 90 | + if: contains(github.event.comment.body, '/label') |
| 91 | + uses: actions/github-script@v7 |
| 92 | + with: |
| 93 | + script: | |
| 94 | + const comment = context.payload.comment.body; |
| 95 | + const labelMatch = comment.match(/\/label\s+(\S+)/); |
| 96 | +
|
| 97 | + if (labelMatch) { |
| 98 | + const label = labelMatch[1]; |
| 99 | + try { |
| 100 | + await github.rest.issues.addLabels({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: context.issue.number, |
| 104 | + labels: [label] |
| 105 | + }); |
| 106 | +
|
| 107 | + await github.rest.issues.createComment({ |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + issue_number: context.issue.number, |
| 111 | + body: `Added label: \`${label}\`` |
| 112 | + }); |
| 113 | + } catch (error) { |
| 114 | + await github.rest.issues.createComment({ |
| 115 | + owner: context.repo.owner, |
| 116 | + repo: context.repo.repo, |
| 117 | + issue_number: context.issue.number, |
| 118 | + body: `Failed to add label \`${label}\`: ${error.message}` |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | +
|
| 123 | + - name: Handle /needs-triage command |
| 124 | + if: contains(github.event.comment.body, '/needs-triage') |
| 125 | + uses: actions/github-script@v7 |
| 126 | + with: |
| 127 | + script: | |
| 128 | + await github.rest.issues.addLabels({ |
| 129 | + owner: context.repo.owner, |
| 130 | + repo: context.repo.repo, |
| 131 | + issue_number: context.issue.number, |
| 132 | + labels: ['needs-triage'] |
| 133 | + }); |
| 134 | +
|
| 135 | + await github.rest.issues.createComment({ |
| 136 | + owner: context.repo.owner, |
| 137 | + repo: context.repo.repo, |
| 138 | + issue_number: context.issue.number, |
| 139 | + body: 'Added `needs-triage` label. A maintainer will review this issue soon.' |
| 140 | + }); |
| 141 | +
|
| 142 | + - name: Handle /cc command |
| 143 | + if: contains(github.event.comment.body, '/cc') |
| 144 | + uses: actions/github-script@v7 |
| 145 | + with: |
| 146 | + script: | |
| 147 | + const comment = context.payload.comment.body; |
| 148 | + const ccMatch = comment.match(/\/cc\s+(.+)/); |
| 149 | +
|
| 150 | + if (ccMatch) { |
| 151 | + const users = ccMatch[1].split(/[,\s]+/).filter(u => u.startsWith('@')).map(u => u.substring(1)); |
| 152 | +
|
| 153 | + if (users.length > 0) { |
| 154 | + const mentions = users.map(u => `@${u}`).join(', '); |
| 155 | + await github.rest.issues.createComment({ |
| 156 | + owner: context.repo.owner, |
| 157 | + repo: context.repo.repo, |
| 158 | + issue_number: context.issue.number, |
| 159 | + body: `CC: ${mentions}` |
| 160 | + }); |
| 161 | + } |
| 162 | + } |
| 163 | +
|
| 164 | + - name: Handle /help command |
| 165 | + if: contains(github.event.comment.body, '/help') |
| 166 | + uses: actions/github-script@v7 |
| 167 | + with: |
| 168 | + script: | |
| 169 | + const helpMessage = ` |
| 170 | + ## Available Commands |
| 171 | +
|
| 172 | + | Command | Description | |
| 173 | + |---------|-------------| |
| 174 | + | \`/assign\` | Assign yourself to this issue | |
| 175 | + | \`/assign @username\` | Assign a specific user to this issue | |
| 176 | + | \`/close\` | Close this issue | |
| 177 | + | \`/reopen\` | Reopen this issue | |
| 178 | + | \`/label <name>\` | Add a label to this issue | |
| 179 | + | \`/needs-triage\` | Mark this issue as needing triage | |
| 180 | + | \`/cc @user1, @user2\` | Mention users to notify them | |
| 181 | + | \`/help\` | Show this help message | |
| 182 | + `; |
| 183 | +
|
| 184 | + await github.rest.issues.createComment({ |
| 185 | + owner: context.repo.owner, |
| 186 | + repo: context.repo.repo, |
| 187 | + issue_number: context.issue.number, |
| 188 | + body: helpMessage |
| 189 | + }); |
0 commit comments