forked from hiero-ledger/hiero-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot-on-comment.js
More file actions
94 lines (83 loc) · 3.06 KB
/
bot-on-comment.js
File metadata and controls
94 lines (83 loc) · 3.06 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: Apache-2.0
//
// bot-on-comment.js
//
// Handles issue comment events: reads the comment body, parses commands, and dispatches
// to the appropriate handler. Implemented commands: /assign, /unassign.
//
// /assign: see commands/assign.js (skill levels, assignment limits, required labels).
// /unassign: see commands/unassign.js (authorization, label reversion).
const { createLogger, buildBotContext } = require('./helpers');
const { handleAssign } = require('./commands/assign');
const { handleUnassign } = require('./commands/unassign');
let logger = createLogger('on-comment');
// =============================================================================
// COMMAND PARSING
// =============================================================================
/**
* Parses the comment body and returns the list of commands to run.
* Commands are recognized by exact match (with optional surrounding whitespace).
*
* @param {string} body - The comment body.
* @returns {{ commands: string[] }} - List of command names (e.g. ['assign'] or []).
*/
function parseComment(body) {
if (typeof body !== 'string') {
return { commands: [] };
}
if (/^\s*\/assign\s*$/i.test(body)) {
logger.log('parseComment: detected /assign');
return { commands: ['assign'] };
}
if (/^\s*\/unassign\s*$/i.test(body)) {
logger.log('parseComment: detected /unassign');
return { commands: ['unassign'] };
}
logger.log('parseComment: no known command', { body: body.substring(0, 80) });
return { commands: [] };
}
// =============================================================================
// ENTRY POINT
// =============================================================================
/**
* Entry point: read comment, parse commands, dispatch to handlers.
* Validates that the event is a comment from a human; then runs each detected command.
*/
module.exports = async ({ github, context }) => {
try {
const botContext = buildBotContext({ github, context });
if (!botContext.comment?.user?.login) {
logger.log('Exit: missing comment user login');
return;
}
if (botContext.comment.user.type === 'Bot') {
logger.log('Exit: comment authored by bot');
return;
}
const parsed = parseComment(botContext.comment.body);
if (parsed.commands.length === 0) {
logger.log('Exit: no known command');
return;
}
for (const command of parsed.commands) {
// Update logger prefix to the command name so helper functions
// (postComment, addLabels, etc.) log with the correct tag.
logger = createLogger(`on-${command}`);
if (command === 'assign') {
await handleAssign(botContext);
} else if (command === 'unassign') {
await handleUnassign(botContext);
} else {
logger.log('Unknown command:', command);
}
}
} catch (error) {
logger.error('Error:', {
message: error.message,
status: error.status,
number: context.payload.issue?.number,
commenter: context.payload.comment?.user?.login,
});
throw error;
}
};