有两个 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/