如何使用 GitHub API v3 创建提交并推送到 repo? [英] How to create a commit and push into repo with GitHub API v3?

查看:27
本文介绍了如何使用 GitHub API v3 创建提交并推送到 repo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个存储库并通过任何 Python 包向其提交一些文件.我该怎么办?

我不明白如何添加提交文件.

解决方案

使用 requests 库的解决方案:

注意:我使用 requests 库来调用 GitHub REST API v3.

1.获取特定分支的最后一次提交 SHA

# GET/repos/:owner/:repo/branches/:branch_namelast_commit_sha = response.json()['commit']['sha']

2.使用文件内容创建 blob(编码 base64 或 utf-8)

# POST/repos/:owner/:repo/git/blobs# {#内容":aGVsbG8gd29ybGQK",# 编码":base64";#}base64_blob_sha = response.json()['sha']# POST/repos/:owner/:repo/git/blobs# {# 内容":你好世界",# 编码":utf-8";#}utf8_blob_sha = response.json()['sha']

3.创建一个定义文件夹结构的树

# POST repos/:owner/:repo/git/trees/# {# base_tree":last_commit_sha,#树":[# {# "path": "myfolder/base64file.txt",#模式":100644",#类型":斑点",# sha":base64_blob_sha# },# {# "路径": "file-utf8.txt",#模式":100644",#类型":斑点",# sha":utf8_blob_sha# }#]# }tree_sha = response.json()['sha']

4.创建提交

# POST/repos/:owner/:repo/git/commits# {# "message": "以编程方式一次添加新文件",# 作者":{#姓名":扬-迈克尔·文森特",# 电子邮件":JanQuadrantVincent16@rickandmorty.com"# },#父母":[# last_commit_sha#],#树":tree_sha# }new_commit_sha = response.json()['sha']

5.更新分支的引用以指向新的提交 SHA(在主分支示例中)

# POST/repos/:owner/:repo/git/refs/heads/master# {# "ref": "refs/heads/master",# sha":new_commit_sha# }

最后,有关更高级的设置,请阅读文档.>

I want to create a repository and Commit a few files to it via any Python package. How do I do?

I do not understand how to add files for commit.

解决方案

Solution using the requests library:

NOTES: I use the requests library to do the calls to GitHub REST API v3.

1. Get the last commit SHA of a specific branch

# GET /repos/:owner/:repo/branches/:branch_name
last_commit_sha = response.json()['commit']['sha']

2. Create the blobs with the files content (encoding base64 or utf-8)

# POST /repos/:owner/:repo/git/blobs
# {
#  "content": "aGVsbG8gd29ybGQK",
#  "encoding": "base64"
#}
base64_blob_sha = response.json()['sha']

# POST /repos/:owner/:repo/git/blobs
# {
#  "content": "hello world",
#  "encoding": "utf-8"
#}
utf8_blob_sha = response.json()['sha']

3. Create a tree which defines the folder structure

# POST repos/:owner/:repo/git/trees/
# {
#   "base_tree": last_commit_sha,
#   "tree": [
#     {
#       "path": "myfolder/base64file.txt",
#       "mode": "100644",
#       "type": "blob",
#       "sha": base64_blob_sha
#     },
#     {
#       "path": "file-utf8.txt",
#       "mode": "100644",
#       "type": "blob",
#       "sha": utf8_blob_sha
#     }
#   ]
# }
tree_sha = response.json()['sha']

4. Create the commit

# POST /repos/:owner/:repo/git/commits
# {
#   "message": "Add new files at once programatically",
#   "author": {
#     "name": "Jan-Michael Vincent",
#     "email": "JanQuadrantVincent16@rickandmorty.com"
#   },
#   "parents": [
#     last_commit_sha
#   ],
#   "tree": tree_sha
# }
new_commit_sha = response.json()['sha']

5. Update the reference of your branch to point to the new commit SHA (on master branch example)

# POST /repos/:owner/:repo/git/refs/heads/master
# {
#     "ref": "refs/heads/master",
#     "sha": new_commit_sha
# }

Finally, for a more advanced setup read the docs.

这篇关于如何使用 GitHub API v3 创建提交并推送到 repo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