-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (170 loc) · 4.78 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
/*
* index.js: Import a CFP Excel into Github issue comments for review.
*
* (C) 2013, EmpireJS.
*
*/
var fs = require('fs'),
path = require('path'),
util = require('util'),
async = require('async'),
xlsxRows = require('xlsx-rows'),
GitHubApi = require('github');
var importer = module.exports = function (options, callback) {
options.commentor = 'indexzero';
options.file = path.join(__dirname, 'empirejs-cfp-2015.xlsx');
options.repo = 'empirejs/empirejs-cfp-2015';
options.github = new GitHubApi({
// required
version: "3.0.0",
// optional
timeout: 5000
});
options.github.authenticate({
type: 'basic',
username: options.commentor,
password: process.env.GITHUB_PASS
});
async.parallel({
reviews: async.apply(importer.readReviews, options),
issues: async.apply(importer.readIssues, options)
}, function (err, res) {
if (err) { return callback(err); }
//
// Attach reviews to each issue
//
var issues = res.issues.filter(function (issue) {
var review = res.reviews[issue.title];
if (!review) { return false; }
issue.review = review;
issue.comment = importer.render(review);
return true;
});
console.log('Adding comments for %s issues', issues.length);
async.forEachLimit(issues, 10, function (issue, next) {
importer.tryAddComment(options, issue, next);
}, callback);
});
};
//
// ### function tryAddComment (options, issue, callback)
// Adds a single comment to the `issue` only if the comment
// from the specified user doesn't already exist.
//
importer.tryAddComment = function (options, issue, callback) {
var github = options.github;
importer.readComments(options, issue, function (err, comments) {
if (err) { return callback(err); }
var users = comments.map(function (comment) {
return comment.user.login
});
if (~users.indexOf(options.commentor)) {
console.log('Ignoring comment on #%s, %s', issue.number, issue.title);
return callback();
}
done = true;
console.log('Adding comment on #%s, %s', issue.number, issue.title);
github.issues.createComment({
user: issue.source.user,
repo: issue.source.repo,
number: issue.number,
body: issue.comment
}, callback);
});
};
//
// ### function readComments (options, issue, callback)
// Reads comments from the issue.
//
importer.readComments = function (options, issue, callback) {
var github = options.github;
github.issues.getComments({
user: issue.source.user,
repo: issue.source.repo,
number: issue.number
}, function (err, comments) {
if (err) { return callback(err); }
return callback(null, comments);
});
};
//
// ### function readReviews (options, callback)
// Reads all of the existing reviews
//
importer.readReviews = function (options, callback) {
var rows, reviews;
try { rows = xlsxRows(options.file); }
catch (ex) { return callback(ex); }
reviews = rows.slice(1)
.reduce(function (acc, cols) {
var title = cols[14];
acc[title] = {
title: cols[14],
total: cols[12],
ratings: {
'Clear/compelling': cols[4],
'Relevancy': cols[5],
'Topic Coverage': cols[6],
'Useful': cols[7],
'Uniqueness': cols[8],
'Expertise on subject': cols[9],
'Speaker Experience': cols[10],
'Personal Score': cols[11]
}
};
return acc;
}, {});
callback(null, reviews);
};
//
// ### function readIssues (options, callback)
// Reads all the existing issues on the repo so we
// don't create duplicates
//
importer.readIssues = function (options, callback) {
var github = options.github,
repo = options.repo.split('/');
console.log('Reading issues | %s', options.repo);
//
// Remark: This is super brittle and only works
// for 200 CFP submissions max.
//
async.parallel([
async.apply(github.issues.repoIssues, {
user: repo[0],
repo: repo[1],
per_page: 100,
page: 1
}),
async.apply(github.issues.repoIssues, {
user: repo[0],
repo: repo[1],
per_page: 100,
page: 2
})
], function (err, pages) {
if (err) { return callback(err); }
var all = pages[0].concat(pages[1])
all.forEach(function (issue) {
issue.source = {
user: repo[0],
repo: repo[1]
}
});
callback(null, all);
})
};
//
// ### function render (review)
// Returns a rendered comment for the review
//
importer.render = function (review) {
var text = Object.keys(review.ratings)
.map(function (name) {
return util.format(' - %s: %d', name, review.ratings[name])
});
text.unshift('### Review: ');
text.push('');
text.push(util.format('### TOTAL SCORE: %d', review.total))
return text.join('\n');
};