Skip to content

Commit

Permalink
chore: Issue PR reminder workflow created (keyshade-xyz#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
csehatt741 authored Feb 20, 2025
1 parent 7654a9d commit 7667ebd
Show file tree
Hide file tree
Showing 5 changed files with 669 additions and 19 deletions.
115 changes: 96 additions & 19 deletions .github/workflows/auto-assign.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,79 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
USER_MAX_CONCURRENT_ISSUE_COUNT: ${{ vars.USER_MAX_CONCURRENT_ISSUE_COUNT }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const comment = context.payload.comment;
const issue = context.issue;
const owner = "keyshade-xyz";
const repo = "keyshade";
const owner = 'keyshade-xyz';
const repo = 'keyshade';
const userMaxConcurrentIssueCount = process.env.USER_MAX_CONCURRENT_ISSUE_COUNT || 5;
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;
}
async function listUserOpenIssuesWithoutActivePullRequest(user) {
const issuesWithoutActivePullRequest = [];
const { data: userOpenIssues } = await github.rest.issues.listForRepo({
owner,
repo,
state: 'open',
assignee: user.login,
per_page: 100
});
for (const issue of userOpenIssues) {
const events = await listIssueEvents(issue);
const userPullRequestIssues = events
.filter(event => event.event === 'cross-referenced')
.map(event => event.source.issue)
.filter(issue => issue && issue.pull_request && issue.user.login === comment.user.login);
if(userPullRequestIssues.length === 0) {
issuesWithoutActivePullRequest.push(issue);
continue;
}
for (const pullRequestIssue of userPullRequestIssues) {
const { data: pullRequest } = await github.rest.pulls.get({
owner,
repo,
pull_number: pullRequestIssue.number
});
if(pullRequest.draft) {
issuesWithoutActivePullRequest.push(issue);
break;
}
}
}
return issuesWithoutActivePullRequest;
}
async function updateProjectStatus(issueNumber) {
const projectsResponse = await github.rest.projects.listForRepo({
Expand All @@ -30,49 +96,60 @@ jobs:
per_page: 100,
});
const inProgressColumn = columnsResponse.data.find(column => column.name === "In Progress");
const inProgressColumn = columnsResponse.data.find(column => column.name === 'In Progress');
if (!inProgressColumn) continue;
const cardsResponse = await github.rest.projects.listCards({
column_id: inProgressColumn.id,
per_page: 100,
});
const issueCardExists = cardsResponse.data.some(card => card.content_id === issueNumber && card.content_type === "Issue");
const issueCardExists = cardsResponse.data.some(card => card.content_id === issueNumber && card.content_type === 'Issue');
if (!issueCardExists) {
await github.rest.projects.createCard({
column_id: inProgressColumn.id,
content_id: issueNumber,
content_type: "Issue",
content_type: 'Issue',
});
}
}
}
if (comment.body.startsWith('/attempt')) {
if (!issue.assignee) {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issue.number,
assignees: [comment.user.login],
});
const userActiveIssues = await listUserOpenIssuesWithoutActivePullRequest(comment.user);
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `Assigned the issue to @${comment.user.login}!`,
});
if(userActiveIssues.length < userMaxConcurrentIssueCount) {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issue.number,
assignees: [comment.user.login],
});
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `Assigned the issue to @${comment.user.login}!`,
});
await updateProjectStatus(issue.number);
await updateProjectStatus(issue.number);
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `@${comment.user.login}, cannot concurrently work on more than ${userMaxConcurrentIssueCount} issues. Tag a maintainer if you need to take over.`,
});
}
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: 'This issue is already assigned. Tag a maintainer if you need to take over.',
body: `@${comment.user.login}, this issue is already assigned. Tag a maintainer if you need to take over.`,
});
}
}
172 changes: 172 additions & 0 deletions .github/workflows/issue-pr-reminder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
name: Issue PR Reminder

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

