Copy To functionality for notebooks (#141)

* Add Copy To functionality for notebooks

* Fix formatting

* Fix linting errors

* Fixes

* Fix build failure

* Rebase and address feedback

* Increase test coverage
This commit is contained in:
Tanuj Mittal
2020-08-11 09:27:57 -07:00
committed by GitHub
parent 7a3e54d43e
commit 5886db81e9
18 changed files with 530 additions and 36 deletions

View File

@@ -317,6 +317,13 @@ export class GitHubClient {
objectExpression: `refs/heads/${branch}:${path || ""}`
} as ContentsQueryParams)) as ContentsQueryResponse;
if (!response.repository.object) {
return {
status: HttpStatusCodes.NotFound,
data: undefined
};
}
let data: IGitHubFile | IGitHubFile[];
const entries = response.repository.object.entries;
const gitHubRepo = GitHubClient.toGitHubRepo(response.repository);

View File

@@ -2,10 +2,11 @@ import { Notebook, stringifyNotebook, makeNotebookRecord, toJS } from "@nteract/
import { FileType, IContent, IContentProvider, IEmptyContent, IGetParams, ServerConfig } from "@nteract/core";
import { from, Observable, of } from "rxjs";
import { AjaxResponse } from "rxjs/ajax";
import * as Base64Utils from "../Utils/Base64Utils";
import { HttpStatusCodes } from "../Common/Constants";
import * as Logger from "../Common/Logger";
import { NotebookUtil } from "../Explorer/Notebook/NotebookUtil";
import { GitHubClient, IGitHubFile, IGitHubResponse, IGitHubCommit, IGitHubBranch } from "./GitHubClient";
import { GitHubClient, IGitHubFile, IGitHubResponse } from "./GitHubClient";
import * as GitHubUtils from "../Utils/GitHubUtils";
import UrlUtility from "../Common/UrlUtility";
@@ -131,7 +132,7 @@ export class GitHubContentProvider implements IContentProvider {
throw new GitHubContentProviderError(`Failed to parse ${uri}`);
}
const content = btoa(stringifyNotebook(toJS(makeNotebookRecord())));
const content = Base64Utils.utf8ToB64(stringifyNotebook(toJS(makeNotebookRecord())));
const options: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "short",
@@ -195,34 +196,63 @@ export class GitHubContentProvider implements IContentProvider {
return from(
this.getContent(uri).then(async (content: IGitHubResponse<IGitHubFile | IGitHubFile[]>) => {
try {
const commitMsg = await this.validateContentAndGetCommitMsg(content, "Save", "Save");
let commitMsg: string;
if (content.status === HttpStatusCodes.NotFound) {
// We'll create a new file since it doesn't exist
commitMsg = await this.params.promptForCommitMsg("Save", "Save");
if (!commitMsg) {
throw new GitHubContentProviderError("Couldn't get a commit message");
}
} else {
commitMsg = await this.validateContentAndGetCommitMsg(content, "Save", "Save");
}
let updatedContent: string;
if (model.type === "notebook") {
updatedContent = btoa(stringifyNotebook(model.content as Notebook));
updatedContent = Base64Utils.utf8ToB64(stringifyNotebook(model.content as Notebook));
} else if (model.type === "file") {
updatedContent = model.content as string;
if (model.format !== "base64") {
updatedContent = btoa(updatedContent);
updatedContent = Base64Utils.utf8ToB64(updatedContent);
}
} else {
throw new GitHubContentProviderError("Unsupported content type");
}
const gitHubFile = content.data as IGitHubFile;
const response = await this.params.gitHubClient.createOrUpdateFileAsync(
gitHubFile.repo.owner,
gitHubFile.repo.name,
gitHubFile.branch.name,
gitHubFile.path,
commitMsg,
updatedContent,
gitHubFile.sha
);
if (response.status !== HttpStatusCodes.OK) {
throw new GitHubContentProviderError("Failed to update", response.status);
const contentInfo = GitHubUtils.fromContentUri(uri);
let gitHubFile: IGitHubFile;
if (content.data) {
gitHubFile = content.data as IGitHubFile;
}
gitHubFile.commit = response.data;
const response = await this.params.gitHubClient.createOrUpdateFileAsync(
contentInfo.owner,
contentInfo.repo,
contentInfo.branch,
contentInfo.path,
commitMsg,
updatedContent,
gitHubFile?.sha
);
if (response.status !== HttpStatusCodes.OK && response.status !== HttpStatusCodes.Created) {
throw new GitHubContentProviderError("Failed to create or update", response.status);
}
if (gitHubFile) {
gitHubFile.commit = response.data;
} else {
const contentResponse = await this.params.gitHubClient.getContentsAsync(
contentInfo.owner,
contentInfo.repo,
contentInfo.branch,
contentInfo.path
);
if (contentResponse.status !== HttpStatusCodes.OK) {
throw new GitHubContentProviderError("Failed to get content", response.status);
}
gitHubFile = contentResponse.data as IGitHubFile;
}
return this.createSuccessAjaxResponse(
HttpStatusCodes.OK,