forked from realm/github-gantt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
400 lines (367 loc) · 11.8 KB
/
index.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const express = require('express');
const app = express();
const GitHub = require('octokat');
const Realm = require('realm');
const path = require('path');
const dateFormat = require('dateformat');
const utilities = require('./utilities');
const config = require('./config/config');
const bodyParser = require('body-parser');
// Realm Model Definition
const TaskSchema = {
name: 'Task',
primaryKey: 'id',
properties: {
text: 'string', // task title
start_date: 'date', // the date when a task is scheduled to begin
duration: 'int', // the task duration
id: 'int', // the task id
body: 'string',
url: 'string',
htmlUrl: 'string',
number: 'int',
// Indicates the state of the issues to return.
// Can be either open, closed, or all.
state: 'string',
isDeleted: 'bool',
// the task type, available values are stored in the types object:
//
// "task" - a regular task (default value).
//
// "project" - a task that starts, when its earliest child task starts, and
// ends, when its latest child ends. The start_date, end_date, duration
// properties are ignored for such tasks.
//
// "milestone" - a zero-duration task that is used to mark out important
// dates of the project. The duration, progress, end_date properties are
// ignored for such tasks.
type: {type: 'string', optional: true},
// the id of the parent task.
// The id of the root task is specified by the root_id config
parent: {type: 'int', optional: true},
// the task's level in the tasks hierarchy (zero-based numbering).
level: {type: 'int', optional: true},
// ( number from 0 to 1 ) the task progress.
progress: {type: 'double', optional: true},
// specifies whether the task branch will be opened initially
// (to show child tasks).
open: {type: 'bool', optional: true},
// the date when a task is scheduled to be completed. Used as an alternative
// to the duration property for setting the duration of a task.
end_date: {type: 'date', optional: true},
// the background color of the task bar
color: {type: 'string', optional: true},
// the label used to identify the color of the task
label: {type: 'string', optional: true},
}
};
const LabelSchema = {
name: 'Label',
primaryKey: 'id',
properties: {
id: 'int',
url: 'string',
name: 'string',
color: 'string',
default: 'bool',
}
}
const MilestoneSchema = {
name: 'Milestone',
primaryKey: 'id',
properties: {
url: 'string', //api.github.com/repos/octocat/Hello-World/milestones/1"
htmlUrl: 'string', // https://github.com/octocat/Hello-World/milestones/v1.0"
id: 'int',
number: 'int',
state: 'string',
title: 'string',
description: 'string',
openIssues: 'int',
closedIssues: 'int',
createdAt: 'date',
updatedAt: {type: 'date', optional: true},
closedAt: {type: 'date', optional: true},
dueOn: {type: 'date', optional: true},
}
}
const gh = new GitHub({
token: config.GITHUB_API_TOKEN
});
var realm;
if (config.RMP_ADMIN_TOKEN &&
config.RMP_SYNC_URL) {
if (config.RMP_ADMIN_TOKEN != "" &&
config.RMP_SYNC_URL != "") {
let adminUser = Realm.Sync.User.adminUser(config.RMP_ADMIN_TOKEN);
realm = new Realm({
sync: {
user: adminUser,
url: config.RMP_SYNC_URL,
},
schema: [TaskSchema, LabelSchema, MilestoneSchema],
});
}
}
else {
realm = new Realm({
path: 'tasks.realm',
schema: [TaskSchema, LabelSchema, MilestoneSchema],
});
}
let repo = gh.repos(config.GITHUB_ORG_NAME, config.GITHUB_REPO_NAME);
function getLabels(completion) {
repo.labels.fetch()
.then((labels) => {
let items = labels.items;
if (utilities.isArray(items)) {
realm.write(() => {
for (index in items) {
let label = items[index];
realm.create('Label', {
id: label.id,
url: label.url,
name: label.name,
color: label.color,
default: label.default,
}, true);
}
});
}
completion();
});
}
function getMilestones(completion) {
repo.milestones.fetch()
.then((milestones) => {
let items = milestones.items;
if (utilities.isArray(items)) {
realm.write(() => {
for (index in items) {
let milestone = items[index];
var updatedAt = null;
var closedAt = null;
var dueOn = null;
if (utilities.isString(milestone.updatedAt)) {
updatedAt = new Date(milestone.updatedAt);
}
if (utilities.isString(milestone.closedAt)) {
closedAt = new Date(milestone.closedAt);
}
if (utilities.isString(milestone.dueOn)) {
dueOn = new Date(milestone.dueOn);
}
realm.create('Milestone', {
url: milestone.url,
htmlUrl: milestone.htmlUrl,
id: milestone.id,
number: milestone.number,
state: milestone.state,
title: milestone.title,
description: milestone.description,
openIssues: milestone.openIssues,
closedIssues: milestone.closedIssues,
createdAt: new Date(milestone.createdAt),
updatedAt: updatedAt,
closedAt: closedAt,
dueOn: dueOn,
}, true);
}
});
}
completion();
});
}
function processIssues(issues, completion, idArray) {
if (!utilities.isArray(idArray)) {
idArray = [];
}
realm.write(() => {
for (index in issues.items) {
let issue = issues.items[index];
var startDate = new Date(issue.createdAt);
var dueDate = null;
var labelName = null;
var color = null;
var progress = null;
// find keywords
if (issue.body != null) {
var lines = issue.body.split(/[\r\n]+/);
for (var j = 0; j < lines.length; j++) {
if (!lines[j].indexOf(config.START_DATE_STRING)) {
let date = new Date(lines[j].replace(config.START_DATE_STRING, ''));
if (utilities.isDate(date)) {
startDate = date;
}
}
if (!lines[j].indexOf(config.DUE_DATE_STRING)) {
let date = new Date(lines[j].replace(config.DUE_DATE_STRING, ''));
if (utilities.isDate(date)) {
dueDate = date;
}
}
if (!lines[j].indexOf(config.LABEL_STRING)) {
var labelString = lines[j].replace(config.LABEL_STRING, '');
if (utilities.isString(labelString)) {
labelString = labelString.trim();
// Find label in realm
let label = realm.objects('Label').filtered('name = $0', labelString)[0];
if (utilities.isRealmObject(label)) {
color = "#"+label.color.toUpperCase();
labelName = label.name;
}
}
}
if (!lines[j].indexOf(config.PROGRESS_STRING)) {
progress = utilities.sanitizeFloat(lines[j].replace(config.PROGRESS_STRING, ''));
}
}
}
realm.create('Task', {
text: utilities.sanitizeStringNonNull(issue.title),
start_date: startDate,
duration: 1,
id: issue.id,
body: utilities.sanitizeStringNonNull(issue.body),
url: utilities.sanitizeStringNonNull(issue.url),
htmlUrl: utilities.sanitizeStringNonNull(issue.htmlUrl),
number: issue.number,
state: issue.state,
isDeleted: false,
end_date: dueDate,
label: labelName,
color: color,
progress: progress,
}, true);
idArray.push(issue.id);
}
});
if (utilities.isString(issues.nextPageUrl)) {
issues.nextPage.fetch()
.then((moreIssues) => {
processIssues(moreIssues, completion, idArray);
});
}
else {
// Prune the deleted issues
oldIds = realm.objects('Task').map(function(task) {
return task.id;
});
deletedIds = oldIds.filter(function(el) {
return idArray.indexOf(el) < 0;
});
realm.write(() => {
for (index in deletedIds) {
let deletedId = deletedIds[index];
let task = realm.objectForPrimaryKey('Task', deletedId);
task.isDeleted = true;
}
});
completion();
}
}
function getTaskChartData() {
let tasks = realm.objects('Task').filtered('isDeleted = false AND state = "open" AND end_date != null').sorted('label', true);
var taskData = {data: []};
for (index in tasks) {
let task = tasks[index];
let formattedTask = {
id: task.id,
text: task.text,
start_date: dateFormat(task.start_date, "mm-dd-yyyy"),
duration: task.duration,
end_date: dateFormat(task.end_date, "mm-dd-yyyy"),
url: task.url,
progress: task.progress,
color: task.color,
htmlUrl: task.htmlUrl,
};
taskData.data.push(formattedTask);
}
return taskData;
}
app.use('/static', express.static(path.join(__dirname, 'node_modules/dhtmlx-gantt/codebase')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
app.get('/data', function (req, res) {
var taskData = getTaskChartData();
res.send(taskData);
});
app.get('/additionalData', function (req, res) {
var data = {};
// Handle milestones
let milestones = realm.objects('Milestone').filtered('dueOn != null');
data.milestones = milestones.map((object) => {
return JSON.stringify(object);
});
// Handle labels
var hash = {},labels = [];
realm.objects('Task').filtered('isDeleted = false AND state = "open" AND end_date != null AND label != null').sorted('label', true).forEach((object, index) => {
if (!hash[object.label]) {
hash[object.label] = true;
labels.push({
name: object.label,
color: object.color,
});
}
});
data.labels = labels;
res.send(data);
});
app.get('/refreshData', function (req, res) {
getLabels(() => {
console.log("--> Retrieved Labels");
});
getMilestones(() => {
console.log("--> Retrieved Milestones");
});
repo.issues.fetch({state: "all", per_page: 100})
.then((issues) => {
processIssues(issues, () => {
console.log("--> Finished Processing Issues");
var taskData = getTaskChartData();
res.send(taskData);
});
});
});
app.post('/updateIssue', bodyParser.json(), function (req, res) {
if (!req.body || utilities.isObject(req.body)) {
return res.sendStatus(400);
}
let chartTask = req.body;
var task = realm.objectForPrimaryKey('Task', chartTask.id);
if (utilities.isRealmObject(task)) {
// Write to Realm first
realm.write(() => {
task.start_date = new Date(chartTask.start_date);
task.end_date = new Date(chartTask.end_date);
task.duration = utilities.sanitizeInt(chartTask.duration);
task.progress = utilities.sanitizeFloat(chartTask.progress);
var lines = task.body.split('\r\n');
for (var j = 0; j < lines.length; j++) {
if (!lines[j].indexOf(config.START_DATE_STRING)) {
lines[j] = config.START_DATE_STRING + " " + dateFormat(task.start_date, "mm-dd-yyyy");
}
if (!lines[j].indexOf(config.DUE_DATE_STRING)) {
lines[j] = config.DUE_DATE_STRING + " " + dateFormat(task.end_date, "mm-dd-yyyy");
}
if (!lines[j].indexOf(config.PROGRESS_STRING && utilities.isNumber(task.progress))) {
lines[j] = config.PROGRESS_STRING + " " + task.progress.toFixed(2);
}
}
let newBody = lines.join('\r\n');
task.body = newBody;
});
// Now post to Github
repo.issues(task.number).update({
body: task.body,
}).then((issue) => {
res.send("Success");
});
}
});
app.listen(process.env.PORT || 3000, function () {
let port = (process.env.PORT || 3000);
console.log('Github-Gantt listening on port ' + port);
});