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 () => {
|
|
|
|
|
let imageRe = /^image::{china-dictatorship-media-base}\/([^/[]+)/;
|
|
|
|
|
let images = [];
|
|
|
|
|
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) {
|
|
|
|
|
images.push(match[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
images = getRandom(images, 10);
|
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-16 11:00:10 +00:00
|
|
|
full_images.push(`<img src="https://raw.githubusercontent.com/cirosantilli/china-dictatorship-media/master/${image}" width="600">`);
|
|
|
|
|
}
|
|
|
|
|
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 12:00:11 +00:00
|
|
|
repo: github.context.payload.repository.name,
|
|
|
|
|
issue_number: github.context.payload.pull_request.number,
|
|
|
|
|
body: full_images.join('\n\n');
|
2021-04-16 11:00:10 +00:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.setFailed(error.message);
|
2021-04-16 05:00:04 +00:00
|
|
|
}
|
|
|
|
|
})()
|