Added GitHub issue caching.

This commit is contained in:
Richard Moore 2019-10-19 20:00:39 +09:00
parent 5b0c15930f
commit fea867a206
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
650 changed files with 288 additions and 1 deletions

@ -0,0 +1,10 @@
"use strict";
const config = require("../config");
const { syncIssues } = require("../github");
(async function() {
const user = await config.get("github-user");
const password = await config.get("github-readonly");
await syncIssues(user, password);
})();

142
admin/cmds/grep-github.js Normal file

@ -0,0 +1,142 @@
"use strict";
const { colorify } = require("../log");
const { getIssues } = require("../github");
const { repeat } = require("../utils");
const Options = {
"body": 1,
"end": 1,
"issue": 1,
"start": 1,
"title": 1,
"user": 1,
};
const Flags = {
"open": 1,
"match-case": 1,
};
(async function() {
const options = { };
for (let i = 2; i < process.argv.length; i++) {
const option = process.argv[i];
if (option.substring(0, 2) === "--") {
const comps = option.substring(2).split(/=/);
if (Flags[comps[0]]) {
if (comps[1] != null) { throw new Error("Invalid flag: " + option); }
options[comps[0]] = true;
} else if (Options[comps[0]]) {
if (comps[1] == null) {
options[comps[0]] = process.argv[++i];
if (options[comps[0]] == null) {
throw new Error("Missing option value: " + option);
}
} else {
options[comps[0]] = comps[1];
}
} else {
throw new Error("Unexpected option: " + option);
}
} else {
throw new Error("Unexpected argument: " + option);
}
}
if (options["title"]) { options.title = new RegExp(options.title, (options["match-case"] ? "": "i")); }
if (options["body"]) { options.body = new RegExp(options.title, (options["match-case"] ? "": "i")); }
if (options["start"]) {
if (options["start"].match(/^[0-9]{4}-[0-9]{2}-[0-9{2}]$/)) {
throw new Error("Expected YYYY-MM-DD");
}
}
if (options["end"]) {
if (options["end"].match(/^[0-9]{4}-[0-9]{2}-[0-9{2}]$/)) {
throw new Error("Expected YYYY-MM-DD");
}
}
const count = { issues: 0, comments: 0, code: 0, responses: 0 };
const issues = await getIssues();
issues.forEach((issue) => {
const info = issue.issue;
const comments = issue.comments;
if (options.issue && parseInt(options.issue) != info.number) { return; }
if (options.open && info.state !== "open") { return; }
if (options.title && !info.title.match(options.title)) { return; }
if (options.body) {
const body = info.body + "\n" + comments.map((c) => (c.body)).join("\n");
if (!body.match(options.body)) {
return;
}
}
if (options.user) {
const users = comments.map((c) => (c.user.login));
users.push(info.user.login);
if (users.indexOf(options.user) === -1) {
return;
}
}
const dates = comments.map((c) => (c.created_at.split("T")[0]));
dates.push(info.created_at.split("T")[0]);
if (options.start) {
if (dates.filter((d) => (d >= options.start)).length === 0) { return; }
}
if (options.end) {
if (dates.filter((d) => (d <= options.start)).length === 0) { return; }
}
count.issues++;
console.log(colorify(repeat("=", 70), "bold"))
console.log(colorify("Issue:", "bold"), info.title, ` (#${ info.number })`);
console.log(colorify("User:","bold"), colorify(info.user.login, "blue"));
console.log(colorify("State:", "bold"), info.state);
if (info.created_at === info.updated_at) {
console.log(colorify("Created:", "bold"), info.created_at);
} else {
console.log(colorify("Created:", "bold"), info.created_at, ` (updated: ${ info.updated_at })`);
}
info.body.trim().split("\n").forEach((line) => {
console.log(" " + line);
});
if (comments.length) {
comments.forEach((info) => {
if (options.start && info.created_at < options.start) { return ; }
if (options.end && info.created_at > options.end) { return; }
count.comments++;
if (options.user && info.user.login !== options.user) { return; }
count.responses++;
if (info.body.indexOf("`") >= 0) { count.code++; }
console.log(colorify(repeat("-", 70), "bold"));
console.log(colorify("User:", "bold"), colorify(info.user.login, "green"));
if (info.created_at === info.updated_at) {
console.log(colorify("Created:", "bold"), info.created_at);
} else {
console.log(colorify("Created:", "bold"), info.created_at, ` (updated: ${ info.updated_at })`);
}
info.body.trim().split("\n").forEach((line) => {
console.log(" " + line);
});
});
}
});
console.log(colorify(repeat("=", 70), "bold"))
// @TODO: Add stats on new/closed issues
//if (options.user) {
// console.log(`${ count.responses } responses (${ count.code } w/ code) on ${ count.comments } comments across ${ count.issues } issues.`);
//} else {
console.log(`${ count.comments } comment${ (count.comments !== 1) ? "s": "" } across ${ count.issues } issue${ (count.issues !== 1) ? "s": ""}.`);
//}
})().catch((error) => {
console.log("Error: " + error.message);
});

