From 21558e927193feb800f9f50d8ac9cd1c973549fe Mon Sep 17 00:00:00 2001 From: rndquu Date: Tue, 22 Aug 2023 23:13:01 +0300 Subject: [PATCH] refactor: remove unused deployment scripts --- utils/download-artifact.js | 18 ---- utils/print-deployments-logs.js | 165 -------------------------------- 2 files changed, 183 deletions(-) delete mode 100644 utils/download-artifact.js delete mode 100644 utils/print-deployments-logs.js diff --git a/utils/download-artifact.js b/utils/download-artifact.js deleted file mode 100644 index f0270e59b..000000000 --- a/utils/download-artifact.js +++ /dev/null @@ -1,18 +0,0 @@ -module.exports = async ({ github, context, fs, workflow_run_id, workspace }) => { - console.log("download_artifact....."); - const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ - owner: context.repo.owner, - repo: context.repo.repo, - run_id: workflow_run_id, - }); - const matchArtifact = artifacts.data.artifacts.filter((artifact) => { - return artifact.name == "pr"; - })[0]; - const download = await github.rest.actions.downloadArtifact({ - owner: context.repo.owner, - repo: context.repo.repo, - artifact_id: matchArtifact.id, - archive_format: "zip", - }); - fs.writeFileSync(`${workspace}/pr.zip`, Buffer.from(download.data)); -}; diff --git a/utils/print-deployments-logs.js b/utils/print-deployments-logs.js deleted file mode 100644 index dcff6e322..000000000 --- a/utils/print-deployments-logs.js +++ /dev/null @@ -1,165 +0,0 @@ -//@ts-check -module.exports = async ({ github, context, fs }) => { - const pullRequestInfo = fs.readFileSync("./pr_number").toString("utf-8"); - console.log({ pullRequestInfo }); - const infoSubstring = pullRequestInfo.split(","); - const eventName = infoSubstring[0].split("=")[1]; - const pullRequestNumber = infoSubstring[1].split("=")[1] ?? 0; - const commitSha = infoSubstring[2].split("=")[1]; - const deploymentsLog = fs.readFileSync("./deployments.log").toString("utf-8"); - - let defaultBody = deploymentsLog; - const uniqueDeployUrl = deploymentsLog.match(/https:\/\/.+\.pages\.dev/gim); - const botCommentsArray = []; - - if (uniqueDeployUrl) { - const slicedSha = commitSha.slice(0, -33); - - defaultBody = `- [${slicedSha}](${uniqueDeployUrl})`; - } - - const verifyInput = (data) => { - return data !== ""; - }; - - const GMTConverter = (bodyData) => { - return bodyData.replace("GMT+0000 (Coordinated Universal Time)", "(UTC)"); - }; - - const sortComments = (bodyData) => { - const bodyArray = bodyData.split("\n").filter((elem) => elem.includes("Deployment")); - let commentBody = ``; - const timeArray = []; - const timeObj = {}; - bodyArray.forEach((element) => { - const timestamp = new Date( - element - .match(/Deployment:.*\(UTC\)/)[0] - .replace("Deployment:", "") - .trim() - ).getTime(); - timeArray.push(timestamp); - timeObj[timestamp] = element; - }); - const timeSortArray = timeArray.sort(); - - timeSortArray.forEach((em) => { - commentBody = commentBody + `${timeObj[em]}\n`; - }); - return commentBody; - }; - - const createNewCommitComment = async (body = defaultBody) => { - body = GMTConverter(body); - verifyInput(body) && - (await github.rest.repos.createCommitComment({ - owner: context.repo.owner, - repo: context.repo.repo, - commit_sha: commitSha, - body: body, - })); - }; - - const createNewPRComment = async (body = defaultBody) => { - body = GMTConverter(body); - verifyInput(body) && - (await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: pullRequestNumber, - body: body, - })); - }; - - const editExistingPRComment = async () => { - const { body: botBody, id: commentId } = botCommentsArray[0]; - let commentBody = `${GMTConverter(defaultBody)}\n` + `${GMTConverter(botBody)}`; - const sortCommentBody = sortComments(commentBody); - verifyInput(sortCommentBody) && - (await github.rest.issues.updateComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: commentId, - body: sortCommentBody, - })); - }; - - const deleteExistingPRComments = async () => { - const delPromises = botCommentsArray.map(async (elem) => { - await github.rest.issues.deleteComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: elem.id, - }); - }); - await Promise.all(delPromises); - }; - - const mergeExistingPRComments = async () => { - let commentBody = `${GMTConverter(defaultBody)}\n`; - botCommentsArray.forEach(({ body }) => { - commentBody = commentBody + `${GMTConverter(body)}\n`; - }); - const sortCommentBody = sortComments(commentBody); - await createNewPRComment(sortCommentBody); - await deleteExistingPRComments(); - }; - - const processPRComments = async () => { - const perPage = 30; - let pageNumber = 1; - let hasMore = true; - const commentsArray = []; - - while (hasMore) { - const { data: issueComments } = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: pullRequestNumber, - per_page: perPage, - page: pageNumber, - }); - pageNumber++; - - if (issueComments.length > 0) { - commentsArray.push(...issueComments); - } else { - hasMore = false; - } - } - - if (commentsArray.length > 0) { - commentsArray.forEach((elem) => { - if (elem.user.type === "Bot" && elem.user.login === "ubiquibot[bot]") { - botCommentsArray.push(elem); - } - }); - const botLen = botCommentsArray.length; - switch (botLen) { - case 0: - //no (bot) comments - createNewPRComment(); - break; - case 1: - //single (bot) comment [] - editExistingPRComment(); - break; - default: - //multiple (bot) comments [] - mergeExistingPRComments(); - break; - } - } else { - //no comments (user|bot) [] - createNewPRComment(); - } - }; - - if (eventName == "pull_request") { - console.log("Creating a comment for the pull request"); - await processPRComments(); - } else { - console.log("Creating a comment for the commit"); - await createNewCommitComment(); - } -};