-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
218 lines (196 loc) · 6.1 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const { Octokit } = require("@octokit/rest");
const { writeFileSync } = require("fs");
const github = require("@actions/github");
const core = require("@actions/core");
async function run() {
try {
const token = core.getInput('token');
const repository = core.getInput('repository');
const isReadmeUpdate = core.getInput('isReadmeUpdate');
const safeUserList = core.getInput('safeUserList').split(",");
const username = repository.split("/")[0];
const reponame = repository.split("/")[1];
const octokit = new Octokit({ auth: `token ${token}` });
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;
}
async function queryUnfollowUnfollowers(unfollowers) {
const unfollower = unfollowers.next();
if( !unfollower.done ) {
const uusername = unfollower.value[1].login;
if (!safeUserList.includes(username)) {
await octokit.users.unfollow({username: uusername});
}
await queryUnfollowUnfollowers(unfollowers);
}
return true;
}
async function queryFollowingUnfollowingUsers(unfollowing, index = 0) {
const follower = unfollowing.next();
if( !follower.done ) {
const uusername = follower.value[1].login;
await octokit.users.follow({username: uusername});
await queryFollowingUnfollowingUsers(unfollowing);
}
return true;
}
const { data: user } = await octokit.users.getByUsername({
username,
});
function dealBlog(blog) {
if (blog) {
return `[${blog}](https://${blog})`;
}
return "-";
}
async function checkFileExistence() {
try {
const { data: { sha }} = await octokit.repos.getReadme({
owner: username,
repo: reponame,
});
return {
status: true,
message: sha
};
} catch (error) {
if (error.status === 404) {
return {
status: false,
message: "File does not exist."
};
} else {
return {
status: false,
message: error
};
}
}
}
const followers = await queryFollowers();
const following = await queryFollowing();
const unfollowers = following.filter(user => !followers.map(follower => follower.login).includes(user.login));
const unfollowing = followers.filter(user => !following.map(follow => follow.login).includes(user.login));
followers.reverse();
if(unfollowers.length > 0) {
await queryUnfollowUnfollowers(unfollowers.entries());
console.log(`You unfollowed the ${unfollowers.length} bad guy${unfollowers.length > 1 ? 's' : ''}.`);
}
if(unfollowing.length > 0){
await queryFollowingUnfollowingUsers(unfollowing.entries());
console.log(`You followed the ${unfollowing.length} good guy${unfollowing.length > 1 ? 's' : ''}.`);
}
if (isReadmeUpdate) {
if(unfollowers.length > 0 || unfollowing.length > 0 ) {
const content = `## ${username}
<img src="${user.avatar_url}" width="150" />
| Name | Bio | Blog | Location | Company |
| -- | -- | -- | -- | -- |
| ${user.name || "-"} | ${user.bio || "-"} | ${dealBlog(user.blog)} | ${
user.location || "-"
} | ${getCompany(user.company)} |
## Followers <kbd>${followers.length}</kbd>
<table width="100%">
${formatTable(followers)}
</table>
## LICENSE
Copyright (c) 2023-present [Huniko519](https://github.com/Huniko519)
`;
let requestData = {
owner: username,
repo: reponame,
path: "README.md",
message: "Updated: Readme.md With New Infos By Github Action",
content: Buffer.from(content).toString('base64'),
committer: {
name: username,
email: `${username}@users.noreply.github.com`
},
author: {
name: username,
email: `${username}@users.noreply.github.com`
},
}
const shainfo = await checkFileExistence();
if( shainfo.status ) {
requestData["sha"] = shainfo.message;
}
await octokit.repos.createOrUpdateFileContents(requestData);
}
}
console.log("Done!");
} catch (error) {
console.log(error.message);
}
}
function formatTable(arr) {
if (arr.length === 0) {
return "";
}
let result = "";
let row = arr.length / 10;
const lastNo = arr.length % 10;
if (lastNo != 0) row += 1;
for (let j = 1; j <= row; j += 1) {
let data = "";
data = `<tr width="100%">
<td width="10%" align="center">${getUser(arr[(j - 1) * 10])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 1])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 2])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 3])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 4])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 5])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 6])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 7])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 8])}
</td>
<td width="10%" align="center">${getUser(arr[(j - 1) * 10 + 9])}
</td>
</tr>`;
result += data;
}
return result;
}
function getUser(user) {
return user
? `
<a href="${user.html_url}">
<img src="${user.avatar_url}" />
</a>`
: "";
}
function getCompany(c) {
if (c) {
c = c.replace("@", "");
return `[@${c}](https://github.com/${c})`;
}
return `-`;
}
run();