2021-04-16 05:00:04 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const readline = require('readline');
|
|
|
|
|
|
2021-04-16 06:00:05 +00:00
|
|
|
const core = require('@actions/core');
|
|
|
|
|
const github = require('@actions/github');
|
|
|
|
|
|
2021-04-16 05:00:04 +00:00
|
|
|
// https://stackoverflow.com/questions/19269545/how-to-get-a-number-of-random-elements-from-an-array/19270021#19270021
|
|
|
|
|
function getRandom(arr, n) {
|
|
|
|
|
var result = new Array(n),
|
|
|
|
|
len = arr.length,
|
|
|
|
|
taken = new Array(len);
|
|
|
|
|
if (n > len)
|
|
|
|
|
throw new RangeError("getRandom: more elements taken than available");
|
|
|
|
|
while (n--) {
|
|
|
|
|
var x = Math.floor(Math.random() * len);
|
|
|
|
|
result[n] = arr[x in taken ? taken[x] : x];
|
|
|
|
|
taken[x] = --len in taken ? taken[len] : len;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(async () => {
|
2021-04-16 15:00:14 +00:00
|
|
|
|
|
|
|
|
// Get images.
|
2021-04-16 05:00:04 +00:00
|
|
|
let imageRe = /^image::{china-dictatorship-media-base}\/([^/[]+)/;
|
2021-05-02 01:00:00 +00:00
|
|
|
let images = new Set();
|
2021-04-16 05:00:04 +00:00
|
|
|
const fileStream = fs.createReadStream('README.adoc');
|
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
|
input: fileStream,
|
|
|
|
|
crlfDelay: Infinity
|
|
|
|
|
});
|
|
|
|
|
for await (const line of rl) {
|
|
|
|
|
let match = imageRe.exec(line);
|
|
|
|
|
if (match !== null) {
|
2021-05-02 01:00:00 +00:00
|
|
|
images.add(match[1]);
|
2021-04-16 05:00:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-02 01:00:00 +00:00
|
|
|
images = getRandom(Array.from(images), 20);
|
2021-04-16 11:00:10 +00:00
|
|
|
full_images = []
|
2021-04-16 05:00:04 +00:00
|
|
|
for (const image of images) {
|
2021-04-18 02:00:01 +00:00
|
|
|
const url = `https://raw.githubusercontent.com/cirosantilli/china-dictatorship-media/master/${image}`;
|
2021-05-01 07:00:06 +00:00
|
|
|
full_images.push(image.replace(/[_.]/g, ' '));
|
2021-04-18 04:00:03 +00:00
|
|
|
full_images.push(`<img src="${url}" width="600">`);
|
2021-04-16 11:00:10 +00:00
|
|
|
}
|
2021-04-16 15:00:14 +00:00
|
|
|
|
|
|
|
|
// Prepare reply body.
|
|
|
|
|
const payload = github.context.payload;
|
2021-04-16 16:00:15 +00:00
|
|
|
const titleAndBody = payload.issue.title + '\n\n' + payload.issue.body;
|
2021-04-16 15:00:14 +00:00
|
|
|
const quoteArray = [];
|
2021-04-16 16:00:15 +00:00
|
|
|
for (const line of titleAndBody.split('\n')) {
|
2021-04-16 15:00:14 +00:00
|
|
|
// Remove some speical chars to remove at mention spam possibilities.
|
|
|
|
|
quoteArray.push('> ' + line.replace(/[@#]/g, ""));
|
|
|
|
|
}
|
|
|
|
|
const replyBody = `Hi ${github.context.payload.issue.user.login},
|
|
|
|
|
|
2021-04-17 01:00:00 +00:00
|
|
|
${quoteArray.join('\n').substring(0,40000)}
|
2021-04-16 15:00:14 +00:00
|
|
|
|
|
|
|
|
${full_images.join('\n\n')}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-04-16 17:00:16 +00:00
|
|
|
// Label handling.
|
|
|
|
|
const labels = new Set(payload.issue.labels.map(label => label.name));
|
|
|
|
|
const newLabels = new Set();
|
2021-04-19 01:00:00 +00:00
|
|
|
const shabiWords = [
|
2021-05-07 01:00:00 +00:00
|
|
|
'shabi',
|
|
|
|
|
'shadiao',
|
2021-04-19 01:00:00 +00:00
|
|
|
'傻逼',
|
|
|
|
|
'沙雕',
|
|
|
|
|
];
|
|
|
|
|
for (const word of shabiWords) {
|
|
|
|
|
if (new RegExp(word, 'i').test(titleAndBody)) {
|
|
|
|
|
newLabels.add('you-are-stupid-argument');
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-04-16 16:00:15 +00:00
|
|
|
}
|
2021-04-16 17:00:16 +00:00
|
|
|
if (/nmsl/i.test(titleAndBody)) {
|
|
|
|
|
newLabels.add('your-mother-died-argument');
|
|
|
|
|
}
|
2021-04-19 01:00:00 +00:00
|
|
|
if (/cnm/i.test(titleAndBody)) {
|
|
|
|
|
newLabels.add('fuck-your-mother-argument');
|
|
|
|
|
}
|
2021-04-16 17:00:16 +00:00
|
|
|
const shitpostWords = [
|
|
|
|
|
'fuck',
|
|
|
|
|
'shit',
|
2021-05-02 01:00:00 +00:00
|
|
|
'垃圾',
|
2021-04-16 17:00:16 +00:00
|
|
|
];
|
|
|
|
|
for (const word of shitpostWords) {
|
|
|
|
|
if (new RegExp(word, 'i').test(titleAndBody)) {
|
|
|
|
|
newLabels.add('shitpost');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newLabels.size > 0) {
|
|
|
|
|
newLabels.add('shitpost');
|
2021-04-28 01:00:00 +00:00
|
|
|
if (labels.has('not-shitpost')) {
|
|
|
|
|
labels.delete('not-shitpost');
|
|
|
|
|
newLabels.add('op-does-not-know-what-shit-is');
|
|
|
|
|
}
|
2021-04-16 16:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-16 17:00:16 +00:00
|
|
|
// Make the request.
|
2021-04-16 11:00:10 +00:00
|
|
|
try {
|
|
|
|
|
console.log(github.context);
|
|
|
|
|
const octokit = new github.getOctokit(process.env.GITHUB_TOKEN);
|
|
|
|
|
const new_comment = octokit.issues.createComment({
|
|
|
|
|
owner: 'cirosantilli',
|
2021-04-16 15:00:14 +00:00
|
|
|
repo: payload.repository.name,
|
|
|
|
|
issue_number: payload.issue.number,
|
|
|
|
|
body: replyBody,
|
2021-04-16 11:00:10 +00:00
|
|
|
});
|
2021-04-16 16:00:15 +00:00
|
|
|
await octokit.issues.update({
|
|
|
|
|
owner: 'cirosantilli',
|
|
|
|
|
repo: payload.repository.name,
|
|
|
|
|
issue_number: payload.issue.number,
|
2021-04-16 17:00:16 +00:00
|
|
|
labels: Array.from([...labels, ...newLabels])
|
2021-04-16 16:00:15 +00:00
|
|
|
});
|
2021-04-16 11:00:10 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
core.setFailed(error.message);
|
2021-04-16 05:00:04 +00:00
|
|
|
}
|
|
|
|
|
})()
|