public save()

in src/GitHub/GitHubContentProvider.ts [195:271]


  public save<FT extends FileType>(
    _: ServerConfig,
    uri: string,
    model: Partial<IContent<FT>>,
  ): Observable<AjaxResponse> {
    return from(
      this.getContent(uri).then(async (content: IGitHubResponse<IGitHubFile | IGitHubFile[]>) => {
        try {
          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 = Base64Utils.utf8ToB64(stringifyNotebook(model.content as Notebook));
          } else if (model.type === "file") {
            updatedContent = model.content as string;
            if (model.format !== "base64") {
              updatedContent = Base64Utils.utf8ToB64(updatedContent);
            }
          } else {
            throw new GitHubContentProviderError("Unsupported content type");
          }

          const contentInfo = GitHubUtils.fromContentUri(uri);
          let gitHubFile: IGitHubFile;
          if (content.data) {
            gitHubFile = content.data as IGitHubFile;
          }

          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,
            this.createContentModel(uri, gitHubFile, { content: 0 }),
          );
        } catch (error) {
          Logger.logError(getErrorMessage(error), "GitHubContentProvider/update", error.errno);
          return this.createErrorAjaxResponse(error);
        }
      }),
    );
  }