Skip to content
511 changes: 511 additions & 0 deletions README.md

Large diffs are not rendered by default.

Binary file added images/Screenshot 2024-07-20 181055.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Screenshot 2024-07-20 181326.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/alpha bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/alpha-bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/bot install.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/closed-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/create-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/deployment-complete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/dev settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/downloaded pvt keys.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/events subscription.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/github app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/github webhook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/install on repo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/installing the app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/new bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/ngrok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pr-being-deployed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/private key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/public page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/read and write.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/realstepa.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/realstepb.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/realstepc.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/repo perm.png
Binary file added images/stepa.PNG
Binary file added images/stepb.PNG
Binary file added images/stepd.png
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ app.post('/webhook', async (req, res) => {
const prNumber = req.body.number;
const branchName = req.body.pull_request.head.ref;
const repoName = req.body.repository.full_name;
const forkName = req.body.pull_request.head.repo.full_name;
const installationId = req.body.installation.id

const repoUrl = `https://github.com/${repoName}.git`;
const repoBranchName = `${repoName.replace('/', '_')}_${branchName.replace('/', '_')}`;
console.log(repoBranchName)
const imageName = repoBranchName.toLowerCase();
const containerName = `${imageName}_pr_${prNumber}`.toLowerCase();
const forkRepoUrl = `https://github.com/${forkName}.git`;

if (action === 'opened' || action === 'reopened' || action === 'synchronize') {
try {
const preDeployMessage = 'The PR is currently being deployed...'
await addPRComment(repoName, prNumber, preDeployMessage);
await addPRComment(repoName, prNumber, preDeployMessage, installationId);

const postDeployMessage = await triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName);
await addPRComment(repoName, prNumber, postDeployMessage);
const postDeployMessage = await triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName, forkRepoUrl);
await addPRComment(repoName, prNumber, postDeployMessage, installationId);

console.log("Deployment completed and comment added!")
} catch (error) {
Expand All @@ -51,7 +54,7 @@ app.post('/webhook', async (req, res) => {
else if (action === 'closed') {
try {
const message = await removeDeployedContainer(containerName, imageName);
await addPRComment(repoName, prNumber, message);
await addPRComment(repoName, prNumber, message, installationId);
console.log('Container removed and comment added')
} catch (error) {
console.error('Error during deployment:', error);
Expand Down
39 changes: 30 additions & 9 deletions services/deploymentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function runCommand(command, args, cwd) {
}


export async function triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName) {
export async function triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName, forkRepoUrl) {
const containerPort = process.env.containerPort || 5000
const hostPort = 9000 + (prNumber % 1000);
const folderName = imageName
Expand All @@ -37,18 +37,39 @@ export async function triggerDeployment(repoUrl, branchName, prNumber, imageName

if (folderExists) {
console.log('Repository folder exists. Changing directory and checking out branch...');
await runCommand(`cd ${folderName} && git checkout`, [branchName]);

await runCommand(`cd ${folderName} && git fetch --all`);
await runCommand(`cd ${folderName} && git branch -a`); // List all branches for debugging

// Check if the branch exists locally
try {
await runCommand(`cd ${folderName} && git checkout ${branchName}`);
} catch (error) {
console.log(`Branch ${branchName} not found locally. Fetching from remote...`);
if (forkRepoUrl !== repoUrl) {
await runCommand(`cd ${folderName} && git fetch ${forkRepoUrl} ${branchName}:${branchName}`);
await runCommand(`cd ${folderName} && git checkout ${branchName}`);
} else {
throw new Error(`Branch ${branchName} not found and forkRepoUrl is not provided`);
}
}

console.log('Pulling the remote branch for updates')
await runCommand(`git pull origin`, [branchName]);
} else {
await runCommand(`cd ${folderName} && git checkout ${branchName} && git pull origin ${branchName}`);

} else {

console.log('Repository folder does not exist. Cloning repository...');
await runCommand(`git clone`, [repoUrl, `${folderName}`], '.');

console.log('Checking out branch...');
await runCommand('git checkout', [branchName], `${folderName}`);
}


if (forkRepoUrl !== repoUrl) {
await runCommand(`cd ${folderName} && git fetch ${forkRepoUrl} ${branchName}:${branchName}`);
await runCommand(`cd ${folderName} && git checkout ${branchName}`);
} else {
await runCommand('git checkout', [branchName], `${folderName}`);
}
}

console.log('Building Docker image...');
await runCommand('docker build -t', [imageName, '.'], `${folderName}`);

Expand Down
5 changes: 2 additions & 3 deletions services/repositoryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import path from "path";
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
const APP_ID = process.env.APP_ID;
const PRIVATE_KEY_PATH = process.env.PRIVATE_KEY_PATH;
const INSTALLATION_ID = process.env.INSTALLATION_ID;

let encoder = new TextEncoder();

export async function addPRComment(repoName, prNumber, message) {
export async function addPRComment(repoName, prNumber, message, installationId) {

const privateKeyPath = path.resolve(PRIVATE_KEY_PATH);
const privateKey = fs.readFileSync(privateKeyPath, "utf8");
Expand All @@ -21,7 +20,7 @@ export async function addPRComment(repoName, prNumber, message) {
auth: {
appId: APP_ID,
privateKey: privateKey,
installationId: INSTALLATION_ID,
installationId,
},
});
const url = `/repos/${repoName}/issues/${prNumber}/comments`;
Expand Down