forked from ubiquity/devpool-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
90 lines (77 loc) · 2.87 KB
/
index.ts
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
import dotenv from "dotenv";
import {
DEVPOOL_OWNER_NAME,
DEVPOOL_REPO_NAME,
getAllIssues,
getIssueByLabel,
getProjectUrls,
getRepoCredentials,
GitHubIssue,
checkIfForked,
calculateStatistics,
writeTotalRewardsToGithub,
handleDevPoolIssue,
createDevPoolIssue,
} from "./helpers/github";
import { readFile, writeFile } from "fs/promises";
import { Statistics } from "./types/statistics";
// init octokit
dotenv.config();
export type TwitterMap = Record<string, string>;
/**
* Main function
* TODO: retry on rate limit error
* TODO: handle project deletion
*/
async function main() {
let twitterMap: TwitterMap = {};
try {
twitterMap = JSON.parse(await readFile("./twitterMap.json", "utf8"));
} catch (error) {
console.log("Couldnt find twitter map artifact, creating a new one");
await writeFile("./twitterMap.json", JSON.stringify({}));
}
// get devpool issues
const devpoolIssues: GitHubIssue[] = await getAllIssues(DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME);
// aggregate projects.urls and opt settings
const projectUrls = await getProjectUrls();
// aggregate all project issues
const allProjectIssues: GitHubIssue[] = [];
const isFork = await checkIfForked(DEVPOOL_OWNER_NAME);
// for each project URL
for (const projectUrl of projectUrls) {
// get owner and repository names from project URL
const [ownerName, repoName] = getRepoCredentials(projectUrl);
// get all project issues (opened and closed)
const projectIssues: GitHubIssue[] = await getAllIssues(ownerName, repoName);
// aggregate all project issues
allProjectIssues.push(...projectIssues);
// for all issues
for (const projectIssue of projectIssues) {
// if issue exists in devpool
const devpoolIssue = getIssueByLabel(devpoolIssues, `id: ${projectIssue.node_id}`);
// adding www creates a link to an issue that does not count as a mention
// helps with preventing a mention in partner's repo especially during testing
const body = isFork ? projectIssue.html_url.replace("https://github.com", "https://www.github.com") : projectIssue.html_url;
// for all issues
if (devpoolIssue) {
// if it exists in the devpool, then update it
await handleDevPoolIssue(projectIssues, projectIssue, projectUrl, devpoolIssue, isFork);
} else {
// if it doesn't exist in the devpool, then create it
await createDevPoolIssue(projectIssue, projectUrl, body, twitterMap);
}
}
}
// Calculate total rewards from devpool issues
const { rewards, tasks } = await calculateStatistics(await getAllIssues(DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME));
const statistics: Statistics = { rewards, tasks };
await writeTotalRewardsToGithub(statistics);
}
void (async () => {
await main();
})();
// Expose the main only for testing purposes
if (process.env.NODE_ENV === "test") {
exports.main = main;
}