Logo
Section 1 / 4

仓库

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

导入

仓库

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

概述

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

...
...
GitHub 仓库创建接口

请求参数

参数类型分类描述
name字符串必填要创建的仓库名称
description字符串可选仓库的描述信息
homepage字符串可选仓库的信息网址
private布尔值可选若为 true,则仓库为私有;否则为公开
has_issues布尔值可选若为 true,则启用问题功能;否则禁用
has_projects布尔值可选若为 true,则启用项目功能;否则禁用
has_wiki布尔值可选若为 true,则启用维基功能;否则禁用
is_template布尔值可选若为 true,则该仓库可作为模板使用;否则不可用
allow_squash_merge布尔值可选若为 true,则允许合并提交(squash merge);否则禁止
allow_merge_commit布尔值可选若为 true,则允许合并提交(merge commit);否则禁止
allow_rebase_merge布尔值可选若为 true,则允许变基合并(rebase merge);否则禁止
allow_auto_merge布尔值可选若为 true,则允许自动合并;否则禁止
delete_branch_on_merge布尔值可选若为 true,则合并后自动删除分支;否则不删除
1const endpointUrl = 'https://api.github.com/user/repos'; 2 3const headers = { 4 Authorization: 'token {{ACCESS_TOKEN}}', 5 Accept: 'application/vnd.github.v3+json', 6}; 7 8const body = JSON.stringify({ 9 name: '{{REPOSITORY}}', 10}); 11 12const options = { 13 method: 'POST', 14 headers, 15 body, 16}; 17 18async function CreateRepo() { 19 try { 20 const response = await fetch(endpointUrl, options); 21 printResponse(response); 22 } catch (error) { 23 printErrors(error); 24 } 25}; 26 27CreateRepo();

下面简要说明上述代码:

  • 第1行: 定义了接口的 URL。
  • 第8–10行: 定义了请求体所需的参数。
  • 第18–25行: 定义了一个 async 函数,用于调用指定接口。
    • 第21行: 若请求成功,通过调用 printResponse 函数输出 API 返回结果。
    • 第23行: 若请求失败,通过调用 printErrors 函数输出错误信息。
  • 第27行: 调用 async 函数。
...
GitHub 列出仓库接口

请求参数

参数类型描述
username字符串要列出仓库的用户名
1const username = '<username>' 2 3const endpointUrl = `https://api.github.com/users/${username}/repos`; 4 5const headers = { 6 Authorization: 'token {{ACCESS_TOKEN}}', 7 Accept: 'application/vnd.github.v3+json', 8}; 9 10const options = { 11 method: 'GET', 12 headers, 13}; 14 15async function GetRepos() { 16 try { 17 const response = await fetch(endpointUrl, options); 18 printResponse(response); 19 } catch (error) { 20 printErrors(error); 21 } 22}; 23 24GetRepos();

下面简要说明上述代码:

  • 第1行: 定义了仓库所有者的用户名。
  • 第3行: 定义了接口的 URL。
  • 第15–22行: 定义了一个 async 函数,用于调用指定接口。
    • 第18行: 若请求成功,通过调用 printResponse 函数输出返回结果。
    • 第20行: 若请求失败,通过调用 printErrors 函数输出错误信息。
  • 第24行: 调用 async 函数。
...
GitHub 更新仓库接口

请求参数

参数类型分类描述
repository字符串必填要更新的仓库名称
name字符串必填仓库的新名称
default_branch字符串可选更新仓库的默认分支
archived布尔值可选若为 true,则启用归档功能;否则禁用
allow_forking布尔值可选若为 true,则允许对该仓库进行 Fork(复刻);否则禁止
1const username = '{{USERNAME}}'; 2 3const repository = '{{REPOSITORY}}'; 4 5const endpointUrl = `https://api.github.com/repos/${username}/${repository}`; 6 7const headers = { 8 Authorization: 'token {{ACCESS_TOKEN}}', 9 Accept: 'application/vnd.github.v3+json', 10}; 11 12const body = JSON.stringify({ 13 name: '<repository-new-name>', 14}); 15 16const options = { 17 method: 'PATCH', 18 headers, 19 body: body, 20}; 21 22async function UpdateRepo() { 23 try { 24 const response = await fetch(endpointUrl, options); 25 printResponse(response); 26 } catch (error) { 27 printErrors(error); 28 } 29}; 30 31UpdateRepo();

下面简要说明上述代码:

  • 第5行: 定义了接口的 URL。
  • 第12–14行: 定义了请求体中必需的参数。
  • 第22–29行: 定义了一个 async 函数,用于调用指定接口。
    • 第25行: 若请求成功,通过调用 printResponse 函数输出返回结果。
    • 第27行: 若请求失败,通过调用 printErrors 函数输出错误信息。
  • 第31行: 调用 async 函数。
...
GitHub 删除仓库接口

请求参数

参数类型描述
username字符串仓库所有者的用户名
repository字符串要删除的仓库名称
1const username = '{{USERNAME}}'; 2 3const repository = '{{REPOSITORY}}'; 4 5const endpointUrl = `https://api.github.com/repos/${username}/${repository}`; 6 7const headers = { 8 Authorization: 'token {{ACCESS_TOKEN}}', 9 Accept: 'application/vnd.github.v3+json', 10}; 11 12const options = { 13 method: 'DELETE', 14 headers, 15}; 16 17async function DeleteRepo() { 18 try { 19 const response = await fetch(endpointUrl, options); 20 printResponse(response); 21 } catch (error) { 22 printErrors(error); 23 } 24}; 25 26DeleteRepo();

下面简要说明上述代码:

  • 第3行: 定义了要删除的仓库名称。
  • 第5行: 定义了接口的 URL。
  • 第17–24行: 定义了一个 async 函数,用于调用指定接口。
    • 第20行: 若请求成功,通过调用 printResponse 函数输出返回结果。
    • 第22行: 若请求失败,通过调用 printErrors 函数输出错误信息。
  • 第26行: 调用 async 函数。

简介

分支