UNPKG

9.66 kBJavaScriptView Raw
1/*
2 * github.js: Handles github requests
3 *
4 * Copyright © 2011 Pavan Kumar Sunkara. All rights reserved
5 */
6
7var github = module.exports;
8
9/*
10 * Requiring modules
11 */
12var moment = require('moment')
13 , async = require('async')
14 , request = require('request')
15 , police = require('../police');
16
17/*
18 * Default configuration
19 */
20github.options = {
21 "protocol": "https",
22 "host": "api.github.com",
23 "url": "https://api.github.com"
24}
25
26/*
27 * Calculate for a single user
28 */
29github.singleUser = function (name, other) {
30 if (other) {
31 var useruri = '';
32 police.winston.info('Getting user data for ' + name.cyan);
33
34 github.get('/users/' + name, function (user) {
35 if (user.type=='User') {
36 useruri = '/users/';
37 } else {
38 useruri = '/orgs/';
39 }
40
41 police.winston.info('Getting repositories for ' + name.cyan);
42
43 github.get(useruri + name + '/repos', function (repos) {
44 github.multipleModule(repos.filter(function (e, i, a) {
45 return e.language=='JavaScript' || e.language=='CoffeeScript';
46 }));
47 })
48 });
49 } else {
50 if (name) {
51 police.winston.info('Getting repositories for ' + name.cyan);
52 }
53
54 github.get('/user/repos', function (repos) {
55 github.multipleModule(repos.filter(function (e, i, a) {
56 return e.language=='JavaScript' || e.language=='CoffeeScript';
57 }));
58 });
59 }
60}
61
62/*
63 * Calculate iterating over array of repos
64 */
65github.multipleModule = function (repos) {
66 async.forEachSeries(repos, function (repo, callback) {
67 github.singleModule(repo.owner.login + '/' + repo.name, callback);
68 }, function (err) {
69 police.exit(err);
70 });
71}
72
73/*
74 * Calculate for a single module
75 */
76github.singleModule = function (name, callback) {
77 police.winston.info('Policing ' + name.cyan);
78 github.refs(name, function (ref) {
79
80 if (ref.object) {
81 police.winston.info(' Getting commit ' + ref.object.sha.substr(0,10).magenta);
82 github.commit(name, ref, function (commit) {
83
84 police.winston.info(' Getting tree ' + commit.tree.sha.substr(0,10).magenta);
85 github.tree(name, commit, function (tree) {
86 var blob = tree.tree.filter(function (e, i, a) {
87 return e.path=='package.json' && e.type=='blob';
88 });
89
90 if (blob.length==1) {
91 police.winston.info(' Getting blob ' + blob[0].sha.substr(0,10).magenta);
92 github.blob(name, blob[0].sha, function (body) {
93 var content = Buffer.from(body.content, 'base64').toString('utf8');
94 var pkg = JSON.parse(content);
95
96 if (police.add || police.edit || police.update) {
97 var sname = name.split('/');
98 police.ask.properties(pkg, {name: name, user: sname[0], tree: tree.sha, mode: blob[0].mode, commit: commit.sha, content: content});
99 }
100
101 police.check.pkg(pkg, false, callback);
102 });
103 } else {
104 police.winston.warn('package.json not found on master'.red.bold);
105
106 if (callback===police.exit) {
107 callback(1);
108 } else {
109 callback();
110 }
111 }
112 });
113 });
114 } else {
115 police.winston.warn('Repository is MIA (missing in action)'.red.bold);
116 police.winston.warn(res.request.uri.href.red);
117 police.winston.warn('Please authenticate'.red.bold);
118
119 if (callback===police.exit) {
120 callback(1);
121 } else {
122 callback();
123 }
124 }
125 });
126}
127
128/*
129 * Commit with updated package.json
130 */
131github.write = function (data, callback) {
132
133 if (data != police.ask.gh.content) {
134 github.get('/user', function (body) {
135 police.ask.gh.puser = body;
136 police.winston.info(' Updating ' + 'blob'.magenta);
137
138 github.post('/repos/' + police.ask.gh.name + '/git/blobs', {content: data, encoding: 'utf-8'}, function (body) {
139 police.winston.info(' Updating ' + 'tree'.magenta);
140
141 github.post('/repos/' + police.ask.gh.name + '/git/trees', {
142 base_tree: police.ask.gh.tree,
143 tree: [
144 {
145 path: 'package.json',
146 mode: police.ask.gh.mode,
147 type: 'blob',
148 sha: body.sha
149 }
150 ]
151 }, function (body) {
152 police.winston.info(' Updating ' + 'commit'.magenta);
153
154 github.post('/repos/' + police.ask.gh.name + '/git/commits', {
155 message: 'Updated `package.json` using `police`',
156 author: {
157 name: 'Pavan Kumar Sunkara',
158 email: 'pavan.sss1991@gmail.com',
159 date: moment(new Date()).format('YYYY-MM-DDTHH:mm:ssZ')
160 },
161 committer: {
162 name: police.ask.gh.puser.name,
163 email: police.ask.gh.puser.email,
164 date: moment(new Date()).format('YYYY-MM-DDTHH:mm:ssZ')
165 },
166 parents: [ police.ask.gh.commit ],
167 tree: body.sha
168 }, function (body) {
169 police.winston.info(' Updating ' + 'master'.magenta);
170
171 github.put('/repos/' + police.ask.gh.name + '/git/refs/heads/master', {sha: body.sha}, function (body) {
172 callback();
173 });
174 });
175 });
176 });
177 });
178 } else {
179 callback();
180 }
181}
182
183/*
184 * Get github ref/heads/master
185 */
186github.refs = function (name, cb) {
187 github.get('/repos/' + name + '/git/refs/heads/master', cb);
188}
189
190/*
191 * Get a github commit
192 */
193github.commit = function (name, ref, cb) {
194 github.get('/repos/' + name + '/git/commits/' + ref.object.sha, cb);
195}
196
197/*
198 * Get a github tree
199 */
200github.tree = function (name, commit, cb) {
201 github.get('/repos/' + name + '/git/trees/' + commit.tree.sha, cb);
202}
203
204/*
205 * Get a github blob
206 */
207github.blob = function (name, sha, cb) {
208 github.get('/repos/' + name + '/git/blobs/' + sha, cb);
209}
210
211/*
212 * Get github token
213 */
214github.token = function (auth) {
215 request({
216 url: github.tokenHost(auth) + '/authorizations',
217 method: 'POST',
218 body: JSON.stringify({
219 "scopes": ["repo"],
220 "note": "npm-police from terminal"
221 }),
222 headers: {
223 "User-Agent": "npm-police/0.4 terminal/0.0",
224 "Content-type": "application/json"
225 },
226 proxy: process.env.https_proxy
227 }, function (err, res, body) {
228 if (err) {
229 police.exit(err);
230 }
231
232 if (res.statusCode==201) {
233 police.winston.info('Authenticated to github as ' + auth.username.cyan);
234 body = JSON.parse(body);
235
236 police.config.set('name', auth.username);
237 police.config.set('token', body.token);
238 police.config.set('id', body.id);
239 police.config.save(police);
240 } else {
241 police.winston.warn("Bad credentials, Unable to login".red.bold);
242 police.exit(1);
243 }
244 });
245}
246
247/*
248 * Build github token host
249 */
250github.tokenHost = function (auth) {
251 return github.options.protocol + '://' + auth.username + ':' + encodeURIComponent(auth.password) + '@' + github.options.host;
252}
253
254/*
255 * Github api GET request
256 */
257github.get = function (uri, callback) {
258 request({
259 url: github.options.url + uri,
260 headers: {
261 "User-Agent": "npm-police/0.4 terminal/0.0",
262 "Authorization": "token " + police.config.token(police)
263 },
264 proxy: process.env.https_proxy
265 }, function (err, res, body) {
266 if (err) police.exit(err);
267 if (res.statusCode==404) {
268 police.winston.warn('Page is MIA (missing in action)'.red.bold);
269 police.winston.warn(res.request.uri.href.red);
270 police.exit(1);
271 } else {
272 callback(JSON.parse(body));
273 }
274 });
275}
276
277/*
278 * Github api POST request
279 */
280github.post = function (uri, content, callback) {
281 request({
282 url: github.options.url + uri,
283 method: 'POST',
284 body: JSON.stringify(content),
285 headers: {
286 "User-Agent": "npm-police/0.4 terminal/0.0",
287 "Content-type": "application/json",
288 "Authorization": "token " + police.config.token(police)
289 },
290 proxy: process.env.https_proxy
291 }, function (err, res, body) {
292 if (err) police.exit(err);
293 if (res.statusCode==404) {
294 police.winston.warn('Page is MIA (missing in action)'.red.bold);
295 police.winston.warn(res.request.uri.href.red);
296 police.exit(1);
297 } else {
298 callback(JSON.parse(body));
299 }
300 });
301}
302
303/*
304 * Github api PUT request
305 */
306github.put = function (uri, content, callback) {
307 request({
308 url: github.options.url + uri,
309 method: 'PUT',
310 body: JSON.stringify(content),
311 headers: {
312 "User-Agent": "npm-police/0.4 terminal/0.0",
313 "Content-type": "application/json",
314 "Authorization": "token " + police.config.token(police)
315 },
316 proxy: process.env.https_proxy
317 }, function (err, res, body) {
318 if (err) police.exit(err);
319 if (res.statusCode==404) {
320 police.winston.warn('Page is MIA (missing in action)'.red.bold);
321 police.winston.warn(res.request.uri.href.red);
322 police.exit(1);
323 } else {
324 callback(JSON.parse(body));
325 }
326 });
327}
328
329/*
330 * Github api DELETE request
331 */
332github.del = function (uri, content, callback) {
333 request({
334 url: github.options.url + uri,
335 method: 'DELETE',
336 body: JSON.stringify(content),
337 headers: {
338 "User-Agent": "npm-police/0.4 terminal/0.0",
339 "Content-type": "application/json",
340 "Authorization": "token " + police.config.token(police)
341 },
342 proxy: process.env.https_proxy
343 }, function (err, res, body) {
344 if (err) police.exit(err);
345 if (res.statusCode==404) {
346 police.winston.warn('Page is MIA (missing in action)'.red.bold);
347 police.winston.warn(res.request.uri.href.red);
348 police.exit(1);
349 } else {
350 callback(JSON.parse(body));
351 }
352 });
353}