-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (182 loc) · 5.68 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Array utils
*/
function flatten(array) {
return array.reduce((acc, item) => {
Array.isArray(item) ? acc.push(...item) : acc.push(item);
return acc;
}, []);
}
/**
* Main class
*/
class Main {
constructor(github, {debug = false} = {}) {
this.github = github;
/**
* Connection utils
*/
function _getAllPages({resArray, count}, res) {
resArray.push(res);
debug && console.log("- getNextPage", resArray.length);
if (count && resArray.reduce((sum, res) => sum + res.data.length, 0) > count) {
return resArray;
}
if (/rel="next"/.test(res.meta.link)) {
return github.getNextPage(res).then(_getAllPages.bind(null, {resArray, count}));
}
return resArray;
}
function getAllPages(opts = {}) {
return _getAllPages.bind(null, {...opts, resArray: []});
}
function retryOnConnectionIssues(cb) {
return cb()
.catch(error => {
if (/ECONNRESET/.test(error.message)) {
debug && console.log(`${error.message}, retrying...`);
return cb();
}
throw error;
});
}
/**
* User class
*/
class User {
static getAll(opts) {
debug && console.log("Fetching all users (this might take awhile)...");
return github.users.getAll({since: 0})
.then(getAllPages(opts))
.then(resArray => {
resArray = resArray.map(res => res.data);
return flatten(resArray);
})
.then(usersProps => usersProps.map(userProps => new User(userProps)));
}
constructor(props) {
Object.assign(this, props);
}
getRepos() {
return Repo.getAll({user: this});
}
}
/**
* Repo class
*/
class Repo {
static getAll({user, otherProps}) {
const username = user.login;
debug && console.log(`Fetching all repos from user ${username}...`);
return github.repos.getForUser({username})
.then(getAllPages(otherProps))
.then(resArray => {
resArray = resArray.map(res => res.data);
return flatten(resArray);
})
.then(reposProps => reposProps.map(repoProps => new Repo(repoProps, {user})));
}
constructor(props, {user}) {
Object.assign(this, props);
Object.defineProperty(this, "user", {get: () => user});
}
getBranch(branch) {
return Branch.get({
repo: this,
user: this.user,
branch
});
}
getBranches() {
return Branch.getAll({
repo: this,
user: this.user
});
}
}
/**
* Branch class
*/
class Branch {
static get({user, repo, branch}) {
const owner = user.login;
return retryOnConnectionIssues(() => {
return github.repos.getBranch({owner, repo: repo.name, branch})
.then(res => res.data);
})
.then(props => new Branch(props, {user, repo}))
.catch(error => {
if (error.status === "Not Found") {
return null;
}
throw error;
});
}
static getAll({user, repo}) {
const owner = user.login;
debug && console.log(`Fetching all branches of ${owner}/${repo.name}...`);
return github.repos.getBranches({owner, repo: repo.name})
.then(getAllPages())
.then(resArray => {
resArray = resArray.map(res => res.data);
return flatten(resArray);
})
.then(branchesProps => branchesProps.map(branchProps => new Branch(branchProps, {user, repo})));
}
constructor(props, {user, repo}) {
Object.assign(this, props);
Object.defineProperty(this, "user", {get: () => user});
Object.defineProperty(this, "repo", {get: () => repo});
}
getContent({path, ref = this.name, ...otherProps} = {}) {
const repo = this.repo.name;
const owner = this.user.login;
return github.repos.getContent({owner, repo, path, ref, ...otherProps})
.then(res => {
let data = res.data;
if (data.content) {
data.content = new Buffer(data.content, "base64").toString("utf8");
}
return data;
});
}
getCommit({sha = this.commit.sha, ...otherProps} = {}) {
sha = sha || this.commit.sha;
const repo = this.repo.name;
const owner = this.user.login;
return github.repos.getCommit({owner, repo, sha, ...otherProps})
.then(res => res.data);
}
getCommits({sha = this.commit.sha, ...otherProps} = {}) {
sha = sha || this.commit.sha;
const repo = this.repo.name;
const owner = this.user.login;
return github.repos.getCommits({owner, repo, sha, ...otherProps})
.then(getAllPages(otherProps))
.then(resArray => {
resArray = resArray.map(res => res.data);
return flatten(resArray);
});
// TODO .then(commitsProps => commitsProps.map(commitProps => new Commit(commitProps, {user, ...??})));
}
getTree({sha = this.commit.sha, recursive = false} = {}) {
const repo = this.repo.name;
const owner = this.user.login;
return retryOnConnectionIssues(() => {
return github.gitdata.getTree({owner, repo, sha, recursive})
.then(res => res.data)
.catch(error => {
if (error.status === "Not Found") {
return {};
}
throw error;
});
});
}
}
this.User = User;
this.Repo = Repo;
this.Branch = Branch;
}
}
module.exports = Main;