-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.js
executable file
·111 lines (101 loc) · 2.85 KB
/
json.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
#!/usr/bin/env node
'use strict';
var request = require('request'),
es = require('event-stream'),
config = require('./config.js');
var baseRequest = request.defaults({
headers: {
'X-Api-Key': config.key,
'X-Api-Secret': config.secret
}
});
function reporterToUser(id) {
if (!config.users[id]) {
console.warn('User with assembla id ' + id + ' was not found in mapping. Populate config.js');
}
return config.users[id] || '';
}
function timeFormat(val) {
return val.replace('Z', '+0100'); // yyyy-MM-dd'T'HH:mm:ss.SSS+0100
}
function guessType(summary) {
if (summary.slice(0,3).toLowerCase() === 'bug') {
return 'Bug';
}
if (summary.slice(0,3).toLowerCase() === 'as ') {
return 'Story';
}
if (summary.slice(0,4).toLowerCase() === 'task') {
return 'Task';
}
return 'Incoming';
}
function priority(prio) {
switch (prio) {
case 1:
return 'Critical';
case 2:
return 'Major';
case 3:
return 'None';
case 4:
return 'Minor';
case 5:
return 'Trivial';
}
}
process.stdin
.pipe(es.split())
.pipe(es.map(function(num, cb) {
if (!parseInt(num, 10)) {
return cb();
}
var url = '{b}/spaces/{s}/tickets/{#}.json'.replace('{b}', config.url).replace('{s}', config.space).replace('{#}', num);
baseRequest.get(url, function(err, res, body) {
if (err) {
return cb(err);
}
if (res.statusCode !== 200) {
return cb('Ticket {n} not found'.replace('{n}', num));
}
var ticket = JSON.parse(body);
return cb(null, ticket);
});
}))
.pipe(es.map(function(ticket, cb) {
var url = '{b}/spaces/{s}/tickets/{#}/ticket_comments.json'.replace('{b}', config.url).replace('{s}', config.space).replace('{#}', ticket.number);
baseRequest.get(url, function(err, res, body) {
if (err) {
return cb(err);
}
if (res.statusCode !== 200) {
return cb('Ticket {n} not found'.replace('{n}', ticket.number));
}
ticket.comments = JSON.parse(body);
return cb(null, ticket);
});
}))
.pipe(es.map(function(ticket, cb) {
var jira = {
'externalId': ticket.number,
'summary': ticket.summary,
'description': ticket.description,
'reporter': reporterToUser(ticket.reporter_id),
'issueType': guessType(ticket.summary),
'priority': priority(ticket.priority),
'created': timeFormat(ticket.created_on)
};
var comments = ticket.comments || [];
jira.comments = comments.map(function(comment) {
return {
'body': comment.comment || comment.ticket_changes,
'author': reporterToUser(comment.user_id),
'created': timeFormat(comment.created_on)
};
}).filter(function(comment) {
return comment.body !== "--- []\n";
});
return cb(null, JSON.stringify(jira));
}))
.pipe(es.join(',\n'))
.pipe(process.stdout);