Skip to content

Commit da8e480

Browse files
committed
chore: improve external converter issue bot
1 parent 48012ce commit da8e480

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

.github/workflows/external_converter_issue.yml

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: External Converter Issue
22

33
on:
44
issues:
5-
types: [opened]
5+
types: [opened, edited]
66

77
permissions:
88
issues: write
@@ -23,6 +23,25 @@ jobs:
2323
return;
2424
}
2525
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+
2645
// Check for placeholder text
2746
const placeholderText = '<!-- REPLACE THIS LINE WITH YOUR EXTERNAL CONVERTER\'S CODE -->';
2847
if (issue.body && issue.body.includes(placeholderText)) {
@@ -46,31 +65,22 @@ jobs:
4665
}
4766
4867
// Check if Tuya manufacturer name is already supported.
49-
const regex = /['"](_T\w+_\w+)['"]/g;
50-
let tuyaManufacturerNames = [];
51-
let match;
52-
while ((match = regex.exec(issue.body)) !== null) {
53-
tuyaManufacturerNames.push(match[1]);
54-
}
68+
const tuyaManufacturerNameRe = /['"](_T\w+_(\w+))['"]/g;
69+
const tuyaManufacturerNames = Array.from(issue.body.matchAll(tuyaManufacturerNameRe), m => [m[1], m[2]]);
5570
if (tuyaManufacturerNames.length > 0) {
5671
// Checkout the repo and grep for the manufacturer name
5772
const execSync = require('child_process').execSync;
58-
execSync(`git clone https://github.com/${context.repo.owner}/${context.repo.repo}.git zhc`, {stdio: 'ignore'});
73+
execSync(`git clone https://github.com/Koenkk/zigbee-herdsman-converters.git zhc`, {stdio: 'ignore'});
5974
60-
for (const name of tuyaManufacturerNames) {
61-
// Grep for the manufacturer name
62-
let grepResult = '';
63-
try {
64-
grepResult = execSync(`grep -r "${name}" zhc`, {encoding: 'utf8'});
65-
} catch (err) {}
66-
67-
if (grepResult) {
75+
for (const [fullName, partialName] of tuyaManufacturerNames) {
76+
const fullMatch = (() => { try { return execSync(`grep -r --include="*.ts" "${fullName}" zhc`, {encoding: 'utf8'}); } catch { return undefined; } })();
77+
if (fullMatch) {
6878
await github.rest.issues.createComment({
6979
owner: context.repo.owner,
7080
repo: context.repo.repo,
7181
issue_number: issue.number,
7282
body: [
73-
`👋 Hi there! The Tuya device with manufacturer name \`${name}\` is already supported in the latest dev branch.`,
83+
`👋 Hi there! The Tuya device with manufacturer name \`${fullName}\` is already supported in the latest dev branch.`,
7484
"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.",
7585
"",
7686
"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.",
@@ -86,6 +96,25 @@ jobs:
8696
});
8797
return;
8898
}
99+
100+
const partialMatch = (() => { try { return execSync(`grep -r --include="*.ts" "${partialName}" zhc`, {encoding: 'utf8'}); } catch { return undefined; } })();
101+
if (partialMatch) {
102+
const candidates = Array.from(partialMatch.matchAll(tuyaManufacturerNameRe), m => m[1]);
103+
await github.rest.issues.createComment({
104+
owner: context.repo.owner,
105+
repo: context.repo.repo,
106+
issue_number: issue.number,
107+
body: [
108+
`👋 Hi there! A similar Tuya device of which the manufacturer name also ends with \`_${partialName}\` is already supported.`,
109+
"This means the device can probably be easily be supported by re-using the existing converter.",
110+
"",
111+
"I found the following matches: " + candidates.map((c) => `\`${c}\``).join(', '),
112+
`Try to stop Z2M, change all occurrences of \`${fullName}\` in the \`data/database.db\` to one of the matches above and start Z2M.`,
113+
"Let us know if it works so we can support this device out-of-the-box!",
114+
].join('\n')
115+
});
116+
return;
117+
}
89118
}
90119
}
91120
@@ -97,9 +126,12 @@ jobs:
97126
body: [
98127
"🙏 Thank you for creating this issue and sharing your external converter!",
99128
"",
100-
"Please submit a pull request on this repository so the device can be supported out-of-the-box with the next release.",
129+
"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.",
101130
"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).",
102131
"",
132+
"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)",
133+
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)" : "",
134+
"",
103135
"If you need help with the process, feel free to ask here and we'll be happy to assist."
104136
].join('\n')
105137
});

0 commit comments

Comments
 (0)