jobs:
issue-pr-reminder:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
CREATE_ISSUE_PR_REMINDER_ENABLED: ${{ vars.CREATE_ISSUE_PR_REMINDER_ENABLED }}
CREATE_ISSUE_PR_REMINDER_AFTER_ISSUE_ASSIGNED_DAYS: ${{ vars.CREATE_ISSUE_PR_REMINDER_AFTER_ISSUE_ASSIGNED_DAYS }}
CREATE_ISSUE_PR_REMINDER_BEFORE_ISSUE_UNASSIGNED_DAYS: ${{ vars.CREATE_ISSUE_PR_REMINDER_BEFORE_ISSUE_UNASSIGNED_DAYS }}
UNASSIGN_ISSUE_AFTER_DAYS: ${{ vars.UNASSIGN_ISSUE_AFTER_DAYS }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = 'keyshade-xyz';
const repo = 'keyshade';
const createPrReminderEnabled = process.env.CREATE_ISSUE_PR_REMINDER_ENABLED || false;
const createPrReminderAfterIssueAssignedDays = process.env.CREATE_ISSUE_PR_REMINDER_AFTER_ISSUE_ASSIGNED_DAYS || 2;
const createPrReminderBeforeIssueUnassignedDays = process.env.CREATE_ISSUE_PR_REMINDER_BEFORE_ISSUE_UNASSIGNED_DAYS || 2;
const unassignIssueAfterDays = process.env.UNASSIGN_ISSUE_AFTER_DAYS || 14;
const createPrReminderAfterIssueAssignedMilliseconds = createPrReminderAfterIssueAssignedDays * 24 * 60 * 60 * 1000;
const createPrReminderBeforeIssueUnassignedMilliseconds = createPrReminderBeforeIssueUnassignedDays * 24 * 60 * 60 * 1000;
const unassignIssueAfterMilliseconds = unassignIssueAfterDays * 24 * 60 * 60 * 1000;
const now = Date.now();
if(!createPrReminderEnabled) {
console.log('!!! Dry run, there are no changes made !!!');
}
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 events = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const eventsResponse = await github.rest.issues.listEventsForTimeline({
owner,
repo,
issue_number: issue.number,
per_page: 100,
page
});
events.push(...eventsResponse.data);
hasNextPage = eventsResponse.headers.link && eventsResponse.headers.link.includes('rel="next"');
page++;
}
return events.sort((a, b) => a.id > b.id);
}
async function listIssueComments(issue) {
const comments = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const commentsResponse = await github.rest.issues.listComments({
owner,
repo,
issue_number: issue.number,
per_page: 100,
page
});
comments.push(...commentsResponse.data);
hasNextPage = commentsResponse.headers.link && commentsResponse.headers.link.includes('rel="next"');
page++;
}
return comments;
}
async function createPrReminder(assignee, issueNumber, comments, createPrReminderAfter, prReminder) {
// Check if it is time to create PR reminder
if (now < createPrReminderAfter) {
return false;
}
// Check if PR reminder has already been created
if (comments.some(comment => comment.body === prReminder)) {
return false;
}
// Create PR reminder
if(createPrReminderEnabled) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: prReminder
});
}
console.log(`Issue '${issueNumber}' PR reminder created for: ${assignee.login}`);
return true;
}
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) {
const assignedEvent = events.find(event => event.event === 'assigned' && event.assignee.login === assignee.login);
const issueAssignedAt = new Date(assignedEvent.created_at);
// Check if assignee has already opened a PR
const assigneePullRequest = pullRequests.find(pullRequest => pullRequest.user.login === assignee.login);
if(assigneePullRequest) {
continue;
}
// Create first PR reminder
const createFirstPrReminderAfter = new Date(issueAssignedAt.getTime() + createPrReminderAfterIssueAssignedMilliseconds);
const firstPrReminder = `@${assignee.login}, please open a draft PR linking this issue!`;
const isFirstPrReminderCreated = await createPrReminder(assignee, issue.number, comments, createFirstPrReminderAfter, firstPrReminder);
if(isFirstPrReminderCreated) {
continue;
}
// Create final PR reminder
const unassignIssueAfter = new Date(issueAssignedAt.getTime() + unassignIssueAfterMilliseconds);
const createFinalPrReminderAfter = new Date(issueAssignedAt.getTime() + unassignIssueAfterMilliseconds - createPrReminderBeforeIssueUnassignedMilliseconds);
const finalPrReminder = `@${assignee.login}, please open a draft PR linking this issue; otherwise you will be unassigned from this issue after ${unassignIssueAfter}!`;
await createPrReminder(assignee, issue.number, comments, createFinalPrReminderAfter, finalPrReminder);
}
}
}
await createPrReminders();
Loading

0 comments on commit 7667ebd

Please sign in to comment.