|
| 1 | +module.exports = async ({ github, context, core }) => { |
| 2 | + const DAYS_UNTIL_STALE = 60; |
| 3 | + const DAYS_UNTIL_CLOSE = 7; |
| 4 | + const STALE_LABEL = 'stale'; |
| 5 | + const EXEMPT_LABELS = new Set([ |
| 6 | + 'Hacktoberfest', |
| 7 | + 'RFC', |
| 8 | + '⭐ EU-FOSSA Hackathon', |
| 9 | + ]); |
| 10 | + const EXEMPT_TYPES = new Set(['Bug', 'Feature']); |
| 11 | + const STALE_COMMENT = [ |
| 12 | + 'This issue has been automatically marked as stale because it has not had', |
| 13 | + 'recent activity. It will be closed if no further activity occurs. Thank you', |
| 14 | + 'for your contributions.', |
| 15 | + ].join(' '); |
| 16 | + const BOT_LOGINS = new Set(['github-actions[bot]', 'github-actions']); |
| 17 | + |
| 18 | + const DRY_RUN = /^(1|true|yes)$/i.test(process.env.DRY_RUN || ''); |
| 19 | + const MAX_ACTIONS_PER_RUN = Number.parseInt(process.env.MAX_ACTIONS_PER_RUN || '25', 10); |
| 20 | + |
| 21 | + const { owner, repo } = context.repo; |
| 22 | + const now = Date.now(); |
| 23 | + const staleCutoff = new Date(now - DAYS_UNTIL_STALE * 86400000); |
| 24 | + const closeCutoff = new Date(now - DAYS_UNTIL_CLOSE * 86400000); |
| 25 | + |
| 26 | + let actionsTaken = 0; |
| 27 | + const budgetExhausted = () => actionsTaken >= MAX_ACTIONS_PER_RUN; |
| 28 | + |
| 29 | + async function* iterateOpenIssues() { |
| 30 | + let cursor = null; |
| 31 | + while (true) { |
| 32 | + const data = await github.graphql(` |
| 33 | + query($owner: String!, $name: String!, $cursor: String) { |
| 34 | + repository(owner: $owner, name: $name) { |
| 35 | + issues(first: 100, after: $cursor, states: OPEN, orderBy: {field: UPDATED_AT, direction: ASC}) { |
| 36 | + pageInfo { hasNextPage endCursor } |
| 37 | + nodes { |
| 38 | + number |
| 39 | + updatedAt |
| 40 | + issueType { name } |
| 41 | + labels(first: 50) { nodes { name } } |
| 42 | + timelineItems(last: 100, itemTypes: [LABELED_EVENT]) { |
| 43 | + nodes { |
| 44 | + ... on LabeledEvent { |
| 45 | + createdAt |
| 46 | + label { name } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }`, { owner, name: repo, cursor }); |
| 54 | + |
| 55 | + const page = data.repository.issues; |
| 56 | + for (const node of page.nodes) yield node; |
| 57 | + if (!page.pageInfo.hasNextPage) break; |
| 58 | + cursor = page.pageInfo.endCursor; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + async function hasNonBotActivitySince(issue_number, since) { |
| 63 | + const events = await github.paginate( |
| 64 | + github.rest.issues.listEventsForTimeline, |
| 65 | + { owner, repo, issue_number, per_page: 100 }, |
| 66 | + ); |
| 67 | + return events.some(e => { |
| 68 | + const ts = e.created_at || e.submitted_at; |
| 69 | + if (!ts) return false; |
| 70 | + if (new Date(ts) <= since) return false; |
| 71 | + const actor = e.actor?.login || e.user?.login; |
| 72 | + if (actor && BOT_LOGINS.has(actor)) return false; |
| 73 | + return true; |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + function mostRecentStaleAt(issue) { |
| 78 | + let latest = null; |
| 79 | + for (const e of issue.timelineItems.nodes) { |
| 80 | + if (e?.label?.name !== STALE_LABEL) continue; |
| 81 | + const at = new Date(e.createdAt); |
| 82 | + if (!latest || at > latest) latest = at; |
| 83 | + } |
| 84 | + return latest; |
| 85 | + } |
| 86 | + |
| 87 | + async function addStale(issue_number) { |
| 88 | + if (DRY_RUN) { |
| 89 | + core.info(`DRY_RUN would stale #${issue_number}`); |
| 90 | + return; |
| 91 | + } |
| 92 | + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [STALE_LABEL] }); |
| 93 | + await github.rest.issues.createComment({ owner, repo, issue_number, body: STALE_COMMENT }); |
| 94 | + } |
| 95 | + |
| 96 | + async function close(issue_number) { |
| 97 | + if (DRY_RUN) { |
| 98 | + core.info(`DRY_RUN would close #${issue_number}`); |
| 99 | + return; |
| 100 | + } |
| 101 | + await github.rest.issues.update({ |
| 102 | + owner, repo, issue_number, state: 'closed', state_reason: 'not_planned', |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + async function unstale(issue_number) { |
| 107 | + if (DRY_RUN) { |
| 108 | + core.info(`DRY_RUN would unstale #${issue_number}`); |
| 109 | + return; |
| 110 | + } |
| 111 | + await github.rest.issues.removeLabel({ |
| 112 | + owner, repo, issue_number, name: STALE_LABEL, |
| 113 | + }).catch(err => { |
| 114 | + if (err.status !== 404) throw err; |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + const summary = { staled: 0, closed: 0, unstaled: 0, exempt: 0, scanned: 0, skipped: 0 }; |
| 119 | + |
| 120 | + for await (const issue of iterateOpenIssues()) { |
| 121 | + summary.scanned++; |
| 122 | + const labels = new Set(issue.labels.nodes.map(l => l.name)); |
| 123 | + const typeName = issue.issueType?.name; |
| 124 | + const exempt = (typeName && EXEMPT_TYPES.has(typeName)) |
| 125 | + || [...labels].some(l => EXEMPT_LABELS.has(l)); |
| 126 | + const hasStale = labels.has(STALE_LABEL); |
| 127 | + |
| 128 | + if (hasStale) { |
| 129 | + const staleAt = mostRecentStaleAt(issue); |
| 130 | + if (!staleAt) continue; |
| 131 | + |
| 132 | + const interacted = await hasNonBotActivitySince(issue.number, staleAt); |
| 133 | + if (interacted) { |
| 134 | + if (budgetExhausted()) { summary.skipped++; continue; } |
| 135 | + await unstale(issue.number); |
| 136 | + summary.unstaled++; |
| 137 | + actionsTaken++; |
| 138 | + } else if (staleAt <= closeCutoff) { |
| 139 | + if (budgetExhausted()) { summary.skipped++; continue; } |
| 140 | + await close(issue.number); |
| 141 | + summary.closed++; |
| 142 | + actionsTaken++; |
| 143 | + } |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + if (exempt) { |
| 148 | + summary.exempt++; |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + if (new Date(issue.updatedAt) <= staleCutoff) { |
| 153 | + if (budgetExhausted()) { summary.skipped++; continue; } |
| 154 | + await addStale(issue.number); |
| 155 | + summary.staled++; |
| 156 | + actionsTaken++; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + const prefix = DRY_RUN ? 'DRY_RUN ' : ''; |
| 161 | + core.info( |
| 162 | + `${prefix}scanned=${summary.scanned} staled=${summary.staled} ` + |
| 163 | + `closed=${summary.closed} unstaled=${summary.unstaled} ` + |
| 164 | + `exempt=${summary.exempt} skipped=${summary.skipped} ` + |
| 165 | + `budget=${MAX_ACTIONS_PER_RUN}`, |
| 166 | + ); |
| 167 | +}; |
0 commit comments