-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·80 lines (71 loc) · 2.33 KB
/
index.js
File metadata and controls
executable file
·80 lines (71 loc) · 2.33 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
const path = require('path');
const crypto = require('crypto');
const waitOn = require('wait-on');
const elasticsearch = require('elasticsearch');
const gitlabModule = require('gitlab');
const config = require('./config');
const waitOpts = {
resources: [config.elasticsearch.url || 'elasticsearch:9200'],
delay: 1000,
interval: 1000,
timeout: 100000,
// log: true,
// verbose: true,
};
waitOn(waitOpts, (err) => {
if (err) {
console.error('Error connecting to elasticsearch instance.');
return;
}
console.log('Connection to elasticsearch successful.');
console.log('Starting import...');
const INDEX = config.elasticsearch.index || 'gitlab_statistics';
const TIMESTAMP = (new Date()).toISOString();
const gitlab = gitlabModule({
url: config.gitlab.url,
token: config.gitlab.token,
});
const es = new elasticsearch.Client({
host: config.elasticsearch.url || 'elasticsearch:9200',
log: config.elasticsearch.log || ['error', 'warning'],
});
const generateHash = input => crypto.createHash('sha256').update(input).digest('hex');
// es.indices.delete({ index: INDEX });
gitlab.projects.all((projects) => {
projects.forEach((project) => {
if (project) {
const projectPath = project.path_with_namespace;
const opts = { recursive: true };
gitlab.projects.repository.listTree(projectPath, opts, (repository) => {
if (repository) {
const files = [];
repository.forEach((file) => {
if (file) {
if (file.type === 'blob') {
files.push({
index: {
_index: INDEX,
_type: projectPath,
_id: generateHash(`${TIMESTAMP}_${projectPath}_${file.id}`),
},
});
files.push({
uniqueFileId: generateHash(`${projectPath}_${file.id}`),
id: file.id,
name: file.name,
path: file.path,
ext: path.extname(file.name),
mode: file.mode,
timestamp: TIMESTAMP,
});
}
}
});
es.bulk({ body: files });
}
});
}
});
console.log('Import finished.');
});
});