forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.js
More file actions
executable file
·177 lines (146 loc) · 4.47 KB
/
report.js
File metadata and controls
executable file
·177 lines (146 loc) · 4.47 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
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
const _ = require('underscore');
const moment = require('moment');
const Base = require('./base');
const Issue = require('./issue');
const MergeRequest = require('./mergeRequest');
const Project = require('./project');
/**
* report model
*/
class report extends Base {
/**
* constructor.
* @param config
* @param project
*/
constructor(config, project) {
super(config);
this.projects = {};
this.setProject(project);
this.issues = [];
this.mergeRequests = [];
}
/**
* get params for querying issues and merge requests
* @returns {string}
*/
params() {
let params = [];
if (this.config.get('iids') && this.config.get('query').length === 1) {
params.push(`iids=${this.config.get('iids').join(',')}`)
}
if (!this.config.get('closed')) {
params.push(`state=opened`);
}
if (this.config.get('includeByLabels')) {
params.push(`labels=${this.config.get('includeByLabels').join(',')}`);
}
if (this.config.get('milestone')) {
params.push(`milestone=${this.config.get('milestone')}`);
}
return `?${params.join('&')}`;
}
/**
* set the project by the given data
* @param project
*/
setProject(project) {
if (!project) return;
this.projects[project.id] = project.path_with_namespace;
this.project = new Project(this.config, project)
}
/**
* query and set the project
* @returns {Promise}
*/
getProject() {
let promise = this.get(`projects/${encodeURIComponent(this.config.get('project'))}`);
promise.then(project => this.setProject(project.body));
return promise;
}
/**
* query and set merge requests
* @returns {Promise}
*/
getMergeRequests() {
let promise = this.all(`projects/${this.project.id}/merge_requests${this.params()}`);
promise.then(mergeRequests => this.mergeRequests = mergeRequests);
return promise;
}
/**
* query and set issues
* @returns {Promise}
*/
getIssues() {
let promise = this.all(`projects/${this.project.id}/issues${this.params()}`);
promise.then(issues => this.issues = issues);
return promise;
}
/**
* filter empty
* @param issues
* @returns {Array}
*/
filter(issues) {
return issues.filter(issue => this.config.get('showWithoutTimes') || (issue.times && issue.times.length > 0));
}
/**
* process the given input
* @param input
* @param model
* @param advance
* @returns {*|Promise}
*/
process(input, model, advance = false) {
let collect = [];
let promise = this.parallel(this[input], (data, done) => {
let item = new model(this.config, data);
item.project_namespace = this.projects[item.project_id];
item.getNotes()
.then(() => item.getTimes())
.catch(error => done(error))
.catch(error => done(error))
.then(() => item.getStats())
.catch(error => done(error))
.then(() => {
if (this.config.get('showWithoutTimes') || item.times.length > 0) {
collect.push(item);
}
if (advance) advance();
return done();
});
// collect items, query times & stats
collect.push();
});
promise.then(() => this[input] = this.filter(collect));
return promise;
}
/**
* merge another report into this report
* @param report
*/
merge(report) {
this.issues = this.issues.concat(report.issues);
this.mergeRequests = this.mergeRequests.concat(report.mergeRequests);
if (!this.members) this.members = [];
this.members = this.members.concat(report.members ? report.members : []);
this.projects = Object.assign(this.projects, report.projects);
}
/**
* process issues
* @param advance
* @returns {Promise}
*/
processIssues(advance = false) {
return this.process('issues', Issue, advance);
}
/**
* process merge requests
* @param advance
* @return {Promise}
*/
processMergeRequests(advance = false) {
return this.process('mergeRequests', MergeRequest, advance);
}
}
module.exports = report;