Merge: Windows: fix bootstrap
[nit.git] / share / nitdoc / js / lib / github-api.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
17 */
18
19 /*
20 * GitHub API wrapper for github plugin
21 */
22 define([
23 "jquery",
24 "utils"
25 ], function($, Utils) {
26 return {
27
28 // try to login the user to github API
29 login: function(user) {
30 var res = false;
31 $.ajax({
32 beforeSend: function (xhr) {
33 xhr.setRequestHeader ("Authorization", user.auth);
34 },
35 type: "GET",
36 url: "https://api.github.com/repos/" + user.login + "/" + user.repo,
37 async: false,
38 dataType: 'json',
39 success: function() {
40 res = true;
41 }
42 });
43 user.infos = this.getUserInfos(user);
44 user.signedOff = this.getSignedOff(user)
45 return res;
46 },
47
48 // request for user github account infos
49 getUserInfos: function(user) {
50 var res = false;
51 $.ajax({
52 beforeSend: function (xhr) {
53 xhr.setRequestHeader ("Authorization", user.auth);
54 },
55 type: "GET",
56 url: "https://api.github.com/users/" + user.login,
57 async: false,
58 dataType: 'json',
59 success: function(response) {
60 res = response;
61 },
62 error: function(response) {
63 res = response;
64 }
65 });
66 return res;
67 },
68
69 // build signedoff user default signature
70 getSignedOff: function(user) {
71 return user.infos.name + " <" + user.infos.email + ">";
72 },
73
74 // get the branches list from a repo
75 getBranches: function(user) {
76 var res = false;
77 $.ajax({
78 beforeSend: function (xhr) {
79 xhr.setRequestHeader ("Authorization", user.auth);
80 },
81 type: "GET",
82 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/branches",
83 async: false,
84 dataType: 'json',
85 success: function(response) {
86 res = response;
87 },
88 error: function(response) {
89 res = response;
90 }
91 });
92 return res;
93 },
94
95 /* GitHub commits */
96
97 // get the latest commit on `branchName`
98 getCommit: function(user, sha) {
99 var res = false;
100 $.ajax({
101 beforeSend: function (xhr) {
102 xhr.setRequestHeader ("Authorization", user.auth);
103 },
104 type: "GET",
105 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/git/commits/" + sha,
106 async: false,
107 dataType: 'json',
108 success: function(response) {
109 res = response;
110 },
111 error: function(response) {
112 res = response;
113 }
114 });
115 return res;
116 },
117
118 // get the base tree for a commit sha
119 getTree: function(user, sha) {
120 var res = false;
121 $.ajax({
122 beforeSend: function (xhr) {
123 xhr.setRequestHeader ("Authorization", user.auth);
124 },
125 type: "GET",
126 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/git/trees/" + sha + "?recursive=1",
127 async: false,
128 dataType: 'json',
129 success: function(response) {
130 res = response;
131 },
132 error: function(response) {
133 res = response;
134 }
135 });
136 return res;
137 },
138
139 // create a new blob
140 createBlob: function(user, content) {
141 var res = false;
142 $.ajax({
143 beforeSend: function (xhr) {
144 xhr.setRequestHeader ("Authorization", user.auth);
145 },
146 type: "POST",
147 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/git/blobs",
148 async: false,
149 dataType: 'json',
150 data: JSON.stringify({
151 content: content.base64Encode(),
152 encoding: "base64"
153 }),
154 success: function(response) {
155 res = response;
156 },
157 error: function(response) {
158 res = response;
159 }
160 });
161 return res;
162 },
163
164 // create a new tree from a base tree
165 createTree: function(user, baseTree, path, blob) {
166 var res = false;
167 $.ajax({
168 beforeSend: function (xhr) {
169 xhr.setRequestHeader ("Authorization", user.auth);
170 },
171 type: "POST",
172 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/git/trees",
173 data: JSON.stringify({
174 base_tree: baseTree.sha,
175 tree: [{
176 path: path,
177 mode: "100644", // file (blob)
178 type: "blob",
179 sha: blob.sha
180 }]
181 }),
182 async: false,
183 dataType: 'json',
184 success: function(response) {
185 res = response;
186 },
187 error: function(response) {
188 res = response;
189 }
190 });
191 return res;
192 },
193
194 // create a new commit
195 createCommit: function(user, message, parentCommit, tree) {
196 var res = false;
197 $.ajax({
198 beforeSend: function (xhr) {
199 xhr.setRequestHeader ("Authorization", user.auth);
200 },
201 type: "POST",
202 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/git/commits",
203 data: JSON.stringify({
204 message: message,
205 parents: parentCommit,
206 tree: tree.sha,
207 }),
208 async: false,
209 dataType: 'json',
210 success: function(response) {
211 res = response;
212 },
213 error: function(response) {
214 res = response;
215 }
216 });
217 return res;
218 },
219
220 // create a pull request
221 createPullRequest: function(user, title, body, origin, head) {
222 var res = false;
223 $.ajax({
224 beforeSend: function (xhr) {
225 xhr.setRequestHeader ("Authorization", user.auth);
226 },
227 type: "POST",
228 url: "https://api.github.com/repos/" + origin.user + "/" + origin.repo + "/pulls",
229 data: JSON.stringify({
230 title: title,
231 body: body,
232 base: origin.branch,
233 head: user.login + ":" + head
234 }),
235 async: false,
236 dataType: 'json',
237 success: function(response) {
238 res = response;
239 },
240 error: function(response) {
241 res = response;
242 }
243 });
244 return res;
245 },
246
247 // update a pull request
248 updatePullRequest: function(user, title, body, state, request) {
249 var res = false;
250 $.ajax({
251 beforeSend: function (xhr) {
252 xhr.setRequestHeader ("Authorization", user.auth);
253 },
254 type: "PATCH",
255 url: request.url,
256 data: JSON.stringify({
257 title: title,
258 body: body,
259 state: state
260 }),
261 async: false,
262 dataType: 'json',
263 success: function(response) {
264 res = response;
265 },
266 error: function(response) {
267 res = response;
268 }
269 });
270 return res;
271 },
272
273 /* Files */
274
275 getFile: function(user, path, branch) {
276 var res = false;
277 $.ajax({
278 type: "GET",
279 url: "https://api.github.com/repos/" + user.login + "/" + user.repo + "/contents/" + path,
280 data: {
281 ref: branch
282 },
283 async: false,
284 dataType: 'json',
285 success: function(response) {
286 res = response;
287 },
288 error: function(response) {
289 res = response;
290 }
291 });
292 return res;
293 }
294 }
295 });