okami.mom/strata

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

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

main
9.4 KiB218 linesraw
1'use strict';
2// Format check for issues and pull requests (see CONTRIBUTING.md and .github/ISSUE_TEMPLATE/*.yml).
3// Pure functions only: no network, no filesystem. Used by intake.yml and reopen.yml through reset-bot.js.
4
5const AREAS = ['Engine', 'GUI', 'TUI', 'Benchmark', 'Docs', 'Other'];
6const TITLE_RE = new RegExp('^\\[(' + AREAS.join('|') + ')\\] \\S');
7const NO_ANSWER = /^(_no response_|none|n\/a|na|-|todo|tbd|\.|\?)$/i;
8const NOT_RUN_LABELS = /^\*\*(passed|failed|not run)\s*:?\*\*\s*:?/gim;
9
10// Version the reopened work must be tested on.
11const CURRENT_VERSION_RE = /\b0\.2\.\d+|0\.2\.0-beta/i;
12// Any version or commit.
13const ANY_VERSION_RE = /\bv?\d+\.\d+(\.\d+)*(-[a-z0-9.]+)?\b|\b[0-9a-f]{7,40}\b|\bcommit\b/i;
14const HARDWARE_RE = /\b(rtx|gtx|geforce|quadro|tesla|radeon|rx ?\d|instinct|arc|a\d{3,4}|b\d{2,3}|apple|m[1-5]( pro| max| ultra)?|nvidia|amd|intel|gpu|cpu|vram|\d+ ?gb|ram|windows|linux|ubuntu|debian|fedora|arch|macos|mac os|wsl)\b/i;
15const REVISION_RE = /0\.2\.\d|beta|\bmain\b|\b[0-9a-f]{7,40}\b/i;
16
17function stripComments(text) {
18  return String(text || '').replace(/<!--[\s\S]*?-->/g, '');
19}
20
21function hasAreaPrefix(title) {
22  return TITLE_RE.test(String(title || ''));
23}
24
25// Issue forms render as "### Label\n\nvalue". Returns { label: value } for those headings.
26function parseSections(body) {
27  const out = {};
28  let cur = null;
29  for (const line of stripComments(body).split(/\r?\n/)) {
30    const m = /^#{1,6}\s+(.+?)\s*#*\s*$/.exec(line);
31    if (m) {
32      cur = m[1].trim();
33      out[cur] = '';
34    } else if (cur !== null) {
35      out[cur] += line + '\n';
36    }
37  }
38  for (const k of Object.keys(out)) out[k] = out[k].trim();
39  return out;
40}
41
42function realText(value) {
43  const t = String(value || '').replace(NOT_RUN_LABELS, '').replace(/\s+/g, ' ').trim();
44  return NO_ANSWER.test(t) ? '' : t;
45}
46
47function findSection(sections, re) {
48  for (const k of Object.keys(sections)) if (re.test(k)) return { key: k, value: sections[k] };
49  return null;
50}
51
52// Required parts of each issue form: [display name, heading regex, minimum characters of real text].
53const FORMS = {
54  bug: {
55    detect: /what happened/i,
56    parts: [
57      ['Area', /^area$/i, 2],
58      ['Desktop app, Terminal or source?', /desktop app, terminal or source/i, 2],
59      ['What happened', /what happened/i, 20],
60      ['Reproduction status', /reproduction status/i, 2],
61      ['Strata version', /strata version/i, 3],
62      ['Graphics card, memory and system', /graphics card|memory and system/i, 3],
63    ],
64    needsCurrentVersion: 'Strata version',
65  },
66  feature: {
67    detect: /what do you want to do/i,
68    parts: [
69      ['Area', /^area$/i, 2],
70      ['What do you want to do?', /what do you want to do/i, 20],
71    ],
72  },
73  question: {
74    detect: /your question/i,
75    parts: [
76      ['Area', /^area$/i, 2],
77      ['Your question', /your question/i, 15],
78    ],
79  },
80  benchmark: {
81    detect: /what did you measure/i,
82    parts: [
83      ['What did you measure, or what went wrong', /what did you measure/i, 30],
84      ['Reproduction status', /reproduction status/i, 2],
85    ],
86    needsCurrentVersionInBody: true,
87  },
88};
89
90function checkIssue({ title, body, labels = [], mode = 'intake' }) {
91  const missing = [];
92  if (!hasAreaPrefix(title)) {
93    missing.push('Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other], then a short description. Example: "[Engine] decode stops after 2 minutes".');
94  }
95  const text = stripComments(body);
96  const sections = parseSections(text);
97  const labelSet = new Set(labels.map((l) => String(l).toLowerCase()));
98  const needCurrent = mode === 'reopen';
99
100  let formKey = null;
101  for (const [key, f] of Object.entries(FORMS)) {
102    if (findSection(sections, f.detect)) { formKey = key; break; }
103  }
104
105  if (formKey) {
106    const form = FORMS[formKey];
107    for (const [name, re, min] of form.parts) {
108      const s = findSection(sections, re);
109      if (!s || realText(s.value).length < min) missing.push('Fill in "' + name + '".');
110    }
111    if (needCurrent && form.needsCurrentVersion) {
112      const s = findSection(sections, /strata version/i);
113      if (s && realText(s.value) && !CURRENT_VERSION_RE.test(s.value)) {
114        missing.push('Re-test on Strata 0.2.0-beta and put that version in "' + form.needsCurrentVersion + '". If it no longer happens, say so in a comment instead.');
115      }
116    }
117    if (needCurrent && form.needsCurrentVersionInBody && !CURRENT_VERSION_RE.test(text)) {
118      missing.push('Say which Strata version you measured on. Re-measure on 0.2.0-beta if you can.');
119    }
120    return { ok: missing.length === 0, missing, kind: formKey };
121  }
122
123  // Free-form text (older issues, blank issues).
124  const plain = realText(text);
125  const lenient = labelSet.has('enhancement') || labelSet.has('question');
126  if (plain.length < (lenient ? 40 : 80)) {
127    missing.push('Describe it in the issue body: what happened, what you expected, and the exact error if there is one. The issue forms (New issue > Bug report) list what to include.');
128  }
129  if (!lenient) {
130    if (!ANY_VERSION_RE.test(text)) missing.push('Add the Strata version (Settings > About, or `strata --version`).');
131    else if (needCurrent && !CURRENT_VERSION_RE.test(text)) missing.push('Re-test on Strata 0.2.0-beta and name that version in the issue. If it no longer happens, say so in a comment instead.');
132    if (!HARDWARE_RE.test(text)) missing.push('Add your graphics card with its VRAM, your RAM and your operating system.');
133  }
134  return { ok: missing.length === 0, missing, kind: 'free-form' };
135}
136
137function checkPull({ title, body, mode = 'intake' }) {
138  const missing = [];
139  if (!hasAreaPrefix(title)) {
140    missing.push('Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other], then a short description. Example: "[Engine] fix decode stop on long prompts".');
141  }
142  const text = stripComments(body);
143  const sections = parseSections(text);
144  const problem = findSection(sections, /problem|result|summary|what.*change/i);
145  const validation = findSection(sections, /validation|testing|tests?\b|how.*tested/i);
146
147  let valText = '';
148  if (problem || validation) {
149    if (!problem || realText(problem.value).length < 20) missing.push('Fill in "Problem and result": what triggered the change, and how it behaves before and after.');
150    if (!validation || realText(validation.value).length < 20) {
151      missing.push('Fill in "Validation": the exact commands, the revision and environment tested, what you saw. Keep Passed, Failed and Not run apart.');
152    } else {
153      valText = validation.value;
154    }
155  } else {
156    const plain = realText(text);
157    if (plain.length < 150) {
158      missing.push('Describe the pull request: the problem and the result. The pull request template shows the parts (Problem and result, Related discussion, Approach, Validation, Limits).');
159    }
160    if (!/(problem|result|before|after|fix|because|bug)/i.test(plain)) missing.push('Add "Problem and result": what triggered the change, and how it behaves before and after.');
161    if (!/(validat|tested|passed|ran |built|build|not run)/i.test(plain)) missing.push('Add "Validation": the exact commands, the revision and environment tested, and what you saw.');
162    valText = plain;
163  }
164  if (mode === 'reopen' && valText && !REVISION_RE.test(valText)) {
165    missing.push('Say in Validation which revision you tested: rebase on main (or 0.2.0-beta) and re-run your checks.');
166  }
167  return { ok: missing.length === 0, missing, kind: 'pull' };
168}
169
170function checkItem({ isPull, ...rest }) {
171  return isPull ? checkPull(rest) : checkIssue(rest);
172}
173
174function checklist(missing) {
175  return missing.map((m) => '- [ ] ' + m).join('\n');
176}
177
178const CONTRIB = 'https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md';
179
180function intakeComment(isPull, missing) {
181  return [
182    'Thanks for the ' + (isPull ? 'pull request' : 'issue') + '. To keep the tracker readable, every item needs an area prefix in the title and the parts of the ' + (isPull ? 'pull request template' : 'issue form') + ' filled in. This one is missing:',
183    '',
184    checklist(missing),
185    '',
186    'Edit the ' + (isPull ? 'pull request' : 'issue') + ' (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment `/reopen`.',
187    '',
188    'Details: [CONTRIBUTING.md](' + CONTRIB + ')',
189  ].join('\n');
190}
191
192function reopenMissingComment(isPull, missing) {
193  return [
194    'Not reopened yet. Still missing:',
195    '',
196    checklist(missing),
197    '',
198    'Edit the ' + (isPull ? 'pull request' : 'issue') + ' and comment `/reopen` again. Details: [CONTRIBUTING.md](' + CONTRIB + ')',
199  ].join('\n');
200}
201
202function reopenOkComment(isPull) {
203  return 'Reopened, thank you. A maintainer will pick it up from here.' + (isPull ? ' If it conflicts with main, rebase it first; that speeds up review.' : '');
204}
205
206function staleComment(isPull) {
207  return [
208    'Closing this ' + (isPull ? 'pull request' : 'issue') + ' because it still did not follow the format after 5 days. That is housekeeping, not a rejection.',
209    '',
210    'To bring it back: fix the title (area prefix) and the missing parts, then comment `/reopen`. A check runs and reopens it, or tells you what is still missing. See [CONTRIBUTING.md](' + CONTRIB + ').',
211  ].join('\n');
212}
213
214module.exports = {
215  AREAS, TITLE_RE, hasAreaPrefix, parseSections, stripComments, realText,
216  checkIssue, checkPull, checkItem, checklist,
217  intakeComment, reopenMissingComment, reopenOkComment, staleComment,
218};