git - 如何通过 github rest api 将子模块更新为指定的提交?

标签 git rest github

有两个 github 存储库:A 和 B。A 将 B 作为其子模块。

https://github.com/org_name/A.git

https://github.com/org_name/B.git

A:
 B@defad9     // a submodule in A

情况如下,在 B 和 PR(#1) 中进行了更改。现在我想用 B 的最新更改在 A 上运行构建。是否有 github rest api 可以执行更新而不是 git submodule update...?

最佳答案

我将 Jason 描述的 GitHub 调用编入一个 Transposit 应用程序,该应用程序可用作 GitHub webhook,以便在子模块更新时自动更新父存储库中的子模块 sha。

Transposit 托管我的 javascript 函数,并有一个 JavaScript API,我用它来调用 GitHub 的 REST API。这个函数在提交到子模块时被 webhook 调用,它为子模块存储库获取最新的 SHA,将父存储库中的树更新为新的 sha,然后提交它。

(params) => {
     let owner = env.get('parentOwner');
     let repo = env.get('parentRepo');
     let branch = env.get('parentBranch');
     let submoduleOwner = env.get('submoduleOwner');
     let submoduleRepo = env.get('submoduleRepo');
     let submoduleBranch = env.get('submoduleBranch');  
     let submodulePath = env.get('submodulePath');

     let parentSha = api.run('github.get_branch_for_repo', {owner, repo, branch})[0].commit.sha;
     let submoduleSha = api.run('github.get_branch_for_repo', {owner: submoduleOwner, repo: submoduleRepo, branch: submoduleBranch})[0].commit.sha;

     let treeSha = api.run('github.create_git_tree', {
       owner: owner,
       repo: repo,
       $body: {
         "base_tree": parentSha,
         "tree": [
             {
                 "path": submodulePath,
                 "mode": "160000", 
                 "type": "commit",
                 "sha": submoduleSha
             }
         ]
       }
     })[0]

     let commitSha = api.run('github.create_git_commit', {
       owner: owner,
       repo: repo,
       $body: {
         "message": `Auto-update submodule ${submoduleOwner}/${submoduleRepo} to ${submoduleSha}`,
         "tree": treeSha.sha,
         "parents": [parentSha]
       }
     })[0]

     let editSha = api.run('github.edit_git_ref', {
       owner: owner,
       ref: `heads/${branch}`,
       repo: repo,
       $body: {
         "sha": commitSha.sha
       }
     })
     return editSha;
   }

有关如何使用此 webhook 的更多信息可在此处找到:https://www.transposit.com/blog/2019.08.23-github_submodule/

关于git - 如何通过 github rest api 将子模块更新为指定的提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45789854/

相关文章:

java - 在 Intellij IDEA 14 上更改远程存储库凭据(身份验证)

git - 覆盖 merge 中的本地更改

git - 从服务器导入更改后 GIT 出错

java - 将 EJB 应用程序共享到 Rest 服务中

c# - ASP.net WebApi 新注册的路由显示,该请求无效

html - 如何获取托管在 github 页面上的 Markdown 文件以显示在 React 应用程序中?

git - 为什么会导致 merge 冲突?

wcf - 使用 Azure Blob 存储从 WCF REST 下载文件

git - 删除我文件夹中的所有 .gitignore 文件

ios - Cocoapods 在 Git 克隆后缺少 project.pbxproj