Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up code for readability #94

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,34 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: ubiquity/.github

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: "18.14.1"
node-version: "20.10.0"

- name: Wrangler Install
run: npm install -g wrangler
run: yarn add -g wrangler

- name: "Download Artifact"
uses: actions/github-script@v6.3.3
with:
script: |
const fs = require("fs");
const downloadArtifact = require('${{ github.workspace }}/utils/download-artifact.js');
const workflow_run_id = '${{ inputs.build_artifact_run_id || github.event.workflow_run.id }}';
const artifact_name = '${{ inputs.build_artifact_name || 'pr' }}';
const workspace = '${{ github.workspace }}';
downloadArtifact({ github, context, fs, workflow_run_id, artifact_name, workspace })
const downloadArtifact = require("${{ github.workspace }}/utils/download-artifact.js");
const workflowRunId = "${{ inputs.build_artifact_run_id || github.event.workflow_run.id }}";
const artifactName = "${{ inputs.build_artifact_name || 'pr' }}";
downloadArtifact({ github, context, fs, workflowRunId, artifactName });

- name: Extract Artifact
run: mkdir ./dist && unzip pr.zip && unzip pull-request.zip -d ./dist && ls
run: |
mkdir ./dist
unzip pull-request-artifact.zip
unzip pull-request.zip -d ./dist
0x4007 marked this conversation as resolved.
Show resolved Hide resolved
ls

- name: Publish to Cloudflare
id: publish-to-cloudflare
Expand All @@ -70,9 +73,9 @@ jobs:
PRODUCTION_BRANCH: ${{ inputs.production_branch || 'development' }}
OUTPUT_DIRECTORY: "./dist"
run: |
IFS='/' read -ra fields <<< "$REPOSITORY"
IFS="/" read -ra fields <<< "$REPOSITORY"
projectName="${fields[1]}"
projectName=$(echo $projectName | sed 's/\./-/g')
projectName=$(echo $projectName | sed "s/\./-/g")
echo $projectName
wrangler pages project list > project_list.txt
if grep -q $projectName project_list.txt; then
Expand All @@ -97,6 +100,6 @@ jobs:
github-token: ${{ steps.get_installation_token.outputs.token }}
script: |
const fs = require("fs");
const printDeploymentsLog = require('${{ github.workspace }}/utils/print-deployments-logs.js');
const printDeploymentsLog = require("${{ github.workspace }}/utils/print-deployments-logs.js");
const customDomain = "${{ inputs.custom_domain || '' }}";
await printDeploymentsLog({ github, context, fs, customDomain });
10 changes: 4 additions & 6 deletions utils/download-artifact.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
module.exports = async ({ github, context, fs, workflow_run_id, workspace, artifact_name }) => {
module.exports = async function downloadArtifact({ github, context, fs, workflowRunId, workspace, artifactName }) {
console.log("download_artifact.....");
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: workflow_run_id,
run_id: workflowRunId,
});
const matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == artifact_name;
})[0];
const matchArtifact = artifacts.data.artifacts.filter((artifact) => artifact.name == artifactName)[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));
fs.writeFileSync(`${workspace}/pull-request-artifact.zip`, Buffer.from(download.data));
};
73 changes: 22 additions & 51 deletions utils/print-deployments-logs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ts-check
module.exports = async ({ github, context, fs, customDomain }) => {
module.exports = async function printDeploymentsLogs({ github, context, fs, customDomain }) {
const pullRequestInfo = fs.readFileSync("./pr_number").toString("utf-8");
console.log({ pullRequestInfo });
const infoSubstring = pullRequestInfo.split(",");
Expand All @@ -21,47 +21,19 @@ module.exports = async ({ github, context, fs, customDomain }) => {
defaultBody = `<a href="${uniqueDeployUrl}"><code>${slicedSha}</code></a>`;
}

const verifyInput = (data) => {
function verifyInput(data) {
return data !== "";
};

const GMTConverter = (bodyData) => {
return bodyData.replace("GMT+0000 (Coordinated Universal Time)", "(UTC)");
};

const alignRight = (bodyData) => {
function alignRight(bodyData) {
if (!bodyData.startsWith('<div align="right">')) {
return `<div align="right">${bodyData}</div>`;
} else {
return bodyData;
}
};

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);
async function createNewCommitComment(body = defaultBody) {
verifyInput(body) &&
(await github.rest.repos.createCommitComment({
owner: context.repo.owner,
Expand All @@ -71,8 +43,7 @@ module.exports = async ({ github, context, fs, customDomain }) => {
}));
};

const createNewPRComment = async (body = defaultBody) => {
body = GMTConverter(body);
async function createNewPullRequestComment(body = defaultBody) {
verifyInput(body) &&
(await github.rest.issues.createComment({
owner: context.repo.owner,
Expand All @@ -82,9 +53,9 @@ module.exports = async ({ github, context, fs, customDomain }) => {
}));
};

const editExistingPRComment = async () => {
async function editExistingPullRequestComment() {
const { body: botBody, id: commentId } = botCommentsArray[0];
let commentBody = alignRight(`${GMTConverter(defaultBody)}\n`) + alignRight(`${GMTConverter(botBody)}`);
let commentBody = alignRight(`${(botBody)}\n`) + alignRight(`${(defaultBody)}`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was in the wrong order. Now it should display in chronological order.

verifyInput(commentBody) &&
(await github.rest.issues.updateComment({
owner: context.repo.owner,
Expand All @@ -94,8 +65,8 @@ module.exports = async ({ github, context, fs, customDomain }) => {
}));
};

const deleteExistingPRComments = async () => {
const delPromises = botCommentsArray.map(async (elem) => {
async function deleteExistingPullRequestComments() {
const delPromises = botCommentsArray.map(async function (elem) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -105,16 +76,16 @@ module.exports = async ({ github, context, fs, customDomain }) => {
await Promise.all(delPromises);
};

const mergeExistingPRComments = async () => {
let commentBody = alignRight(`${GMTConverter(defaultBody)}\n`);
async function mergeExistingPullRequestComments() {
let commentBody = alignRight(`${(defaultBody)}\n`);
botCommentsArray.forEach(({ body }) => {
commentBody = commentBody + alignRight(`${GMTConverter(body)}\n`);
commentBody = commentBody + alignRight(`${(body)}\n`);
});
await createNewPRComment(commentBody);
await deleteExistingPRComments();
};
await createNewPullRequestComment(commentBody);
await deleteExistingPullRequestComments();
}

const processPRComments = async () => {
async function processPullRequestComments() {
const perPage = 30;
let pageNumber = 1;
let hasMore = true;
Expand Down Expand Up @@ -147,26 +118,26 @@ module.exports = async ({ github, context, fs, customDomain }) => {
switch (botLen) {
case 0:
//no (bot) comments
createNewPRComment();
createNewPullRequestComment();
break;
case 1:
//single (bot) comment []
editExistingPRComment();
editExistingPullRequestComment();
break;
default:
//multiple (bot) comments []
mergeExistingPRComments();
mergeExistingPullRequestComments();
break;
}
} else {
//no comments (user|bot) []
createNewPRComment();
createNewPullRequestComment();
}
};
}

if (eventName == "pull_request") {
console.log("Creating a comment for the pull request");
await processPRComments();
await processPullRequestComments();
} else {
console.log("Creating a comment for the commit");
await createNewCommitComment();
Expand Down