Update to ADO 5ed9b2130da7f822153531489d802c28986f5d30

This commit is contained in:
Steve Faulkner
2020-05-26 13:53:41 -05:00
parent 36581fb6d9
commit 0494da4162
42 changed files with 1186 additions and 2242 deletions

View File

@@ -1,50 +1,32 @@
import { GitHubUtils } from "./GitHubUtils";
const owner = "owner-1";
const repo = "repo-1";
const branch = "branch/name.1-2";
const path = "folder name/file name1:2.ipynb";
describe("GitHubUtils", () => {
describe("fromGitHubUri", () => {
it("parses github repo url for a branch", () => {
const gitHubInfo = GitHubUtils.fromGitHubUri("https://github.com/owner/repo/tree/branch");
expect(gitHubInfo).toEqual({
owner: "owner",
repo: "repo",
branch: "branch",
path: ""
});
});
it("parses github file url", () => {
const gitHubInfo = GitHubUtils.fromGitHubUri("https://github.com/owner/repo/blob/branch/dir/file.ext");
expect(gitHubInfo).toEqual({
owner: "owner",
repo: "repo",
branch: "branch",
path: "dir/file.ext"
});
});
it("parses github file url with spaces", () => {
const gitHubInfo = GitHubUtils.fromGitHubUri("https://github.com/owner/repo/blob/branch/dir/file name.ext");
expect(gitHubInfo).toEqual({
owner: "owner",
repo: "repo",
branch: "branch",
path: "dir/file name.ext"
});
});
it("parses github file url with encoded chars", () => {
const gitHubInfo = GitHubUtils.fromGitHubUri("https://github.com/owner/repo/blob/branch/dir/file%20name.ext");
expect(gitHubInfo).toEqual({
owner: "owner",
repo: "repo",
branch: "branch",
path: "dir/file%20name.ext"
});
it("fromRepoUri parses github repo url correctly", () => {
const repoInfo = GitHubUtils.fromRepoUri(`https://github.com/${owner}/${repo}/tree/${branch}`);
expect(repoInfo).toEqual({
owner,
repo,
branch
});
});
it("toRepoFullName returns full name in expected format", () => {
const fullName = GitHubUtils.toRepoFullName("owner", "repo");
expect(fullName).toBe("owner/repo");
it("toContentUri generates github uris correctly", () => {
const uri = GitHubUtils.toContentUri(owner, repo, branch, path);
expect(uri).toBe(`github://${owner}/${repo}/${path}?ref=${branch}`);
});
it("fromContentUri parses the github uris correctly", () => {
const contentInfo = GitHubUtils.fromContentUri(`github://${owner}/${repo}/${path}?ref=${branch}`);
expect(contentInfo).toEqual({
owner,
repo,
branch,
path
});
});
});

View File

@@ -1,56 +1,46 @@
import UrlUtility from "../Common/UrlUtility";
import { RepoListItem } from "../Explorer/Controls/GitHub/GitHubReposComponent";
import { IGitHubFile, IGitHubRepo } from "../GitHub/GitHubClient";
import { IPinnedRepo } from "../Juno/JunoClient";
export class GitHubUtils {
// The pattern is https://github.com/<owner>/<repo>/(blob|tree)/<branch>/<path>
private static readonly UriPattern = "https://github.com/([^/]*)/([^/]*)/(blob|tree)/([^/]*)/?(.*)";
// 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=(.*)/;
public static toRepoFullName(owner: string, repo: string): string {
return `${owner}/${repo}`;
}
public static toGitHubUriForRepoAndBranch(owner: string, repo: string, branch: string, path?: string): string {
return UrlUtility.createUri(`https://github.com/${owner}/${repo}/tree/${branch}`, path);
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]
};
}
return undefined;
}
public static toGitHubUriForFile(gitHubFile: IGitHubFile): string {
return decodeURIComponent(gitHubFile.html_url);
}
public static fromGitHubUri(
gitHubUri: string
public static fromContentUri(
contentUri: string
): undefined | { owner: string; repo: string; branch: string; path: string } {
try {
const matches = gitHubUri.match(GitHubUtils.UriPattern);
const matches = contentUri.match(GitHubUtils.ContentUriPattern);
if (matches && matches.length > 4) {
return {
owner: matches[1],
repo: matches[2],
branch: matches[4],
path: matches[5]
path: matches[3]
};
} catch (error) {
return undefined;
}
return undefined;
}
public static toPinnedRepo(item: RepoListItem): IPinnedRepo {
return {
owner: item.repo.owner.login,
name: item.repo.name,
private: item.repo.private,
branches: item.branches.map(element => ({ name: element.name }))
};
}
public static toGitHubRepo(pinnedRepo: IPinnedRepo): IGitHubRepo {
return {
owner: {
login: pinnedRepo.owner
},
name: pinnedRepo.name,
private: pinnedRepo.private
};
public static toContentUri(owner: string, repo: string, branch: string, path: string): string {
return `github://${owner}/${repo}/${path}?ref=${branch}`;
}
}

View File

@@ -1,5 +1,8 @@
import * as DataModels from "../Contracts/DataModels";
import { config } from "../Config";
import { RepoListItem } from "../Explorer/Controls/GitHub/GitHubReposComponent";
import { IPinnedRepo } from "../Juno/JunoClient";
import { IGitHubRepo } from "../GitHub/GitHubClient";
export class JunoUtils {
public static async getLikedNotebooks(authorizationToken: string): Promise<DataModels.LikedNotebooksJunoResponse> {
@@ -41,4 +44,23 @@ export class JunoUtils {
return undefined;
//TODO: add notebookMetadata updation code
}
public static toPinnedRepo(item: RepoListItem): IPinnedRepo {
return {
owner: item.repo.owner.login,
name: item.repo.name,
private: item.repo.private,
branches: item.branches.map(element => ({ name: element.name }))
};
}
public static toGitHubRepo(pinnedRepo: IPinnedRepo): IGitHubRepo {
return {
owner: {
login: pinnedRepo.owner
},
name: pinnedRepo.name,
private: pinnedRepo.private
};
}
}