如何解决pygithub 提交改进
我正在尝试编写一个可以一次性提交自动生成的 .json 文件的脚本。但是我的脚本只能一个一个提交文件,这不是我想要的...请帮忙!
接下来是我的代码。我认为 repo.update_file/create_file 可能不会一次性提交。
from github import Github
def github_login(tokens):
g = Github(login_or_token=tokens)
return g
def github_all_files_name(repo):
contents = repo.get_contents("")
all_files = []
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
return all_files
def main(tokens,docker_path,github_path,branch):
g = github_login(tokens)
repo = g.get_repo(github_path)
github_files_name = github_all_files_name(repo)
generated_jsonnet_list = os.listdir(docker_path)
for jsonnet in generated_jsonnet_list:
try:
with open(docker_path + "/{jsonnet_name}".format(jsonnet_name=jsonnet),"r") as file:
content = file.read()
# whether jsonnet/wrapper jsonnet
if jsonnet == "xxx.jsonnet":
git_file = jsonnet
else:
git_file = "xxx/" + jsonnet
# check whether file is updated/created
if git_file in github_files_name:
contents = repo.get_contents(git_file)
code1 = contents.sha
repo.update_file(contents.path,"Update jsonnet",content,contents.sha,branch=branch)
code2 = repo.get_contents(git_file).sha
if code1 == code2:
print(git_file + " No Change!")
else:
print(git_file + " is updating!")
else:
repo.create_file(git_file,"Create jsonnet",branch=branch)
print(git_file + ' is pushing!')
except Exception as e:
print(e)
if __name__ == "__main__":
token = "ur token code from GitHub private token"
local_path1 = "/Users/tmp"
github_path1 = "Lei/test"
branch1 = "test"
main(token,docker_path1,github_path1,branch1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。