From 5304e102816c667fb29a0cdb0dc9d8c52978ca2b Mon Sep 17 00:00:00 2001 From: "Mr. H" Date: Wed, 23 Oct 2024 15:53:14 +0300 Subject: [PATCH] Update main.js --- main.js | 246 +++++++++++++++++--------------------------------------- 1 file changed, 73 insertions(+), 173 deletions(-) diff --git a/main.js b/main.js index b6618bc..e405750 100644 --- a/main.js +++ b/main.js @@ -1,126 +1,91 @@ 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 isReadmeUpdate = core.getInput('isReadmeUpdate') === 'true'; const safeUserList = core.getInput('safeUserList').split(","); - const username = repository.split("/")[0]; - const reponame = repository.split("/")[1]; + const [username, reponame] = repository.split("/"); 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; - } + const followers = await fetchAllPages(octokit.users.listFollowersForUser, { username }); + const following = await fetchAllPages(octokit.users.listFollowingForUser, { username }); + + const unfollowers = following.filter(user => !followers.some(follower => follower.login === user.login)); + const unfollowing = followers.filter(user => !following.some(follow => follow.login === user.login)); - 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; + if (unfollowers.length > 0) { + await processUsers(unfollowers, octokit.users.unfollow, safeUserList); + console.log(`You unfollowed ${unfollowers.length} user(s).`); } - 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; + if (unfollowing.length > 0) { + await processUsers(unfollowing, octokit.users.follow); + console.log(`You followed ${unfollowing.length} user(s).`); } - 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; + if (isReadmeUpdate && (unfollowers.length > 0 || unfollowing.length > 0)) { + await updateReadme(octokit, username, reponame, followers); } - const { data: user } = await octokit.users.getByUsername({ - username, - }); + console.log("Done!"); + } catch (error) { + console.log(error.message); + } +} - function dealBlog(blog) { - if (blog) { - return `[${blog}](https://${blog})`; - } - return "-"; - } +async function fetchAllPages(apiMethod, params, page = 1, results = []) { + const { data } = await apiMethod({ ...params, per_page: 100, page }); + results.push(...data); + return data.length === 100 ? fetchAllPages(apiMethod, params, page + 1, results) : results; +} - 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 - }; - } - } +async function processUsers(users, apiMethod, safeUserList = []) { + for (const user of users) { + if (!safeUserList.includes(user.login)) { + await apiMethod({ username: user.login }); } + } +} - 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' : ''}.`); - } +async function updateReadme(octokit, username, reponame, followers) { + const { data: user } = await octokit.users.getByUsername({ username }); + const content = generateReadmeContent(user, followers); + const { status, message: sha } = await checkFileExistence(octokit, username, reponame); + + const 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` }, + ...(status && { sha }) + }; + + await octokit.repos.createOrUpdateFileContents(requestData); +} + +async function checkFileExistence(octokit, owner, repo) { + try { + const { data: { sha } } = await octokit.repos.getReadme({ owner, repo }); + return { status: true, message: sha }; + } catch (error) { + return { status: false, message: error.status === 404 ? "File does not exist." : error.message }; + } +} - if (isReadmeUpdate) { - if(unfollowers.length > 0 || unfollowing.length > 0 ) { - const content = `## ${username} +function generateReadmeContent(user, followers) { + return `## ${user.login} | Name | Bio | Blog | Location | Company | | -- | -- | -- | -- | -- | -| ${user.name || "-"} | ${user.bio || "-"} | ${dealBlog(user.blog)} | ${ - user.location || "-" - } | ${getCompany(user.company)} | +| ${user.name || "-"} | ${user.bio || "-"} | ${formatBlog(user.blog)} | ${user.location || "-"} | ${formatCompany(user.company)} | ## Followers ${followers.length} @@ -129,90 +94,25 @@ async function run() { ## LICENSE -Copyright (c) 2023-present [Huniko519](https://github.com/Huniko519) +Copyright (c) 2023-present [${user.login}](https://github.com/${user.login}) `; - - 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 = ` - ${getUser(arr[(j - 1) * 10])} - - ${getUser(arr[(j - 1) * 10 + 1])} - - ${getUser(arr[(j - 1) * 10 + 2])} - - ${getUser(arr[(j - 1) * 10 + 3])} - - ${getUser(arr[(j - 1) * 10 + 4])} - - ${getUser(arr[(j - 1) * 10 + 5])} - - ${getUser(arr[(j - 1) * 10 + 6])} - - ${getUser(arr[(j - 1) * 10 + 7])} - - ${getUser(arr[(j - 1) * 10 + 8])} - - ${getUser(arr[(j - 1) * 10 + 9])} - - `; - result += data; - } - return result; +function formatBlog(blog) { + return blog ? `[${blog}](https://${blog})` : "-"; } -function getUser(user) { - return user - ? ` - - - ` - : ""; +function formatCompany(company) { + return company ? `[@${company.replace("@", "")}](https://github.com/${company.replace("@", "")})` : "-"; } -function getCompany(c) { - if (c) { - c = c.replace("@", ""); - return `[@${c}](https://github.com/${c})`; - } - return `-`; +function formatTable(users) { + return users.reduce((acc, user, index) => { + if (index % 10 === 0) acc += ""; + acc += ``; + if (index % 10 === 9) acc += ""; + return acc; + }, ""); } run();