Skip to content

Commit 38d305e

Browse files
committed
chore: add new device support bot
1 parent 8865429 commit 38d305e

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: New Device Support Issue
2+
3+
on:
4+
issues:
5+
types: [opened, edited]
6+
7+
permissions:
8+
issues: write
9+
contents: read
10+
11+
jobs:
12+
comment:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Comment on new device support issue
16+
uses: actions/github-script@v8
17+
with:
18+
script: |
19+
const issue = context.payload.issue;
20+
21+
// Only proceed if the issue title starts with "[New device support]"
22+
if (!issue.title || !issue.title.startsWith("[New device support]")) {
23+
return;
24+
}
25+
26+
// Hide previous bot comments
27+
const comments = await github.rest.issues.listComments({
28+
owner: context.repo.owner,
29+
repo: context.repo.repo,
30+
issue_number: issue.number,
31+
});
32+
33+
for (const comment of comments.data) {
34+
if (comment.user.type === 'Bot' && comment.user.login === 'github-actions[bot]') {
35+
await github.graphql(`
36+
mutation {
37+
minimizeComment(input: {subjectId: "${comment.node_id}", classifier: OUTDATED}) {
38+
clientMutationId
39+
}
40+
}
41+
`);
42+
}
43+
}
44+
45+
// Check if Tuya manufacturer name is already supported.
46+
const tuyaManufacturerNameRe = /['"](_T\w+_(\w+))['"]/g;
47+
const tuyaManufacturerNames = Array.from(issue.body.matchAll(tuyaManufacturerNameRe), m => [m[1], m[2]]);
48+
if (tuyaManufacturerNames.length > 0) {
49+
// Checkout the repo and grep for the manufacturer name
50+
const execSync = require('child_process').execSync;
51+
execSync(`git clone https://github.com/Koenkk/zigbee-herdsman-converters.git zhc`, {stdio: 'ignore'});
52+
53+
for (const [fullName, partialName] of tuyaManufacturerNames) {
54+
const fullMatch = (() => { try { return execSync(`grep -r --include="*.ts" "${fullName}" zhc`, {encoding: 'utf8'}); } catch { return undefined; } })();
55+
if (fullMatch) {
56+
await github.rest.issues.createComment({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
issue_number: issue.number,
60+
body: [
61+
`👋 Hi there! The Tuya device with manufacturer name \`${fullName}\` is already supported in the latest dev branch.`,
62+
"See this [guide](https://www.zigbee2mqtt.io/advanced/more/switch-to-dev-branch.html) on how to update, after updating you can remove your external converter.",
63+
"",
64+
"In case you created the external converter with the goal to extend or fix an issue with the out-of-the-box support, please submit a pull request.",
65+
"For instructions on how to create a pull request, see the [GitHub documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests).",
66+
"If you need help with the process, feel free to ask here and we'll be happy to assist."
67+
].join('\n')
68+
});
69+
await github.rest.issues.update({
70+
owner: context.repo.owner,
71+
repo: context.repo.repo,
72+
issue_number: issue.number,
73+
state: "closed",
74+
});
75+
return;
76+
}
77+
78+
const partialMatch = (() => { try { return execSync(`grep -r --include="*.ts" "${partialName}" zhc`, {encoding: 'utf8'}); } catch { return undefined; } })();
79+
if (partialMatch) {
80+
const candidates = Array.from(partialMatch.matchAll(tuyaManufacturerNameRe), m => m[1]);
81+
await github.rest.issues.createComment({
82+
owner: context.repo.owner,
83+
repo: context.repo.repo,
84+
issue_number: issue.number,
85+
body: [
86+
`👋 Hi there! A similar Tuya device of which the manufacturer name also ends with \`_${partialName}\` is already supported.`,
87+
"This means the device can probably be easily be supported by re-using the existing converter.",
88+
"",
89+
"I found the following matches: " + candidates.map((c) => `\`${c}\``).join(', '),
90+
`Try to stop Z2M, change all occurrences of \`${fullName}\` in the \`data/database.db\` to one of the matches above and start Z2M.`,
91+
"Let us know if it works so we can support this device out-of-the-box!",
92+
].join('\n')
93+
});
94+
return;
95+
}
96+
}
97+
}
98+
99+
// Create a request to pull request comment
100+
await github.rest.issues.createComment({
101+
owner: context.repo.owner,
102+
repo: context.repo.repo,
103+
issue_number: issue.number,
104+
body: [
105+
"🙏 Thank you for creating this issue and sharing your external converter!",
106+
"",
107+
"In case all features work, please submit a pull request on this repository so the device can be supported out-of-the-box with the next release.",
108+
"For instructions on how to create a pull request, see the [GitHub documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests).",
109+
"",
110+
"If **NOT** all features work, please follow the [How To Support new devices](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html)",
111+
tuyaManufacturerNames.length > 0 ? "Since this is a Tuya also consider [How To Support new Tuya devices](https://www.zigbee2mqtt.io/advanced/support-new-devices/02_support_new_tuya_devices.html)" : "",
112+
"",
113+
"If you need help with the process, feel free to ask here and we'll be happy to assist."
114+
].join('\n')
115+
});

0 commit comments

Comments
 (0)