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

bug[website-builder]: [issue#1057 correct functions and convert to ts] #6

Open
wants to merge 4 commits into
base: the-one
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 42 additions & 22 deletions index.js → index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
dotenv.config();

const app = express();
const port = process.env.PORT || 3000;
const port = process.env.PORT || '3333';
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
const secret = process.env.GITHUB_SECRET || "defaultKey";
Fixed Show fixed Hide fixed
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved

let isDocumentationWebsiteUpdated = false;
let isMindmapUpdated = false;
Expand All @@ -20,64 +21,83 @@

app.post("/webhook", async (req, res) => {
console.log("req receieved");
const signature = req.headers["x-hub-signature"];
const signature = req.headers["x-hub-signature"] as string;
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
console.log(signature)
Fixed Show fixed Hide fixed
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
console.log("header",req.headers)
const payload = JSON.stringify(req.body);

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

if (crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(calculatedSignature))) {
const { result, respMessage } = await getBranchStatus();
const {result, respMessage } = await getBranchStatus(req.body);
console.log("Result: ", result);
res.status(result).send(respMessage);
res.status(200).send({ result, respMessage });
} else {
res.status(400).send("Invalid Github signature");
}
});

app.listen(process.env.PORT, () => {
interface BranchStatus {
result: number | string;
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
respMessage: string ;
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
}

Idrinth marked this conversation as resolved.
Show resolved Hide resolved
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});

const executeCmd = async (cmd) => {
const executeCmd = async (cmd: string) => {
try {
const { stdout, stderr } = await exec(cmd);
const {stdout, stderr} = await exec(cmd);
Fixed Show fixed Hide fixed
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
Dismissed Show dismissed Hide dismissed
return stderr + "\n" + stdout;
} catch (error) {
} catch (error: any) {
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
console.error(`exec error: ${error}`);
throw new Error(stderr + "\n" + stdout);
throw new Error(error.stderr + "\n" + error.stdout);
}
};

const getBranchStatus = async (req) => {
async function getBranchStatus(req: any): Promise<BranchStatus> {
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
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." };
}

if (branchName === process.env.BRANCH_NAME) {
const { status, message } = await buildProject();
return { result: status, respMessage: message };
} else {
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
return { result: 200, respMessage: "Build not required." };
}
return branchName === process.env.BRANCH_NAME ? await buildProject() : 202, "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 mindMapUpdateInterval = parseInt("process.env.MINDMAP_UPDATE_TIME_INTERVAL", 10); // converted to number uusing variable
const documentationWebsiteUpdateInterval = parseInt("process.env.DOCUMENTATION_WEBSITE_UPDATE_TIME_INTERVAL", 10); // converted to num using variable
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved

isMindmapUpdated = (currentTime - mindmapBuildTime) / 1000 / 60 > mindMapUpdateInterval ? true : false;
isDocumentationWebsiteUpdated = (currentTime - documentationWebsiteBuildTime) / 1000 / 60 > documentationWebsiteUpdateInterval ? true : false;
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
return isMindmapUpdated || isDocumentationWebsiteUpdated;
};

const buildProject = async () => {
const buildProject = async (): Promise<{ status: number; message: string }> => {
Idrinth marked this conversation as resolved.
Show resolved Hide resolved
const currentTime = Date.now();
const contributionUpdateTimeInterval = parseInt('process.env.CONTRIBUTORS_UPDATE_TIME_INTERVAL', 10); // adjusted to variable
if (!isUpdateRequired()) {
if (contributorsBuildRequired || (currentTime - contributorsBuildTime) / 1000 / 60 > process.env.CONTRIBUTORS_UPDATE_TIME_INTERVAL) {
if (contributorsBuildRequired || (currentTime - contributorsBuildTime) / 1000 / 60 > contributionUpdateTimeInterval) {
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);
contributorsBuildTime = currentTime;
contributorsBuildRequired = false;
return 200;
return { status: 200, message: "Contributors build has been created." }
} else {
contributorsBuildRequired = true;
return 202, "Contributors build will be done after the next build.";
return { status: 202, message: "Contributors build will be done after the next build." } // adjusted return value
}
}
if (isMindmapUpdated) {
Expand All @@ -95,12 +115,12 @@
isDocumentationWebsiteUpdated = false;
}

return 200, "Build has been created.";
return {status: 200, message: "Contributors build will be done after the next build."};
};

const initiateBuild = async (command, projectPath, destPath) => {
const initiateBuild = async (command:any, projectPath: any, destPath: any) => {
aceppaluni marked this conversation as resolved.
Show resolved Hide resolved
await executeCmd(`cd ${projectPath}/ && git pull`);
await executeCmd(`cd ${projectPath}/ && npm ci`);
await executeCmd(`cd ${projectPath}/ && ${command}`);
await executeCmd(`cp -r ${projectPath}/dist/ ${destPath}/`);
};
};
Idrinth marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "This repository is our website deploy and update tool to minimize github api queries.",
"main": "index.js",
"type" : "module",
"type": "module",
"scripts": {
"start": "node index.js",
"lint": "eslint --ext=.ts --debug .",
Expand Down Expand Up @@ -36,8 +36,8 @@
"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",
"@idrinth-api-bench/eslint-config": "https://github.com/idrinth-api-bench/eslint-config#setup-base-config",
"simple-git-hooks": "^2.11.1"
},
"engineStrict": true,
Expand Down
Loading