-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (75 loc) · 2.51 KB
/
index.js
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
require("dotenv").config();
const http = require("http");
const appConfig = require("./src/config/app.config");
const gitConfig = require("./src/config/git.config");
const gitService = require("./src/services/git.service");
const httpUtils = require("./src/utils/http.util");
const gitUtils = require("./src/utils/git.util");
const commonUtils = require("./src/utils/common.util");
const { logger } = require("./src/utils/logger.util");
const validateWebhookToken = (req) => {
const webhookToken = req.headers["x-gitlab-token"];
if (!webhookToken || webhookToken !== gitConfig.WEBHOOK_TOKEN) {
throw new Error("Invalid webhook token");
}
};
const discardAndResetRepo = (repo, branch) =>
commonUtils.executeWithLogging(
async () => {
await repo.discardAndResetRepo(branch);
},
`Discarding and resetting local changes for ${repo.sourceRepoUrl} - ${branch}`,
`Local changes discarded and resetted for ${repo.sourceRepoUrl} - ${branch}`
);
const gitPushToTarget = (repo, branch) =>
commonUtils.executeWithLogging(
async () => {
await repo.gitPushToTarget(branch);
},
`Pushing to target for ${repo.sourceRepoUrl} - ${branch}`,
`Push successful for ${repo.sourceRepoUrl} - ${branch}`
);
const gitFetchFromSource = (repo, branch) =>
commonUtils.executeWithLogging(
async () => {
await repo.gitFetchFromSource(branch);
},
`Fetching from source for ${repo.sourceRepoUrl} - ${branch}`,
`Fetch successful`
);
const handleGitFlow = async (repo, branch) => {
await discardAndResetRepo(repo, branch);
await gitFetchFromSource(repo, branch);
await gitPushToTarget(repo, branch);
};
const handleRequest = async (req, res) => {
try {
validateWebhookToken(req);
const body = await httpUtils.getRequestBody(req);
const branch = gitUtils.getBranchName(body);
const repo = gitService.getRepoBySourceUrl(body.repository.git_http_url);
await handleGitFlow(repo, branch);
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("ok");
} catch (error) {
res.writeHead(400);
logger.error(error);
}
res.end();
};
const startServer = () => {
http.createServer(handleRequest).listen(appConfig.PORT, async () => {
logger.info(`Jumphost version ${appConfig.VERSION}`);
await gitService.initRepositories(false);
logger.info("Git init done");
logger.info(`Server is listening on port ${appConfig.PORT}`);
});
};
(async () => {
try {
startServer();
} catch (error) {
logger.error(error);
process.exit(1);
}
})();