|
1 | 1 | import {execSync} from "node:child_process"; |
2 | 2 |
|
| 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 | + |
3 | 38 | export async function newDeviceSupport(github, _core, context, zhcDir) { |
4 | 39 | const issue = context.payload.issue; |
5 | 40 | // Hide previous bot comments |
@@ -27,6 +62,7 @@ export async function newDeviceSupport(github, _core, context, zhcDir) { |
27 | 62 | console.log("Found tuyaManufacturerNames", tuyaManufacturerNames); |
28 | 63 | if (tuyaManufacturerNames.length > 0) { |
29 | 64 | for (const [fullName, partialName] of tuyaManufacturerNames) { |
| 65 | + if (await checkDuplicateIssue(github, context, fullName)) return; |
30 | 66 | const fullMatch = (() => { |
31 | 67 | try { |
32 | 68 | 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!`, |
93 | 129 |
|
94 | 130 | if (zigbeeModels.length > 0) { |
95 | 131 | for (const zigbeeModel of zigbeeModels) { |
| 132 | + if (await checkDuplicateIssue(github, context, fullName)) return; |
96 | 133 | const fullMatch = (() => { |
97 | 134 | try { |
98 | 135 | return execSync(`grep -r --include="*.ts" '"${zigbeeModel}"' "${zhcDir}"`, {encoding: "utf8"}); |
|
0 commit comments