-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
86 lines (70 loc) · 2.05 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
'use strict';
const GitHub = require('github-base');
const orgs = require('orgs');
/**
* Get repositories for one or more users.
*
* ```js
* const repos = require('repos');
* const options = {
* // see github-base for other authentication options
* token: 'YOUR_GITHUB_AUTH_TOKEN'
* };
* repos(['doowb', 'jonschlinkert'], options)
* .then(function(repos) {
* // array of repository objects
* console.log(repos);
* })
* .catch(console.error)
* ```
* @param {String|Array} `users` One or more users or organization names.
* @param {Object} `options` See available [options](#options).
* @return {Promise}
* @api public
*/
module.exports = async (users, options) => {
if (isObject(users)) {
return new GitHub(users).paged('/user/repos');
}
if (typeof users === 'string') {
users = [users];
}
if (!Array.isArray(users)) {
return Promise.reject(new TypeError('expected users to be a string or array'));
}
const acc = { repos: [], names: [] };
const opts = Object.assign({}, options);
const noop = () => true;
const filterRepos = opts.filterRepos || noop;
const filterOrgs = opts.filterOrgs || noop;
// don't pass custom options to module dependencies
delete opts.filterRepos;
delete opts.filterOrgs;
const github = new GitHub(opts);
const arr = await orgs(users, opts);
for (const org of arr) {
if (filterOrgs(org) !== true) continue;
const rep = await github.paged(`/${type(org)}/${org.login}/repos`);
for (const page of rep.pages) {
for (const repo of page.body) {
if (filterRepos(repo, acc) === true) {
acc.names.push(repo.name);
acc.repos.push(repo);
}
}
}
}
if (opts.sort !== false) {
return acc.repos.sort(compare('name'));
}
return acc.repos;
};
function isObject(val) {
return val !== null && typeof val === 'object' && !Array.isArray(val);
}
function compare(prop) {
return (a, b) => (a[prop] < b[prop] ? -1 : a[prop] > b[prop] ? 1 : 0);
}
function type(org) {
return org.type === 'User' ? 'users' : 'orgs';
}