Logo
Section 1 / 4

仓库

学习并实践 GitHub API 中仓库的功能。

导入

仓库

学习并实践 GitHub API 中仓库的功能。

概述

仓库是 GitHub 上存放完整项目及其文件的容器。此外,文件的各个版本也存在于该仓库中。仓库的所有者可以是个人本身,也可以是他们愿意共享所有权的任何人。我们可以在仓库内创建分支,以便将不同类型的文件相互隔离。

...
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/user/repos' 7 8data = json.dumps({ 9 'name': '{{REPOSITORY}}' 10}) 11 12response = requests.post(url, headers=headers, data=data) 13print(json.dumps(response.json(), indent=4))

创建仓库的参数

参数名类型位置说明
name字符串请求体仓库的名称。
description字符串请求体仓库的描述信息。
homepage字符串请求体仓库的主页链接地址。
private布尔值请求体若为 true,则表示仓库为私有;否则为公开。
visibility字符串请求体指定仓库是私有还是公开。
has_issues布尔值请求体为 true 表示启用了问题功能,false 表示禁用。
has_projects布尔值请求体为 true 表示启用了项目功能,false 表示禁用。
has_wiki布尔值请求体为 true 表示启用了维基功能,false 表示禁用。
is_template布尔值请求体为 true 表示该仓库可作为模板使用,false 则不可。
auto_init布尔值请求体为 true 表示在空仓库中自动提交一个 README 文件。
gitignore_template字符串请求体指定所需平台或语言对应的 .gitignore 模板名称。
license_template字符串请求体选择合适的许可证模板。
allow_squash_merge布尔值请求体为 true 表示允许合并时压缩提交,false 表示禁用。
allow_merge_commit布尔值请求体为 true 表示允许合并时创建合并提交,false 表示禁用。
allow_rebase_merge布尔值请求体为 true 表示允许使用变基方式合并,false 表示禁用。
allow_auto_merge布尔值请求体为 true 表示允许自动合并,false 表示禁用。
delete_branch_on_merge布尔值请求体为 true 表示合并后自动删除分支,false 表示不自动删除。
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = "https://api.github.com/users/<username>/repos" 7 8response = requests.get(url, headers=headers) 9print(json.dumps(response.json(), indent=4))
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6data = json.dumps({ 7 'name': '<repository-new-name>' 8}) 9 10url = "https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}" 11 12response = requests.patch(url, headers=headers, data=data) 13print(json.dumps(response.json(), indent=4))

更新仓库的附加参数

参数名类型位置说明
security_and_analysis对象或 null请求体用于传递所需的安全部署项目(如适用)。
default_branch字符串请求体更新仓库的默认分支。
archived布尔值请求体为 true 表示启用归档状态,false 表示未归档。
allow_forking布尔值请求体为 true 表示允许创建分叉,false 表示禁止。
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}' 7 8response = requests.delete(url, headers=headers) 9 10if response.status_code == 204: 11 print('仓库已成功删除。') 12elif response.status_code == 307: 13 print('请求临时重定向。') 14else: 15 print('未找到指定的仓库。')

简介

分支