-
Notifications
You must be signed in to change notification settings - Fork 0
/
findRepositories.js
87 lines (72 loc) · 1.98 KB
/
findRepositories.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
/**
* @see
* REST API v3: Repositories
* https://developer.github.com/v3/repos/
*
* REST API v3: Search
* https://developer.github.com/v3/search/
*/
'use strict';
const MODULE_REQUIRE = 1
/* built-in */
/* NPM */
, htp = require('htp')
, noda = require('noda')
/* in-package */
, getGithubAgent = noda.inRequire('lib/getGithubAgent')
;
/**
* @param {string} options.token
* @param {string} [options.username] username of repository's owner
* @param {string} [options.orgname]
* @param {string} [options.name]
* @return {Promise}
*/
function run(options) {
let _agent = getGithubAgent(options);
let urlname;
/**
* Search
* https://developer.github.com/v3/search/
*/
if (options.name) {
let Qs = [];
Qs.push(`${options.name}in:name`);
if (options.username) {
Qs.push(`user:${options.username}`);
}
if (options.orgname) {
Qs.push(`org:${options.orgname}`);
}
urlname = `/search/repositories?q=${Qs.join('+')}`;
}
else if (options.username) {
/**
* List user repositories
* https://developer.github.com/v3/repos/#list-user-repositories
*/
urlname = `/user/${options.username}/repos`;
}
else if (options.orgname) {
/**
* List organization repositories
* https://developer.github.com/v3/repos/#list-organization-repositories
*/
urlname = `/orgs/${options.orgname}/repos`;
}
else {
/**
* List your repositories
* https://developer.github.com/v3/repos/#list-your-repositories
*
* Here "you" refers to the token owner.
*/
urlname = '/user/repos';
}
return _agent.get(urlname).then(data => {
if (data == null) return [];
if (data instanceof Array) return data;
if (data.items instanceof Array) return data.items;
});
};
module.exports = run;