【问题标题】:How to clone .git from GitHub to Google Drive with Google Apps Script?如何使用 Google Apps 脚本将 .git 从 GitHub 克隆到 Google Drive?
【发布时间】:2020-04-20 02:23:57
【问题描述】:

我正在编写一个 Google Apps 脚本,以经常将我的所有 GitHub 存储库备份到 Google Drive。

对于 JSON 响应,它工作正常。

对于 .git 存档本身,我没有找到任何解决方案。

我的目标是获取包含所有分支的 .git 存档。

我的最后一次尝试:

const response = UrlFetchApp.fetch('https://' + accessToken + ':x-oauth-basic@github.com/' + repoFullName + '.git');
const content = response.getContentText();
DriveApp.createFile('Repository.git', content);

导致 GitHub 下载的 HTML 页面“找不到页面”:-(

还有:

const response = UrlFetchApp.fetch('http://github.com/' + repo.full_name + '/zipball/master/', { headers: { Authorization: 'token ' + accessToken } });
const content = response.getContent();
DriveApp.createFile('Repository.zip', content);

导致 macOS 无法读取的存档。

有什么想法吗?

最好的问候,

丹尼斯

【问题讨论】:

  • 我能问一下你的目标吗?关于你的目标,例如,这个流程怎么样? 1. 检索存储库列表。 2. 将列表中的所有存储库下载为 zip 文件。 3. 当所有文件的总大小小于 50 MB 时,可以通过 Google Apps Script 压缩文件。当总大小超过 50 MB 时,Google Apps 脚本无法压缩文件。这是谷歌方面的规范。在这种情况下,它假设可以使用您的访问令牌。如果这不是您期望的方向,我很抱歉。
  • 真正备份 git repo 的最简单方法是使用 git 克隆它。如果您担心 GitHub 丢失数据,您可以在另一个托管的 git 平台上设置镜像,例如 GitLab、BitBucket、Azure DevOps,或者在其他地方托管您自己的 git 服务器。然后,您的备份就像为每个 repo 运行 git fetch --all 一样简单(其中镜像的远程列表可以分别包括 GitHub 上的原始 repo 和您创建的任何其他镜像)。

标签: google-apps-script github-api


【解决方案1】:

感谢您的想法。我的目标是备份所有问题、cmets、文件等。

我解决了将 master 分支下载为 zip 文件的问题,如下所示:

const gitFileResponse = UrlFetchApp.fetch('http://github.com/' + repo.full_name + '/zipball/master/', { headers: { Authorization: 'token ' + accessToken } })
const gitFileContent = gitFileResponse.getBlob()
gitFileContent.setContentTypeFromExtension() 
DriveApp.createFile(gitFileContent).setName('Master.zip')

:-)

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多