2020-05-25 21:30:55 -05:00
|
|
|
export class GitHubUtils {
|
2020-05-26 13:53:41 -05:00
|
|
|
// https://github.com/<owner>/<repo>/tree/<branch>
|
|
|
|
// The url when users visit a repo/branch on github.com
|
|
|
|
private static readonly RepoUriPattern = /https:\/\/github.com\/([^/]*)\/([^/]*)\/tree\/([^?]*)/;
|
|
|
|
|
|
|
|
// github://<owner>/<repo>/<path>?ref=<branch>
|
|
|
|
// Custom scheme for github content
|
|
|
|
private static readonly ContentUriPattern = /github:\/\/([^/]*)\/([^/]*)\/([^?]*)\?ref=(.*)/;
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2020-06-05 12:22:41 -07:00
|
|
|
// https://github.com/<owner>/<repo>/blob/<branch>/<path>
|
|
|
|
// We need to support this until we move to newer scheme for quickstarts
|
|
|
|
private static readonly LegacyContentUriPattern = /https:\/\/github.com\/([^/]*)\/([^/]*)\/blob\/([^/]*)\/([^?]*)/;
|
|
|
|
|
2020-05-25 21:30:55 -05:00
|
|
|
public static toRepoFullName(owner: string, repo: string): string {
|
|
|
|
return `${owner}/${repo}`;
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:53:41 -05:00
|
|
|
public static fromRepoUri(repoUri: string): undefined | { owner: string; repo: string; branch: string } {
|
|
|
|
const matches = repoUri.match(GitHubUtils.RepoUriPattern);
|
|
|
|
if (matches && matches.length > 3) {
|
|
|
|
return {
|
|
|
|
owner: matches[1],
|
|
|
|
repo: matches[2],
|
|
|
|
branch: matches[3]
|
|
|
|
};
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2020-05-26 13:53:41 -05:00
|
|
|
return undefined;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
2020-05-26 13:53:41 -05:00
|
|
|
public static fromContentUri(
|
|
|
|
contentUri: string
|
2020-05-25 21:30:55 -05:00
|
|
|
): undefined | { owner: string; repo: string; branch: string; path: string } {
|
2020-06-05 12:22:41 -07:00
|
|
|
let matches = contentUri.match(GitHubUtils.ContentUriPattern);
|
2020-05-26 13:53:41 -05:00
|
|
|
if (matches && matches.length > 4) {
|
2020-05-25 21:30:55 -05:00
|
|
|
return {
|
|
|
|
owner: matches[1],
|
|
|
|
repo: matches[2],
|
|
|
|
branch: matches[4],
|
2020-05-26 13:53:41 -05:00
|
|
|
path: matches[3]
|
2020-05-25 21:30:55 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-05 12:22:41 -07:00
|
|
|
matches = contentUri.match(GitHubUtils.LegacyContentUriPattern);
|
|
|
|
if (matches && matches.length > 4) {
|
|
|
|
console.log(`Using legacy github content uri scheme ${contentUri}`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
owner: matches[1],
|
|
|
|
repo: matches[2],
|
|
|
|
branch: matches[3],
|
|
|
|
path: matches[4]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:53:41 -05:00
|
|
|
return undefined;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
2020-05-26 13:53:41 -05:00
|
|
|
public static toContentUri(owner: string, repo: string, branch: string, path: string): string {
|
|
|
|
return `github://${owner}/${repo}/${path}?ref=${branch}`;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
}
|