我正在尝试使用octokit.rb从几个子目录中删除多个文件-明确地说,我真正想做的只是删除整个子目录。如果有更好的方法(一次提交),请指出正确的方向。在创建“ foo”目录及其文件时,我有一个有效的示例,但是删除目录给我带来了问题。这是我的代码,使用octokit:
client = Octokit::Client.new(...)
repo = 'dudo/k8s_colors'
namespace = 'foo'
ref = 'heads/master'
sha_latest_commit = client.ref(repo, ref).object.sha
sha_base_tree = client.commit(repo, sha_latest_commit).commit.tree.sha
base_tree = client.tree(repo, sha_base_tree, recursive: true)
clean_tree = base_tree.tree.select { |o| o.type == 'blob' && !o.path.include?("overlays/#{namespace}") }
.map { |o| o.to_h.slice(:path, :mode, :type, :sha) }
sha_new_tree = client.create_tree(repo, clean_tree).sha # TODO: this doesn't work
对create_tree
的此调用不会产生我期望的树。
> base_tree
{:sha=>"2c89f29eab56c8f53bcbb5649bb326433e439629",
:url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/2c89f29eab56c8f53bcbb5649bb326433e439629",
:tree=>
[{:path=>".gitignore", :mode=>"100644", :type=>"blob", :sha=>"f494b1b6ae2590ef5998845626566708ec9e2908", :size=>15, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/f494b1b6ae2590ef5998845626566708ec9e2908"},
{:path=>"blue", :mode=>"040000", :type=>"tree", :sha=>"ac10637c3435b466ef29ddc5c0f40f047f1b7e92", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/ac10637c3435b466ef29ddc5c0f40f047f1b7e92"},
{:path=>"blue/base", :mode=>"040000", :type=>"tree", :sha=>"4996aaefbf8099595515907a8e80dd266638afcc", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/4996aaefbf8099595515907a8e80dd266638afcc"},
{:path=>"blue/base/deployment.yaml", :mode=>"100644", :type=>"blob", :sha=>"770bdc76ea5a29ba7e5219cd9cedfa7333adb084", :size=>512, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/770bdc76ea5a29ba7e5219cd9cedfa7333adb084"},
{:path=>"blue/base/ingress.yaml", :mode=>"100644", :type=>"blob", :sha=>"f2e9e9babb19978ec3dc3e10e71a2b3cbd80840f", :size=>292, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/f2e9e9babb19978ec3dc3e10e71a2b3cbd80840f"},
{:path=>"blue/base/service.yaml", :mode=>"100644", :type=>"blob", :sha=>"80a9ccdb3a395395b58fa462b2b4d0339665862e", :size=>122, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/80a9ccdb3a395395b58fa462b2b4d0339665862e"},
{:path=>"blue/overlays", :mode=>"040000", :type=>"tree", :sha=>"34e882c2a2b76ef926bfe179aaba688e2956ff12", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/34e882c2a2b76ef926bfe179aaba688e2956ff12"},
{:path=>"blue/overlays/foo", :mode=>"040000", :type=>"tree", :sha=>"aa9a92503f622aa6d61c74a63dda655a3afe0bf8", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/aa9a92503f622aa6d61c74a63dda655a3afe0bf8"},
{:path=>"blue/overlays/foo/ingress.yaml", :mode=>"100644", :type=>"blob", :sha=>"2245b69ea811665de2bea620db394d69fe41f768", :size=>311, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/2245b69ea811665de2bea620db394d69fe41f768"},
{:path=>"blue/overlays/foo/service.yaml", :mode=>"100644", :type=>"blob", :sha=>"af1742cdd917b7207377874ba34263a782fce673", :size=>147, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/af1742cdd917b7207377874ba34263a782fce673"},
{:path=>"red", :mode=>"040000", :type=>"tree", :sha=>"a8442ac6ab5be147f5452aa4a94061b81b0dc32b", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/a8442ac6ab5be147f5452aa4a94061b81b0dc32b"},
{:path=>"red/base", :mode=>"040000", :type=>"tree", :sha=>"9374bbaec9f3ca88bfc4dcbdee421d8edecd9d73", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/9374bbaec9f3ca88bfc4dcbdee421d8edecd9d73"},
{:path=>"red/base/deployment.yaml", :mode=>"100644", :type=>"blob", :sha=>"3a8d2448475d851e851dff1fc36d1bb443c8acea", :size=>507, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/3a8d2448475d851e851dff1fc36d1bb443c8acea"},
{:path=>"red/base/ingress.yaml", :mode=>"100644", :type=>"blob", :sha=>"b9322cc79212813285b93467e15aa5aea08eb442", :size=>289, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/b9322cc79212813285b93467e15aa5aea08eb442"},
{:path=>"red/base/service.yaml", :mode=>"100644", :type=>"blob", :sha=>"5113689b6a1c1aca2b36a3c513d9c143e8ae12e0", :size=>120, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/5113689b6a1c1aca2b36a3c513d9c143e8ae12e0"},
{:path=>"red/overlays", :mode=>"040000", :type=>"tree", :sha=>"a4f8d5b81643b26684f7db7af17bf5d2d95c3c1b", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/a4f8d5b81643b26684f7db7af17bf5d2d95c3c1b"},
{:path=>"red/overlays/foo", :mode=>"040000", :type=>"tree", :sha=>"bccf70cc1e84f1724116644ee8c82cf4ab734834", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/bccf70cc1e84f1724116644ee8c82cf4ab734834"},
{:path=>"red/overlays/foo/deployment.yaml", :mode=>"100644", :type=>"blob", :sha=>"3e050646dd49721b3d701929c0a89db831ebea14", :size=>524, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/3e050646dd49721b3d701929c0a89db831ebea14"},
{:path=>"red/overlays/foo/ingress.yaml", :mode=>"100644", :type=>"blob", :sha=>"840d45bb3c5c6c81a1ac71d82ee49d348653ffc7", :size=>308, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/840d45bb3c5c6c81a1ac71d82ee49d348653ffc7"},
{:path=>"red/overlays/foo/namespace.yaml", :mode=>"100644", :type=>"blob", :sha=>"9eb7d29d67093ec524dcea1b32f3eeb7c2cecc81", :size=>57, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/9eb7d29d67093ec524dcea1b32f3eeb7c2cecc81"},
{:path=>"red/overlays/foo/service.yaml", :mode=>"100644", :type=>"blob", :sha=>"43d2053a6f237d643036499fa7b01d7b01fc2111", :size=>142, :url=>"https://api.github.com/repos/dudo/k8s_colors/git/blobs/43d2053a6f237d643036499fa7b01d7b01fc2111"}],
:truncated=>false}
> clean_tree
[
{:path=>".gitignore", :mode=>"100644", :type=>"blob", :sha=>"f494b1b6ae2590ef5998845626566708ec9e2908"},
{:path=>"blue/base/deployment.yaml", :mode=>"100644", :type=>"blob", :sha=>"770bdc76ea5a29ba7e5219cd9cedfa7333adb084"},
{:path=>"blue/base/ingress.yaml", :mode=>"100644", :type=>"blob", :sha=>"f2e9e9babb19978ec3dc3e10e71a2b3cbd80840f"},
{:path=>"blue/base/service.yaml", :mode=>"100644", :type=>"blob", :sha=>"80a9ccdb3a395395b58fa462b2b4d0339665862e"},
{:path=>"red/base/deployment.yaml", :mode=>"100644", :type=>"blob", :sha=>"3a8d2448475d851e851dff1fc36d1bb443c8acea"},
{:path=>"red/base/ingress.yaml", :mode=>"100644", :type=>"blob", :sha=>"b9322cc79212813285b93467e15aa5aea08eb442"},
{:path=>"red/base/service.yaml", :mode=>"100644", :type=>"blob", :sha=>"5113689b6a1c1aca2b36a3c513d9c143e8ae12e0"}
]
> client.create_tree(repo, clean_tree)
{:sha=>"2a8800e18b5a4dc50414fc380e2d2d66401082d3",
:url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/2a8800e18b5a4dc50414fc380e2d2d66401082d3",
:tree=>
[{:path=>"blue", :mode=>"040000", :type=>"tree", :sha=>"d0554827144fae7e578edb545d07e8e43bd17b74", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/d0554827144fae7e578edb545d07e8e43bd17b74"},
{:path=>"red", :mode=>"040000", :type=>"tree", :sha=>"f2d7ff860c04b80fb4f9b6cba5304d0ca0a6816f", :url=>"https://api.github.com/repos/dudo/k8s_colors/git/trees/f2d7ff860c04b80fb4f9b6cba5304d0ca0a6816f"}],
:truncated=>false}
答案 0 :(得分:2)
1)您从存储库中的最后一个commit
中获得了sha
sha_base_tree = client.commit(repo, sha_latest_commit).commit.tree.sha
2)您获取了所有tree
objects
base_tree = client.tree(repo, sha_base_tree, recursive: true)
3)您循环并修改那些threes
。
cleaned_tree = base_tree.tree.reject { |blob| blob.path.include? ['overlays', namespace].join('/') }
sanitized_tree = cleaned_tree.map { |o| o.to_h.slice(:path, :mode, :type, :sha) }
sha_new_tree = client.create_tree(repo, sanitized_tree)
4)您需要在create_tree(repo, sanitized_tree)
dudo/k8s_colors
client.create_tree('dudo/k8s_colors', sanitized_tree)
我试图通过github api get request检索您的树。 request returns not found
,这意味着您没有创建任何树。回购甚至不存在on your github repository。
.create_tree
还会返回以下head
个提交。
2a8800e18b5a4dc50414fc380e2d2d66401082d3
d0554827144fae7e578edb545d07e8e43bd17b74
f2d7ff860c04b80fb4f9b6cba5304d0ca0a6816f
为进一步提供帮助,我们需要查看github上的所有树,来自基础的树和源自经过清理的树的树,并查看这些提交。我会在历史记录中确认那些提交不存在。