forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest.js
236 lines (222 loc) · 9.7 KB
/
suggest.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
var nodeUrl = require('url');
var request = require('request');
var Promise = require('promise');
var serverSecrets;
try {
// Everything that cannot be checked in but is useful server-side
// is stored in this JSON data.
serverSecrets = require('./secret.json');
} catch(e) { console.error('No secret data (secret.json, see server.js):', e); }
// data: {url}, JSON-serializable object.
// end: function(json), with json of the form:
// - badges: list of objects of the form:
// - link: target as a string URL.
// - badge: shields image URL.
// - name: string
var suggest = function(data, end, ask) {
ask.res.setHeader('Access-Control-Allow-Origin', 'http://shields.io');
try {
var url = nodeUrl.parse(data.url);
} catch(e) { end({err:''+e}); return; }
if (url.hostname === 'github.com') {
github(url, end);
}
};
// url: string
// cb: function({badges})
var github = function(url, cb) {
var userRepo = url.pathname.slice(1).split('/');
var user = userRepo[0];
var repo = userRepo[1];
Promise.all([
githubIssues(user, repo),
githubForks(user, repo),
githubStars(user, repo),
githubLicense(user, repo),
]).then(function(badges) {
cb({badges:badges.filter(function(b) { return b != null; })});
}).catch(function(err) {
cb({badges:[], err:err});
});
};
var githubIssues = function(user, repo) {
var userRepo = user + '/' + repo;
return Promise.resolve({
name: 'GitHub issues',
link: 'https://github.com/' + userRepo + '/issues',
badge: 'https://img.shields.io/github/issues/' + userRepo + '.svg',
});
};
var githubForks = function(user, repo) {
var userRepo = user + '/' + repo;
return Promise.resolve({
name: 'GitHub forks',
link: 'https://github.com/' + userRepo + '/network',
badge: 'https://img.shields.io/github/forks/' + userRepo + '.svg',
});
};
var githubStars = function(user, repo) {
var userRepo = user + '/' + repo;
return Promise.resolve({
name: 'GitHub stars',
link: 'https://github.com/' + userRepo + '/stargazers',
badge: 'https://img.shields.io/github/stars/' + userRepo + '.svg',
});
};
// user: eg, qubyte
// repo: eg, rubidium
// returns a promise of {link, badge, name}
var githubLicense = function(user, repo) {
return new Promise(function(resolve, reject) {
// Step 1: Get the repo's default branch.
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '';
// Using our OAuth App secret grants us 5000 req/hour
// instead of the standard 60 req/hour.
if (serverSecrets) {
apiUrl += '?client_id=' + serverSecrets.gh_client_id
+ '&client_secret=' + serverSecrets.gh_client_secret;
}
var badgeData = {text:['license',''], colorscheme:'blue'};
// A special User-Agent is required:
// http://developer.github.com/v3/#user-agent-required
request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) {
if (err != null) { resolve(null); return; }
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) { resolve(null); return; }
var data = JSON.parse(buffer);
var defaultBranch = data.default_branch;
// Step 2: Get the SHA-1 hash of the branch tip.
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/branches/' + defaultBranch;
if (serverSecrets) {
apiUrl += '?client_id=' + serverSecrets.gh_client_id
+ '&client_secret=' + serverSecrets.gh_client_secret;
}
request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) {
if (err != null) { resolve(null); return; }
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) { resolve(null); return; }
var data = JSON.parse(buffer);
var branchTip = data.commit.sha;
// Step 3: Get the tree at the commit.
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/trees/' + branchTip;
if (serverSecrets) {
apiUrl += '?client_id=' + serverSecrets.gh_client_id
+ '&client_secret=' + serverSecrets.gh_client_secret;
}
request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) {
if (err != null) { resolve(null); return; }
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) { resolve(null); return; }
var data = JSON.parse(buffer);
var treeArray = data.tree;
var licenseBlob;
var licenseFilename;
// Crawl each file in the root directory
for (var i = 0; i < treeArray.length; i++) {
if (treeArray[i].type != 'blob') {
continue;
}
if (treeArray[i].path.match(/(LICENSE|COPYING|COPYRIGHT).*/i)) {
licenseBlob = treeArray[i].sha;
licenseFilename = treeArray[i].path;
break;
}
}
// Could not find license file
if (!licenseBlob) { resolve(null); return; }
// Step 4: Get the license blob.
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/blobs/' + licenseBlob;
var link = 'https://raw.githubusercontent.com/' +
[user, repo, defaultBranch, licenseFilename].join('/');
if (serverSecrets) {
apiUrl += '?client_id=' + serverSecrets.gh_client_id
+ '&client_secret=' + serverSecrets.gh_client_secret;
}
// Get the raw blob instead of JSON
// https://developer.github.com/v3/media/
request(apiUrl, { headers: { 'User-Agent': 'Shields.io', 'Accept': 'appplication/vnd.github.raw' } },
function(err, res, buffer) {
if (err != null) { resolve(null); return; }
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) { resolve(null); return; }
var license = guessLicense(buffer);
if (license) {
badgeData.text[1] = license;
resolve({
link: link,
badge: shieldsBadge(badgeData),
name: 'GitHub license'
});
return;
} else {
// Not a recognized license
resolve(null);
return;
}
} catch(e) { reject(e); return; }
});
} catch(e) { reject(e); return; }
});
} catch(e) { reject(e); return; }
});
} catch(e) { reject(e); return; }
});
});
};
// Key phrases for common licenses
var licensePhrases = {
'Apache 1.1': 'apache (software)? license,? (version)? 1\\.1',
'Apache 2': 'apache (software)? license,? (version)? 2',
'Original BSD': 'all advertising materials mentioning features or use of this software must display the following acknowledgement',
'New BSD': 'may be used to endorse or promote products derived from this software without specific prior written permission',
'BSD': 'redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met',
'GPLv2': 'gnu general public license,? version 2',
'GPLv3': 'gnu general public license,? version 3',
'GPL': 'gnu general public license',
'LGPLv2.0': 'gnu library general public license,? version 2',
'LGPLv2.1': 'gnu lesser general public license,? version 2\\.1',
'LGPLv3': 'gnu lesser general public license,? version 3',
'LGPL': 'gnu (library|lesser) general public license',
'MIT': '\\(?(mit|expat|x11)\\)? license|permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files',
'MPL 1.1': 'mozilla public license,? (\\(MPL\\) )?(version |v|v\\.)?1\\.1',
'MPL 2': 'mozilla public license,? (\\(MPL\\) )?(version |v|v\\.)?2',
'MPL': 'mozilla public license',
'CDDL': 'common development and distribution license',
'Eclipse': 'eclipse public license',
'Artistic': 'artistic license',
'zlib': 'the origin of this software must not be misrepresented',
'AGPLv1': 'affero general public license,? version 1',
'AGPLv3': 'affero general public license,? version 3',
'AGPL': 'affero general public license',
'ISC': 'permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted',
'CC0': 'cc0',
'Unlicense': 'this is free and unencumbered software released into the public domain',
}
var licenseCodes = Object.keys(licensePhrases);
var spaceMetaRegex = new RegExp(' ', 'g');
// Spaces can be any whitespace
for (var i = 0; i < licenseCodes.length; i++) {
licensePhrases[licenseCodes[i]] = licensePhrases[licenseCodes[i]].replace(spaceMetaRegex, '\\s+');
}
// Try to guess the license based on the text and return an abbreviated name (or null if not recognized).
var guessLicense = function(text) {
var licenseRegex;
for (var i = 0; i < licenseCodes.length; i++) {
licenseRegex = licensePhrases[licenseCodes[i]];
if (text.match(new RegExp(licenseRegex, 'i'))) {
return licenseCodes[i];
}
}
// Not a recognized license
return null;
};
var shieldsBadge = function(badgeData) {
return ('https://img.shields.io/badge/'
+ escapeField(badgeData.text[0])
+ '-' + escapeField(badgeData.text[1])
+ '-' + badgeData.colorscheme + '.svg');
};
var escapeField = function(s) {
return encodeURIComponent(s.replace(/-/g, '--').replace(/_/g, '__'));
};
module.exports = suggest;