dsh-brain/vendor/dsh-mnemon/.github/workflows/issue-template-enforcer.yml

141 lines
6.3 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Issue 信息检查 / Check Issue Information
on:
issues:
types: [opened, reopened, edited]
concurrency:
group: issue-template-check-${{ github.event.issue.number }}
cancel-in-progress: false
permissions:
contents: read
issues: write
jobs:
enforce:
name: 提示缺失的 Issue 信息 / Request missing issue information
runs-on: ubuntu-latest
steps:
- name: 校验 Issue 模板 / Validate issue template
uses: actions/github-script@v8
with:
script: |
const eventIssue = context.payload.issue;
if (!eventIssue || eventIssue.pull_request) {
return;
}
const issueParams = {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: eventIssue.number,
};
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const readSection = (body, label) => {
const heading = escapeRegExp(label);
const pattern = new RegExp(`(?:^|\\n)###\\s+${heading}\\s*\\n+([\\s\\S]*?)(?=\\n###\\s+|$)`, 'i');
const match = body.match(pattern);
return match ? match[1].replace(/<!--[\s\S]*?-->/g, '').trim() : '';
};
const isBlank = (value) => {
const normalized = value.replace(/\s+/g, ' ').trim().toLowerCase();
return normalized === '' || normalized === 'no response' || normalized === '_no response_';
};
const hasCheckedBox = (value) => /^-\s*\[[xX]\]\s+\S+/m.test(value);
const labels = {
duplicate: '提交前查重 / Duplicate check',
area: '涉及区域 / Affected area',
type: 'Issue 类型 / Issue type',
summary: '摘要 / Summary',
expected: '预期结果 / Expected outcome',
details: '详情或复现步骤 / Details or reproduction steps',
environment: '环境信息 / Environment',
};
const requiredSections = [
labels.duplicate,
labels.area,
labels.type,
labels.summary,
labels.expected,
labels.details,
labels.environment,
];
const validateIssue = (issue) => {
const section = (label) => readSection(issue.body || '', label);
// Evidence is assessed by maintainers across the whole report.
// Its section, code-block language and formatting are not gates.
const missingSections = requiredSections.filter((label) => isBlank(section(label)));
const invalidSections = [];
if (!isBlank(section(labels.duplicate)) && !hasCheckedBox(section(labels.duplicate))) {
invalidSections.push('请确认已经搜索过 open/closed Issue。 / Please confirm that you searched open and closed issues.');
}
return [
...(missingSections.length > 0 ? [`缺少或为空的必填部分 / Missing or empty required sections: ${missingSections.join(', ')}`] : []),
...invalidSections,
];
};
// The event payload is a snapshot. Read current state before acting,
// including after comment lookup or adding a label.
const { data: initialIssue } = await github.rest.issues.get(issueParams);
if (initialIssue.state !== 'open') return;
const marker = '<!-- dsh-brain-mnemon:issue-template-check -->';
const comments = await github.paginate(github.rest.issues.listComments, {
...issueParams,
per_page: 100,
});
const previousComment = comments.find((comment) =>
comment.user?.login === 'github-actions[bot]' &&
comment.user?.type === 'Bot' &&
comment.body?.startsWith(marker),
);
let { data: currentIssue } = await github.rest.issues.get(issueParams);
if (currentIssue.state !== 'open') return;
const issueType = readSection(currentIssue.body || '', labels.type).trim().toLowerCase();
const hasBugLabel = (currentIssue.labels || []).some((label) => String(label.name || '').toLowerCase() === 'bug');
if (issueType === 'bug 报告 / bug report' && !hasBugLabel) {
// API/CLI reporters may not have permission to classify an issue.
await github.rest.issues.addLabels({ ...issueParams, labels: ['bug'] });
currentIssue = (await github.rest.issues.get(issueParams)).data;
if (currentIssue.state !== 'open') return;
}
const details = validateIssue(currentIssue);
if (details.length === 0 && !previousComment) return;
const comment = details.length > 0 ? [
marker,
'此 Issue 保持打开。为便于排查,请补充以下基础信息:',
'The issue remains open. Please add the following basic information to help triage:',
'',
...details.map((detail) => `- ${detail}`),
'',
'在正文中补充即可,本提示会在编辑后更新。证据可以是普通文本、JSON 或其他代码块,也可以直接放在复现步骤中。',
'Update the issue body and this reminder will refresh. Evidence may be plain text, JSON or other code blocks, including content in the reproduction steps.',
].join('\n') : [
marker,
'基础信息已补齐,后续由维护者排查和处理。',
'Required report information is present. Maintainers will review the report.',
].join('\n');
// Update only this check's own reminder. Never change issue state.
if (previousComment) {
if (previousComment.body !== comment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: previousComment.id,
body: comment,
});
}
} else {
await github.rest.issues.createComment({ ...issueParams, body: comment });
}