Logo
Section 1 / 3

协作者

学习并实践 GitHub API 中协作者的功能。

导入

协作者

学习并实践 GitHub API 中协作者的功能。

概述

协作者 是由仓库所有者邀请参与软件开发的用户。读写权限意味着协作者可以拉取仓库内容,进行所需修改,并将更改推回仓库。仓库所有者会向希望合作的用户发送协作邀请,该邀请需在七天内接受,逾期将失效。一个特定仓库可添加的协作者数量没有限制。

...

获取仓库的协作者列表

具有推送权限的已认证用户可使用此接口。可通过提供访问令牌进行身份验证。返回的协作者列表包含所有拥有默认权限访问该仓库的成员。

请求参数

参数类型说明
repository字符串要列出协作者的仓库名称
1const endpointUrl = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators'; 2 3const headers = { 4 Authorization: 'token {{ACCESS_TOKEN}}', 5 Accept: 'application/vnd.github.v3+json', 6}; 7 8const options = { 9 method: 'GET', 10 headers, 11}; 12 13async function GetCollaborators() { 14 try { 15 const response = await fetch(endpointUrl, options); 16 printResponse(response); 17 } catch (error) { 18 printErrors(error); 19 } 20}; 21 22GetCollaborators();

上述代码成功执行后,将返回响应码 200。

简要说明如下:

  • 第1行: 定义了接口的URL。
  • 第13至20行: 定义了一个异步函数,用于调用指定的接口。
    • 第16行: 若请求成功,通过调用 printResponse 函数打印API响应。
    • 第18行: 否则,通过调用 printErrors 函数打印错误信息。
  • 第22行: 调用该异步函数。

请求参数

参数类型说明
repository字符串要搜索协作者的仓库名称
username字符串要搜索的协作者用户名
1const endpointUrl = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>'; 2 3const headers = { 4 Authorization: 'token {{ACCESS_TOKEN}}', 5 Accept: 'application/vnd.github.v3+json', 6}; 7 8const options = { 9 method: 'GET', 10 headers, 11}; 12 13async function CheckCollaborator() { 14 try { 15 const response = await fetch(endpointUrl, options); 16 printResponse(response); 17 } catch (error) { 18 printErrors(error); 19 } 20}; 21 22CheckCollaborator();

上述代码成功执行后,将返回响应码 204。

简要说明如下:

  • 第1行: 定义了接口的URL。
  • 第13至20行: 定义了一个异步函数,用于调用指定的接口。
    • 第16行: 若请求成功,通过调用 printResponse 函数打印API响应。
    • 第18行: 否则,通过调用 printErrors 函数打印错误信息。
  • 第22行: 调用该异步函数。

请求参数

参数类型说明
repository字符串要添加协作者的仓库名称
username字符串要添加的新协作者用户名
1const endpointUrl = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>'; 2 3const headers = { 4 Authorization: 'token {{ACCESS_TOKEN}}', 5 Accept: 'application/vnd.github.v3+json', 6}; 7 8const body = JSON.stringify({ 9 permission: '<permission>', 10}); 11 12const options = { 13 method: 'PUT', 14 headers, 15 body, 16}; 17 18async function AddCollaborator() { 19 try { 20 const response = await fetch(endpointUrl, options); 21 printResponse(response); 22 } catch (error) { 23 printErrors(error); 24 } 25}; 26 27AddCollaborator();

HTTP响应码可能包括以下情况:

  • 201:当新协作者被成功添加到仓库时。
  • 204:当来自其他仓库的现有协作者被添加为当前仓库的协作者时。

简要说明如下:

  • 第1行: 定义了接口的URL。
  • 第8至10行: 定义了所需的请求体参数。
  • 第18至25行: 定义了一个异步函数,用于调用指定的接口。
    • 第21行: 若请求成功,通过调用 printResponse 函数打印API响应。
    • 第23行: 否则,通过调用 printErrors 函数打印错误信息。
  • 第27行: 调用该异步函数。

请求参数

参数类型说明
repository字符串要获取用户权限的仓库名称
username字符串要获取权限的协作者用户名
1const endpointUrl = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>/permission'; 2 3const headers = { 4 Authorization: 'token {{ACCESS_TOKEN}}', 5 Accept: 'application/vnd.github.v3+json', 6}; 7 8const options = { 9 method: 'GET', 10 headers, 11}; 12 13async function GetCollaborators() { 14 try { 15 const response = await fetch(endpointUrl, options); 16 printResponse(response); 17 } catch (error) { 18 printErrors(error); 19 } 20}; 21 22GetCollaborators();

上述代码成功执行后,将返回响应码 200。

简要说明如下:

  • 第1行: 定义了接口的URL。
  • 第13至20行: 定义了一个异步函数,用于调用指定的接口。
    • 第16行: 若请求成功,通过调用 printResponse 函数打印API响应。
    • 第18行: 否则,通过调用 printErrors 函数打印错误信息。
  • 第22行: 调用该异步函数。

请求参数

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

上述代码成功执行后,将返回响应码 204。

简要说明如下:

  • 第1行: 定义了接口的URL。
  • 第13至20行: 定义了一个异步函数,用于调用指定的接口。
    • 第16行: 若请求成功,通过调用 printResponse 函数打印API响应。
    • 第18行: 否则,通过调用 printErrors 函数打印错误信息。
  • 第22行: 调用该异步函数。

代码管理

拉取请求