Просмотр исходного кода

chore: label issues by product interface (#205)

DanielWalnut 2 месяцев назад
Родитель
Сommit
91001a7694

+ 1 - 0
.github/ISSUE_TEMPLATE/01-bug-report.yml

@@ -10,6 +10,7 @@ body:
     id: product
     attributes:
       label: Product or interface
+      description: Desktop app and interactive TUI reports automatically receive the desktop or tui label.
       options:
         - Desktop app
         - CLI - interactive TUI

+ 1 - 0
.github/ISSUE_TEMPLATE/02-feature-request.yml

@@ -7,6 +7,7 @@ body:
     id: product
     attributes:
       label: Product or interface
+      description: Desktop app and interactive TUI reports automatically receive the desktop or tui label.
       options:
         - Desktop app
         - CLI - interactive TUI

+ 1 - 1
.github/ISSUE_TEMPLATE/03-question.yml

@@ -7,7 +7,7 @@ body:
     id: product
     attributes:
       label: Product or interface
-      description: Leave blank if unsure or asking about repository policy.
+      description: Leave blank if unsure or asking about repository policy. Desktop app and interactive TUI reports automatically receive the desktop or tui label.
       options:
         - Desktop app
         - CLI - interactive TUI

+ 13 - 0
.github/ISSUE_TEMPLATE/docs.yml

@@ -3,6 +3,19 @@ description: Report missing, incorrect, or unclear documentation.
 title: "[Docs] "
 labels: ["documentation", "needs-triage"]
 body:
+  - type: dropdown
+    id: product
+    attributes:
+      label: Product or interface
+      description: Desktop app and interactive TUI reports automatically receive the desktop or tui label.
+      options:
+        - Desktop app
+        - CLI - interactive TUI
+        - CLI - headless
+        - CLI - ACP
+        - Source build or repository tooling
+    validations:
+      required: true
   - type: input
     id: location
     attributes:

+ 71 - 0
.github/workflows/label-issue-product.yml

@@ -0,0 +1,71 @@
+name: Label issue product
+
+on:
+  issues:
+    types: [opened, edited]
+
+permissions:
+  issues: write
+
+concurrency:
+  group: issue-product-${{ github.event.issue.number }}
+  cancel-in-progress: false
+
+jobs:
+  label-product:
+    runs-on: ubuntu-latest
+    timeout-minutes: 5
+    if: ${{ github.repository == 'MiniMax-AI/minimax-code' && github.event.repository.private == false && !github.event.issue.pull_request }}
+    steps:
+      - name: Synchronize product labels
+        env:
+          GH_TOKEN: ${{ github.token }}
+        shell: bash
+        run: |
+          node <<'EOF'
+          const fs = require('node:fs');
+
+          async function main() {
+            const event = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'));
+            const issuePath = `/repos/${process.env.GITHUB_REPOSITORY}/issues/${event.issue.number}`;
+            async function request(path, method = 'GET', body) {
+              const response = await fetch(`${process.env.GITHUB_API_URL}${path}`, {
+                method,
+                headers: {
+                  Authorization: `Bearer ${process.env.GH_TOKEN}`,
+                  Accept: 'application/vnd.github+json',
+                  'Content-Type': 'application/json',
+                },
+                body: body === undefined ? undefined : JSON.stringify(body),
+              });
+              if (!response.ok) throw new Error(`GitHub ${method} failed: ${response.status}`);
+              return response.status === 204 ? undefined : response.json();
+            }
+
+            // Read current state so queued edits cannot restore an outdated label.
+            const issue = await request(issuePath);
+            const body = (issue.body || '').replace(/\r\n/g, '\n');
+            const product = body.match(/^### Product or interface\n+([^\n]+)\n*(?=### |$)/m)?.[1].trim();
+            const products = new Map([
+              ['Desktop app', 'desktop'],
+              ['CLI - interactive TUI', 'tui'],
+              ['CLI - headless', null],
+              ['CLI - ACP', null],
+              ['Source build or repository tooling', null],
+            ]);
+            // Leave manually triaged issues alone when no valid form answer exists.
+            if (!products.has(product)) return;
+            const desired = products.get(product);
+            const labels = issue.labels.map(label => label.name);
+            if (desired && !labels.includes(desired)) {
+              await request(`${issuePath}/labels`, 'POST', { labels: [desired] });
+            }
+            for (const label of ['desktop', 'tui']) {
+              if (label !== desired && labels.includes(label)) {
+                await request(`${issuePath}/labels/${label}`, 'DELETE');
+              }
+            }
+          }
+
+          main();
+          EOF

+ 1 - 0
release/public-source.json

@@ -19,6 +19,7 @@
     ".github/dependabot.yml",
     ".github/workflows/ci.yml",
     ".github/workflows/compatibility.yml",
+    ".github/workflows/label-issue-product.yml",
     ".github/workflows/security.yml",
     ".github/workflows/source-candidate.yml",
     ".github/workflows/sync-issue-to-feishu.yml",

+ 52 - 1
test/source-sync.test.mjs

@@ -14,6 +14,7 @@ import { gzipSync } from 'node:zlib';
 import { createHash } from 'node:crypto';
 import { fileURLToPath } from 'node:url';
 import { parse as parseYaml } from 'yaml';
+import { runInNewContext } from 'node:vm';
 
 function fixture(t) {
   const home = mkdtempSync(path.join(tmpdir(), 'source-sync-'));
@@ -424,11 +425,13 @@ test('suite runner preserves gate arguments and canonicalizes Windows temporary
 });
 
 test('public support forms preserve destination URLs and separate Desktop from CLI reports', () => {
-  const forms = ['01-bug-report.yml', '02-feature-request.yml', '03-question.yml'];
+  const forms = ['01-bug-report.yml', '02-feature-request.yml', '03-question.yml', 'docs.yml'];
   for (const name of forms) {
     const form = parseYaml(readFileSync(new URL(`../.github/ISSUE_TEMPLATE/${name}`, import.meta.url), 'utf8'));
     const product = form.body.find(field => field.id === 'product');
     assert.equal(product.validations.required, name !== '03-question.yml');
+    assert.equal(product.attributes.label, 'Product or interface');
+    assert.ok(product.attributes.options.includes('CLI - interactive TUI'));
     assert.ok(product.attributes.options.includes('Desktop app'));
     assert.ok(product.attributes.options.includes('CLI - ACP'));
     assert.ok(product.attributes.options.includes('CLI - headless'));
@@ -442,6 +445,54 @@ test('public support forms preserve destination URLs and separate Desktop from C
   assert.equal(classifyChanges(['README_ZH.md', 'README.md']).docsOnly, true);
 });
 
+test('issue product labels follow current form answers without replacing unrelated labels', async () => {
+  const workflow = parseYaml(readFileSync(new URL('../.github/workflows/label-issue-product.yml', import.meta.url), 'utf8'));
+  assert.deepEqual(workflow.on, { issues: { types: ['opened', 'edited'] } });
+  assert.deepEqual(workflow.permissions, { issues: 'write' });
+  assert.equal(workflow.concurrency['cancel-in-progress'], false);
+  const job = workflow.jobs['label-product'];
+  assert.equal(job.if, "${{ github.repository == 'MiniMax-AI/minimax-code' && github.event.repository.private == false && !github.event.issue.pull_request }}");
+  assert.equal(job.steps.length, 1); // No checkout or execution of issue-supplied code.
+  const run = job.steps[0].run;
+  assert.doesNotMatch(run, /\$\{\{/);
+  const script = run.match(/^node <<'EOF'\r?\n([\s\S]*?)\r?\nEOF\s*$/)?.[1];
+  assert.ok(script);
+  async function replay(body, labels, failureStatus) {
+    const writes = [];
+    const issuePath = '/repos/MiniMax-AI/minimax-code/issues/7';
+    await runInNewContext(script, {
+      require: name => {
+        assert.equal(name, 'node:fs');
+        return { readFileSync: () => JSON.stringify({ issue: { number: 7, body: '### Product or interface\n\nDesktop app', labels: [] } }) };
+      },
+      process: { env: { GITHUB_EVENT_PATH: 'fixture.json', GITHUB_REPOSITORY: 'MiniMax-AI/minimax-code', GITHUB_API_URL: 'https://api.github.invalid', GH_TOKEN: 'synthetic' } },
+      fetch: async (url, options) => {
+        assert.equal(options.headers.Authorization, 'Bearer synthetic');
+        if (options.method === 'GET') {
+          assert.equal(url, `https://api.github.invalid${issuePath}`);
+          return { ok: !failureStatus, status: failureStatus || 200, json: async () => ({ body, labels: labels.map(name => ({ name })) }) };
+        }
+        writes.push({ method: options.method, path: url.replace(`https://api.github.invalid${issuePath}`, ''), body: options.body && JSON.parse(options.body) });
+        return { ok: true, status: 204 };
+      },
+    });
+    return writes;
+  }
+  const answer = product => `### Product or interface\n\n${product}\n\n### Question\n\nSynthetic question`;
+  const add = name => ({ method: 'POST', path: '/labels', body: { labels: [name] } });
+  const remove = name => ({ method: 'DELETE', path: `/labels/${name}`, body: undefined });
+  assert.deepEqual(await replay(answer('CLI - interactive TUI'), ['bug', 'desktop']), [add('tui'), remove('desktop')]);
+  assert.deepEqual(await replay(answer('Desktop app').replace(/\n/g, '\r\n'), ['question']), [add('desktop')]);
+  assert.deepEqual(await replay(answer('Desktop app'), ['desktop', 'bug']), []);
+  for (const product of ['CLI - headless', 'CLI - ACP', 'Source build or repository tooling']) {
+    assert.deepEqual(await replay(answer(product), ['tui', 'bug']), [remove('tui')]);
+  }
+  for (const body of [null, 'Desktop app', answer('_No response_'), answer('$(touch must-not-execute)')]) {
+    assert.deepEqual(await replay(body, ['desktop', 'bug']), []);
+  }
+  await assert.rejects(replay(answer('Desktop app'), [], 403), /GitHub GET failed: 403/);
+});
+
 test('issue forms label incoming reports for triage and retain collaborator-only PR guidance', () => {
   for (const [name, type] of [
     ['01-bug-report.yml', 'bug'], ['02-feature-request.yml', 'enhancement'],