Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 5f9ea12

Browse files
committed
Add option to generate a report from a dump file
1 parent 953e725 commit 5f9ea12

File tree

3 files changed

+81
-32
lines changed

3 files changed

+81
-32
lines changed

src/gtt-report.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ program
5757
.option('--check_token', 'check the access token')
5858
.option('--show_without_times', 'show issues/merge requests without time records')
5959
.option('-p --proxy <proxy>', 'use a proxy server with the given url')
60+
.option('--from_dump <file>', 'instead of querying gitlab, use data from the given dump file')
6061
.parse(process.argv);
6162

6263
// init helpers
6364
let config = new Config(process.cwd());
6465
let cli = new Cli(program.args);
6566

67+
// if using a dump, set the config accordingly
68+
if (program.from_dump && fs.existsSync(program.from_dump)) {
69+
let data = JSON.parse(fs.readFileSync(program.from_dump));
70+
71+
if (data.data) _.each(data.data, (v, i) => {
72+
config.set(i, v);
73+
});
74+
75+
if (data._dump) config.dump = data._dump;
76+
}
77+
6678
// overwrite config with args and opts
6779
config
6880
.set('project', cli.project())
@@ -95,12 +107,12 @@ config
95107
.set('type', program.type)
96108
.set('subgroups', program.subgroups)
97109
.set('_verbose', program.verbose)
98-
.set('_checkToken', program.check_token);
110+
.set('_checkToken', program.check_token)
111+
.set('_createDump', program.output === 'dump');
99112

100113
Cli.quiet = config.get('quiet');
101114
Cli.verbose = config.get('_verbose');
102115

103-
104116
// create stuff
105117
let reports = new ReportCollection(config),
106118
master = new Report(config),

src/models/base.js

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const request = require('request-promise-native');
22
const url = require('url');
33
const async = require('async');
4+
const crypto = require('crypto');
45

56
/**
67
* base model
@@ -29,17 +30,25 @@ class base {
2930
* @returns {*}
3031
*/
3132
post(path, data) {
33+
let key = base.createDumpKey(path, data);
34+
if (this.config.dump) return this.getDump(key);
35+
3236
data.private_token = this.token;
3337

34-
return request.post(`${this.url}${path}`, {
35-
json: true,
36-
body: data,
37-
insecure: this._insecure,
38-
proxy: this._proxy,
39-
resolveWithFullResponse: true,
40-
headers: {
41-
'PRIVATE-TOKEN': this.token
42-
}
38+
return new Promise((resolve, reject) => {
39+
request.post(`${this.url}${path}`, {
40+
json: true,
41+
body: data,
42+
insecure: this._insecure,
43+
proxy: this._proxy,
44+
resolveWithFullResponse: true,
45+
headers: {
46+
'PRIVATE-TOKEN': this.token
47+
}
48+
}).then(response => {
49+
if (this.config.get('_createDump')) this.setDump(response, key);
50+
resolve(response);
51+
}).catch(e => reject(e));
4352
});
4453
}
4554

@@ -51,17 +60,25 @@ class base {
5160
* @returns {Promise}
5261
*/
5362
get(path, page = 1, perPage = this._perPage) {
63+
let key = base.createDumpKey(path, page, perPage);
64+
if (this.config.dump) return this.getDump(key);
65+
5466
path += (path.includes('?') ? '&' : '?') + `private_token=${this.token}`;
5567
path += `&page=${page}&per_page=${perPage}`;
5668

57-
return request(`${this.url}${path}`, {
58-
json: true,
59-
insecure: this._insecure,
60-
proxy: this._proxy,
61-
resolveWithFullResponse: true,
62-
headers: {
63-
'PRIVATE-TOKEN': this.token
64-
}
69+
return new Promise((resolve, reject) => {
70+
request(`${this.url}${path}`, {
71+
json: true,
72+
insecure: this._insecure,
73+
proxy: this._proxy,
74+
resolveWithFullResponse: true,
75+
headers: {
76+
'PRIVATE-TOKEN': this.token
77+
}
78+
}).then(response => {
79+
if (this.config.get('_createDump')) this.setDump(response, key);
80+
resolve(response);
81+
}).catch(e => reject(e));
6582
});
6683
}
6784

@@ -124,6 +141,29 @@ class base {
124141
}, runners);
125142
}
126143

144+
/**
145+
* save the given response to dump
146+
* @param response
147+
* @param key
148+
*/
149+
setDump(response, key) {
150+
if (!this.config._dump) this.config._dump = {};
151+
152+
this.config._dump[key] = {
153+
headers: response.headers,
154+
body: response.body
155+
};
156+
}
157+
158+
/**
159+
* get from dump
160+
* @param key
161+
* @returns {Promise}
162+
*/
163+
getDump(key) {
164+
return new Promise(r => r(this.config.dump[key]));
165+
}
166+
127167
/**
128168
* create a task list to get all pages from
129169
* the given path
@@ -142,6 +182,14 @@ class base {
142182

143183
return tasks;
144184
}
185+
186+
/**
187+
* create a key representing a request
188+
* @param args
189+
*/
190+
static createDumpKey(...args) {
191+
return crypto.createHash('md5').update(JSON.stringify(args)).digest("hex");
192+
}
145193
}
146194

147195
module.exports = base;

src/output/dump.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,11 @@ class dump extends Base {
77

88
config.set('url', null, true);
99
config.set('token', null, true);
10+
config.set('_createDump', false);
1011
config.workDir = null;
1112
config.cache = null;
1213

13-
let dump = {config};
14-
15-
['issues', 'mergeRequests'].forEach(type => {
16-
dump[type] =
17-
report[type].map(resource => {
18-
return {
19-
data: resource.data,
20-
notes: resource.notes
21-
};
22-
});
23-
});
24-
25-
this.write(JSON.stringify(dump));
14+
this.write(JSON.stringify(config));
2615
}
2716

2817
makeStats() {

0 commit comments

Comments
 (0)