-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (101 loc) · 3.47 KB
/
index.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
const core = require('@actions/core');
const github = require('@actions/github');
const words = require('./src/words');
const gifs = require('./src/gifs');
const imageUrl = 'https://github.com/jayehernandez/the-good-place/blob/master/images/curse.png?raw=true';
const note = `\n\n![Janet's Reminder on Cursing](${imageUrl})`;
async function run() {
try {
const token = core.getInput('repo-token');
const octokit = github.getOctokit(token);
if (github.context.eventName === 'issues') {
if (github.context.payload.action === 'labeled') {
await checkIssueLabel(github, octokit);
} else {
await checkIssue(github, octokit);
}
} else if (github.context.eventName === 'issue_comment') {
await checkIssueComment(github, octokit);
} else if (github.context.eventName === 'pull_request') {
await checkPullRequest(github, octokit);
} else if (github.context.eventName === 'pull_request_review_comment') {
await checkPullRequestComment(github, octokit);
}
} catch (error) {
core.setFailed(error.message);
}
}
async function checkIssue(github, octokit) {
let issue = github.context.payload.issue;
let newTitle = checkString(issue.title);
let newBody = checkString(issue.body);
if (issue.title !== newTitle || issue.body != newBody) {
if (!newBody.includes(imageUrl)) newBody += note;
await octokit.issues.update({
...github.context.repo,
issue_number: issue.number,
title: newTitle,
body: newBody
});
}
}
async function checkIssueLabel(github, octokit) {
let { label, issue } = github.context.payload;
// To avoid spam for issues with multiple labels
if (issue.labels.length === 1) {
let gifsOfLabel = gifs[label.name];
if (gifsOfLabel && gifsOfLabel.length > 0) {
const randomGif = gifsOfLabel[Math.floor(Math.random() * gifsOfLabel.length)];
await octokit.issues.createComment({
...github.context.repo,
issue_number: issue.number,
body: `![Random GIF related to ${label.name}](${randomGif})`
});
}
}
}
async function checkIssueComment(github, octokit) {
let comment = github.context.payload.comment;
let newBody = checkString(comment.body);
if (comment.body != newBody) {
if (!newBody.includes(imageUrl)) newBody += note;
await octokit.issues.updateComment({
...github.context.repo,
comment_id: comment.id,
body: newBody
});
}
}
async function checkPullRequest(github, octokit) {
let pull_request = github.context.payload.pull_request;
let newTitle = checkString(pull_request.title);
let newBody = checkString(pull_request.body);
if (pull_request.title !== newTitle || pull_request.body != newBody) {
if (!newBody.includes(imageUrl)) newBody += note;
await octokit.pulls.update({
...github.context.repo,
pull_number: pull_request.number,
title: newTitle,
body: newBody
});
}
}
async function checkPullRequestComment(github, octokit) {
let comment = github.context.payload.comment;
let newBody = checkString(comment.body);
if (comment.body != newBody) {
if (!newBody.includes(imageUrl)) newBody += note;
await octokit.pulls.updateReviewComment({
...github.context.repo,
comment_id: comment.id,
body: newBody
});
}
}
function checkString(str) {
var re = new RegExp(`\\b${Object.keys(mapObj).join('\\b|\\b')}\\b`, 'gi');
return str.replace(re, function(matched){
return words[matched.toLowerCase()];
});
}
run();