Skip to content

Commit ff4e822

Browse files
authored
support for parsing github pages links (microsoft#6890)
* support for parsing github pages links * better normalize * cleanup * refresh github repo parsing * some error checking
1 parent f9f4155 commit ff4e822

4 files changed

Lines changed: 35 additions & 37 deletions

File tree

pxtlib/github.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ namespace pxt.github {
798798

799799
export async function repoAsync(id: string, config: pxt.PackagesConfig): Promise<GitRepo> {
800800
const rid = parseRepoId(id);
801+
if (!rid)
802+
return undefined;
801803
const status = repoStatus(rid, config);
802804
if (status == GitRepoStatus.Banned)
803805
return undefined;
@@ -857,49 +859,47 @@ namespace pxt.github {
857859
.catch(err => []); // offline
858860
}
859861

860-
export function parseRepoUrl(url: string): { repo: string; tag?: string; path?: string; } {
862+
function parseRepoUrl(url: string): { repo: string; tag?: string; path?: string; } {
861863
if (!url) return undefined;
862-
863-
let m = /^((https:\/\/)?github.com\/)?([^/]+\/[^/#]+)\/?(#(\w+))?$/i.exec(url.trim());
864-
if (!m) return undefined;
865-
866-
let r: { repo: string; tag?: string; path?: string; } = {
867-
repo: m ? m[3].toLowerCase() : null,
868-
tag: m ? m[5] : null
864+
url = url.trim()
865+
// match github.com urls
866+
let m = /^((https:\/\/)?github.com\/)?([^/]+\/[^/#]+)\/?(#(\w+))?$/i.exec(url);
867+
if (m) {
868+
const r: { repo: string; tag?: string; path?: string; } = {
869+
repo: m ? m[3].toLowerCase() : null,
870+
tag: m ? m[5] : null
871+
}
872+
r.path = r.repo + (r.tag ? '#' + r.tag : '');
873+
return r;
869874
}
870-
r.path = r.repo + (r.tag ? '#' + r.tag : '');
871-
return r;
875+
return undefined;
872876
}
873877

874878
// parse https://github.com/[company]/[project](/filepath)(#tag)
875879
export function parseRepoId(repo: string): ParsedRepo {
876880
if (!repo) return undefined;
881+
repo = repo.trim();
882+
883+
// convert github pages into github repo
884+
const mgh = /^https:\/\/([^./#]+)\.github\.io\/([^/#]+)\/?$/i.exec(repo);
885+
if (mgh)
886+
repo = `github:${mgh[1]}/${mgh[2]}`;
877887

878888
repo = repo.replace(/^github:/i, "")
879889
repo = repo.replace(/^https:\/\/github\.com\//i, "")
880890
repo = repo.replace(/\.git\b/i, "")
881891

882-
let m = /([^#]+)(#(.*))?/.exec(repo)
883-
const nameAndFile = m ? m[1] : null;
884-
const tag = m ? m[3] : null;
885-
let owner: string;
886-
let project: string;
887-
let fullName: string;
888-
let fileName: string;
889-
if (m) {
890-
const parts = nameAndFile.split('/');
891-
owner = parts[0];
892-
project = parts[1];
893-
fullName = `${owner}/${project}`;
894-
if (parts.length > 2)
895-
fileName = parts.slice(2).join('/');
896-
} else {
897-
fullName = repo.toLowerCase();
898-
}
892+
const m = /^([^#\/:]+)\/([^#\/:]+)(\/([^#]+))?(#([^\/:]*))?$/.exec(repo);
893+
if (!m)
894+
return undefined;
895+
const owner = m[1];
896+
const project = m[2];
897+
const fileName = m[4];
898+
const tag = m[6];
899899
return {
900900
owner,
901901
project,
902-
fullName,
902+
fullName: `${owner}/${project}`,
903903
tag,
904904
fileName
905905
}
@@ -923,6 +923,7 @@ namespace pxt.github {
923923

924924
export function normalizeRepoId(id: string) {
925925
const gid = parseRepoId(id);
926+
if (!gid) return undefined;
926927
gid.tag = gid.tag || "master";
927928
return stringifyRepo(gid);
928929
}
@@ -946,7 +947,7 @@ namespace pxt.github {
946947
if (targetVersion && config.releases && config.releases["v" + targetVersion.major]) {
947948
const release = config.releases["v" + targetVersion.major]
948949
.map(repo => pxt.github.parseRepoId(repo))
949-
.filter(repo => repo.fullName.toLowerCase() == parsed.fullName.toLowerCase())
950+
.filter(repo => repo && repo.fullName.toLowerCase() == parsed.fullName.toLowerCase())
950951
[0];
951952
if (release) {
952953
// this repo is frozen to a particular tag for this target

webapp/public/vs/editor/editor.main.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/src/dialogs.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,9 @@ export function showImportUrlDialogAsync() {
389389
if (res) {
390390
pxt.tickEvent("app.open.url");
391391
const url = input.value;
392-
let projectId: string;
393-
if (/^(github:|https:\/\/github\.com\/)/.test(url)) {
394-
projectId = pxt.github.normalizeRepoId(url)
395-
} else {
392+
let projectId = pxt.github.normalizeRepoId(url);
393+
if (!projectId)
396394
projectId = pxt.Cloud.parseScriptId(url);
397-
}
398-
399395
if (!projectId) {
400396
return Promise.reject(new Error(lf("Sorry, the project url looks invalid.")));
401397
}
@@ -626,7 +622,7 @@ export function showImportFileDialogAsync(options?: pxt.editor.ImportFileOptions
626622

627623
export function showReportAbuseAsync(pubId?: string) {
628624
// send users to github directly for unwanted repoes
629-
const ghid = /^https:\/\/github\.com\//i.test(pubId) && pxt.github.parseRepoUrl(pubId);
625+
const ghid = pxt.github.parseRepoId(pubId);
630626
if (ghid) {
631627
pxt.tickEvent("reportabuse.github");
632628
window.open("https://github.com/contact/report-content", "_blank");

webapp/src/package.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ export class EditorPackage {
297297
let p = this.ksPkg.resolveDep(pkgid);
298298
if (!p || p.verProtocol() != "github") return Promise.resolve();
299299
let parsed = pxt.github.parseRepoId(p.verArgument())
300+
if (!parsed) return Promise.resolve();
300301
return pxt.targetConfigAsync()
301302
.then(config => pxt.github.latestVersionAsync(parsed.fullName, config.packages))
302303
.then(tag => { parsed.tag = tag })

0 commit comments

Comments
 (0)