Skip to content

Commit

Permalink
Issue unassign workflow created
Browse files Browse the repository at this point in the history
  • Loading branch information
csehatt741 committed Feb 18, 2025
1 parent 1375534 commit 3e90d65
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 108 deletions.
253 changes: 145 additions & 108 deletions .github/workflows/issue-pr-reminder.yaml
Original file line number Diff line number Diff line change
@@ -1,108 +1,145 @@
name: Issue PR Reminder

on:
schedule:
- cron: '0 * * * *' # Runs every hour
workflow_dispatch:

jobs:
issue-reminder:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
CREATE_REMINDER_AFTER_HOURS: ${{ vars.CREATE_REMINDER_AFTER_HOURS }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = "keyshade-xyz";
const repo = "keyshade";
const createReminderAfterHours = process.env.CREATE_REMINDER_AFTER_HOURS || 48;
const createReminderAfterMilliseconds = createReminderAfterHours * 60 * 60 * 1000;
const now = Date.now();
async function listOpenIssuesWithoutPR() {
const allIssues = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listForRepo({
owner,
repo,
state: "open",
per_page: 100,
page
});
const issuesWithoutPR = issuesResponse.data.filter(issue => !issue.pull_request);
allIssues.push(...issuesWithoutPR);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return allIssues;
}
async function listIssueComments(issue) {
const allComments = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listComments({
owner,
repo,
issue_number: issue.number,
per_page: 100,
page
});
allComments.push(...issuesResponse.data);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return allComments;
}
async function createReminders() {
const issues = await listOpenIssuesWithoutPR();
for (const issue of issues) {
const issueCreatedAt = new Date(issue.created_at);
const createReminderAfter = new Date(issueCreatedAt.getTime() + createReminderAfterMilliseconds);
// Check if it's time to create the reminder
if (now < createReminderAfter) {
continue;
}
// Create reminder for each assignee
const comments = await listIssueComments(issue);
for (const assignee of issue.assignees) {
const reminder = `@${assignee.login}, please open a draft PR linking this issue!`;
// Check if the issue already has the reminder
if (comments.some(comment => comment.body === reminder)) {
continue;
}
// Create the reminder
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: reminder
});
console.log(`Reminder created for: ${assignee.login}`);
}
}
}
await createReminders();
name: Issue PR Reminder

on:
schedule:
- cron: '0 * * * *' # Runs every hour
workflow_dispatch:

jobs:
issue-reminder:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
CREATE_PR_REMINDER_ENABLED: ${{ vars.CREATE_PR_REMINDER_ENABLED }}
CREATE_PR_REMINDER_AFTER_DAYS: ${{ vars.CREATE_PR_REMINDER_AFTER_DAYS }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = "keyshade-xyz";
const repo = "keyshade";
const createPrReminderEnabled = process.env.CREATE_PR_REMINDER_ENABLED || false;
const createPrReminderAfterDays = process.env.CREATE_PR_REMINDER_AFTER_DAYS || 2;
const createPrReminderAfterMilliseconds = createPrReminderAfterDays * 24 * 60 * 60 * 1000;
const now = Date.now();
async function listOpenIssues() {
const issues = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listForRepo({
owner,
repo,
state: "open",
per_page: 100,
page
});
issues.push(...issuesResponse.data);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return issues;
}
async function listIssueEvents(issue) {
const allEvents = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listEventsForTimeline({
owner,
repo,
issue_number: issue.number,
per_page: 100,
page
});
allEvents.push(...issuesResponse.data);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return allEvents.sort((a, b) => a.id > b.id);
}
async function listIssueComments(issue) {
const allComments = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listComments({
owner,
repo,
issue_number: issue.number,
per_page: 100,
page
});
allComments.push(...issuesResponse.data);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return allComments;
}
async function createPrReminders() {
const issues = await listOpenIssues();
for (const issue of issues) {
const events = await listIssueEvents(issue);
const pullRequests = events
.filter(event => event.event === 'cross-referenced')
.map(event => event.source.issue)
.filter(issue => issue && issue.pull_request);
const comments = await listIssueComments(issue);
for (const assignee of issue.assignees) {
// Check if assignee has already opened a PR
const assigneePullRequest = pullRequests.find(pullRequest => pullRequest.user.login === assignee.login);
if(assigneePullRequest) {
continue;
}
// Check if it is time to create a reminder
const assignedEvent = events.find(event => event.event === 'assigned' && event.assignee.login === assignee.login);
const issueAssignedAt = new Date(assignedEvent.created_at);
const createPrReminderAfter = new Date(issueAssignedAt.getTime() + createPrReminderAfterMilliseconds);
if (now < createPrReminderAfter) {
continue;
}
// Check if reminder has already been created
const reminder = `@${assignee.login}, please open a draft PR linking this issue!`;
if (comments.some(comment => comment.body === reminder)) {
continue;
}
// Create reminder
if(createPrReminderEnabled) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: reminder
});
}
console.log(`Issue '${issue.number}' reminder created for: ${assignee.login}`);
}
}
}
await createPrReminders();
120 changes: 120 additions & 0 deletions .github/workflows/issue-unassign.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: Issue Unassign

