Skip to content

Commit

Permalink
feat:auto-assign worklow - fixes BuildCLI#107
Browse files Browse the repository at this point in the history
  • Loading branch information
omatheusmesmo committed Feb 11, 2025
1 parent 6772c3d commit 580aabe
Showing 1 changed file with 190 additions and 0 deletions.
190 changes: 190 additions & 0 deletions .github/workflows/auto-assign.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
name: Auto Assign on Issue Comment
on:
issue_comment:
types: [created]
issues:
types: [opened]

jobs:
auto-assign:
runs-on: ubuntu-latest
steps:
- name: Check comment and assign user
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue = context.payload.issue;
const comment = context.payload.comment.body.toLowerCase();
const commenter = context.payload.comment.user.login;
const assignedUsers = issue.assignees.map(user => user.login);
const issueNumber = issue.number;
const repoOwner = context.repo.owner;
const repoName = context.repo.repo;
const keywords = [
"assign me",
"can you assign this issue to me",
"i would like to work on this",
"i want to do this issue"
];
#17
if (keywords.some(keyword => comment.includes(keyword))) {
if (assignedUsers.length > 0) {
const assignedUser = assignedUsers[0];

if (assignedUser === commenter) {
console.log(`⚠️ ${commenter} is already assigned.`);
return;
}

await github.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
body: `@${assignedUser}, are you still working on this issue? If not, @${commenter} is interested in taking it over. Reply with "no" if you are not working on it anymore.`
});

console.log(`✅ Issue already assigned to ${assignedUser}. Asked if they are still working.`);

await github.rest.issues.createOrUpdateLabel({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
labels: [`pending-reassignment-${commenter}`]
});

return;
}

await github.rest.issues.addAssignees({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
assignees: [commenter]
});

await github.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
body: `@${commenter}, you have been assigned to this issue! :coffee:`
});

console.log(`✅ Issue assigned to ${commenter}`);
}

- name: Reassign if "no" is replied
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue = context.payload.issue;
const comment = context.payload.comment.body.toLowerCase();
const commenter = context.payload.comment.user.login;
const issueNumber = issue.number;
const repoOwner = context.repo.owner;
const repoName = context.repo.repo;
if (comment === "no") {
const comments = await github.rest.issues.listComments({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber
});
let lastRequester = null;
for (const c of comments.data.reverse()) {
if (c.body.includes("is interested in taking it over")) {
lastRequester = c.body.match(/@(\w+)/)[1];
break;
}
}
if (lastRequester) {
await github.rest.issues.addAssignees({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
assignees: [lastRequester]
});
await github.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
body: `@${lastRequester}, the issue has been reassigned to you! :coffee:`
});
console.log(`✅ Issue reassigned to ${lastRequester}`);
}
}
- name: Reassign if no response after 7 days
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { repo, owner } = context.repo;
const issueNumber = context.payload.issue.number;
const issue = await github.rest.issues.get({
owner,
repo,
issue_number: issueNumber
});
const labels = issue.data.labels.map(label => label.name);
const pendingLabel = labels.find(label => label.startsWith("pending-reassignment-"));
if (pendingLabel) {
const lastRequester = pendingLabel.replace("pending-reassignment-", "");
const lastUpdated = new Date(issue.data.updated_at);
const now = new Date();
const diffDays = (now - lastUpdated) / (1000 * 60 * 60 * 24);
if (diffDays >= 7) {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [lastRequester]
});
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: `@${lastRequester}, the issue has been reassigned to you after 7 days of no response! :coffee:`
});
console.log(`✅ Issue reassigned to ${lastRequester} due to inactivity.`);
}
}
- name: Inform how to use the bot when a new issue is created
if: github.event.action == 'opened'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue = context.payload.issue;
const repoOwner = context.repo.owner;
const repoName = context.repo.repo;
const issueNumber = issue.number;
const instructions = `To assign yourself to this issue, comment with one of the following:
- "assign me"
- "can you assign this issue to me"
- "i would like to work on this"
- "i want to do this issue"
If the issue is already assigned, the assigned user will be asked if they are still working on it.`;
await github.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
body: instructions
});
console.log('✅ Instructions posted on the issue creation.');

0 comments on commit 580aabe

Please sign in to comment.