Repository navigation
Expand file tree
/
Copy pathparse-command.js
More file actions
50 lines (38 loc) · 1.5 KB
/
Copy pathparse-command.js
File metadata and controls
50 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function parseCommand(commentBody) {
if (!commentBody || typeof commentBody !== 'string') return null;
const lines = commentBody.split('\n');
for (const rawLine of lines) {
const line = rawLine.trim();
const assignMatch = line.match(/^\/assign\s+@([^\s]+)\s*$/i);
if (assignMatch) return { command: 'assign', username: assignMatch[1] };
const unassignMatch = line.match(/^\/unassign\s+@([^\s]+)\s*$/i);
if (unassignMatch) return { command: 'unassign', username: unassignMatch[1] };
const claimMatch = line.match(/^\/claim\s*$/i);
if (claimMatch) return { command: 'claim' };
const unclaimMatch = line.match(/^\/unclaim\s*$/i);
if (unclaimMatch) return { command: 'unclaim' };
const pingMatch = line.match(/^\/ping\s*$/i);
if (pingMatch) return { command: 'ping' };
const addlabelMatch = line.match(/^\/addlabel\s+(.+)$/i);
if (addlabelMatch) {
const labels = parseLabels(addlabelMatch[1]);
if (labels.length > 0) return { command: 'addlabel', labels };
return { command: 'addlabel', labels: [] };
}
if (/^\/addlabel\s*$/i.test(line)) {
return { command: 'addlabel', labels: [] };
}
}
return null;
}
function parseLabels(raw) {
const labels = [];
const tokenRegex = /"([^"]+)"|'([^']+)'|(\S+)/g;
let match;
while ((match = tokenRegex.exec(raw)) !== null) {
const label = (match[1] || match[2] || match[3]).trim();
if (label) labels.push(label);
}
return labels;
}
module.exports = { parseCommand, parseLabels };