Files
china-dictatorship/action.js

245 lines
6.7 KiB
JavaScript
Raw Normal View History

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-06-23 19:19:05 +01:00
images = getRandom(Array.from(images), 30);
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, ' '));
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.
2022-08-02 22:01:06 +00:00
const payload = github.context.payload
console.log('payload: ' + require('util').inspect(payload))
2021-06-30 08:39:34 +01:00
const isComment = payload.comment !== undefined;
let titleAndBody;
let author;
if (isComment) {
titleAndBody = payload.comment.body;
author = payload.comment.user.login;
} else {
titleAndBody = payload.issue.title + '\n\n' + payload.issue.body;
author = payload.issue.user.login;
}
2021-04-16 15:00:14 +00:00
const quoteArray = [];
2022-08-02 23:01:07 +00:00
const noQuoteArray = [];
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.
2022-08-02 23:01:07 +00:00
const l = line.replace(/[@#]/g, "")
quoteArray.push('> ' + l);
noQuoteArray.push(l);
2021-04-16 15:00:14 +00:00
}
2022-08-02 19:01:03 +00:00
const replyBody = `Hi @${author},
2021-04-16 15:00:14 +00:00
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.
2021-06-30 08:41:24 +01:00
let labels;
2021-06-30 08:42:51 +01:00
let newLabels;
2021-06-30 08:39:34 +01:00
if (!isComment) {
2021-06-30 08:41:24 +01:00
labels = new Set(payload.issue.labels.map(label => label.name));
2021-06-30 08:42:51 +01:00
newLabels = new Set();
2021-06-30 08:39:34 +01:00
const shabiWords = [
2021-12-22 20:20:30 +00:00
'(sha|沙|啥|煞)(b|bi|diao|雕|比|笔)',
2021-06-30 08:39:34 +01:00
'傻',
'智障',
2021-07-11 12:05:59 +01:00
'垃圾',
2021-11-17 09:20:18 +00:00
'脑瘫',
2021-06-30 08:39:34 +01:00
'stupid',
];
for (const word of shabiWords) {
if (new RegExp(word, 'i').test(titleAndBody)) {
newLabels.add('you-are-stupid-argument');
break;
}
2021-04-19 01:00:00 +00:00
}
2021-09-14 09:51:18 +01:00
const maWords = '(马|吗|妈|m)'
2021-06-30 08:39:34 +01:00
const fuckMotherWords = [
'cnm',
'fuck.*\\b(mom|mum|mother)\\b',
'尼玛',
2022-05-06 12:30:25 +01:00
'(叼|去|日|操|草)(你|泥|拟)' + maWords,
2021-06-30 08:39:34 +01:00
]
for (const word of fuckMotherWords) {
if (new RegExp(word, 'i').test(titleAndBody)) {
newLabels.add('fuck-your-mother-argument');
break;
}
2021-05-19 02:00:01 +00:00
}
2022-08-02 18:01:02 +00:00
for (const word of [
'中国共产党万岁',
]) {
if (new RegExp(word, 'i').test()) {
newLabels.add('i-like-my-dictatorship')
break;
}
}
2021-06-30 08:39:34 +01:00
const motherDiedWords = [
'nmsl',
2021-09-14 09:51:18 +01:00
'你' + maWords + '死',
2021-06-30 08:39:34 +01:00
'司马',
]
for (const word of motherDiedWords) {
if (new RegExp(word, 'i').test(titleAndBody)) {
newLabels.add('your-mother-died-argument');
break;
}
2021-05-19 02:00:01 +00:00
}
2021-07-12 09:47:20 +01:00
const meantToBeUsedWords = [
'技术',
2022-08-02 10:00:54 +00:00
'github',
2021-07-12 09:47:20 +01:00
]
for (const word of meantToBeUsedWords) {
if (new RegExp(word, 'i').test(titleAndBody)) {
newLabels.add('meant-to-be-used');
break;
}
}
2021-06-30 08:39:34 +01:00
const shitpostWords = [
'fuck',
'shit',
'bitch',
'垃圾',
'婊子',
'恶心',
'操你',
2021-07-15 09:04:52 +01:00
'丑',
2021-06-30 08:39:34 +01:00
];
for (const word of shitpostWords) {
if (new RegExp(word, 'i').test(titleAndBody)) {
newLabels.add('shitpost');
break;
}
2021-04-16 17:00:16 +00:00
}
2022-08-02 02:00:14 +00:00
if (new RegExp('狗', 'i').test(titleAndBody)) {
newLabels.add('you-are-dog-argument');
}
2021-06-30 08:39:34 +01:00
if (newLabels.size > 0) {
newLabels.add('shitpost');
if (labels.has('not-shitpost')) {
labels.delete('not-shitpost');
newLabels.add('op-does-not-know-what-shit-is');
}
2021-04-28 01:00:00 +00:00
}
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 {
const octokit = new github.getOctokit(process.env.GITHUB_TOKEN);
const new_comment = octokit.issues.createComment({
2022-03-23 10:40:06 +00:00
owner: payload.repository.owner.login,
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
});
2022-08-03 05:01:13 +00:00
let html_url
2022-08-02 22:01:06 +00:00
if (isComment) {
2022-08-03 00:01:08 +00:00
const title = (`@${author}: ` + noQuoteArray.join('\n').replaceAll('\n', ' ')).substring(0, 255)
2022-08-03 05:01:13 +00:00
html_url = payload.comment.html_url
2022-08-02 22:01:06 +00:00
const new_issue = octokit.issues.create({
owner: payload.repository.owner.login,
repo: payload.repository.name,
title,
2022-08-03 05:01:13 +00:00
body: html_url + '\n\n' + replyBody,
2022-08-02 22:01:06 +00:00
})
} else {
2021-06-30 08:39:34 +01:00
// Update labels.
await octokit.issues.update({
2022-03-23 10:40:06 +00:00
owner: payload.repository.owner.login,
2022-03-23 10:48:42 +00:00
repo: payload.repository.name,
2021-06-30 08:39:34 +01:00
issue_number: payload.issue.number,
labels: Array.from([...labels, ...newLabels])
2022-08-02 22:01:06 +00:00
})
2022-08-03 05:01:13 +00:00
html_url = payload.issue.html_url
2021-06-30 08:39:34 +01:00
}
2022-08-03 04:01:12 +00:00
// Get the latest news from duty-machine.
{
const commits = await octokit.rest.repos.listCommits({
owner: 'duty-machine',
repo: 'news',
per_page: 1,
})
console.log('commits: ' + require('util').inspect(commits, { depth: null }))
const sha = commits.data[0].sha
const commit = await octokit.rest.repos.getCommit({
owner: 'duty-machine',
repo: 'news',
ref: sha,
})
console.log('commit: ' + require('util').inspect(commit, { depth: null }))
let filename
for (const file of commit.data.files) {
filename = file.filename
if (filename.startsWith('articles/')) {
break
}
2022-08-03 03:01:11 +00:00
}
2022-08-03 04:01:12 +00:00
console.log('filename: ' + require('util').inspect(filename, { depth: null }));
const content = await octokit.rest.repos.getContent({
owner: 'duty-machine',
repo: 'news',
ref: sha,
path: filename,
})
console.log('content: ' + require('util').inspect(content, { depth: null }));
contentS = Buffer.from(content.data.content, 'base64').toString('utf-8')
2022-08-03 06:01:14 +00:00
console.error('contentS: ' + require('util').inspect(contentS))
2022-08-03 04:01:12 +00:00
const lines = contentS.split('\n')
const titleAndLink = lines[2]
const match = titleAndLink.match(/\[([^\]]+)\]\(([^)]+)\)/)
const title = match[1]
const link = match[2]
const body = lines[5]
2022-08-03 06:01:14 +00:00
const new_issue_duty = await octokit.issues.create({
2022-08-03 04:01:12 +00:00
owner: payload.repository.owner.login,
repo: payload.repository.name,
title,
2022-08-03 05:01:13 +00:00
body: content.data.html_url + '\n\n' + link + '\n\n' + html_url + '\n\n' + body,
2022-08-03 04:01:12 +00:00
})
2022-08-03 06:01:14 +00:00
console.error('new_issue_duty: ' + require('util').inspect(new_issue_duty, { depth: null }))
2022-08-03 03:01:11 +00:00
}
2021-04-16 11:00:10 +00:00
} catch (error) {
core.setFailed(error.message);
2021-04-16 05:00:04 +00:00
}
})()