-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathprofile.js
96 lines (84 loc) · 2.74 KB
/
profile.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
var async = require("async")
, manifest = require("./manifest")
, brains = require("./brains")
, github = require("./github")
/**
* Get repositories for a user
*
* @param {String} user Username
* @param {String} authToken OAuth access token or null
* @param {Object|Function} [options] Options or callback function
* @param {Number} [options.page] Page of repos to start from
* @param {Array} [options.repos] Repositories retrieved so far
* @param {Number} [options.pageSize] Page size, max 100
* @param {Function} cb Callback function
*/
function getRepos (user, authToken, options, cb) {
// Allow callback to be passed as second parameter
if (!cb) {
cb = options
options = {page: 0, repos: [], pageSize: 100}
} else {
options = options || {page: 0, repos: [], pageSize: 100}
}
setImmediate(function () {
var gh = github.getInstance(authToken)
, repoMethod = authToken ? "getAll" : "getFromUser"
gh.repos[repoMethod]({type: "all", user: user, page: options.page, per_page: options.pageSize}, function (er, data) {
if (er) return cb(er)
if (data.length) {
options.repos = options.repos.concat(data)
if (data.length === options.pageSize) {
// Maybe another page?
options.page++
getRepos(user, authToken, options, cb)
} else {
cb(null, options.repos)
}
} else {
// All done!
cb(null, options.repos)
}
})
})
}
/**
* Create a function to be used with async.parallel that"ll get info from brains for a repo.
*
* @param {String} user Username
* @param {Object} repo Repository data as returned by GitHub API
* @param {String} authToken OAuth access token or null
* @returns {Function}
*/
function createGetInfoTask (user, repo, authToken) {
return function (cb) {
manifest.getManifest(user, repo.name, null, authToken, function (er, manifest) {
// This is fine - perhaps the repo doesn"t have a package.json
if (er) return cb()
brains.getInfo(manifest, function (er, info) {
if (er) return cb(er)
cb(null, {repo: repo, manifest: manifest, info: info})
})
})
}
}
/**
* @param {String} user Username
* @param {String} authToken OAuth access token or null
* @param {Function} cb Passed array of objects with properties repo, info, manifest.
*/
module.exports.get = function (user, authToken, cb) {
getRepos(user, authToken, function (er, repos) {
if (er) return cb(er)
// Get repository status information
async.parallel(
repos.map(function (repo) {
return createGetInfoTask(user, repo, authToken)
}),
function (er, data) {
if (er) return cb(er)
cb(null, data.filter(function (d) {return !!d}))
}
)
})
}