forked from allevaton/starterupper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
287 lines (271 loc) · 9.71 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
// Quick and dirty Github Javascript API wrapper
// Workarounds for legacy browsers
// Courtesy: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
if ( !Date.prototype.toISOString ) {
( function() {
function pad(number) {
var r = String(number);
if ( r.length === 1 ) {
r = '0' + r;
}
return r;
}
Date.prototype.toISOString = function() {
return this.getUTCFullYear()
+ '-' + pad( this.getUTCMonth() + 1 )
+ '-' + pad( this.getUTCDate() )
+ 'T' + pad( this.getUTCHours() )
+ ':' + pad( this.getUTCMinutes() )
+ ':' + pad( this.getUTCSeconds() )
+ '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
+ 'Z';
};
}() );
}
// btoa (worry about this) IE10+
// see: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22
// JSON.stringify (don't worry about this) IE8+
// see: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
var Github = {
badCredentials: false,
setOTP: false,
username: "",
password: "",
otp: "",
authenticated: function () {
return localStorage.hasOwnProperty("Github.token") && localStorage.hasOwnProperty("Github.username");
},
getAuthorization: function () {
if (localStorage.hasOwnProperty("Github.token")) {
return "token " + localStorage.getItem("Github.token");
} else {
return "Basic " + btoa(Github.username + ":" + Github.password)
}
},
getUsername: function() {
return localStorage.getItem("Github.username");
},
// Generic Github API invoker
invoke: function (settings) {
$.ajax({
crossDomain: true,
url: "https://api.github.com" + settings.url,
type: settings.method,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", Github.getAuthorization());
xhr.setRequestHeader("Content-Type", "application/json");
if (Github.setOTP) {
xhr.setRequestHeader("X-Github-OTP", Github.otp);
// It is one time, after all
Github.setOTP=false;
}
},
contentType: "application/json",
dataType: "json",
processData: false,
data: JSON.stringify(settings.data),
success: settings.success,
error: settings.fail
});
},
// Login to Github
// Github.login({
// authenticated: function() { /* Do this when authenticated */ },
// badCredential: function() { /* Do this if we have a bad credential */ },
// twoFactor: function() { /* Do this if we need a one time password */ }
// })
login: function (settings) {
// username could be the email or Github username
Github.username = settings.username;
Github.password = settings.password;
Github.otp = settings.otp;
var date = new Date();
if (Github.authenticated()) {
settings.authenticated();
} else {
Github.invoke({
url: "/authorizations",
method: "POST",
data: {
scopes: ["repo","public_repo","user","write:public_key","user:email"],
note: "starterupper " + date.toISOString()
},
success: function (data) {
Github.badCredentials = false;
localStorage.setItem("Github.token", data.token);
Github.getUser({
success: function (response) {
// Change Github.username to Github login
localStorage.setItem("Github.username", response.login);
settings.authenticated();
}
});
},
fail: function (response) {
if (response.status == 401) {
// We should be looking at the response headers instead, probably: response.getResponseHeader('some_header')
if (response.responseJSON.message == "Bad credentials") {
Github.badCredentials = true;
settings.badCredential();
} else if (response.responseJSON.message == "Must specify two-factor authentication OTP code.") {
Github.setOTP = true;
settings.twoFactor();
}
}
}
});
}
},
logout: function() {
localStorage.removeItem('Github.token');
localStorage.removeItem('Github.username');
},
// Get email configuration
// Github.getEmail({
// email: "something@domain",
// added: function() {/* what do we do when the email has been added? */}
// missing: function() {/* what do we do when the email wasn't even added? */}
// verified: function() {/* what do we do when verified? */}
// unverified: function() {/* what do we do when the email is added, but unverified? */}
// fail: function() {/* what do we do when things aren't working? */}
// })
getEmail: function(settings) {
Github.invoke({
url: "/user/emails",
method: "GET",
data: {},
success: function (response) {
for (index in response) {
if (response[index].email == settings.email) {
settings.added();
if (response[index].verified) {
settings.verified();
return;
} else {
settings.unverified();
return;
}
}
}
settings.missing();
},
fail: settings.fail
});
},
// Github.setEmail({
// email: "something@domain",
// success: function() {/* what do we do if it worked? */},
// fail: function() {/* what do we do if it didn't */}
// })
setEmail: function(settings) {
Github.invoke({
url: "/user/emails",
method: "POST",
data: [settings.email],
success: settings.success,
fail: settings.fail
});
},
getUser: function(settings) {
Github.invoke({
url: "/user",
method: "GET",
data: {},
success: settings.success,
fail: settings.fail
});
},
setUser: function(settings) {
Github.invoke({
url: "/user",
method: "PATCH",
data: settings.data,
success: settings.success,
fail: settings.fail
});
},
// Github.shareKey({
// key: "Public SSH key here",
// title: "some title here",
// success: function() {/* what to do if it worked */},
// fail: function() {/* what to do if it didn't */}
//});
shareKey: function(settings) {
Github.invoke({
url: "/user/keys",
method: "GET",
data: {},
success: function(response) {
for (index in response) {
if (response[index].key == settings.key) {
settings.success(response);
return;
}
}
// Send key
Github.invoke({
url: "/user/keys",
method: "POST",
data: {
title: settings.title,
key: settings.key
},
success: settings.success,
fail: settings.fail
});
},
fail: settings.fail
});
},
// Github.createRepo({
// repo: Repository name,
// success: function() {/* what to do if it worked */},
// fail: function() {/* what to do if it didn't */}
//});
createRepo: function(settings) {
Github.invoke({
url: "/repos/" + Github.getUsername() + "/" + settings.repo,
method: "GET",
data: {},
// If the repo is created already, we're done
success: settings.success,
// Otherwise, we need to make it
fail: function(response) {
Github.invoke({
url: "/user/repos",
method: "POST",
data: {
name: settings.repo,
"private": true
},
success: settings.success,
fail: settings.fail
});
}
});
},
// Github.addCollaborator({
// repo: Repository name,
// collaborator: a collaborator,
// success: function() {/* what to do if it worked */},
// fail: function() {/* what to do if it didn't */}
//});
addCollaborator: function(settings) {
var url = "/repos/" + Github.getUsername() + "/" + settings.repo + "/collaborators/" + settings.collaborator;
Github.invoke({
method: "GET",
url: url,
data: {},
success: settings.success,
fail: function() {
Github.invoke({
method: "PUT",
url: url,
data: {},
success: settings.success,
fail: settings.fail
});
}
});
}
}