on:
schedule:
- cron: '0 * * * *' # Runs every hour
workflow_dispatch:

jobs:
issue-reminder:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
UNASSIGN_ISSUE_ENABLED: ${{ vars.UNASSIGN_ISSUE_ENABLED }}
UNASSIGN_ISSUE_AFTER_DAYS: ${{ vars.UNASSIGN_ISSUE_AFTER_DAYS }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = "keyshade-xyz";
const repo = "keyshade";
const unassignIssueEnabled = process.env.UNASSIGN_ISSUE_ENABLED || false;
const unassignIssueAfterDays = process.env.CREATE_PR_REMINDER_AFTER_DAYS || 14;
const unassignIssueAfterMilliseconds = unassignIssueAfterDays * 24 * 60 * 60 * 1000;
const now = Date.now();
async function listOpenIssues() {
const issues = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listForRepo({
owner,
repo,
state: "open",
per_page: 100,
page
});
issues.push(...issuesResponse.data);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return issues;
}
async function listIssueEvents(issue) {
const allEvents = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const issuesResponse = await github.rest.issues.listEventsForTimeline({
owner,
repo,
issue_number: issue.number,
per_page: 100,
page
});
allEvents.push(...issuesResponse.data);
hasNextPage = issuesResponse.headers.link && issuesResponse.headers.link.includes('rel="next"');
page++;
}
return allEvents.sort((a, b) => a.id > b.id);
}
async function unassignIssues() {
const issues = await listOpenIssues();
for (const issue of issues) {
const events = await listIssueEvents(issue);
const pullRequests = events
.filter(event => event.event === 'cross-referenced')
.map(event => event.source.issue)
.filter(issue => issue && issue.pull_request);
for (const assignee of issue.assignees) {
// Check if assignee has already opened a PR
const assigneePullRequest = pullRequests.find(pullRequest => pullRequest.user.login === assignee.login);
if(assigneePullRequest) {
continue;
}
// Check if it is time to unassign issue
const assignedEvent = events.find(event => event.event === 'assigned' && event.assignee.login === assignee.login);
const issueAssignedAt = new Date(assignedEvent.created_at);
const unassignIssueAfter = new Date(issueAssignedAt.getTime() + unassignIssueAfterMilliseconds);
if (now < unassignIssueAfter) {
continue;
}
if(unassignIssueEnabled) {
await github.rest.issues.removeAssignees({
owner,
repo,
issue_number: issue.number,
assignees: [assignee.login],
});
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `Unassigned the issue from @${assignee.login}!`,
});
}
console.log(`Issue '${issue.number}' user unassigned: ${assignee.login}`);
}
}
}
await unassignIssues();

0 comments on commit 3e90d65

Please sign in to comment.