-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
124 lines (105 loc) · 3.48 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module.exports = (app, { getRouter }) => {
app.on(
[
"issue_comment.created",
"issue_comment.edited"
],
async context => {
const config = await context.config("comment-agent.yml", {
caseSensitive: true
});
if (!config) {
console.log('Config file named "comment-agent.yml" not found. Exiting.');
return;
}
if (context.payload.pull_request === null) {
console.log('Not a PR. Exiting.')
return;
}
if (typeof config.aliasMappings === 'undefined' || config.aliasMappings === null) {
console.log('No alias mappings specified in the config. Exiting.')
return;
}
var issueComment = context.payload.comment;
if (!issueComment) return;
let commentBody = issueComment.body.slice();
let PRNumber = context.payload.issue.number;
let repoName = context.payload.repository.full_name;
let commentAuthorName = issueComment.user.login;
let commentAuthorAssociation = issueComment.author_association;
let PRData = await context.octokit.request(`GET /repos/${repoName}/pulls/${PRNumber}`);
PRData = PRData.data;
let PRHeadRef = PRData.head.ref;
let PRHeadFullName = PRData.head.repo.full_name;
let PRAuthor = PRData.user.login;
let metadata = {
pr_number: PRNumber,
comment_author: commentAuthorName,
pr_head_full_repo_name: PRHeadFullName,
pr_head_ref: PRHeadRef,
mergeable: PRData.mergeable
}
if (config.caseSensitive === false) {
commentBody = commentBody.toLowerCase();
}
// Iter through our aliases and send the dispatch if it's valid and authorized
for (let token in config.aliasMappings) {
let tokenName = token.slice();
let eventName = config.aliasMappings[token];
if (config.caseSensitive === false) {
tokenName = tokenName.toLowerCase();
}
if (commentBody.includes(tokenName)) {
if (isAuthorized(config, commentAuthorAssociation, eventName, PRAuthor, commentAuthorName)) {
reactComment(context, context.payload, issueComment.id, 'eyes'); // to let them know we saw it the comment
sendDispatch(context, context.payload, eventName, metadata);
reactComment(context, context.payload, issueComment.id, 'rocket'); // to let them know we sent the dispatch
}
}
}
}
);
};
async function sendDispatch(context, payload, event_type, metadata) {
await context.octokit.rest.repos.createDispatchEvent(
{
owner: payload.repository.owner.login,
repo: payload.repository.name,
event_type: event_type,
client_payload: metadata
}
);
}
function isAuthorized(config, association, event, pr_author, comment_author) {
let map = config.permissionMappings;
// If no permission map is specified at all, default to contrib. or owner
if (typeof map === 'undefined' || map === null) {
if (association == 'CONTRIBUTOR' || association == 'OWNER') {
return true;
} else {
return false;
}
}
let permission = map[event];
if (Array.isArray(permission)) {
if (pr_author == comment_author && permission.includes('PRAUTHOR'))
return true;
return permission.includes(association);
}
else if (typeof permission === 'string') {
if (pr_author == comment_author && permission == 'PRAUTHOR')
return true;
return association == permission;
}
else {
return false;
}
}
async function reactComment(context, payload, id, reaction) {
await context.octokit.rest.reactions.createForIssueComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
comment_id: id,
content: reaction,
});
}