-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
222 lines (181 loc) · 6.34 KB
/
app.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
let express = require('express'),
bodyParser = require('body-parser'),
errorhandler = require('errorhandler'),
morgan = require('morgan'),
compression = require('compression'),
fs = require('fs'),
path = require('path'),
yaml = require('js-yaml');
const injectiblesFilePath = '../config/';
// Import the plugin functions
const projectCreateEvent = require("./events/project_create");
const url = process.env.GITLAB_URL,
accessToken = process.env.GITLAB_ACCESS_TOKEN,
defaultBranch = process.env.GITLAB_DEFAULT_BRANCH || 'main',
configFilePath = process.env.CONFIG_FILE_PATH || 'config.yml',
port = process.env.LISTEN_PORT || 3002;
// Map event names to corresponding plugin functions
const eventPlugins = {
project_create: projectCreateEvent
};
const { Gitlab } = require('@gitbeaker/rest');
const api = new Gitlab({
host: url,
token: accessToken
});
let projectConfigs,
webserver,
server,
serverPromise; // Add a variable to keep track of the server promise
verifyEnvironment();
watchConfigFile();
console.log(`Connecting to GitLab instance: ${url} `);
getUserInfo();
// Export a function to get the loaded project configurations
module.exports.getProjectConfigs = function () {
return projectConfigs;
};
// Functions
function loadConfig() {
try {
const fullConfigPath = path.join(injectiblesFilePath, configFilePath);
const configFile = fs.readFileSync(fullConfigPath, 'utf8');
const loadedConfigs = yaml.load(configFile, { schema: yaml.JSON_SCHEMA });
loadedConfigs.forEach((gic) => {
if (gic.regex) {
gic.regex = new RegExp(gic.regex); // Convert the regex string to a regular expression
}
});
// Update the projectConfigs with the new loaded configurations
projectConfigs = loadedConfigs;
console.log('Configuration file loaded successfully.');
} catch (err) {
console.error('Error loading configuration file:', err);
console.log('Check that a valid Personal Access Token is provided to GITLAB_ACCESS_TOKEN.');
}
}
function watchConfigFile() {
fs.watchFile(configFilePath, (curr, prev) => {
if (curr.mtime !== prev.mtime) {
console.log('Configuration file has changed. Reloading...');
if (serverPromise) {
// If the server promise exists, it means the server is running, so stop it and reload the configuration
serverPromise.then(() => {
stopWebserver(() => {
loadConfig(); // Simply reload the configuration without checking the return value
startWebserver();
});
});
} else {
// If the server promise does not exist, the server is not running, so simply reload the configuration and start the server
loadConfig(); // Simply reload the configuration without checking the return value
startWebserver();
}
}
});
}
function verifyEnvironment() {
if (typeof url == 'undefined') {
console.log('GITLAB_URL not set. Please provide the url of the instance you wish to connect to.');
process.exit(1);
}
if (typeof accessToken == 'undefined') {
console.log('GITLAB_ACCESS_TOKEN not set. Please generate a Personal Access Token for the account you wish to connect.');
process.exit(1);
}
if (typeof process.env.GITLAB_DEFAULT_BRANCH == 'undefined') {
console.log('GITLAB_DEFAULT_BRANCH not set, defaulting to Main.');
}
if (typeof process.env.LISTEN_PORT == 'undefined') {
console.log('LISTEN_PORT not set, defaulting to 3002.');
}
console.log('Environment verification successful.');
}
process.on('SIGINT', () => {
console.log('Received SIGINT signal. Stopping the web server...');
stopWebserver();
process.exit(0);
});
function startWebserver() {
webserver = express();
webserver.use(
errorhandler({
dumpExceptions: true,
showStack: true,
})
);
webserver.use(morgan('combined'));
webserver.use(compression());
// parse various different custom JSON types as JSON
webserver.use(bodyParser.json({ type: 'application/json' }));
webserver.get('/', function (req, res) {
res.send('Hello, please give me a POST!\n');
});
// Route to handle the '/config' endpoint
webserver.get('/config', function (req, res) {
// Generate the human-friendly output
const formattedOutput = projectConfigs.map((config, index) => {
return `
Project Config ${index + 1}:
Regex: ${config.regex}
Commit Message: ${config.commit.message}
Groups: ${JSON.stringify(config.groups)}
Commit Paths: ${JSON.stringify(config.commit.paths)}
`;
}).join('\n');
// Send the formatted output back to the client
res.send(`<pre>${formattedOutput}</pre>`);
});
webserver.post('/', function (req, res) {
const eventName = req.body['event_name'];
const eventData = req.body;
// Check if the event has a corresponding plugin
if (eventPlugins[eventName]) {
const pluginFunction = eventPlugins[eventName];
if (typeof pluginFunction === 'function') {
pluginFunction(eventData, projectConfigs, api, defaultBranch, () => {
res.send('Project Create Event handled successfully.');
});
}
} else {
console.log(`No plugin found for event: ${eventName}`);
}
});
// Start the server and save the promise
serverPromise = new Promise((resolve) => {
server = webserver.listen(port, () => {
console.log('Webserver is listening on ' + port);
resolve(server); // Resolve the promise with the server instance
});
});
}
// Function to stop the web server
function stopWebserver(callback) {
if (serverPromise) {
serverPromise.then((server) => {
server.close(() => {
console.log('Web server has been stopped.');
if (callback) {
callback();
}
});
});
}
}
// Functions for handling project_create event
function getProjectConfig(projectName) {
// Load and return the project configuration based on the projectName
const projectConfig = projectConfigs.find(
(gic) => gic.regex && gic.regex.test(projectName)
);
return projectConfig;
}
async function getUserInfo() {
try {
let user = await api.Users.showCurrentUser(); // Gives you your userId
console.log(`Success. Connected. Authenticated user: ${user.name} (${user.username})`);
} catch (error) {
console.error('Error connecting to GitLab:', error);
}
}