This repository was archived by the owner on Jun 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
174 lines (153 loc) · 7.33 KB
/
Copy pathissue-pr-bot.yml
File metadata and controls
174 lines (153 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
name: Issue and PR Duplicate Detection Bot
on:
issues:
types: [opened]
pull_request:
types: [opened]
permissions:
contents: read
issues: write
pull-requests: write
jobs:
duplicate-check:
name: Check for Duplicates
runs-on: ubuntu-latest
steps:
- name: Detect duplicates and triage
uses: actions/github-script@v7
with:
script: |
const DUPLICATE_LABEL = 'duplicate';
const TRIAGE_LABEL = 'needs-triage';
// Minimum fraction of shared keywords to consider two items duplicates.
const SIMILARITY_THRESHOLD = 0.3;
// Stop words that carry no semantic meaning for matching.
const STOP_WORDS = new Set([
'a','an','the','and','or','but','in','on','at','to','for','of',
'with','is','are','was','were','be','been','being','have','has',
'had','do','does','did','will','would','could','should','may',
'might','shall','can','not','no','this','that','it','its',
'i','we','you','he','she','they','my','your','our','their',
'how','what','when','where','why','which','who','whom',
]);
const isPR = !!context.payload.pull_request;
const item = isPR ? context.payload.pull_request : context.payload.issue;
const itemNumber = item.number;
const itemTitle = (item.title || '').toLowerCase();
const itemBody = (item.body || '').toLowerCase();
const itemText = `${itemTitle} ${itemBody}`;
// ── helpers ──────────────────────────────────────────────────────
function tokenize(text) {
return new Set(
text
.replace(/[^a-z0-9\s]/g, ' ')
.split(/\s+/)
.filter(w => w.length > 2 && !STOP_WORDS.has(w))
);
}
function jaccardSimilarity(setA, setB) {
// Two empty token sets share no meaningful keywords — treat as dissimilar
// so that blank issues are not flagged as duplicates of each other.
if (setA.size === 0 || setB.size === 0) return 0;
const intersection = [...setA].filter(w => setB.has(w)).length;
const union = new Set([...setA, ...setB]).size;
return intersection / union;
}
// ── ensure required labels exist ─────────────────────────────────
async function ensureLabel(name, color, description) {
try {
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name,
});
} catch (err) {
if (err.status === 404) {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name,
color,
description,
});
} else {
throw err;
}
}
}
await ensureLabel(DUPLICATE_LABEL, 'cfd3d7', 'This issue or PR is a duplicate of another');
await ensureLabel(TRIAGE_LABEL, 'e4e669', 'Needs triage by a maintainer');
// ── fetch existing open issues (excluding the current one) ───────
const existingItems = await github.paginate(
github.rest.issues.listForRepo,
{
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100,
}
);
const candidates = existingItems.filter(i => i.number !== itemNumber);
// ── compare against each candidate ───────────────────────────────
const newTokens = tokenize(itemText);
let bestMatch = null;
let bestScore = 0;
for (const candidate of candidates) {
const candidateText = `${(candidate.title || '').toLowerCase()} ${(candidate.body || '').toLowerCase()}`;
const candidateTokens = tokenize(candidateText);
const score = jaccardSimilarity(newTokens, candidateTokens);
if (score > bestScore) {
bestScore = score;
bestMatch = candidate;
}
}
const isDuplicate = bestScore >= SIMILARITY_THRESHOLD;
const itemType = isPR ? 'Pull Request' : 'Issue';
// ── act on result ────────────────────────────────────────────────
if (isDuplicate) {
core.info(`Duplicate detected: #${bestMatch.number} (score ${bestScore.toFixed(3)})`);
// Add 'duplicate' label
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: itemNumber,
labels: [DUPLICATE_LABEL],
});
// Post duplicate comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: itemNumber,
body: [
`⚠️ This looks like a duplicate of #${bestMatch.number}.`,
'',
`Please check that ${bestMatch.pull_request ? 'PR' : 'issue'} for updates and continue the discussion there.`,
'If you believe this is **not** a duplicate, please leave a comment explaining the difference and a maintainer will re-triage.',
].join('\n'),
});
} else {
core.info(`No duplicate found (best score ${bestScore.toFixed(3)})`);
// Add 'needs-triage' label
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: itemNumber,
labels: [TRIAGE_LABEL],
});
// Post welcome / acknowledgement comment
const welcomeLines = [
`👋 Thanks for opening this ${itemType}!`,
'',
'A maintainer will review it shortly. In the meantime:',
'',
'- Please make sure your description is as detailed as possible.',
'- Search existing issues at https://github.com/' + context.repo.owner + '/' + context.repo.repo + '/issues to avoid duplicates.',
'- Follow the contribution guidelines if they apply.',
];
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: itemNumber,
body: welcomeLines.join('\n'),
});
}