-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (134 loc) · 4.43 KB
/
index.js
File metadata and controls
141 lines (134 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const path = require("path");
require("dotenv").config({ path: path.join(__dirname, ".env") });
const express = require("express");
const { Octokit } = require("@octokit/rest");
const _exec = require("child_process").exec;
const util = require("util");
const shell = require("shelljs");
const colors = require("chalk");
const octokit = new Octokit({
auth: process.env.GH_ACCESS_TOKEN,
userAgent: "Longshot Dev Services/1.0",
timeZone: "America/Los_Angeles",
});
let server = express();
/*
Using the same layout as github's sinatra
*/
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
server.get("/", (r, rs) => {
rs.send("deployment main page.");
});
let fuckyou;
server.post("/deploy", async (req, res) => {
let { payload } = req.body;
console.log(payload);
payload = JSON.parse(payload);
const action = req.header("X-GitHub-Event");
console.log(action);
switch (action) {
case "pull_request":
if (payload["action"] == "closed" && payload["pull_request"]["merged"]) {
await start_deployment(payload["pull_request"], "Development");
console.log("YES YES WE ARE FUCKING HERE");
}
break;
case "deployment":
await process_deployment(payload);
break;
case "deployment_status":
update_deployment_status(payload);
break;
default:
console.log(`Recived ${action} action. `);
break;
}
res.sendStatus(200);
});
async function start_deployment(pr, type) {
user = pr["user"]["login"];
payload = {
environment: type,
deploy_user: user,
};
await octokit.repos.createDeployment({
owner: pr["head"]["repo"]["owner"]["login"],
repo: pr["head"]["repo"]["name"],
ref: pr["head"]["sha"],
auto_merge: false,
});
}
async function process_deployment(payload) {
let { deployment, repository } = payload;
console.log("Processing Deployments.");
// PROCESS ENABLE
setTimeout(async () => {
await octokit.repos.createDeploymentStatus({
owner: repository["owner"]["login"],
repo: repository["name"],
deployment_id: deployment["id"],
state: "pending",
});
}, 1000);
console.log("pending state");
if (type == "Development") {
await deploy_stage();
} else {
await deploy_production();
}
setTimeout(async () => {
octokit.repos.createDeploymentStatus({
owner: repository["owner"]["login"],
repo: repository["name"],
deployment_id: deployment["id"],
state: "success",
});
}, 30000);
}
function update_deployment_status(payload) {
// console.log(`Deployment status for ${fuckyou['id']} is ${payload['state' ]}`);
console.log(
`🚧 Deployment Status: ${payload["deployment"]["environment"]} | ${payload["deployment_status"]["state"]}`
);
}
// rice bot dir is /home/pi/.microservices/rice-bot
async function deploy_stage() {
// const DIR = process.cwd() + "/NiggaBonkHead";
// const repoURL = "https://github.com/longshotdev/rice-bot.git";
// const version = "v0.1";
// console.log(DIR);
// console.log(colors.green("Initalizing Local Repo"));
// await exec(`git init`, { cwd: DIR });
// console.log(colors.green("Add Remote"));
// try {
// await exec(`git remote add ship ${repoURL}`, { cwd: DIR });
// console.log(colors.green("Fetch"));
// await exec(`git fetch ship --prune "refs/tags/*:refs/tags/*"`, {
// cwd: DIR,
// });
// } catch (e) {}
// console.log(colors.green("Checking out"));
// await exec(`git checkout ${version}`, { cwd: DIR });
// console.log(colors.green("Resetting"));
// await exec(`git reset --hard`, { cwd: DIR }, execCB);
// console.log(colors.green("Cleaning"));
// await exec(`git clean -df`, { cwd: DIR }, execCB);
// console.log(colors.green("Pulling."));
// await exec(`git pull -f ship ${version}`, { cwd: DIR }, execCB);
// await exec(`yarn --production`, { cwd: DIR }, execCB);
// function execCB(err, stdout, stderr) {
// if (stdout) console.log(stdout);
// if (stderr) console.log(stderr);
// if (err) console.log(err);
// }
shell.cd("..");
await shell.exec("~/scripts/deploy_stage", { async: true });
}
async function deploy_production() {
shell.cd("..");
await shell.exec("~/scripts/deploy_prod", { async: true });
}
server.listen(3000, () => {
console.log("🚀 Rice Deployment listening on port 3000!");
});