|
| 1 | +const INACTIVITY_DAYS = 45; |
| 2 | +const MS_PER_DAY = 24 * 60 * 60 * 1000; |
| 3 | +const REMINDER_MARKER = "<!-- assignee-reminder -->"; |
| 4 | +const COMMENT_TEMPLATE = `${REMINDER_MARKER} |
| 5 | +Dear {mentions}, |
| 6 | + |
| 7 | +This is a gentle reminder that it has been {days} days since the last update on this issue. |
| 8 | +Please review the issue and provide any updates or progress in the comments. |
| 9 | + |
| 10 | +Thank you.`; |
| 11 | + |
| 12 | +async function run({ github, context, core, dryRun }) { |
| 13 | + const owner = context.repo.owner; |
| 14 | + const repo = context.repo.repo; |
| 15 | + const now = Date.now(); |
| 16 | + const inactivityMs = INACTIVITY_DAYS * MS_PER_DAY; |
| 17 | + |
| 18 | + let totalChecked = 0; |
| 19 | + let totalReminders = 0; |
| 20 | + |
| 21 | + core.info(`Starting assignee-reminder (dryRun=${dryRun})`); |
| 22 | + core.info(`Inactivity threshold: ${INACTIVITY_DAYS} days`); |
| 23 | + |
| 24 | + for await (const { data: issues } of github.paginate.iterator( |
| 25 | + github.rest.issues.listForRepo, |
| 26 | + { |
| 27 | + owner, |
| 28 | + repo, |
| 29 | + state: "open", |
| 30 | + assignee: "*", |
| 31 | + per_page: 100, |
| 32 | + }, |
| 33 | + )) { |
| 34 | + for (const issue of issues) { |
| 35 | + if (issue.pull_request) continue; |
| 36 | + |
| 37 | + totalChecked++; |
| 38 | + |
| 39 | + const reminded = await processIssue({ |
| 40 | + github, |
| 41 | + owner, |
| 42 | + repo, |
| 43 | + issue, |
| 44 | + now, |
| 45 | + inactivityMs, |
| 46 | + dryRun, |
| 47 | + core, |
| 48 | + }); |
| 49 | + |
| 50 | + if (reminded) totalReminders++; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + core.info("--- Summary ---"); |
| 55 | + core.info(`Total issues checked: ${totalChecked}`); |
| 56 | + core.info(`Total reminders sent: ${totalReminders}`); |
| 57 | +} |
| 58 | + |
| 59 | +async function processIssue({ |
| 60 | + github, |
| 61 | + owner, |
| 62 | + repo, |
| 63 | + issue, |
| 64 | + now, |
| 65 | + inactivityMs, |
| 66 | + dryRun, |
| 67 | + core, |
| 68 | +}) { |
| 69 | + const assignees = issue.assignees.map((a) => a.login); |
| 70 | + const assigneeSet = new Set(assignees.map((a) => a.toLowerCase())); |
| 71 | + |
| 72 | + const activityByAssignee = {}; |
| 73 | + let lastReminderAt = 0; |
| 74 | + |
| 75 | + for await (const { data: comments } of github.paginate.iterator( |
| 76 | + github.rest.issues.listComments, |
| 77 | + { owner, repo, issue_number: issue.number, per_page: 100 }, |
| 78 | + )) { |
| 79 | + for (const comment of comments) { |
| 80 | + if (comment.body?.includes(REMINDER_MARKER)) { |
| 81 | + const t = new Date(comment.created_at).getTime(); |
| 82 | + lastReminderAt = Math.max(lastReminderAt, t); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + const login = comment.user?.login?.toLowerCase(); |
| 87 | + if (login && assigneeSet.has(login)) { |
| 88 | + const t = new Date(comment.created_at).getTime(); |
| 89 | + activityByAssignee[login] = Math.max(activityByAssignee[login] || 0, t); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + for await (const { data: events } of github.paginate.iterator( |
| 95 | + github.rest.issues.listEvents, |
| 96 | + { owner, repo, issue_number: issue.number, per_page: 100 }, |
| 97 | + )) { |
| 98 | + for (const event of events) { |
| 99 | + if (event.event === "assigned" && event.assignee) { |
| 100 | + const login = event.assignee.login.toLowerCase(); |
| 101 | + if (assigneeSet.has(login)) { |
| 102 | + const t = new Date(event.created_at).getTime(); |
| 103 | + activityByAssignee[login] = Math.max(activityByAssignee[login] || 0, t); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + const issueCreatedAt = new Date(issue.created_at).getTime(); |
| 110 | + const lastAssigneeActivity = Math.max( |
| 111 | + ...Object.values(activityByAssignee), |
| 112 | + issueCreatedAt, |
| 113 | + ); |
| 114 | + |
| 115 | + if (now - lastAssigneeActivity < inactivityMs) return false; |
| 116 | + |
| 117 | + if (lastReminderAt > lastAssigneeActivity && now - lastReminderAt < inactivityMs) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + const mentions = assignees.map((a) => `@${a}`).join(", "); |
| 122 | + const days = Math.floor((now - lastAssigneeActivity) / MS_PER_DAY); |
| 123 | + |
| 124 | + if (dryRun) { |
| 125 | + core.info( |
| 126 | + `[DRY RUN] #${issue.number}: Would remind ${mentions} (inactive for ${days} days)`, |
| 127 | + ); |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + const body = COMMENT_TEMPLATE |
| 132 | + .replace("{mentions}", mentions) |
| 133 | + .replace("{days}", days); |
| 134 | + |
| 135 | + await github.rest.issues.createComment({ |
| 136 | + owner, |
| 137 | + repo, |
| 138 | + issue_number: issue.number, |
| 139 | + body, |
| 140 | + }); |
| 141 | + |
| 142 | + core.info(`#${issue.number}: Reminder sent to ${mentions} (inactive for ${days} days)`); |
| 143 | + return true; |
| 144 | +} |
| 145 | + |
| 146 | +module.exports = { run }; |
0 commit comments