forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask.js
More file actions
212 lines (173 loc) 路 6.17 KB
/
Copy pathtask.js
File metadata and controls
212 lines (173 loc) 路 6.17 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
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
import dayjs from '../dayjs.js';
import GitlabClient from './gitlab-client.js';
import Time from '../time.js';
import chargeRatio from '../billing.js';
import Project from '../../reporting/api/project.js';
class Task {
/**
* @param config
* @param data
* @param client
* @param type
* @param {Project} [project] the owning project's, if known
*/
constructor(config, data = {}, client = new GitlabClient(config), type, project) {
this.config = config;
this.client = client;
this.times = [];
this.data = data;
this.type = type;
this.project = project;
}
get iid() {
return this.data.iid;
}
get id() {
return this.data.id;
}
get title() {
return this.data.title;
}
get project_id() {
return this.data.project_id;
}
get project_namespace() {
return this.project.namespace;
}
get project_name() {
return this.project.name;
}
get description() {
return this.data.description;
}
get labels() {
let excludeLabels = this.config.get('excludeLabels');
let labels = Array.isArray(excludeLabels)
? (this.data.labels || []).filter(label => !excludeLabels.includes(label))
: (this.data.labels || []);
let include = this.config.get('includeLabels');
return include.length > 0 ? labels.filter(label => include.includes(label)) : labels;
}
get milestone() {
return this.data.milestone ? this.data.milestone.title : null;
}
get assignee() {
return this.data.assignee ? this.data.assignee.username : null;
}
get author() {
return this.data.author.username;
}
get closed() {
return this.data.state === 'closed';
}
get updated_at() {
return dayjs(this.data.updated_at);
}
get created_at() {
return dayjs(this.data.created_at);
}
get state() {
return this.data.state;
}
get spent() {
return this.config.toHumanReadable(this.timeSpent, this._type);
}
get due_date() {
return this.data.due_date ? dayjs(this.data.due_date): null;
}
get total_spent() {
return this.data.time_stats ? this.config.toHumanReadable(this.data.time_stats.total_time_spent, this._type) : null;
}
get total_spent_s() {
return this.data.time_stats ? this.data.time_stats.total_time_spent : 0;
}
get total_estimate() {
return this.data.time_stats ? this.config.toHumanReadable(this.data.time_stats.time_estimate, this._type) : null;
}
get total_estimate_s() {
return this.data.time_stats ? this.data.time_stats.time_estimate : 0;
}
get _type() {
return this.type;
}
get _typeSingular() {
return this.type === 'merge_requests' ? 'Merge Request' : 'Issue';
}
make(project, id, create = false) {
let promise = create
? this.client.post(`projects/${encodeURIComponent(project)}/${this._type}`, {title: id})
: this.client.get(`projects/${encodeURIComponent(project)}/${this._type}/${id}`);
return promise.then(response => {
this.data = response.body;
return this;
});
}
async getNotes() {
let notes = await this.client.all(`projects/${this.data.project_id}/${this._type}/${this.iid}/notes`);
this.notes = notes;
return notes;
}
createTime(time, created_at, note) {
if(note === null || note === undefined) {
note = '';
}
else {
note = '\n\n' + note;
}
var date = new Date(created_at);
var spentAt = date.getUTCFullYear()+"-"+(date.getUTCMonth()+1)+"-"+date.getUTCDate();
const query =
`mutation($input: CreateNoteInput!) {
createNote(input: $input) {
note {
id
body
}
errors
}
}`
let noteablePath = (this._type == 'merge_requests') ? 'MergeRequest' : 'WorkItem';
let request = {
"query": query,
"variables": {
"input": {
"noteableId": `gid://gitlab/${noteablePath}/${this.id}`,
"body": '/spend '+Time.toHumanReadable(time, this.config.get('hoursPerDay'), '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]' + ' ' + spentAt + note),
}
}
};
return this.client.graphQL(request).then(response => {
let errors = response.body?.errors ?? response.body?.data?.createNote?.errors;
if(!response.body || (errors && errors.length)) {
throw new Error(`createTime failed: ${JSON.stringify(response.body)}`);
}
return response.body.data.createNote.note;
});
}
recordTimelogs(timelogs){
let ratio = chargeRatio(this.labels, this.config);
let times = [],
timeSpent = 0,
timeUsers = {},
timeFormat = this.config.get('timeFormat', this._type);
timelogs.forEach(
(timelog) => {
let spentAt = dayjs(timelog.spentAt);
let time = new Time(spentAt, {
author: {username: timelog.user.username},
created_at: timelog.spentAt,
noteable_type: this._typeSingular
}, this.config, timelog.timeSpent, timelog.note && timelog.note.body ? timelog.note.body : null, ratio);
// only include times by the configured user
if (this.config.get('user') && this.config.get('user') !== timelog.user.username) return;
if (!timeUsers[timelog.user.username]) timeUsers[timelog.user.username] = 0;
timeSpent += time.seconds;
timeUsers[timelog.user.username] += time.seconds;
times.push(time);
});
Object.entries(timeUsers).forEach(([name, time]) => this[`time_${name}`] = Time.toHumanReadable(time, this.config.get('hoursPerDay'), timeFormat));
this.timeSpent = timeSpent;
this.times = times;
}
}
export default Task;