134
admin/github.js Normal file

@ -0,0 +1,134 @@
"use strict";
const fs = require("fs");
const { resolve } = require("path");
const zlib = require("zlib");
const { id } = require("../packages/hash");
const { fetchJson } = require("../packages/web");
const CacheDir = resolve(__dirname, "../github-cache/");
function addResponse(result, response) {
return { result, response };
}
function loadFile(filename) {
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString());
//return JSON.parse(fs.readFileSync(filename).toString());
}
// @TODO: atomic
function saveFile(filename, content) {
fs.writeFileSync(filename, zlib.gzipSync(JSON.stringify(content)));
//fs.writeFileSync(filename, JSON.stringify(content));
}
function mockFetchJson(url, body, headers) {
return {
result: null,
response: {
statusCode: 304
}
}
}
async function _fetchGitHub(user, password, fetchJson, url) {
const result = [ ];
while (true) {
const filename = resolve(CacheDir, id(url).substring(2, 14));
const headers = {
"User-Agent": "ethers-io",
};
let items = null;
let link = null;
try {
const data = loadFile(filename);
headers["if-none-match"] = data.etag;
items = data.items;
link = data.link;
} catch (error) {
if (error.code !== "ENOENT") { throw error; }
}
const fetch = await fetchJson({
url: url,
user: user,
password: password,
headers: headers
}, null, addResponse);
// Cached response is good; use it!
if (fetch.response.statusCode !== 304) {
items = fetch.result;
if (fetch.response.headers) {
link = (fetch.response.headers.link || null);
}
if (fetch.response.headers.etag){
saveFile(filename, {
timestamp: (new Date()).getTime(),
url: url,
link: link,
etag: fetch.response.headers.etag,
items: items,
version: 1
});
}
}
items.forEach((item) => { result.push(item)});
url = null;
(link || "").split(",").forEach((item) => {
if (item.indexOf('rel="next"') >= 0) {
const match = item.match(/<([^>]*)>/);
if (match) { url = match[1]; }
}
});
if (!url) { break; }
}
return result;
}
async function fetchGitHub(user, password, url, cacheOnly) {
if (cacheOnly) {
return await _fetchGitHub("none", "none", mockFetchJson, url);
}
const results = await _fetchGitHub(user, password, fetchJson, url);
return results;
}
async function _getIssues(user, password) {
const cacheOnly = (user == null);
let issues = await fetchGitHub(user, password, "https://api.github.com/repos/ethers-io/ethers.js/issues?state=all&per_page=100", cacheOnly)
if (!cacheOnly) { console.log(`Found ${ issues.length } issues`); }
const result = [ ];
for (let i = 0; i < issues.length; i++) {
const issue = issues[i];
let comments = await fetchGitHub(user, password, issue.comments_url, cacheOnly);
result.push({ issue, comments});
if (!cacheOnly) { console.log(` Issue ${ issue.number }: ${ comments.length } comments`); }
}
result.sort((a, b) => (a.issue.number - b.issue.number));
return result;
}
function getIssues() {
return _getIssues();
}
function syncIssues(user, password) {
return _getIssues(user, password);
}
module.exports = {
getIssues,
syncIssues,
}

BIN
github-cache/0021245234e1 Normal file

Binary file not shown.

BIN
github-cache/00b424c41eb4 Normal file

Binary file not shown.

BIN
github-cache/0187d6d5fa5a Normal file

Binary file not shown.

BIN
github-cache/01b9c61ed32d Normal file

Binary file not shown.

BIN
github-cache/01fc95de636a Normal file

Binary file not shown.

BIN
github-cache/020b2817f386 Normal file

Binary file not shown.

BIN
github-cache/025023c5f720 Normal file

Binary file not shown.

BIN
github-cache/025f73edd66b Normal file

Binary file not shown.

BIN
github-cache/02cad84efaa5 Normal file

Binary file not shown.

BIN
github-cache/02e68d9902c5 Normal file

Binary file not shown.

BIN
github-cache/030b26dabc1c Normal file

Binary file not shown.

BIN
github-cache/0347eac16a9e Normal file

Binary file not shown.

BIN
github-cache/03904c300f1d Normal file

Binary file not shown.

BIN
github-cache/0487a716ddcb Normal file

Binary file not shown.

BIN
github-cache/049e20fa6d90 Normal file

Binary file not shown.

BIN
github-cache/05949c9a864a Normal file

Binary file not shown.

BIN
github-cache/05e27216a6d3 Normal file

Binary file not shown.

BIN
github-cache/05e49cf709b1 Normal file

Binary file not shown.

BIN
github-cache/0632d7b3a88d Normal file

Binary file not shown.

BIN
github-cache/06951e334666 Normal file

Binary file not shown.

BIN
github-cache/06b567286f9a Normal file

Binary file not shown.

BIN
github-cache/06f65f522822 Normal file

Binary file not shown.

BIN
github-cache/07f317c59e81 Normal file

Binary file not shown.

BIN
github-cache/07fa40c22821 Normal file

Binary file not shown.

BIN
github-cache/080588b1c830 Normal file

Binary file not shown.

BIN
github-cache/082ca0a35555 Normal file

Binary file not shown.

BIN
github-cache/0876f22405d4 Normal file

Binary file not shown.

BIN
github-cache/09b2ad5631e5 Normal file

Binary file not shown.

BIN
github-cache/0a13ed0eb79b Normal file

Binary file not shown.

BIN
github-cache/0a2aea48f219 Normal file

Binary file not shown.

BIN
github-cache/0a83d5ad54fc Normal file

Binary file not shown.

BIN
github-cache/0aa644d9792f Normal file

Binary file not shown.

BIN
github-cache/0ac28a370896 Normal file

Binary file not shown.

BIN
github-cache/0b89f4fa0714 Normal file

Binary file not shown.

BIN
github-cache/0c483ebe1d6b Normal file

Binary file not shown.

BIN
github-cache/0e4b18422ecd Normal file

Binary file not shown.

BIN
github-cache/0f1424a34b60 Normal file

Binary file not shown.

BIN
github-cache/0f858aa8afa6 Normal file

Binary file not shown.

BIN
github-cache/0f973d35737c Normal file

Binary file not shown.

BIN
github-cache/0fbc7d5474d6 Normal file

Binary file not shown.

BIN
github-cache/119007dca37b Normal file

Binary file not shown.

BIN
github-cache/12456630ab47 Normal file

Binary file not shown.

BIN
github-cache/124e66c7944c Normal file

Binary file not shown.

BIN
github-cache/1278908a05f7 Normal file

Binary file not shown.

BIN
github-cache/12a9abfcdb9a Normal file

Binary file not shown.

BIN
github-cache/135ad2423b99 Normal file

Binary file not shown.

BIN
github-cache/136bf94f1461 Normal file

Binary file not shown.

BIN
github-cache/13d47f6aaaba Normal file

Binary file not shown.

BIN
github-cache/14129ba72bcb Normal file

Binary file not shown.

BIN
github-cache/14ab7a534202 Normal file

Binary file not shown.

BIN
github-cache/15157e6fd83d Normal file

Binary file not shown.

BIN
github-cache/1571c1bb5230 Normal file

Binary file not shown.

BIN
github-cache/157bd237a5ae Normal file

Binary file not shown.

BIN
github-cache/15b077194937 Normal file

Binary file not shown.

BIN
github-cache/16859ac8de8a Normal file

Binary file not shown.

BIN
github-cache/16e9d885479d Normal file

Binary file not shown.

BIN
github-cache/17585707a650 Normal file

Binary file not shown.

BIN
github-cache/1762c9fc5767 Normal file

Binary file not shown.

BIN
github-cache/188b1fcaaf70 Normal file

Binary file not shown.

BIN
github-cache/195b35167e9c Normal file

Binary file not shown.

BIN
github-cache/197016a56cd6 Normal file

Binary file not shown.

BIN
github-cache/19b1177c1ca7 Normal file

Binary file not shown.

BIN
github-cache/19b5acffe158 Normal file

Binary file not shown.

BIN
github-cache/19c0f292e1f7 Normal file

Binary file not shown.

BIN
github-cache/1a6b400340e1 Normal file

Binary file not shown.

BIN
github-cache/1aeed0f4cc1c Normal file

Binary file not shown.

BIN
github-cache/1afe8f3a86cd Normal file

Binary file not shown.

BIN
github-cache/1b853cfd5991 Normal file

Binary file not shown.

BIN
github-cache/1c9c9056e6f9 Normal file

Binary file not shown.

BIN
github-cache/1d21aab07fe5 Normal file

Binary file not shown.

BIN
github-cache/1d2b41b904f2 Normal file

Binary file not shown.

BIN
github-cache/1d53a568ff5e Normal file

Binary file not shown.

BIN
github-cache/1d9f66b8c13b Normal file

Binary file not shown.

BIN
github-cache/1da6a3185d6e Normal file

Binary file not shown.

BIN
github-cache/1e0112a68ff0 Normal file

Binary file not shown.

BIN
github-cache/1ea8dd444e36 Normal file

Binary file not shown.

BIN
github-cache/1eedc6cbd80c Normal file

Binary file not shown.

BIN
github-cache/1f46c02e47b5 Normal file

Binary file not shown.

BIN
github-cache/1f9e4918a78c Normal file

Binary file not shown.

BIN
github-cache/1f9fd828db24 Normal file

Binary file not shown.

BIN
github-cache/2006cc34ad59 Normal file

Binary file not shown.

BIN
github-cache/2075f385da58 Normal file

Binary file not shown.

BIN
github-cache/20a7ff89ae2c Normal file

Binary file not shown.

BIN
github-cache/21d5bd593128 Normal file

Binary file not shown.

BIN
github-cache/228e5896f98a Normal file

Binary file not shown.

BIN
github-cache/22d091c58f33 Normal file

Binary file not shown.

BIN
github-cache/239205bd2aa4 Normal file

Binary file not shown.

BIN
github-cache/23a0d05256dc Normal file

Binary file not shown.

BIN
github-cache/242e47b2ef84 Normal file

Binary file not shown.

BIN
github-cache/243fcaaba838 Normal file

Binary file not shown.

BIN
github-cache/245426494b61 Normal file

Binary file not shown.

BIN
github-cache/2457c5e5e0f7 Normal file

Binary file not shown.

BIN
github-cache/24fc6825356c Normal file

Binary file not shown.

BIN
github-cache/25a85d1e6780 Normal file

Binary file not shown.

BIN
github-cache/266fea58ddd8 Normal file

Binary file not shown.

BIN
github-cache/2690eaff2523 Normal file

Binary file not shown.

BIN
github-cache/26bd140eb7a2 Normal file

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More