-
Notifications
You must be signed in to change notification settings - Fork 46
/
github.js
322 lines (302 loc) · 7.68 KB
/
github.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import {
getUser,
getReposForUser,
getCommitsForRepo,
getRepoForUser,
getIssuesForRepo,
getCommentsForIssue,
getTreeForRepo,
getStatusesForRepo,
getBranchesForRepo,
} from './apis/github';
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLEnumType,
GraphQLInt,
GraphQLList,
GraphQLUnionType,
} from 'graphql';
import { isObject, isString } from 'lodash';
let CommitAuthorType = new GraphQLObjectType({
name : 'GithubCommitAuthor',
description : 'Commit author that is not associated with a Github acount',
fields: {
email : { type : GraphQLString },
name : { type : GraphQLString },
}
});
let StatusType = new GraphQLObjectType({
name : 'GithubStatus',
description : 'Status of a commit',
fields: {
state : { type : GraphQLString },
description : { type : GraphQLString },
target_url: { type : GraphQLString },
context: { type : GraphQLString },
updated_at: { type : GraphQLString }
}
});
let UserType = new GraphQLObjectType({
name : 'GithubUser',
fields() {
return {
login : { type : GraphQLString },
id : { type : GraphQLInt },
company : { type : GraphQLString },
avatar_url : { type : GraphQLString },
repos : {
type : new GraphQLList(RepoType),
resolve(user) {
return getReposForUser(user.login);
}
},
};
}
});
let UserOrCommitAuthorType = new GraphQLUnionType({
name: 'UserOrCommitAuthor',
resolveType: (author) => {
if (isObject(author) && author.login) {
return UserType;
}
return CommitAuthorType;
},
types: [ CommitAuthorType, UserType ]
});
let TreeEntryType = new GraphQLObjectType({
name: 'GithubTreeEntry',
fields() {
return {
path: {
type: GraphQLString
},
last_commit: {
type: CommitType,
resolve(data) {
const path = data.path;
const { username, reponame } = grabUsernameAndReponameFromURL(data.url);
return getCommitsForRepo(username, reponame, { path, limit: 1 })
.then(list => list[0]); // just the commit object
}
}
};
}
})
let TreeType = new GraphQLObjectType({
name: 'GithubTree',
fields() {
return {
entries: {
type: new GraphQLList(TreeEntryType),
resolve(data) {
return data;
}
}
};
}
});
let CommitType = new GraphQLObjectType({
name : 'GithubCommit',
fields() {
return {
sha : { type : GraphQLString },
author : {
type : UserOrCommitAuthorType,
resolve(obj) {
return obj.author || (obj.commit && obj.commit.author);
}
},
message : {
type : GraphQLString,
resolve(commit) {
return commit.commit && commit.commit.message;
}
},
date : {
type : GraphQLString,
resolve(commit) {
return commit.commit && commit.commit.committer.date;
}
},
status : {
type : new GraphQLList(StatusType),
resolve(commit) {
const { username, reponame } = grabUsernameAndReponameFromURL(commit.url);
const { sha } = commit;
return getStatusesForRepo(username, reponame, sha);
}
},
tree: {
type: TreeType,
resolve(commit) {
if (!commit.commit) return null;
const { tree } = commit.commit;
const { username, reponame } = grabUsernameAndReponameFromURL(tree.url);
return commit.commit && getTreeForRepo(username, reponame, tree.sha);
}
}
};
}
});
let IssueCommentType = new GraphQLObjectType({
name : 'GithubIssueCommentType',
fields : {
id : { type : GraphQLInt },
body : { type : GraphQLString },
user : {
type : UserType,
resolve(issueComment) {
return issueComment.user;
}
}
},
});
let IssueLabelType = new GraphQLObjectType({
name : 'GithubIssueLabelType',
fields: {
url : { type : GraphQLString },
name : { type : GraphQLString },
color: { type : GraphQLString }
}
});
let grabUsernameAndReponameFromURL = (url) => {
let array = url.split('https://api.github.com/repos/')[1].split('/');
return {
username : array[0],
reponame : array[1],
};
}
let IssueType = new GraphQLObjectType({
name : 'GithubIssue',
fields : {
id : { type : GraphQLInt },
state: { type : GraphQLString },
title : { type : GraphQLString },
body : { type : GraphQLString },
user : { type : UserType },
assignee : { type : UserType },
closed_by : { type : UserType },
labels : { type : new GraphQLList(IssueLabelType) },
commentCount : {
type : GraphQLInt,
resolve(issue) {
return issue.comments;
}
},
comments : {
type : new GraphQLList(IssueCommentType),
resolve(issue) {
let { username, reponame } = grabUsernameAndReponameFromURL(issue.url);
return getCommentsForIssue(username, reponame, issue);
}
}
}
});
let BranchType = new GraphQLObjectType({
name : 'GithubBranch',
fields : {
name : { type : GraphQLString },
lastCommit: {
type: CommitType,
resolve: (branch) => {
const { ownerUsername, reponame } = branch; // info has been added while loading
return getCommitsForRepo(ownerUsername, reponame, { sha: branch.sha })
.then(list => list[0]); // just the commit object
}
}
}
});
let RepoType = new GraphQLObjectType({
name : 'GithubRepo',
fields : {
id : { type : GraphQLInt },
name : { type : GraphQLString },
commits : {
type : new GraphQLList(CommitType),
args : {
limit: {
type: GraphQLInt
}
},
resolve(repo, args) {
return getCommitsForRepo(repo.owner.login, repo.name, args);
}
},
issues : {
type : new GraphQLList(IssueType),
args : {
limit : { type : GraphQLInt }
},
resolve(repo, { limit }) {
return getIssuesForRepo(repo.owner.login, repo.name).then((issues) => {
if (limit) {
return issues.slice(0, limit);
}
return issues;
});
}
},
branches : {
type : new GraphQLList(BranchType),
args : {
limit : { type : GraphQLInt }
},
resolve(repo, { limit }) {
const ownerUsername = repo.owner.login;
const reponame = repo.name;
return getBranchesForRepo(ownerUsername, reponame).then((branches) => {
// add repo referenceData
return branches.map( (b) => ({reponame, ownerUsername, ...b}) );
}).then((branches) => {
if (limit) {
// Later: optimise query...
return branches.slice(0, limit);
}
return branches;
});
}
},
owner: {
type: UserType
}
}
});
let githubType = new GraphQLObjectType({
name : 'GithubAPI',
description : 'The Github API',
fields : {
user : {
type: UserType,
args : {
username : {
description : 'Username of the user',
type: new GraphQLNonNull(GraphQLString),
}
},
resolve: function(root, { username }) {
return getUser(username);
}
},
repo : {
type : RepoType,
args : {
name : {
description : 'Name of the repo',
type: new GraphQLNonNull(GraphQLString),
},
ownerUsername : {
description : 'Username of the owner',
type: new GraphQLNonNull(GraphQLString),
}
},
resolve: function(root, { ownerUsername, name }) {
return getRepoForUser(ownerUsername,name);
}
}
}
});
export const QueryObjectType = githubType;