簡體   English   中英

如何通過github rest api將子模塊更新為指定的提交?

[英]How to update a submodule to a specified commit via github rest api?

有兩個github repos: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)提交的更改。 現在我想用A的最新更改在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/

我今天花了一些時間搞清楚這一點。 首先,獲取父repo的提交哈希:

GET /repos/:owner/:REPO_A/branches/master

$PARENT_SHA = {commit.sha}

您還需要從要更新的子模塊倉庫中提交:

GET /repos/:owner/:REPO_B/branches/master

$TARGET_SHA = {commit.sha}

接下來,您將創建一個更新子模塊引用的git樹:

POST /repos/:owner/:REPO_A/git/trees
{
    "base_tree": $PARENT_SHA,
    "tree": [
        {
            "path": "path/to/submodule",
            "mode": "160000", 
            "type": "commit",
            "sha": $TARGET_SHA
        }
    ]
}

$TREE_SHA = {sha}

現在你可以提交樹了:

POST /repos/:owner/:REPO_A/git/commits
{
    "message": "Synchronize Handbook",
    "tree": $TREE_SHA,
    "parents": [$PARENT_SHA]
}

$COMMIT_SHA = {sha}

最后,更新master以指向您的新提交:

PATCH /repos/:owner/:REPO_A/git/refs/heads/master
{
    "sha": $COMMIT_SHA
}

不是我知道的:你需要在本地做一個git submodule update --remote以便更新B(假設最新的更改是在master完成的)。
然后添加,提交和推送A以便為B記錄新的gitlink。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM