Add live data fetching

This commit is contained in:
Yuri Sizov
2021-08-22 00:43:40 +03:00
parent 0b3bd1e597
commit 11304093ff
6 changed files with 77 additions and 24 deletions

View File

@@ -1,22 +1,44 @@
const fs = require('fs').promises;
const path = require('path');
const fetch = require('node-fetch');
async function fetchPulls() {
const teams = {};
const pulls = [];
let page_count = 1;
const LINK_RE = /&page=([0-9]+)/g;
async function fetchPulls(page) {
try {
const json = await fs.readFile("pulls.raw.json", {encoding: "utf-8", flag: "r"});
return JSON.parse(json);
let page_text = page;
if (page_count > 1) {
page_text = `${page}/${page_count}`;
}
console.log(` Requesting page ${page_text} of pull request data.`);
const res = await fetch(`https://api.github.com/repos/godotengine/godot/pulls?state=open&per_page=100&page=${page}`);
if (res.status !== 200) {
return [];
}
const links = res.headers.get("link").split(",");
links.forEach((link) => {
if (link.includes('rel="last"')) {
const matches = LINK_RE.exec(link);
if (matches && matches[1]) {
page_count = Number(matches[1]);
}
}
});
return await res.json();
} catch (err) {
console.error("Error fetching the pull requests: " + err);
return {};
console.error("Error fetching pull request data: " + err);
return [];
}
}
async function processPulls() {
const pullsRaw = await fetchPulls();
let teams = {};
let pulls = [];
function processPulls(pullsRaw) {
console.log(" Processing retrieved pull requests.");
pullsRaw.forEach((item) => {
// Compile basic information about a PR.
let pr = {
@@ -59,15 +81,15 @@ async function processPulls() {
// Add labels, if available.
item.labels.forEach((labelItem) => {
pr.labels.push({
"id": labelItem.id,
"name": labelItem.name,
"color": "#" + labelItem.color
"id": labelItem.id,
"name": labelItem.name,
"color": "#" + labelItem.color
});
});
pr.labels.sort((a, b) => {
if (a.name > b.name) return 1;
if (a.name < b.name) return -1;
return 0;
if (a.name > b.name) return 1;
if (a.name < b.name) return -1;
return 0;
});
// Add teams, if available.
@@ -97,13 +119,32 @@ async function processPulls() {
pulls.push(pr);
});
}
async function main() {
console.log("[*] Building local pull request database.");
console.log("[*] Fetching pull request data from GitHub.");
// Pages are starting with 1 (but 0 returns the same results).
let page = 1;
while (page <= page_count) {
const pullsRaw = await fetchPulls(page);
processPulls(pullsRaw);
page++;
}
console.log("[*] Finalizing database.")
const output = {
"generated_at": Date.now(),
"teams": teams,
"pulls": pulls,
};
fs.writeFile("out/data.json", JSON.stringify(output), { encoding: "utf-8" });
try {
console.log("[*] Storing database to file.")
await fs.writeFile("out/data.json", JSON.stringify(output), {encoding: "utf-8"});
} catch (err) {
console.error("Error saving database file: " + err);
}
}
processPulls();
main();

5
package-lock.json generated
View File

@@ -716,6 +716,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",

View File

@@ -15,6 +15,7 @@
"dompurify": "^2.0.7",
"lit-element": "^2.2.1",
"marked": "^0.7.0",
"node-fetch": "^2.6.1",
"posthtml": "^0.12.0",
"rollup": "^1.24.0",
"rollup-plugin-babel": "^4.3.3",

View File

@@ -208,7 +208,7 @@ export default class EntryComponent extends LitElement {
title="Show older PRs first"
@click="${this.onSortClicked.bind(this, "age")}"
>
Age
Lifetime
</span> |
<span
class="pulls-sort-action ${(this._sortBy === "stale" ? "pulls-sort-action--active" : "")}"

View File

@@ -185,7 +185,7 @@ export default class PullRequestItem extends LitElement {
}
@property({ type: Number }) id = -1;
@property({ type: String, reflect: true }) title = '';
@property({ type: String }) title = '';
@property({ type: String, reflect: true }) url = '';
@property({ type: String, reflect: true }) diff_url = '';
@property({ type: String, reflect: true }) patch_url = '';
@@ -287,7 +287,10 @@ export default class PullRequestItem extends LitElement {
</div>
<div class="pr-stats">
<div class="pr-stat">
<div
class="pr-stat"
title="Days since this PR was created"
>
<span>lifetime: </span>
<span
class="pr-stat--temp${this.getStatTemp(created_days, 14)}"
@@ -295,7 +298,10 @@ export default class PullRequestItem extends LitElement {
${greports.format.formatDays(created_days)}
</span>
</div>
<div class="pr-stat">
<div
class="pr-stat"
title="Days since last update to this PR was made"
>
<span>stale for: </span>
<span
class="pr-stat--temp${this.getStatTemp(stale_days, 14)}"

View File

@@ -12,7 +12,7 @@ const ReportsAPI = {
},
async getData() {
return await this.get("/data.json");
return await this.get("data.json");
},
};