跳转至

添加项目成员或更新项目成员权限

PUT /api/v5/repos/{owner}/{repo}/collaborators/{username}

该接口用于添加项目成员或更新项目成员权限:当目标用户尚未成为仓库成员时,将其添加为成员;当目标用户已是仓库成员时,更新其成员权限,权限可为参与者(pull)、开发者(push)或仓库维护者(admin),并返回更新后的成员信息。

访问令牌支持说明

  • AtomGit 用户访问令牌(AtomGit user access tokens)

是否允许匿名访问

此接口不允许匿名访问

请求参数

参数名 位置 类型 必填 说明
owner path string 是 仓库所属空间地址(企业、组织或个人的地址 path)
repo path string 是 仓库路径(path)
username path string 是 用户名(username/login)
access_token query string 是 用户授权码

Request Body:

请求体为可选的 JSON 对象,当前仅包含一个可选字段 permission。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "permission": {
      "description": "成员权限:参与者(pull)、开发者(push)、仓库维护者(admin),或自定义角色传入角色名。默认:push",
      "type": "string",
      "enum": ["pull", "push", "admin"],
      "default": "push"
    }
  }
}

响应参数

字段 类型 必返 可为空 说明
id string 否 是 用户 ID
login string 否 是 登录名(login)
name string 否 是 用户名(name)
avatar_url string 否 是 头像 URL,可能为 null
html_url string 否 是 用户主页 URL
remark string 否 是 企业备注名
type string 否 是 用户类型(如 User)
url string 否 是 用户 URL
followers_url string 否 是 关注者列表 URL
permissions object 否 是 该成员在仓库中拥有的权限集合
memberRole string 否 是 成员角色名称

permissions 对象

字段 类型 必返 可为空 说明
permissions.pull boolean 否 是 是否拥有拉取代码(pull)权限
permissions.push boolean 否 是 是否拥有推送代码(push)权限
permissions.admin boolean 否 是 是否拥有仓库管理(admin)权限

失败响应

以下为接口可能返回的失败状态码示例,错误响应体格式以后端实际返回为准。

400 参数错误

{
  "error": "bad_request",
  "message": "角色不存在"
}

401 未认证

{
  "error": "unauthorized",
  "message": "Bad credentials"
}

404 用户不存在

{
  "error": "not_found",
  "message": "user not found"
}

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误(Bad request)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 用户不存在(Not found)
429 请求过于频繁(Too many requests)
500 系统内部错误(Internal server error)

请求示例

参数说明

<owner> 替换为仓库所属空间地址(企业、组织或个人的地址 path);<repo> 替换为仓库路径(path);<username> 替换为目标用户的用户名(username/login);<token> 替换为你的 AtomGit 用户访问令牌。

cURL

curl -X PUT 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/collaborators/<username>?access_token=<token>' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{"permission": "push"}'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch("https://api.atomgit.com/api/v5/repos/<owner>/<repo>/collaborators/<username>?access_token=<token>", {
  method: "PUT",
  headers: {
    "Accept": "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ permission: "push" }),
});

if (!response.ok) {
  throw new Error(`请求失败: ${response.status}`);
}

const data = await response.json();
console.log(data);

Python

import requests

resp = requests.put(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/collaborators/<username>",
    params={"access_token": "<token>"},
    headers={
        "Accept": "application/json",
        "Content-Type": "application/json",
    },
    json={"permission": "push"},
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "id": "7543745",
  "login": "centking",
  "name": "占分",
  "avatar_url": null,
  "html_url": "https://api.atomgit.com/centking",
  "remark": "",
  "type": "User",
  "permissions": {
    "pull": true,
    "push": true,
    "admin": false
  }
}

错误码

错误码 说明
400 参数错误或请求无法解析(如角色不存在)
401 未认证或凭证无效(缺少/无效的访问令牌)
403 禁止访问(当前令牌无权操作该仓库成员)
404 用户不存在或请求路径不存在
429 请求过于频繁,触发限流
500 系统内部错误

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc