-
Notifications
You must be signed in to change notification settings - Fork 7
/
modes.js
70 lines (58 loc) · 2.93 KB
/
modes.js
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
import { findMatchingComment, findMatchingComments } from './comment/commenter';
import getCommentPrefix from './identifier';
// normal mode creates a comment when there is no existing comment that match identifier
// and updates the matching comment if found
export async function normalMode(commenter, identifier, message) {
console.log(`Checking if a comment already exists for ${identifier}.`);
const matchingComment = await findMatchingComment(commenter, identifier);
const comment = `${getCommentPrefix(identifier)}\n${message}`;
if (matchingComment) {
console.log(`Updating an existing comment for ${identifier}.`);
const resp = await commenter.updateComment(matchingComment.id, comment);
console.log(`Updated comment: ${resp.data.html_url}`);
return;
}
console.log(`Creating a new comment for ${identifier}.`);
const resp = await commenter.createComment(comment);
console.log(`Created comment: ${resp.data.html_url}`);
}
// recreate mode deletes existing comments that match the identifier
// and creates a new comment
export async function recreateMode(commenter, identifier, message) {
console.log(`Finding matching comments for ${identifier}.`);
const matchingComments = await findMatchingComments(commenter, identifier);
for (const comment of matchingComments) {
console.log(`Deleting github comment ${comment.id}`);
await commenter.deleteComment(comment.id);
}
console.log(`Creating a new comment for ${identifier}.`);
const comment = `${getCommentPrefix(identifier)}\n${message}`;
const resp = await commenter.createComment(comment);
console.log(`Created comment: ${resp.data.html_url}`);
}
// append mode creates a comment when there is no existing comment that match identifier
// and appends message to a matching comment if found.
export async function appendMode(commenter, identifier, message) {
console.log(`Checking if a comment already exists for ${identifier}.`);
const matchingComment = await findMatchingComment(commenter, identifier);
if (matchingComment) {
console.log(`Updating an existing comment for ${identifier}.`);
const comment = `${matchingComment.body}\n${message}`;
const resp = await commenter.updateComment(matchingComment.id, comment);
console.log(`Updated comment: ${resp.data.html_url}`);
return;
}
console.log(`Creating a new comment for ${identifier}.`);
const comment = `${getCommentPrefix(identifier)}\n${message}`;
const resp = await commenter.createComment(comment);
console.log(`Created comment: ${resp.data.html_url}`);
}
// delete mode deletes an existing comment that matches the identifier
export async function deleteMode(commenter, identifier) {
console.log(`Finding matching comment for ${identifier}.`);
const matchingComment = await findMatchingComment(commenter, identifier);
if (matchingComment) {
console.log(`Deleting github comment ${matchingComment.id}`);
await commenter.deleteComment(matchingComment.id);
}
}