-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsnapshots.js
84 lines (72 loc) · 2.52 KB
/
snapshots.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fetch from "node-fetch";
import {DateTime} from "luxon";
import fs from "fs";
import core from "@actions/core";
import {repos} from "./snapshots/repos.js";
// process.argv[0] // node
// process.argv[1] // snapshots.js
// process.argv[2] // PREFIX
// process.argv[3] // trim
const prefix = process.argv[2];
if (!prefix) {
core.error(`Expected PREFIX`);
process.exit(1);
}
const trim = parseInt(process.argv[3]);
query(prefix, trim);
function getIssues(repo, state) {
let [org, prj] = repo.split("/");
const url = "https://api.github.com/graphql";
const options = {
method: "POST",
body: JSON.stringify({
query: `
query {
repository(owner: "${ org }", name:"${ prj }") {
issues(states:${ state }) {
totalCount
}
}
}
`
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `bearer ${ process.env.GITHUB_TOKEN }`
}
};
return fetch(url, options)
.then(resp => resp.json())
.then(data => {
return data.data.repository.issues.totalCount;
});
}
function writeSnapshot(path, prefix, snapshots) {
// read in old data, if successful append new entry and write it out again
try {
fs.writeFileSync(path, `const ${ prefix } = ${ JSON.stringify(snapshots) };\nexport default ${ prefix };\n`);
} catch (err) {
core.error(`Error writing file: ${ err }`);
process.exit(1);
}
core.notice(`${ snapshots.timestamp.length } snapshots successfully written!`);
}
const trimmed = (s, t) => t ? s.slice(0, t) : s;
async function query(prefix, trim) {
const path = `./snapshots/${ prefix }.js`;
const {default: snapshots} = await import(path);
let n;
for (let repo of repos) {
n = await getIssues(repo, "OPEN");
if (repo in snapshots) {
// if we were already tracking this we will continue to track it even if there are zero open issues
snapshots[repo] = trimmed([n, ...snapshots[repo]], trim);
} else if (n) {
// if untracked we will only start tracking it if there are 1+ open issues
snapshots[repo] = trimmed([n], trim);
}
}
snapshots.timestamp = trimmed([DateTime.now().startOf("hour").toMillis(), ...snapshots.timestamp], trim);
writeSnapshot(path, prefix, snapshots, trim);
}