Skip to content

Commit 5797b56

Browse files
committed
chore: refactor issue bot
1 parent 6e001b4 commit 5797b56

File tree

3 files changed

+183
-148
lines changed

3 files changed

+183
-148
lines changed

.github/workflows/issue_bot.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Issue Bot
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+
if: |
15+
contains(github.event.issue.labels.*.name, 'new device support') ||
16+
contains(github.event.issue.labels.*.name, 'external converter') ||
17+
startsWith(github.event.issue.title, '[New device support]') ||
18+
startsWith(github.event.issue.title, '[External Converter]')
19+
steps:
20+
- uses: actions/checkout@v5
21+
with:
22+
sparse-checkout: |
23+
scripts
24+
path: z2m
25+
26+
- uses: actions/checkout@v5
27+
with:
28+
repository: Koenkk/zigbee-herdsman-converters
29+
ref: master
30+
fetch-depth: 1
31+
path: zhc
32+
33+
- name: Comment on new device support/external converter issue
34+
uses: actions/github-script@v8
35+
with:
36+
script: |
37+
const {newDeviceSupport} = await import("${{ github.workspace }}/z2m/scripts/issueBot.mjs")
38+
await newDeviceSupport(github, core, context, "${{ github.workspace }}/zhc")

.github/workflows/new_device_external_converter_bot.yml

Lines changed: 0 additions & 148 deletions
This file was deleted.

scripts/issueBot.mjs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import {execSync} from "node:child_process";
2+
3+
export async function newDeviceSupport(github, _core, context, zhcDir) {
4+
const issue = context.payload.issue;
5+
// Hide previous bot comments
6+
const comments = await github.rest.issues.listComments({
7+
owner: context.repo.owner,
8+
repo: context.repo.repo,
9+
issue_number: issue.number,
10+
});
11+
12+
for (const comment of comments.data) {
13+
if (comment.user.type === "Bot" && comment.user.login === "github-actions[bot]") {
14+
await github.graphql(`mutation {
15+
minimizeComment(input: {subjectId: "${comment.node_id}", classifier: OUTDATED}) {
16+
clientMutationId
17+
}
18+
}`);
19+
}
20+
}
21+
22+
// Check if Tuya manufacturer name is already supported.
23+
const tuyaManufacturerNameRe = /['"](_T\w+_(\w+))['"]/g;
24+
const tuyaManufacturerNames = Array.from(issue.body.matchAll(tuyaManufacturerNameRe), (m) => [m[1], m[2]]);
25+
26+
if (tuyaManufacturerNames.length > 0) {
27+
for (const [fullName, partialName] of tuyaManufacturerNames) {
28+
const fullMatch = (() => {
29+
try {
30+
return execSync(`grep -r --include="*.ts" "${fullName}" "${zhcDir}"`, {encoding: "utf8"});
31+
} catch {
32+
return undefined;
33+
}
34+
})();
35+
36+
if (fullMatch) {
37+
await github.rest.issues.createComment({
38+
owner: context.repo.owner,
39+
repo: context.repo.repo,
40+
issue_number: issue.number,
41+
body: `👋 Hi there! The Tuya device with manufacturer name \`${fullName}\` is already supported in the latest dev branch.
42+
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.
43+
44+
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.
45+
For instructions on how to create a pull request see the [docs](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html#_4-create-a-pull-request).
46+
If you need help with the process, feel free to ask here and we'll be happy to assist.`,
47+
});
48+
await github.rest.issues.update({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
issue_number: issue.number,
52+
state: "closed",
53+
});
54+
55+
return;
56+
}
57+
58+
const partialMatch = (() => {
59+
try {
60+
return execSync(`grep -r --include="*.ts" "${partialName}" "${zhcDir}"`, {encoding: "utf8"});
61+
} catch {
62+
return undefined;
63+
}
64+
})();
65+
66+
if (partialMatch) {
67+
const candidates = Array.from(partialMatch.matchAll(tuyaManufacturerNameRe), (m) => m[1]);
68+
69+
await github.rest.issues.createComment({
70+
owner: context.repo.owner,
71+
repo: context.repo.repo,
72+
issue_number: issue.number,
73+
body: `👋 Hi there! A similar Tuya device of which the manufacturer name also ends with \`_${partialName}\` is already supported.
74+
This means the device can probably be easily be supported by re-using the existing converter.
75+
76+
I found the following matches: ${candidates.map((c) => `\`${c}\``).join(", ")}
77+
Try to stop Z2M, change all occurrences of \`${fullName}\` in the \`data/database.db\` to one of the matches above and start Z2M.
78+
79+
Let us know if it works so we can support this device out-of-the-box!`,
80+
});
81+
82+
return;
83+
}
84+
}
85+
} else {
86+
// Check if zigbee model is already supported.
87+
const zigbeeModelRe = /zigbeeModel: \[['"](.+)['"]\]/g;
88+
const zigbeeModels = Array.from(issue.body.matchAll(zigbeeModelRe), (m) => m[1]);
89+
90+
if (zigbeeModels.length > 0) {
91+
for (const zigbeeModel of zigbeeModels) {
92+
const fullMatch = (() => {
93+
try {
94+
return execSync(`grep -r --include="*.ts" '"${zigbeeModel}"' "${zhcDir}"`, {encoding: "utf8"});
95+
} catch {
96+
return undefined;
97+
}
98+
})();
99+
100+
if (fullMatch) {
101+
await github.rest.issues.createComment({
102+
owner: context.repo.owner,
103+
repo: context.repo.repo,
104+
issue_number: issue.number,
105+
body: `👋 Hi there! The device with zigbee model \`${zigbeeModel}\` is already supported in the latest dev branch.
106+
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.
107+
108+
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.
109+
For instructions on how to create a pull request see the [docs](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html#_4-create-a-pull-request).
110+
111+
If you need help with the process, feel free to ask here and we'll be happy to assist.`,
112+
});
113+
await github.rest.issues.update({
114+
owner: context.repo.owner,
115+
repo: context.repo.repo,
116+
issue_number: issue.number,
117+
state: "closed",
118+
});
119+
120+
return;
121+
}
122+
}
123+
}
124+
}
125+
126+
// Create a request to pull request comment
127+
await github.rest.issues.createComment({
128+
owner: context.repo.owner,
129+
repo: context.repo.repo,
130+
issue_number: issue.number,
131+
body: `🙏 Thank you for creating this issue and sharing your external converter!
132+
133+
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.
134+
For instructions on how to create a pull request see the [docs](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html#_4-create-a-pull-request).
135+
136+
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).
137+
${
138+
tuyaManufacturerNames.length > 0
139+
? "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)."
140+
: ""
141+
}
142+
143+
If you need help with the process, feel free to ask here and we'll be happy to assist.`,
144+
});
145+
}

0 commit comments

Comments
 (0)