forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
473 lines (413 loc) · 17.6 KB
/
github.ts
File metadata and controls
473 lines (413 loc) · 17.6 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
namespace pxt.github {
interface GHRef {
ref: string;
url: string;
object: {
sha: string;
type: string;
url: string;
}
}
export interface RefsResult {
refs: pxt.Map<string>;
head?: string;
}
export function useProxy() {
if (U.isNodeJS)
return false // bypass proxy for CLI
if (pxt.appTarget && pxt.appTarget.cloud && pxt.appTarget.cloud.noGithubProxy)
return false // target requests no proxy
return true
}
export interface CachedPackage {
files: Map<string>;
}
// caching
export interface IGithubDb {
loadConfigAsync(repopath: string, tag: string): Promise<pxt.PackageConfig>;
loadPackageAsync(repopath: string, tag: string): Promise<CachedPackage>;
}
export class MemoryGithubDb implements IGithubDb {
private configs: pxt.Map<pxt.PackageConfig> = {};
private packages: pxt.Map<CachedPackage> = {};
private proxyLoadPackageAsync(repopath: string, tag: string): Promise<CachedPackage> {
// cache lookup
const key = `${repopath}/${tag}`;
let res = this.packages[key];
if (res) {
pxt.debug(`github cache ${repopath}/${tag}/text`);
return Promise.resolve(res);
}
// load and cache
return U.httpGetJsonAsync(`${pxt.Cloud.apiRoot}gh/${repopath}/${tag}/text`)
.then(v => this.packages[key] = { files: v });
}
loadConfigAsync(repopath: string, tag: string): Promise<pxt.PackageConfig> {
if (!tag) tag = "master";
// cache lookup
const key = `${repopath}/${tag}`;
let res = this.configs[key];
if (res) {
pxt.debug(`github cache ${repopath}/${tag}/config`);
return Promise.resolve(U.clone(res));
}
const cacheConfig = (v: string) => {
const cfg = JSON.parse(v) as pxt.PackageConfig;
this.configs[key] = cfg;
return U.clone(cfg);
}
// download and cache
if (useProxy()) {
// this is a bit wasteful, we just need pxt.json and download everything
return this.proxyLoadPackageAsync(repopath, tag)
.then(v => cacheConfig(v.files[pxt.CONFIG_NAME]))
}
let url = "https://raw.githubusercontent.com/" + repopath + "/" + tag + "/" + pxt.CONFIG_NAME
return U.httpGetTextAsync(url)
.then(cfg => cacheConfig(cfg));
}
loadPackageAsync(repopath: string, tag: string): Promise<CachedPackage> {
if (!tag) tag = "master";
if (useProxy())
return this.proxyLoadPackageAsync(repopath, tag).then(v => U.clone(v));
return tagToShaAsync(repopath, tag)
.then(sha => {
// cache lookup
const key = `${repopath}/${sha}`;
let res = this.packages[key];
if (res) {
pxt.debug(`github cache ${repopath}/${tag}/text`);
return Promise.resolve(U.clone(res));
}
// load and cache
const pref = "https://raw.githubusercontent.com/" + repopath + "/" + sha + "/"
pxt.log(`Downloading ${repopath}/${tag} -> ${sha}`)
return U.httpGetTextAsync(pref + pxt.CONFIG_NAME)
.then(pkg => {
const current: CachedPackage = {
files: {}
}
current.files[pxt.CONFIG_NAME] = pkg
const cfg: pxt.PackageConfig = JSON.parse(pkg)
return Promise.map(cfg.files.concat(cfg.testFiles || []),
fn => U.httpGetTextAsync(pref + fn)
.then(text => {
current.files[fn] = text
}))
.then(() => {
// cache!
this.packages[key] = current;
return U.clone(current);
})
})
})
}
}
// overriden by client
export let db: IGithubDb = new MemoryGithubDb();
export function listRefsAsync(repopath: string, namespace = "tags"): Promise<string[]> {
return listRefsExtAsync(repopath, namespace)
.then(res => Object.keys(res.refs))
}
export function listRefsExtAsync(repopath: string, namespace = "tags"): Promise<RefsResult> {
let head: string = null
let fetch = !useProxy ?
U.httpGetJsonAsync("https://api.github.com/repos/" + repopath + "/git/refs/" + namespace + "/?per_page=100") :
U.httpGetJsonAsync(`${pxt.Cloud.apiRoot}gh/${repopath}/refs`)
.then(r => {
let res = Object.keys(r.refs)
.filter(k => U.startsWith(k, "refs/" + namespace + "/"))
.map(k => ({ ref: k, object: { sha: r.refs[k] } }))
head = r.refs["HEAD"]
return res
})
let clean = (x: string) => x.replace(/^refs\/[^\/]+\//, "")
return fetch.then<RefsResult>((resp: GHRef[]) => {
resp.sort((a, b) => semver.strcmp(clean(a.ref), clean(b.ref)))
let r: pxt.Map<string> = {}
for (let obj of resp) {
r[clean(obj.ref)] = obj.object.sha
}
return { refs: r, head }
}, err => {
if (err.statusCode == 404) return { refs: {} }
else return Promise.reject(err)
})
}
function resolveRefAsync(r: GHRef): Promise<string> {
if (r.object.type == "commit")
return Promise.resolve(r.object.sha)
else if (r.object.type == "tag")
return U.httpGetJsonAsync(r.object.url)
.then((r: GHRef) =>
r.object.type == "commit" ? r.object.sha :
Promise.reject(new Error("Bad type (2nd order) " + r.object.type)))
else
return Promise.reject(new Error("Bad type " + r.object.type))
}
function tagToShaAsync(repopath: string, tag: string) {
if (/^[a-f0-9]{40}$/.test(tag))
return Promise.resolve(tag)
return U.httpGetJsonAsync("https://api.github.com/repos/" + repopath + "/git/refs/tags/" + tag)
.then(resolveRefAsync, e =>
U.httpGetJsonAsync("https://api.github.com/repos/" + repopath + "/git/refs/heads/" + tag)
.then(resolveRefAsync))
}
export function pkgConfigAsync(repopath: string, tag = "master") {
return db.loadConfigAsync(repopath, tag)
}
export function downloadPackageAsync(repoWithTag: string, config: pxt.PackagesConfig): Promise<CachedPackage> {
let p = parseRepoId(repoWithTag)
if (!p) {
pxt.log('Unknown github syntax');
return Promise.resolve<CachedPackage>(undefined);
}
if (isRepoBanned(p, config)) {
pxt.tickEvent("github.download.banned");
pxt.log('Github repo is banned');
return Promise.resolve<CachedPackage>(undefined);
}
return db.loadPackageAsync(p.fullName, p.tag);
}
interface Repo {
id: number;
name: string; // "pxt-microbit-cppsample",
full_name: string; // "Microsoft/pxt-microbit-cppsample",
owner: {
login: string; // "Microsoft",
id: number; // 6154722,
avatar_url: string; // "https://avatars.githubusercontent.com/u/6154722?v=3",
gravatar_id: string; // "",
html_url: string; // "https://github.com/Microsoft",
type: string; // "Organization"
},
private: boolean;
html_url: string; // "https://github.com/Microsoft/pxt-microbit-cppsample",
description: string; // "Sample C++ extension for PXT/microbit",
fork: boolean;
created_at: string; // "2016-05-05T11:18:12Z",
updated_at: string; // "2016-06-20T02:25:03Z",
pushed_at: string; // "2016-05-05T11:59:42Z",
homepage: string; // null,
size: number; // 4
stargazers_count: number;
watchers_count: number;
forks_count: number;
open_issues_count: number;
forks: number;
open_issues: number;
watchers: number;
default_branch: string; // "master",
score: number; // 6.7371006
// non-github, added to track search request
tag?: string;
}
interface SearchResults {
total_count: number;
incomplete_results: boolean;
items: Repo[];
}
export interface ParsedRepo {
owner?: string;
fullName: string;
tag?: string;
}
export enum GitRepoStatus {
Unknown,
Approved,
Banned
}
export interface GitRepo extends ParsedRepo {
name: string;
description: string;
defaultBranch: string;
status?: GitRepoStatus;
}
export function repoIconUrl(repo: GitRepo): string {
if (repo.status != GitRepoStatus.Approved) return undefined;
return mkRepoIconUrl(repo)
}
export function mkRepoIconUrl(repo: ParsedRepo): string {
return Cloud.apiRoot + `gh/${repo.fullName}/icon`;
}
function mkRepo(r: Repo, config: pxt.PackagesConfig, tag?: string): GitRepo {
if (!r) return undefined;
const rr: GitRepo = {
owner: r.owner.login.toLowerCase(),
fullName: r.full_name.toLowerCase(),
name: r.name,
description: r.description,
defaultBranch: r.default_branch,
tag: tag
}
rr.status = repoStatus(rr, config);
return rr;
}
export function repoStatus(rr: ParsedRepo, config: pxt.PackagesConfig): GitRepoStatus {
return isRepoBanned(rr, config) ? GitRepoStatus.Banned
: isRepoApproved(rr, config) ? GitRepoStatus.Approved
: GitRepoStatus.Unknown;
}
function isOrgBanned(repo: ParsedRepo, config: pxt.PackagesConfig): boolean {
if (!config) return false; // don't know
if (!repo || !repo.owner) return true;
if (config.bannedOrgs
&& config.bannedOrgs.some(org => org.toLowerCase() == repo.owner.toLowerCase()))
return true;
return false;
}
function isRepoBanned(repo: ParsedRepo, config: pxt.PackagesConfig): boolean {
if (isOrgBanned(repo, config))
return true;
if (!config) return false; // don't know
if (!repo || !repo.fullName) return true;
if (config.bannedRepos
&& config.bannedRepos.some(fn => fn.toLowerCase() == repo.fullName.toLowerCase()))
return true;
return false;
}
function isOrgApproved(repo: ParsedRepo, config: pxt.PackagesConfig): boolean {
if (!repo || !config) return false;
if (repo.owner
&& config.approvedOrgs
&& config.approvedOrgs.some(org => org.toLowerCase() == repo.owner.toLowerCase()))
return true;
return false;
}
function isRepoApproved(repo: ParsedRepo, config: pxt.PackagesConfig): boolean {
if (isOrgApproved(repo, config))
return true;
if (!repo || !config) return false;
if (repo.fullName
&& config.approvedRepos
&& config.approvedRepos.some(fn => fn.toLowerCase() == repo.fullName.toLowerCase()))
return true;
return false;
}
export function repoAsync(id: string, config: pxt.PackagesConfig): Promise<GitRepo> {
const rid = parseRepoId(id);
const status = repoStatus(rid, config);
if (status == GitRepoStatus.Banned)
return Promise.resolve<GitRepo>(undefined);
if (!useProxy())
return U.httpGetJsonAsync("https://api.github.com/repos/" + rid.fullName)
.then((r: Repo) => mkRepo(r, config, rid.tag));
// always use proxy
return Util.httpGetJsonAsync(`${pxt.Cloud.apiRoot}gh/${rid.fullName}`)
.then(meta => {
if (!meta) return undefined;
return {
github: true,
owner: rid.owner,
fullName: rid.fullName,
name: meta.name,
description: meta.description,
defaultBranch: "master",
tag: rid.tag,
status
};
})
}
export function searchAsync(query: string, config: pxt.PackagesConfig): Promise<GitRepo[]> {
if (!config) return Promise.resolve([]);
let repos = query.split('|').map(parseRepoUrl).filter(repo => !!repo);
if (repos.length > 0)
return Promise.all(repos.map(id => repoAsync(id.path, config)))
.then(rs => rs.filter(r => r.status != GitRepoStatus.Banned)); // allow deep links to github repos
let fetch = () => useProxy()
? U.httpGetJsonAsync(`${pxt.Cloud.apiRoot}ghsearch/${appTarget.id}/${appTarget.platformid || appTarget.id}?q=`
+ encodeURIComponent(query))
: U.httpGetJsonAsync("https://api.github.com/search/repositories?q="
+ encodeURIComponent(query + ` in:name,description,readme "for PXT/${appTarget.platformid || appTarget.id}"`))
return fetch()
.then((rs: SearchResults) =>
rs.items.map(item => mkRepo(item, config))
.filter(r => r.status == GitRepoStatus.Approved || (config.allowUnapproved && r.status == GitRepoStatus.Unknown)))
.catch(err => []); // offline
}
export function parseRepoUrl(url: string): { repo: string; tag?: string; path?: string; } {
if (!url) return undefined;
let m = /^((https:\/\/)?github.com\/)?([^/]+\/[^/#]+)(#(\w+))?$/i.exec(url.trim());
if (!m) return;
let r: { repo: string; tag?: string; path?: string; } = {
repo: m ? m[3].toLowerCase() : null,
tag: m ? m[5] : null
}
r.path = r.repo + (r.tag ? '#' + r.tag : '');
return r;
}
export function parseRepoId(repo: string): ParsedRepo {
if (!repo) return undefined;
repo = repo.replace(/^github:/i, "")
let m = /([^#]+)(#(.*))?/.exec(repo)
let owner = m ? m[1].split('/')[0].toLowerCase() : undefined;
return {
owner,
fullName: m ? m[1].toLowerCase() : repo.toLowerCase(),
tag: m ? m[3] : null
}
}
export function isGithubId(id: string) {
return id.slice(0, 7) == "github:"
}
export function stringifyRepo(p: ParsedRepo) {
return p ? "github:" + p.fullName.toLowerCase() + "#" + (p.tag || "master") : undefined;
}
export function noramlizeRepoId(id: string) {
return stringifyRepo(parseRepoId(id))
}
export function latestVersionAsync(path: string, config: TargetConfig): Promise<string> {
let parsed = parseRepoId(path)
if (!parsed) return Promise.resolve<string>(null);
return repoAsync(parsed.fullName, config)
.then(scr => {
if (!scr) return undefined;
return listRefsExtAsync(scr.fullName, "tags")
.then(refsRes => {
let tags = Object.keys(refsRes.refs)
tags.reverse()
// only look for vxx.xx.xx tags
tags = tags.filter(t => /^v\d+(\.\d+(\.\d+)?)?$/i.test(t));
if (tags[0])
return Promise.resolve(tags[0])
else
return refsRes.head || tagToShaAsync(scr.fullName, scr.defaultBranch)
})
});
}
export function publishGistAsync(token: string, forceNew: boolean, files: any, name: string, currentGistId: string): Promise<any> {
// Github gist API: https://developer.github.com/v3/gists/
const data = {
"description": name,
"public": false, /* there is no API to make a gist public or private, so it's easier/safer to always make it private and let the user make it public from the UI */
"files": files
};
const headers: Map<string> = {};
let method: string, url: string = "https://api.github.com/gists";
if (token) headers['Authorization'] = `token ${token}`;
if (currentGistId && token && !forceNew) {
// Patch existing gist
method = 'PATCH';
url += `/${currentGistId}`;
} else {
// Create new gist
method = 'POST';
}
return U.requestAsync({
url: url,
allowHttpErrors: true,
headers: headers,
method: method,
data: data || {}
})
.then((resp) => {
if ((resp.statusCode == 200 || resp.statusCode == 201) && resp.json.id) {
return Promise.resolve<string>(resp.json.id);
} else if (resp.statusCode == 404 && method == 'PATCH') {
return Promise.reject(resp.statusCode);
} else if (resp.statusCode == 404) {
return Promise.reject("Make sure to add the ``gist`` scope to your token. " + resp.text);
} return Promise.reject(resp.text);
});
}
}