-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtodos.js
323 lines (270 loc) · 8.43 KB
/
todos.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
"use strict";
/**
* Format of a todo:
* * file: String - Relative path
* * sha: String - Commit's sha
* * line: Number - File line number
* * title: String - Issue's title
* * label: String - Issue's label
* * issue: Number - (optional) Issue's number
**/
var path = require("path");
var _ = require("lodash");
var Promise = require("bluebird");
var ask = require("./ask");
var fs = require("./fs");
var config = require("./config");
var service = require("./issue-service");
var git = require("./git");
module.exports = {
"fromDiff": fromDiff
};
var SKIP_FILE_NAME = ".github-todos-ignore";
function readIgnoreFile () {
return git.dir(path.join("..", SKIP_FILE_NAME))
.then(function (filename) {
return fs.readFile(filename).then(function (content) {
var ignored = _.filter(_.invoke((content || "").split("\n"), "trim"));
return [ignored, filename];
});
});
}
function shouldSkip (title, caseSensitive) {
title = String(title || "").trim();
if (caseSensitive) {
title = title.toLowerCase();
}
return readIgnoreFile().spread(function (ignores) {
if (caseSensitive) {
ignores = _.invoke(ignores, "toLowerCase");
}
return _.contains(ignores, title);
});
}
function createOrCommentIssue (repo, todo, conf) {
if (todo.issue) {
return commentIssue(todo, conf);
}
// Create issue?
return shouldSkip(todo.title, conf["case-sensitive"]).then(function (skip) {
if (skip) {
return null;
}
var Service = service(conf.service);
return Service.findIssueByTitle(repo, todo.title).then(function (issue) {
if (!issue) {
return createIssue(repo, todo, conf);
}
// comment issue
todo.issue = issue.number;
var ops = [commentIssue(repo, todo, conf)];
if (!_.contains(issue.labels, todo.label)) {
ops.push(Service.tagIssue(repo, todo.issue, todo.label));
}
return Promise.all(ops).spread(function (comment /*, tagresult */) {
return comment;
});
});
});
}
// Add line to github-todos-ignore
function rememberSkip (title) {
return readIgnoreFile().spread(function (ignores, skipFile) {
if (!_.contains(ignores, title)) {
return fs.writeFile(skipFile, ignores.concat([title]).join("\n"));
}
});
}
function createIssue (repo, todo, conf) {
if (!conf["confirm-create"]) {
return create();
}
return ask([{
"type": "expand",
"message": "Create new issue \"" + todo.title + "\" (" + todo.file + ":" + todo.line + ")",
"name": "choice",
"choices": [
{"key": "y", "name": "Create issue", "value": "create"},
{"key": "e", "name": "Edit title and create issue", "value": "edit"},
{"key": "n", "name": "Do not create issue", "value": "skip"},
{"key": "r", "name": "Do not create issue and remember for next times", "value": "skip_and_remember"},
{"key": "q", "name": "Abort", "value": "abort"}
],
"default": 0
}]).choices("choice", {
"create": create,
"edit": edit,
"skip_and_remember": skipAndRemember,
"abort": abort,
"skip": null, // skip
"default": null // skip
});
function abort () {
var e = new Error("User aborted");
e.code = "EINTERRUPT";
throw e;
}
function skipAndRemember () {
return rememberSkip(todo.title).then(null, function (err) {
console.error("[Github-Todos] Failed adding info to '" + SKIP_FILE_NAME + "'");
throw err;
});
}
function create (forceTitle) {
return getCommentText(repo, todo, conf).then(function (text) {
var title = (typeof forceTitle === "string" && forceTitle) ? forceTitle : todo.title;
return service(conf.service).createIssue(repo, title, text, [todo.label]).then(function (issue) {
return issue;
});
});
}
function edit () {
return ask([{
"type": "input",
"message": "Issue title",
"name": "title",
"default": todo.title
}]).then(function (answers) {
return create(answers.title);
});
}
}
function commentIssue (repo, todo, conf) {
return getCommentText(repo, todo, conf).then(function (text) {
return service(conf.service).commentIssue(repo, todo.issue, text);
});
}
function getCommentText (repo, todo, conf) {
var text = "";
// Link to file
text += "Ref. [" + todo.file + ":" + todo.line + "](" + service(conf.service).getFileUrl(repo, todo.file, todo.sha, todo.line) + ")";
function generateCommentText (content) {
var lines = content.split(/\r\n|\r|\n/);
// Remove trailing new lines
while (lines[lines.length - 1] === "") {
lines.pop();
}
while (lines[0] === "") {
lines.shift();
}
if (conf.context > 0) {
// Extract: line to line + conf.context
var extract = lines.slice(todo.line - 1, todo.line + conf.context).join("\n");
if (todo.line + conf.context < lines.length) {
extract += "\n…";
}
text += "\n\n```" + getLanguage(todo.file, content) + "\n" + extract + "\n```\n";
}
if (conf.signature) {
text += "\n" + conf.signature;
}
return text;
}
// Add code information
return git.dir(path.join("..", todo.file))
.then(fs.readFileStrict)
.then(generateCommentText);
}
// Language detection: very naive only based on filename for now
function getLanguage (filename /*, content */) {
var index = filename.lastIndexOf(".");
if (index === -1) {
return "";
}
return filename.substring(index + 1);
}
function fromDiff (repo, diff, sha, conf) {
return config.defaults().then(function (defaults) {
conf = _.merge({ "onProgress": _.noop }, defaults, conf || {});
var todos = _.flatten(_.map(diff, function (file) {
var addedLines = _.filter(file.lines, "add");
var lineTodos = _.map(addedLines, lineToTodoMapper(file.to, sha, conf));
// keep only those with required field
return _.filter(lineTodos, "title");
}));
return todos.reduce(function (previous, todo) {
return previous.then(todoHandler(repo, todo, conf));
}, Promise.resolve([])).then(function (results) {
return [results, todos];
});
});
}
function todoHandler (repo, todo, conf) {
return function (results) {
return createOrCommentIssue(repo, todo, conf)
.then(function (result) {
conf.onProgress(null, result, todo);
return results.concat([result]);
})
.then(null, function (err) {
conf.onProgress(err, null, todo);
throw err;
});
};
}
// String, Sha → String → {file, sha, line, title, label}
function lineToTodoMapper (filename, sha, conf) {
return function lineToTodo (line) {
return _.merge({
"file": filename,
"sha": sha,
"line": line.ln
}, extractTodoTitle(line.content, conf));
};
}
// String → {title, label}
function extractTodoTitle (content, conf) {
var result = null;
var labels = {};
_.each(conf, function (value, key) {
if (value && key.match(/^label\./)) {
var trigger = key.substring(6);
if (conf["label-whitespace"]) {
trigger += " ";
}
labels[trigger] = value;
}
});
if (_.isString(content)) {
_.find(Object.keys(labels), function (trigger) {
var index;
if (conf["case-sensitive"]) {
index = content.indexOf(trigger);
} else {
index = content.toUpperCase().indexOf(trigger.toUpperCase());
}
if (index !== -1) {
var title = content.substring(index + trigger.length).trim();
var issue = null;
if (title && !isCode(title)) {
var match = title.match(/^\s+#(\d+)\s+/);
if (match) {
issue = match[1];
title = title.substring(match[0].length);
}
result = {
"title": title,
"label": labels[trigger],
"issue": Number(issue)
};
}
return true; // break
}
});
}
return result;
}
// TODO Better heuristic for code vs words detection
// Simple heuristic to detect if a title is really a title or some valid code
// String → Boolean
function isCode (string) {
// If symbols are more than 20% of the code, it may be code more than human text
var symbols = _.filter(string, isSymbol);
return symbols.length / string.length > 0.20;
}
var RE_SYMBOL = /[^\sa-z0-9\u00E0-\u00FC]/i;
// Matches a symbol: non alphanumeric character
// Character → Boolean
function isSymbol (character) {
return RE_SYMBOL.test(character);
}