跳转至

Fork仓库

POST /api/v5/repos/{owner}/{repo}/forks

该接口用于将指定仓库复制(Fork)到目标空间,可指定组织空间、新仓库名称及路径,未指定时默认复制到用户个人空间并沿用源仓库名称与地址,操作成功后返回新仓库的完整信息。

访问令牌支持说明

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

是否允许匿名访问

此接口不允许匿名访问

请求参数

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

Request Body:

请求体为 application/json 格式,以下字段均可选,不填写时使用默认值:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "organization": {
      "description": "组织空间完整地址,不填写默认 Fork 到用户个人空间地址",
      "type": "string"
    },
    "name": {
      "description": "Fork 后仓库名称。默认:源仓库名称",
      "type": "string"
    },
    "path": {
      "description": "Fork 后仓库地址。默认:源仓库地址",
      "type": "string"
    }
  }
}

响应参数

字段 类型 必返 可为空 说明
id integer 是 否 仓库 ID
full_name string 是 否 仓库完整名称(命名空间/仓库名),如 xiaogang_test/fork2
human_name string 是 否 仓库显示名称,如 测试组织 / fork2
url string 是 否 仓库 API 地址
namespace object 是 否 仓库所属空间(企业、组织或个人)信息

namespace 对象

字段 类型 必返 可为空 说明
namespace.id integer 是 否 空间 ID
namespace.name string 是 否 空间名称
namespace.path string 是 否 空间地址路径
namespace.develop_mode string 是 否 开发模式
namespace.cell string 是 否 空间单元
namespace.kind string 是 否 空间类型,如 group
namespace.full_path string 是 否 空间完整路径
namespace.full_name string 是 否 空间完整名称
namespace.visibility_level integer 是 否 可见性级别
namespace.enable_file_control integer 是 否 是否启用文件控制

失败响应

以下为常见失败响应的示例,错误字段名以后端实际返回为准。

400 参数错误

{
  "error": "invalid_request",
  "message": "参数 owner 不能为空"
}

401 未认证

{
  "error": "unauthorized",
  "message": "缺少或无效的访问令牌"
}

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误(Bad request)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 请求路径不存在(Not found)

请求示例

参数说明

示例中的占位符说明:

<owner> 替换为仓库所属空间地址(企业、组织或个人的地址 path); <repo> 替换为仓库路径(path); <token> 替换为你的 AtomGit 用户访问令牌; <org_path>、<new_name>、<new_path> 为可选的请求体字段,不传入时使用默认值。

cURL

curl -X POST 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/forks?access_token=<token>' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d '{
    "organization": "<org_path>",
    "name": "<new_name>",
    "path": "<new_path>"
  }'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch("https://api.atomgit.com/api/v5/repos/<owner>/<repo>/forks?access_token=<token>", {
  method: "POST",
  headers: {
    "Accept": "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    organization: "<org_path>",
    name: "<new_name>",
    path: "<new_path>",
  }),
});

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

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

Python

import requests

resp = requests.post(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/forks",
    params={"access_token": "<token>"},
    headers={
        "Accept": "application/json",
        "Content-Type": "application/json",
    },
    json={
        "organization": "<org_path>",
        "name": "<new_name>",
        "path": "<new_path>",
    },
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "id": 729292,
  "full_name": "xiaogang_test/fork2",
  "human_name": "测试组织 / fork2",
  "url": "https://api.atomgit.com/api/v5/repos/xiaogang_test/fork2",
  "namespace": {
    "id": 138108,
    "name": "测试组织",
    "path": "xiaogang_test",
    "develop_mode": "normal",
    "cell": "default",
    "kind": "group",
    "full_path": "xiaogang_test",
    "full_name": "测试组织",
    "visibility_level": 20,
    "enable_file_control": false
  }
}

错误码

错误码 说明
400 参数错误或请求无法解析
401 未认证或凭证无效
403 禁止访问
404 请求路径不存在
500 系统内部错误

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