-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
213 lines (183 loc) Β· 7.83 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title></title>
<link href='https://www.mapbox.com/base/latest/base.css' rel='stylesheet' />
</head>
<body class="prose" style="margin: 50px;">
<h1 id="title">Top Github Issues</h1>
<div id="error" class="note error" style="display: none;"></div>
<div id="loading" class="loading"></div>
<table class="small sortable">
<thead>
<tr>
<th class="sorttable_numeric"></th>
<th>Issue</th>
<th class="sorttable_numeric">Comments</th>
<th class="sorttable_numeric">Reactions</th>
<th class="sorttable_numeric" width="200">Labels</th>
<th>Assignee</th>
<th class="sorttable_numeric">Created</th>
<th class="sorttable_numeric">Updated</th>
<th class="sorttable_numeric">Score</th>
</tr>
</thead>
<tbody id="issues">
</tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://www.kryogenix.org/code/browser/sorttable/sorttable.js"></script>
<script>
function showError(message) {
document.getElementById('loading').style.display = 'none';
const errorElement = document.getElementById('error');
errorElement.style.display = '';
errorElement.innerHTML = message;
}
const repository = getHash();
const githubUsername = getParam('github_username') || denull(localStorage.getItem("githubUsername"));
const githubAccessToken = getParam('github_access_token') || denull(localStorage.getItem("githubAccessToken"));
localStorage.setItem("githubUsername", githubUsername);
localStorage.setItem("githubAccessToken", githubAccessToken);
function fetchGithubIssues(repository) {
return fetchGithubIssuesPage(repository, 0).then(function(firstPage) {
if (firstPage.status !== 200) {
showError(`<strong>Could not access repository.</strong> This may be because it is a <a href="https://github.com/mapbox/top-issues#private-repos">private repository</a>, you have misspelled the repository name, or you have <a href="https://github.com/mapbox/top-issues#rate-limiting">exceeded your rate limit</a>.`);
}
var pageCount = getPageCountFromLink(firstPage.headers.get('Link'));
var pagePromises = [firstPage.json()];
for (var i = 1; i < pageCount; i++) {
pagePromises.push(fetchGithubIssuesPage(repository, i).then((page) => page.json()));
}
return Promise.all(pagePromises).then(flatten);
});
};
function fetchGithubIssuesPage(repository, index) {
const url = `https://api.github.com/repos/${repository}/issues?per_page=${100}&page=${index + 1}`;
return fetch(url, {
headers: Object.assign(
{ 'Accept': 'application/vnd.github.squirrel-girl-preview' },
githubUsername && { 'Authorization': 'Basic ' + btoa(`${githubUsername}:${githubAccessToken}`) }
)
});
}
const defaultSettings = {
labels: {
'release blocker': 25,
'beta blocker': 20,
'high priority': 7,
'medium priority': 5,
'crash': 5,
'build': 5,
'bug': 2
},
reactions: {
'+1': 1,
'-1': -1,
'laugh': 1,
'hooray': 1,
'confused': 1,
'heart': 1
}
}
function fetchSettings(repository) {
return fetch(`https://raw.githubusercontent.com/${repository}/master/.topissuesrc`)
.then((response) => response.status === 404 ? {} : response.json())
.then((settings) => Object.assign({}, defaultSettings, settings))
}
function getPageCountFromLink(link) {
return link ? parseInt(link.match(/&page=([0-9]+)>; rel="last"/)[1]) : 1;
}
function flatten(input) {
return Array.prototype.concat.apply([], input);
}
function getParam(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function getHash() {
const match = window.location.hash.match(/#!([a-zA-Z0-9-_]+\/[a-zA-Z0-9-_\.]+)/);
if (!match) {
showError('<strong>No repository specified.</strong> Please append a repository name as a URL fragment like <a href="https://mapbox.github.io/top-issues/#!mapbox/mapbox-gl-js">https://mapbox.github.io/top-issues/#!mapbox/mapbox-gl-js</a>')
}
return match[1];
}
function compareScores(settings, a, b) {
return getScore(settings, b) - getScore(settings, a);
}
function getScore(settings, issue) {
return (
getReactionsScore(settings, issue) +
getLabelsScore(settings, issue) +
getCreatedAtScore(settings, issue) +
getCommentsScore(settings, issue)
);
}
function getReactionsScore(settings, issue) {
return Object.keys(issue.reactions).reduce((score, name) => {
const reactionCount = issue.reactions[name];
const reactionScore = settings.reactions[name];
if (!reactionCount || !reactionScore) return score;
return score + reactionScore * reactionCount;
}, 0);
}
function getCreatedAtScore(settings, issue) {
return -10 * Math.log(Date.parse(issue.created_at) / Date.now());
}
function getCommentsScore(settings, issue) {
return 3 * issue.comments / (issue.comments + 2);
}
function getLabelsScore(settings, issue) {
return issue.labels.reduce((score, label) => score + (settings.labels[label.name] || 0), 0);
}
const reactionEmojis = {
'+1': "π",
'-1': "π",
'laugh': "π",
'hooray': "π",
'confused': "π",
'heart': "β€οΈ"
};
function getReactionsHTML(issue) {
return Object.keys(issue.reactions).map((name) => {
const count = issue.reactions[name];
const emoji = reactionEmojis[name];
if (!count || !emoji) return;
return `<div class="inline" style="color: #555; margin: 0 3px;">
${count}β${emoji}
</div>`;
}).join('\n');
}
function denull(value) {
return value === "null" ? null : value;
}
document.getElementById('title').innerText = `Top Issues for ${repository}`
fetchSettings(repository).then(function(settings) {
fetchGithubIssues(repository).then(function(issues) {
document.getElementById('loading').style.display = 'none';
document.getElementById('issues').innerHTML = issues.sort(compareScores.bind(this, settings)).filter((issue) => !issue.pull_request).map((issue, index) => `
<tr>
<td>${index + 1}</td>
<td><a href="${issue.html_url}">${issue.title}</a></td>
<td title="${getCommentsScore(settings, issue).toFixed(2)} points">${issue.comments}</td>
<td title="${getReactionsScore(settings, issue)} points" sorttable_customkey=${getReactionsScore(settings, issue)}>${getReactionsHTML(issue)}</td>
<td title="${getLabelsScore(settings, issue)} points" sorttable_customkey=${getLabelsScore(settings, issue)}>${
issue.labels.map((label) => {
return `<a
href="https://github.com/${repository}/issues?q=is%3Aissue+is%3Aopen+label%3A${label.name.replace(' ', '+')}"
class="button" style="font-size: 10px; padding: 0 5px 0 5px; background-color: #${label.color};"
>${label.name}</a>`
}).join('\n')
}</td>
<td title="Not scored">${issue.assignee ? `<a href="${issue.assignee.html_url}"><img width="20" height="20" style="display:inline-block;" class="round" src="${issue.assignee.avatar_url}"> ${issue.assignee.login}</a>` : ''}</td>
<td title="${getCreatedAtScore(settings, issue).toFixed(2)} points" sorttable_customkey=${moment(issue.created_at).unix()}>${moment(issue.created_at).fromNow()}</td>
<td title="Not scored" sorttable_customkey=${moment(issue.updated_at).unix()}>${moment(issue.updated_at).fromNow()}</td>
<td>${getScore(settings, issue).toFixed(1)}</td>
</tr>
`).join('\n');
});
});
</script>
</body>
</html>