Skip to content

Commit

Permalink
refactor ( website-builder ): Refactor website-builder to typescript
Browse files Browse the repository at this point in the history
The index.js file is now refactored to index.ts and a tsconfig.json has
been added along with required type packages as dev dependencies

closes #1054
  • Loading branch information
hamza.asif committed May 21, 2024
1 parent 13180b5 commit e9b8460
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 37 deletions.
66 changes: 37 additions & 29 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import express from "express";
import express = require("express");
import {exec} from "child_process";
import crypto from "crypto";
import dotenv from "dotenv";
import crypto = require("crypto");
import dotenv = require("dotenv");

dotenv.config();

const app = express();
const port = process.env.PORT || 3000;

let isDocumentationWebsiteUpdated = false;
let isMindmapUpdated = false;
let contributorsBuildRequired = false;
let isDocumentationWebsiteUpdated: boolean = false;
let isMindmapUpdated: boolean = false;
let contributorsBuildRequired: boolean = false;

let documentationWebsiteBuildTime = 0;
let mindmapBuildTime = 0;
let contributorsBuildTime = 0;
let documentationWebsiteBuildTime: number = 0;
let mindmapBuildTime: number = 0;
let contributorsBuildTime: number = 0;

app.use(express.json());

app.post("/webhook", async (req, res) => {
app.post("/webhook", async (req: express.Request, res: express.Response) => {
console.log("req receieved");
const signature = req.headers["x-hub-signature"];
const payload = JSON.stringify(req.body);

const hmac = crypto.createHmac("sha1", process.env.GITHUB_SECRET);
const hmac = crypto.createHmac("sha1", process.env.GITHUB_SECRET as string);
const calculatedSignature = `sha1=${hmac.update(payload).digest("hex")}`;

if (crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(calculatedSignature))) {
const { result, respMessage } = await getBranchStatus();
if (crypto.timingSafeEqual(Buffer.from(signature as string), Buffer.from(calculatedSignature))) {
const { result, respMessage } = await getBranchStatus(null);
console.log("Result: ", result);
res.status(result).send(respMessage);
} else {
Expand All @@ -39,66 +39,74 @@ app.listen(process.env.PORT, () => {
console.log(`Server listening on port ${port}`);
});

const executeCmd = async (cmd) => {
const executeCmd = async (cmd: string) => {
let stdout;
let stderr;
try {
const { stdout, stderr } = await exec(cmd);
const cmdResp = await exec(cmd);

Check warning

Code scanning / CodeQL

Indirect uncontrolled command line Medium

This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
stderr = cmdResp.stderr;
stdout = cmdResp.stdout
return stderr + "\n" + stdout;
} catch (error) {
console.error(`exec error: ${error}`);
throw new Error(stderr + "\n" + stdout);
}
};

const getBranchStatus = async (req) => {
const getBranchStatus = async (req: any) => {
console.log("Webhook received successfully");

const branchName = req.body?.ref?.split("/").pop();
if (!branchName) {
return 400, "Branch name not found in the request.";
return {result: 400, respMessage: "Branch name not found in the request."};
}
return branchName === process.env.BRANCH_NAME ? await buildProject() : 202, "Build not required.";
return branchName === process.env.BRANCH_NAME ? await buildProject() : {result: 202, respMessage: "Build not required."};
};

const isUpdateRequired = () => {
const currentTime = Date.now();
isMindmapUpdated = (currentTime - mindmapBuildTime) / 1000 / 60 > process.env.MINDMAP_UPDATE_TIME_INTERVAL ? true : false;
isDocumentationWebsiteUpdated = (currentTime - documentationWebsiteBuildTime) / 1000 / 60 > process.env.DOCUMENTATION_WEBSITE_UPDATE_TIME_INTERVAL ? true : false;
const currentTime: number = Date.now();
let mindmapUpdateTimeInterval = parseInt(process.env.MINDMAP_UPDATE_TIME_INTERVAL as string)
let docWebsiteUpdateTimeInterval = parseInt(process.env.DOCUMENTATION_WEBSITE_UPDATE_TIME_INTERVAL as string)

isMindmapUpdated = (currentTime - mindmapBuildTime) / 1000 / 60 > mindmapUpdateTimeInterval ? true : false;
isDocumentationWebsiteUpdated = (currentTime - documentationWebsiteBuildTime) / 1000 / 60 > docWebsiteUpdateTimeInterval ? true : false;
return isMindmapUpdated || isDocumentationWebsiteUpdated;
};

const buildProject = async () => {
const currentTime = Date.now();
if (!isUpdateRequired()) {
if (contributorsBuildRequired || (currentTime - contributorsBuildTime) / 1000 / 60 > process.env.CONTRIBUTORS_UPDATE_TIME_INTERVAL) {
let contUpdateTimeInterval = parseInt(process.env.CONTRIBUTORS_UPDATE_TIME_INTERVAL as string)

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (94% of all statements in
the enclosing function
have an explicit semicolon).
if (contributorsBuildRequired || (currentTime - contributorsBuildTime) / 1000 / 60 > contUpdateTimeInterval) {
console.log("No update required, updating the contributors only");
await initiateBuild("npm run contributor-build", process.env.DOCUMENTATION_WEBSITE_PATH, process.env.DOCUMENTATION_WEBSITE_DEST_PATH);
await initiateBuild("npm run contributor-build", process.env.DOCUMENTATION_WEBSITE_PATH as string, process.env.DOCUMENTATION_WEBSITE_DEST_PATH as string);
contributorsBuildTime = currentTime;
contributorsBuildRequired = false;
return 200;
return {result: 200, respMessage: ""};
} else {
contributorsBuildRequired = true;
return 202, "Contributors build will be done after the next build.";
return {result: 202, respMessage: "Contributors build will be done after the next build."};
}
}
if (isMindmapUpdated) {
console.log("Building Mindmap");
await initiateBuild("npm run build", process.env.MINDMAP_PATH, process.env.MINDMAP_DEST_PATH);
await initiateBuild("npm run build", process.env.MINDMAP_PATH as string, process.env.MINDMAP_DEST_PATH as string);
mindmapBuildTime = currentTime;
isMindmapUpdated = false;
}

if (isDocumentationWebsiteUpdated) {
console.log("Building Documentation Website");
await initiateBuild("npm run build", process.env.DOCUMENTATION_WEBSITE_PATH, process.env.DOCUMENTATION_WEBSITE_DEST_PATH);
await initiateBuild("npm run build", process.env.DOCUMENTATION_WEBSITE_PATH as string, process.env.DOCUMENTATION_WEBSITE_DEST_PATH as string);
documentationWebsiteBuildTime = currentTime;
contributorsBuildTime = currentTime;
isDocumentationWebsiteUpdated = false;
}

return 200, "Build has been created.";
return {result: 200, respMessage: "Build has been created."};
};

const initiateBuild = async (command, projectPath, destPath) => {
const initiateBuild = async (command: string, projectPath: string, destPath: string) => {
await executeCmd(`cd ${projectPath}/ && git pull`);
await executeCmd(`cd ${projectPath}/ && npm ci`);
await executeCmd(`cd ${projectPath}/ && ${command}`);
Expand Down
100 changes: 95 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.0.0",
"description": "This repository is our website deploy and update tool to minimize github api queries.",
"main": "index.js",
"type" : "module",
"scripts": {
"start": "node index.js",
"lint": "eslint --ext=.ts --debug .",
Expand Down Expand Up @@ -36,9 +35,12 @@
"express": "^4.19.2"
},
"devDependencies": {
"@idrinth-api-bench/eslint-config": "https://github.com/idrinth-api-bench/eslint-config#setup-base-config",
"@commitlint/cli": "^19.3.0",
"simple-git-hooks": "^2.11.1"
"@idrinth-api-bench/eslint-config": "https://github.com/idrinth-api-bench/eslint-config#setup-base-config",
"@types/express": "^4.17.21",
"@types/node": "^20.12.12",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.4.5"
},
"engineStrict": true,
"engines": {
Expand Down
Loading

0 comments on commit e9b8460

Please sign in to comment.