Skip to content

Commit 8a3c4bd

Browse files
committed
chore: issueBot: handle duplicates
1 parent b70701f commit 8a3c4bd

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

scripts/issueBot.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
import {execSync} from "node:child_process";
22

3+
async function checkDuplicateIssue(github, context, name) {
4+
// Search for existing issues with the same `name`
5+
const searchQuery = `repo:${context.repo.owner}/${context.repo.repo} is:issue -is:pr "${name}" label:"new device support","external converter"`;
6+
7+
try {
8+
const searchResults = await github.rest.search.issuesAndPullRequests({q: searchQuery, per_page: 100});
9+
10+
// Filter out the current issue and return the first duplicate found
11+
const existingIssues = searchResults.data.items.filter((item) => item.number !== context.payload.issue.number).map((i) => `#${i.number}`);
12+
if (existingIssues.length > 0) {
13+
await github.rest.issues.createComment({
14+
owner: context.repo.owner,
15+
repo: context.repo.repo,
16+
issue_number: context.payload.issue.number,
17+
body: `👋 Hi there! This issue appears to be a duplicate of ${existingIssues.join(", ")}
18+
19+
This issue will be closed. Please follow the existing issue for updates.`,
20+
});
21+
22+
await github.rest.issues.update({
23+
owner: context.repo.owner,
24+
repo: context.repo.repo,
25+
issue_number: context.payload.issue.number,
26+
state: "closed",
27+
});
28+
29+
return true;
30+
}
31+
} catch (error) {
32+
console.error(`Error searching for duplicate issues with ${name}:`, error);
33+
}
34+
35+
return false;
36+
}
37+
338
export async function newDeviceSupport(github, _core, context, zhcDir) {
439
const issue = context.payload.issue;
540
// Hide previous bot comments
@@ -27,6 +62,7 @@ export async function newDeviceSupport(github, _core, context, zhcDir) {
2762
console.log("Found tuyaManufacturerNames", tuyaManufacturerNames);
2863
if (tuyaManufacturerNames.length > 0) {
2964
for (const [fullName, partialName] of tuyaManufacturerNames) {
65+
if (await checkDuplicateIssue(github, context, fullName)) return;
3066
const fullMatch = (() => {
3167
try {
3268
return execSync(`grep -r --include="*.ts" "${fullName}" "${zhcDir}"`, {encoding: "utf8"});
@@ -93,6 +129,7 @@ Let us know if it works so we can support this device out-of-the-box!`,
93129

94130
if (zigbeeModels.length > 0) {
95131
for (const zigbeeModel of zigbeeModels) {
132+
if (await checkDuplicateIssue(github, context, fullName)) return;
96133
const fullMatch = (() => {
97134
try {
98135
return execSync(`grep -r --include="*.ts" '"${zigbeeModel}"' "${zhcDir}"`, {encoding: "utf8"});

0 commit comments

Comments
 (0)