okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-020-reset' into f-020-intf57c0adcb

main
7.0 KiB164 linesraw
1'use strict';
2// GitHub-side handlers for intake.yml, reopen.yml and the 5-day sweep.
3// They take the actions/github-script objects ({ github, context, core }) and never touch PR code:
4// everything comes from the event payload and the REST API.
5
6const check = require('./item-check');
7
8const LABEL = 'needs-format';
9const STALE_DAYS = 5;
10const MAINTAINER = new Set(['OWNER', 'MEMBER', 'COLLABORATOR']);
11// Items a maintainer closed on purpose stay closed.
12const BLOCK_LABELS = new Set(['wontfix', 'invalid', 'duplicate', 'declined']);
13
14function labelNames(item) {
15  return (item.labels || []).map((l) => (typeof l === 'string' ? l : l.name));
16}
17
18async function ensureLabel(github, context) {
19  const { owner, repo } = context.repo;
20  try {
21    await github.rest.issues.getLabel({ owner, repo, name: LABEL });
22  } catch (e) {
23    if (e.status !== 404) throw e;
24    await github.rest.issues.createLabel({
25      owner, repo, name: LABEL, color: 'fbca04',
26      description: 'Title or description does not follow CONTRIBUTING.md yet',
27    });
28  }
29}
30
31async function removeLabel(github, context, number) {
32  const { owner, repo } = context.repo;
33  try {
34    await github.rest.issues.removeLabel({ owner, repo, issue_number: number, name: LABEL });
35  } catch (e) {
36    if (e.status !== 404) throw e;
37  }
38}
39
40function itemFromPayload(context) {
41  const p = context.payload;
42  return p.pull_request || p.issue;
43}
44
45function isPullItem(item, context) {
46  return Boolean(context.payload.pull_request || item.pull_request);
47}
48
49// issues: opened/edited/reopened, pull_request_target: opened/edited/reopened
50async function intake({ github, context, core }) {
51  const { owner, repo } = context.repo;
52  const item = itemFromPayload(context);
53  if (!item || (item.user && item.user.type === 'Bot')) return;
54  if (MAINTAINER.has(item.author_association) && context.payload.action === 'opened') {
55    // Maintainer items are not nagged on creation.
56    return;
57  }
58  const isPull = isPullItem(item, context);
59  const labels = labelNames(item);
60  const hasLabel = labels.includes(LABEL);
61  const res = check.checkItem({ isPull, title: item.title, body: item.body, labels, mode: 'intake' });
62  core.info('check ' + (isPull ? 'PR' : 'issue') + ' #' + item.number + ': ' + (res.ok ? 'ok' : res.missing.join(' | ')));
63
64  if (res.ok) {
65    if (hasLabel) {
66      await removeLabel(github, context, item.number);
67      await github.rest.issues.createComment({ owner, repo, issue_number: item.number, body: 'Thanks, this follows the format now.' });
68    }
69    return;
70  }
71  if (context.payload.action === 'edited' && !hasLabel) return; // edits only matter for items already flagged
72  if (hasLabel) return;                                        // one note only
73  await ensureLabel(github, context);
74  await github.rest.issues.addLabels({ owner, repo, issue_number: item.number, labels: [LABEL] });
75  await github.rest.issues.createComment({ owner, repo, issue_number: item.number, body: check.intakeComment(isPull, res.missing) });
76}
77
78// issue_comment created, body starts with /reopen
79async function reopen({ github, context, core }) {
80  const { owner, repo } = context.repo;
81  const comment = context.payload.comment;
82  const issue = context.payload.issue;
83  if (!/^\s*\/reopen\b/.test(comment.body || '')) return;
84  if (comment.user.type === 'Bot') return;
85  const isAuthor = comment.user.login === issue.user.login;
86  const isMaint = MAINTAINER.has(comment.author_association);
87  if (!isAuthor && !isMaint) {
88    core.info('ignored /reopen from ' + comment.user.login + ' (not the author or a maintainer)');
89    return;
90  }
91  const isPull = Boolean(issue.pull_request);
92  const say = (body) => github.rest.issues.createComment({ owner, repo, issue_number: issue.number, body });
93  const labels = labelNames(issue);
94  const wasOpen = issue.state === 'open';
95
96  if (labels.some((l) => BLOCK_LABELS.has(l.toLowerCase())) && !isMaint) {
97    await say('This one was closed on purpose (' + labels.filter((l) => BLOCK_LABELS.has(l.toLowerCase())).join(', ') + '). Please add your new evidence in a comment and a maintainer will decide.');
98    return;
99  }
100  if (wasOpen && !labels.includes(LABEL)) {
101    core.info('already open, nothing to do');
102    return;
103  }
104
105  // The bot reads the live item, not the possibly stale event copy.
106  const live = (await github.rest.issues.get({ owner, repo, issue_number: issue.number })).data;
107  const res = check.checkItem({ isPull, title: live.title, body: live.body, labels: labelNames(live), mode: 'reopen' });
108  core.info('reopen check #' + live.number + ': ' + (res.ok ? 'ok' : res.missing.join(' | ')));
109
110  if (!res.ok) {
111    await say(check.reopenMissingComment(isPull, res.missing));
112    return;
113  }
114  if (!wasOpen) {
115    try {
116      await github.rest.issues.update({ owner, repo, issue_number: live.number, state: 'open' });
117    } catch (e) {
118      await say('The check passed, but GitHub would not reopen it (' + (e.message || e.status) + '). ' + (isPull ? 'If the branch was deleted, restore it or open a new pull request and link this one.' : 'Please open a new issue and link this one.'));
119      return;
120    }
121  }
122  await removeLabel(github, context, live.number);
123  let extra = '';
124  if (isPull) {
125    try {
126      const pr = (await github.rest.pulls.get({ owner, repo, pull_number: live.number })).data;
127      if (pr.mergeable_state === 'dirty') extra = '\n\nIt has conflicts with main now: please rebase it.';
128    } catch (e) { /* best effort */ }
129  }
130  await say(check.reopenOkComment(isPull) + extra);
131}
132
133async function labelTime(github, owner, repo, number, fallback) {
134  const events = await github.paginate(github.rest.issues.listEvents, { owner, repo, issue_number: number, per_page: 100 });
135  let t = null;
136  for (const e of events) {
137    if (e.event === 'labeled' && e.label && e.label.name === LABEL) t = e.created_at;
138  }
139  return t || fallback;
140}
141
142// scheduled: close items that kept the needs-format label for more than 5 days
143async function sweep({ github, context, core }) {
144  const { owner, repo } = context.repo;
145  const cutoff = Date.now() - STALE_DAYS * 86400 * 1000;
146  const items = await github.paginate(github.rest.issues.listForRepo, { owner, repo, state: 'open', labels: LABEL, per_page: 100 });
147  core.info(items.length + ' open items with ' + LABEL);
148  for (const item of items) {
149    const since = new Date(await labelTime(github, owner, repo, item.number, item.created_at)).getTime();
150    if (since > cutoff) continue;
151    const isPull = Boolean(item.pull_request);
152    // Re-check first: an edit that fixed it should have cleared the label already; do not close good items.
153    const res = check.checkItem({ isPull, title: item.title, body: item.body, labels: labelNames(item), mode: 'intake' });
154    if (res.ok) {
155      await removeLabel(github, context, item.number);
156      continue;
157    }
158    await github.rest.issues.createComment({ owner, repo, issue_number: item.number, body: check.staleComment(isPull) });
159    await github.rest.issues.update({ owner, repo, issue_number: item.number, state: 'closed', state_reason: 'not_planned' });
160    core.info('closed #' + item.number);
161  }
162}
163
164module.exports = { intake, reopen, sweep, LABEL, STALE_DAYS };