-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
197 lines (164 loc) · 5.57 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
197
const express = require('express');
const async = require('async');
const request = require('superagent');
const printf = require('printf');
const https = require('https');
const marked = require('marked');
const cheerio = require('cheerio');
const redis = require('redis');
const crypto = require('crypto');
const url_is_badge = require('./lib/url_is_badge');
var redisCache = redis.createClient(process.env.REDIS_URL);
redisCache.on('error', function (err) {
console.error('Redis error:', err);
});
var app = express();
app.set('x-powered-by', false);
app.get('/', function(req, res, next) {
res.redirect('https://github.com/defunctzombie/badginator');
});
app.get('/:org/:repo.svg', function(req, res, next) {
var org = req.params.org;
var repo = req.params.repo;
// if badginator request, serve up dummy badge
if (req.headers['badginator-request'] === 'sup dog') {
var url = printf('https://img.shields.io/badge/badges-dummy-green.svg');
https.get(url, function(img_res) {
res.set({
'content-type': img_res.headers['content-type'],
'cache-control': 'no-cache, no-store, must-revalidate',
});
img_res.pipe(res);
});
return;
}
var count_fn = process.env.USE_REGEX_BADGES ? regex_count_readme_badges : count_readme_badges;
if (req.query.image_analysis) {
count_fn = count_readme_badges;
}
count_fn({
org: org,
repo: repo,
}, function(err, badge_count) {
if (err) {
return next(err);
}
var color = 'red';
if (badge_count <= 2) {
color = 'red';
}
else if (badge_count <= 4) {
color = 'orange';
}
else if (badge_count <= 6) {
color = 'green';
}
else {
color = 'brightgreen';
}
var etag = badge_count + color;
var style = req.query.style || 'flat';
var url = printf('https://img.shields.io/badge/badges-%d-%s.svg?style=%s', badge_count, color, style);
if (process.env.USE_REDIRECT_URL === '1') {
return res.redirect(url);
}
https.get(url, function(img_res) {
res.set({
'content-type': img_res.headers['content-type'],
'cache-control': 'no-cache, no-store, must-revalidate',
'etag': etag,
});
img_res.pipe(res);
});
});
});
function regex_count_readme_badges(opt, cb) {
fetch_readme(opt, function(err, readme) {
if (err) {
return cb(err);
}
// remove markdown sections
readme = readme.replace(/```[^]*?```/g, '');
// count instances of [![](.svg or .png)]
// followed by () which is a link
var regexp = /(\[[!]\[.*\]\(.*(\.svg|\.png).*\)\]\(.*\)|[!]\[.*\]\(.*(\.svg|\.png).*\))/g;
var count = 0;
while(regexp.exec(readme) !== null) {
count++;
}
cb(null, count);
});
};
function count_readme_badges(opt, cb) {
fetch_readme(opt, function(err, readme) {
if (err) {
return cb(err);
}
// process readme
var html = marked(readme);
var $ = cheerio.load(html);
var urls = $('img').map(function(idx, img) {
return $(img).attr('src');
}).get();
var count = 0;
async.each(urls, function(url, done) {
//async.reduce(urls, 0, function(count, url, done) {
// this is gnarly and could be a function now
var hash = crypto.createHash('md5').update(url).digest('hex');
var url_key = 'url-cache:' + hash;
redisCache.get(url_key, function(err, val) {
if (err) {
console.error(err);
return done();
}
// have cached value
if (val !== null) {
count += (val === 'true' ? 1 : 0);
// redis values are strings, yay
//return done(null, count + (val === 'true' ? 1 : 0));
return done();
}
url_is_badge(url, function(err, is_badge) {
if (err) {
console.error(err);
}
else {
redisCache.setex(url_key, 60 * 15, is_badge, function(err) {
if (err) {
console.error(err);
}
});
}
count += (is_badge ? 1 : 0);
done();
//done(null, count + (is_badge ? 1 : 0));
});
});
}, function(err) {
cb(err, count);
});
});
}
function fetch_readme(opt, cb) {
var org = opt.org;
var repo = opt.repo;
var base_url = printf('https://raw.githubusercontent.com/%s/%s/master/', org, repo);
var readme_variants = ['README.md', 'readme.md', 'Readme.md', 'README.MD', 'readme.markdown'];
var readme_text = '';
async.some(readme_variants, function(name, cb) {
var url = base_url + name;
request.get(url).end(function(err, res) {
if (err) {
return cb(false);
}
if (res.status !== 200) {
return cb(false);
}
readme_text = res.text;
cb(true);
});
}, function(found) {
cb(null, readme_text);
});
}
app.listen(process.env.PORT || 3000);