-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-bugspots.js
176 lines (147 loc) · 5.9 KB
/
github-bugspots.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
(function() {
'use strict';
var mainTemplateUrl = chrome.extension.getURL('templates/main.html');
var rankingTemplateUrl = chrome.extension.getURL('templates/ranking.html');
var buttonTemplateUrl = chrome.extension.getURL('templates/button.html');
var graphButton = $('.js-selected-navigation-item.reponav-item').last();
var bugspotsButton = $('.js-selected-navigation-item[aria-label=Bugspots]');
var re = /^http[s]?:[/]+github.com[/]*([^/]*)\/([^/]*)[/]?.*$/;
var parsedUrl = re.exec(location.href);
var userName = parsedUrl[1];
var repoName = parsedUrl[2].split('#')[0];
/* currentTimestamp is used for calculation of score */
var currentTimestamp = new Date().getTime();
var oldestCommitTimestamp = 0;
/* bugDetectionRegex is used for judging whether given commits are bug fix or not */
var bugDetectionRegexString = localStorage.getItem('bugspots-bug-detection-regex') || '[f|F]ix(es|ed)?|[C|c]lose(s|d)?]';
var bugDetectionRegex = new RegExp(bugDetectionRegexString);
/* accessToken is used for accessing private repositories */
var accessToken = localStorage.getItem('bugspots-access-token');
var githubClient = new Github({
apiUrl: 'https://api.github.com',
token: accessToken,
auth: 'oauth'
});
var repo = githubClient.getRepo(userName, repoName);
Handlebars.registerHelper('if_eq', function(a, b, opts) {
if(a == b)
return opts.fn(this);
else
return opts.inverse(this);
});
function selectNavigationItem() {
$('.js-selected-navigation-item.selected').removeClass('selected');
bugspotsButton.addClass('selected');
}
function renderButton() {
return $.get(buttonTemplateUrl, function (loadedHtml) {
var templateHtmlString = $(loadedHtml).html();
graphButton.parent().append(templateHtmlString);
bugspotsButton = $('#bugspotNavItem');
bugspotsButton.click(selectNavigationItem);
bugspotsButton.click(clickBugspotsButton);
});
}
function renderMain(renderInfo) {
var templateInfo = renderInfo || {};
templateInfo['bugDetectionRegex'] = bugDetectionRegexString;
templateInfo['accessToken'] = accessToken;
return $.get(mainTemplateUrl, function (loadedHtml) {
var templateHtmlString = $(loadedHtml).html();
var template = Handlebars.compile(templateHtmlString);
var resultHtmlString = template(templateInfo);
$('#js-repo-pjax-container > .container').html(resultHtmlString);
activateJsEventHandler();
});
}
function renderRanking(renderInfo) {
var templateInfo = renderInfo || {};
return $.get(rankingTemplateUrl, function (loadedHtml) {
var templateHtmlString = $(loadedHtml).html();
var template = Handlebars.compile(templateHtmlString);
var resultHtmlString = template(templateInfo);
$('#jsRankingResult').replaceWith(resultHtmlString);
});
}
function activateJsEventHandler(){
$('#jsBugspotsSaveButton').click(function(){
/* update bug detection regex */
bugDetectionRegexString = $('#bugspotsRegexField').val();
localStorage.setItem('bugspots-bug-detection-regex', bugDetectionRegexString);
bugDetectionRegex = new RegExp(bugDetectionRegexString);
/* update token */
accessToken = $('#bugspotsTokenField').val();
localStorage.setItem('bugspots-access-token', accessToken);
});
}
function clickBugspotsButton() {
/*
* -- to debug --
* var _test = {
* summary: [
* { path: "buggy-text",
* url: "https://...",
* score: 0.1 }
* ]};
* renderMain(_test);
*/
renderMain({});
repo.getCommits({'sha': 'master', 'perpage': 100}, analyzeCommits);
}
function analyzeCommits(err, commits) {
var promiseList = [];
if (err) { renderMain({error: true, errorType: '403'}); return; }
var fix_commits = _.filter(commits, function (c) {
return Boolean(c.commit.message.match(bugDetectionRegex));
});
if (fix_commits.length == 0) {
renderMain({error: true, errorType: 'NoCommits'});
return;
}
oldestCommitTimestamp = (new Date(fix_commits[fix_commits.length - 1].commit.author.date)).getTime();
_.each(fix_commits, function (v) {
var p = new Promise(function(res, _rej) {
getCommit(repoName, userName, v.sha)
.then(createRenderingInfo)
.then(res);
});
promiseList.push(p);
});
Promise.all(promiseList).then(function(renderInfo) {
var summary = {};
_.each(renderInfo, function (v) {
if (v.path in summary) { summary[v.path].score += v.score; }
else { summary[v.path] = v; }
});
var summaryInfo = [];
$.each(summary, function (k, v) {
summaryInfo.push({path: k, score: v.score.toFixed(4), url: v.url})
});
var sortedSummary = _.sortBy(summaryInfo, 'score').reverse();
renderRanking({summary: sortedSummary});
});
}
function getCommit(repo, owner, sha) {
return new Promise(function(resolve, reject) {
var _url = 'https://api.github.com/repos/' + owner + '/' + repo + '/commits/' + sha;
var _token = localStorage.getItem('bugspots-access-token');
if (_token) _url += '?access_token=' + _token;
$.ajax({ url: _url, type: 'GET', success: resolve, error: reject })
});
}
function createRenderingInfo(commitInfo) {
return new Promise(function (resolve, _reject) {
var timestamp = (new Date(commitInfo.commit.author.date)).getTime();
var normalizedTimestamp = (currentTimestamp - oldestCommitTimestamp) / (currentTimestamp - timestamp);
var score = 1 / (1 + Math.exp(12.0 * (1.0 - normalizedTimestamp)));
_.each(commitInfo.files, function (f) {
var blameUrl = 'https://github.com/' + userName + '/' + repoName + '/blame/master/' + f.filename;
resolve({path: f.filename, url: blameUrl, timestamp: timestamp, score: score})
});
});
}
function initialize() {
renderButton();
}
initialize();
})();