-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfindUsersToRemove.js
executable file
·88 lines (71 loc) · 2.52 KB
/
findUsersToRemove.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
#!/usr/bin/env node
'use strict';
(async () => {
const fs = require('fs');
const ora = require('ora');
const path = require('path');
const data = require('./impl/data');
const DbFile = require('./impl/dbFile');
const github = require('./impl/github');
const scriptUtils = require('./impl/scriptUtils');
scriptUtils.printUnhandledRejections();
await findUsersToRemove();
return;
async function findUsersToRemove() {
// The goal is to find users who meet all these criteria:
// * have had their profiles for a while,
// * aren't marked not to be deleted,
// * haven't starred the project, and
// * have an empty profile.
let spinner;
const now = new Date;
const minAgeMonths = 1;
const users = [];
for (const file of fs.readdirSync(data.users)) {
if (file.endsWith('.json')) {
const user = new DbFile(path.join(data.users, file));
if (!user.ghuser_deleted_because && !user.ghuser_keep_because && !user.removed_from_github
&& now - Date.parse(user.ghuser_created_at) > minAgeMonths * 30 * 24 * 60 * 60 * 1000) {
let isToBeRemoved = !user.contribs.repos.length;
if (!isToBeRemoved) {
const contribs = new DbFile(path.join(data.contribs, file));
let total_stars = 0;
for (const repo in contribs.repos) {
total_stars += contribs.repos[repo].stargazers_count;
}
isToBeRemoved = !total_stars;
}
if (isToBeRemoved) {
users.push(user);
}
}
}
}
const stargazers = await fetchStargazers('ghuser-io/ghuser.io');
const toRemove = users.map(user => user.login).filter(user => stargazers.indexOf(user) === -1);
if (toRemove.length) {
console.log(`
Users to remove:
`);
for (const user of toRemove) {
console.log(user);
}
}
return;
async function fetchStargazers(repo) {
let stargazers = [];
spinner = ora(`Fetching ${repo}'s stargazers...`).start();
const perPage = 100;
for (let page = 1;; ++page) {
const ghUrl = `https://api.github.com/repos/${repo}/stargazers?page=${page}&per_page=${perPage}`;
const ghDataJson = await github.fetchGHJson(ghUrl, spinner);
stargazers = [...stargazers, ...ghDataJson.map(stargazer => stargazer.login)];
if (ghDataJson.length < perPage) {
break;
}
}
spinner.succeed(`${repo} has ${stargazers.length} stargazers`);
return stargazers;
}
}
})();