Issue Command Bot #49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Issue Command Bot | |
on: | |
issue_comment: | |
types: [created] | |
permissions: | |
issues: write | |
pull-requests: write | |
jobs: | |
process-command: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Process Commands | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const comment = context.payload.comment.body.trim().toLowerCase(); | |
const commenter = context.payload.comment.user.login; | |
const issueNumber = context.issue.number; | |
const repoOwner = context.repo.owner; | |
const repoName = context.repo.repo; | |
async function handleLabel(labelName, color, description) { | |
try { | |
await github.rest.issues.addLabels({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
labels: [labelName] | |
}); | |
} catch (error) { | |
if (error.status === 404) { | |
await github.rest.issues.createLabel({ | |
owner: repoOwner, | |
repo: repoName, | |
name: labelName, | |
color: color || "0e8a16", | |
description: description || "" | |
}); | |
await github.rest.issues.addLabels({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
labels: [labelName] | |
}); | |
} else { | |
throw error; | |
} | |
} | |
} | |
function parseCommand(comment, cmd) { | |
if (!comment?.includes(cmd)) return null; | |
const cmdIndex = comment.indexOf(cmd); | |
return comment.substring(cmdIndex + cmd.length).trim() || null; | |
} | |
try { | |
if (comment?.includes("/assign")) { | |
try { | |
await github.rest.issues.addAssignees({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
assignees: [commenter] | |
}); | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ Failed to assign: ${error.message} !` | |
}); | |
} | |
} | |
if (comment?.includes("/close")) { | |
try { | |
await github.rest.issues.update({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
state: "closed", | |
state_reason: "completed" | |
}); | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ Failed to close issue: ${error.message} !` | |
}); | |
} | |
} | |
if (comment?.includes("/reopen")) { | |
try { | |
await github.rest.issues.update({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
state: "open" | |
}); | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ Failed to reopen issue: ${error.message} !` | |
}); | |
} | |
} | |
if (comment?.includes("/label")) { | |
try { | |
const labelName = parseCommand(comment, "/label"); | |
if (labelName) { | |
await handleLabel(labelName, "0e8a16"); | |
} | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ Failed to add label: ${error.message} !` | |
}); | |
} | |
} | |
if (comment?.includes("/help")) { | |
try { | |
await handleLabel("help wanted", "008672", "Extra attention is needed"); | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ Failed to add help label: ${error.message} !` | |
}); | |
} | |
} | |
if (comment?.includes("/needs-triage")) { | |
try { | |
await handleLabel("needs-triage", "d73a4a", "This issue needs triage"); | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ Failed to add triage label: ${error.message} !` | |
}); | |
} | |
} | |
} catch (error) { | |
await github.rest.issues.createComment({ | |
owner: repoOwner, | |
repo: repoName, | |
issue_number: issueNumber, | |
body: `❌ ${error.message} !` | |
}); | |
} |