Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add option to generate a report from a dump file
  • Loading branch information
kriskbx committed Jan 28, 2018
commit 5f9ea12d159861fe7d89be0494fdd2dfab006ef9
16 changes: 14 additions & 2 deletions src/gtt-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ program
.option('--check_token', 'check the access token')
.option('--show_without_times', 'show issues/merge requests without time records')
.option('-p --proxy <proxy>', 'use a proxy server with the given url')
.option('--from_dump <file>', 'instead of querying gitlab, use data from the given dump file')
.parse(process.argv);

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

// if using a dump, set the config accordingly
if (program.from_dump && fs.existsSync(program.from_dump)) {
let data = JSON.parse(fs.readFileSync(program.from_dump));

if (data.data) _.each(data.data, (v, i) => {
config.set(i, v);
});

if (data._dump) config.dump = data._dump;
}

// overwrite config with args and opts
config
.set('project', cli.project())
Expand Down Expand Up @@ -95,12 +107,12 @@ config
.set('type', program.type)
.set('subgroups', program.subgroups)
.set('_verbose', program.verbose)
.set('_checkToken', program.check_token);
.set('_checkToken', program.check_token)
.set('_createDump', program.output === 'dump');

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


// create stuff
let reports = new ReportCollection(config),
master = new Report(config),
Expand Down
82 changes: 65 additions & 17 deletions src/models/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const request = require('request-promise-native');
const url = require('url');
const async = require('async');
const crypto = require('crypto');

/**
* base model
Expand Down Expand Up @@ -29,17 +30,25 @@ class base {
* @returns {*}
*/
post(path, data) {
let key = base.createDumpKey(path, data);
if (this.config.dump) return this.getDump(key);

data.private_token = this.token;

return request.post(`${this.url}${path}`, {
json: true,
body: data,
insecure: this._insecure,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
return new Promise((resolve, reject) => {
request.post(`${this.url}${path}`, {
json: true,
body: data,
insecure: this._insecure,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
}).then(response => {
if (this.config.get('_createDump')) this.setDump(response, key);
resolve(response);
}).catch(e => reject(e));
});
}

Expand All @@ -51,17 +60,25 @@ class base {
* @returns {Promise}
*/
get(path, page = 1, perPage = this._perPage) {
let key = base.createDumpKey(path, page, perPage);
if (this.config.dump) return this.getDump(key);

path += (path.includes('?') ? '&' : '?') + `private_token=${this.token}`;
path += `&page=${page}&per_page=${perPage}`;

return request(`${this.url}${path}`, {
json: true,
insecure: this._insecure,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
return new Promise((resolve, reject) => {
request(`${this.url}${path}`, {
json: true,
insecure: this._insecure,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
}).then(response => {
if (this.config.get('_createDump')) this.setDump(response, key);
resolve(response);
}).catch(e => reject(e));
});
}

Expand Down Expand Up @@ -124,6 +141,29 @@ class base {
}, runners);
}

/**
* save the given response to dump
* @param response
* @param key
*/
setDump(response, key) {
if (!this.config._dump) this.config._dump = {};

this.config._dump[key] = {
headers: response.headers,
body: response.body
};
}

/**
* get from dump
* @param key
* @returns {Promise}
*/
getDump(key) {
return new Promise(r => r(this.config.dump[key]));
}

/**
* create a task list to get all pages from
* the given path
Expand All @@ -142,6 +182,14 @@ class base {

return tasks;
}

/**
* create a key representing a request
* @param args
*/
static createDumpKey(...args) {
return crypto.createHash('md5').update(JSON.stringify(args)).digest("hex");
}
}

module.exports = base;
15 changes: 2 additions & 13 deletions src/output/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@ class dump extends Base {

config.set('url', null, true);
config.set('token', null, true);
config.set('_createDump', false);
config.workDir = null;
config.cache = null;

let dump = {config};

['issues', 'mergeRequests'].forEach(type => {
dump[type] =
report[type].map(resource => {
return {
data: resource.data,
notes: resource.notes
};
});
});

this.write(JSON.stringify(dump));
this.write(JSON.stringify(config));
}

makeStats() {
Expand Down