-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateReports.js
100 lines (86 loc) · 2.32 KB
/
generateReports.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Octokit } from "octokit";
import fs from "node:fs";
import path from "node:path";
import { parseReportData } from "./utils/parseReportData.js";
import config from "./config.js";
import "dotenv/config";
import { ensureDirectory } from "./utils/ensureDirectory.js";
function saveToCsv(repoData) {
const header = [
"repo_name",
"date",
"stars",
"forks",
"stars_gained",
"forks_gained",
];
let rowData = [
repoData.name,
new Date().toISOString(),
repoData.stars,
repoData.forks,
];
const pathname = path.resolve(config.reportOutputDir, `${repoData.name}.csv`);
try {
let existingData = "";
if (fs.existsSync(pathname)) {
existingData = fs.readFileSync(pathname, "utf8");
}
const { data: rows } = parseReportData(existingData);
if (rows.length < 1) {
rowData = [...rowData, 0, 0];
} else {
rowData = [
...rowData,
repoData.stars - rows[rows.length - 1][2],
repoData.forks - rows[rows.length - 1][3],
];
}
if (rows.length < config.trackedPeriod) {
if (existingData.trim() === "") {
fs.appendFileSync(pathname, header.join(","));
}
fs.appendFileSync(pathname, "\n" + rowData.join(","));
return;
}
const newRows = [...rows.slice(1), rowData];
fs.writeFileSync(
pathname,
header.join(",") + "\n" + newRows.map((x) => x.join(",")).join("\n")
);
} catch (err) {
fs.writeFileSync(pathname, header.join(",") + "\n" + rowData.join(","));
}
}
async function getOrganizationRepos() {
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
try {
const res = await octokit.request(
`GET /orgs/${config.organization}/repos`,
{
org: "ORG",
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
}
);
ensureDirectory(config.reportOutputDir);
res.data
.filter((repo) => !repo.private)
.forEach((repo) => {
const repoProj = {
stars: repo.stargazers_count,
forks: repo.forks_count,
full_name: repo.full_name,
name: repo.name,
date: new Date().toISOString(),
};
saveToCsv(repoProj);
});
} catch (error) {
console.error("Error fetching repositories:", error);
}
}
await getOrganizationRepos();