-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-search.js
More file actions
68 lines (62 loc) · 1.76 KB
/
github-search.js
File metadata and controls
68 lines (62 loc) · 1.76 KB
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
/**
* @author Alex Jayaraj
*
* Last run node index.js
*
*/
/*jshint esversion: 6 */
'use strict';
var util = require('util');
var GitHubApi = require('github');
var fs = require('fs');
var async = require('async');
var userRepos = {}
var github = new GitHubApi({});
if (process.env['GIT_TOKEN']) {
github.authenticate({
type: 'oauth',
token: process.env.GIT_TOKEN
});
}
exports.searchGIT = function(keyword, user, cb) {
var response = {};
var repos = [];
github.search.code({
q: keyword + '+user:' + user
}).then(result => {
var res = result;
async.whilst(
function() {
return github.hasNextPage(result);
},
function(callback) {
handleResults(result, repos);
github.getNextPage(result)
.then(res => {
result = res;
callback();
});
},
function(err) {
handleResults(result, repos);
cb(null, repos);
console.log('Done');
}
);
}).catch(err => {
console.log('Query failed for user ' + user + '. May be no repositories created. Ignore this error.' + util.inspect(err));
cb(null, repos);
});
};
// var processed = 0;
function handleResults (result, repos) {
var items = result.data.items;
items.forEach(function(item) {
if (!userRepos[item.repository.full_name]) {
userRepos[item.repository.full_name] = item.repository.url;
repos.push(item.repository.html_url);
}
});
// processed += result.data.items.length;
// console.log("Processed: " + processed);
}