-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.js
141 lines (115 loc) · 3.49 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require('dotenv').config();
const { Octokit } = require('@octokit/rest');
const github = require('@actions/github');
const { writeFileSync } = require('fs');
async function run() {
try {
const { token } = process.env;
const octokit = new Octokit({ auth: `token ${token}` });
const username = github.context.repo.owner;
async function queryFollowers(page = 1) {
let { data: followers } = await octokit.users.listFollowersForUser({
username,
per_page: 100,
page,
});
if (followers.length >= 100) {
followers = followers.concat(await queryFollowers(page + 1));
}
return followers;
}
async function queryFollowing(page = 1) {
let { data: following } = await octokit.users.listFollowingForUser({
username,
per_page: 100,
page,
});
if (following.length >= 100) {
following = following.concat(await queryFollowing(page + 1));
}
return following;
}
const { data: user } = await octokit.users.getByUsername({
username,
});
const followers = await queryFollowers();
followers.reverse();
const following = await queryFollowing();
const before = `# 😳 List All Followers And Following
Easy view and filter all follows and following. Auto update by GitHub Action.
- Since GitHub's default follows and following does not support paging and filtering
- [How to use in my own project?](https://github.com/xrkffgg/list-all-followers-and-following/issues/1)
- If you have any questions, please open a new [issue](https://github.com/xrkffgg/list-all-followers-and-following/issues)
`;
function dealBlog(blog) {
if (blog) {
return `[${blog}](https://${blog})`;
}
return '-';
}
const middle = `## ${username}
<img src="${user.avatar_url}" width="120" />
| Name | Bio | Blog | Location | Company |
| -- | -- | -- | -- | -- |
| ${user.name || '-' } | ${user.bio || '-' } | ${dealBlog(user.blog)} | ${user.location || '-' } | ${getCompany(user.company)} |
## Followers <kbd>${followers.length}</kbd>
<table>
${formatTable(followers)}
</table>
## Following <kbd>${following.length}</kbd>
<table>
${formatTable(following)}
</table>
`
const end = `## LICENSE
[MIT](https://github.com/xrkffgg/list-all-followers-and-following/blob/main/LICENSE)
Copyright (c) 2021-present [xrkffgg](https://github.com/xrkffgg)
`
writeFileSync('./README.md', before + middle + end);
console.log('Done!')
} catch (error) {
console.log(error.message);
}
}
function formatTable(arr) {
if (arr.length === 0) {
return '';
}
let result = '';
let row = arr.length / 5;
const lastNo = arr.length % 5;
if (lastNo != 0) row += 1;
for (let j = 1; j <= row; j += 1) {
let data = '';
data = `<tr>
<td width="150" align="center">${getUser(arr[(j-1)*5])}
</td>
<td width="150" align="center">${getUser(arr[(j-1)*5+1])}
</td>
<td width="150" align="center">${getUser(arr[(j-1)*5+2])}
</td>
<td width="150" align="center">${getUser(arr[(j-1)*5+3])}
</td>
<td width="150" align="center">${getUser(arr[(j-1)*5+4])}
</td>
</tr>`;
result += data;
}
return result;
}
function getUser(user) {
return user ? `
<a href="${user.html_url}">
<img src="${user.avatar_url}" width="50" />
<br />
${user.login}
</a>` : '';
}
function getCompany(c) {
if (c) {
c = c.replace('@', '');
return `[@${c}](https://github.com/${c})`;
}
return `-`;
}
run();