You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Octokit在GitHub Enterprise组织中创建内部仓库?

如何通过Octokit在GitHub Enterprise组织中创建内部仓库

背景

我就职于一家大型企业,使用GitHub Enterprise版本,已创建组织,希望通过Octokit API客户端创建仓库。通过Postman直接调用API可成功创建内部仓库,但使用Octokit的repos.createInOrg方法时,无法设置visibility为internal——Octokit的类型定义仅接受private或public作为该参数的值。

Postman成功请求信息

请求Headers

Authorization: Bearer <personal api token>
Content-Type: application/json
User-Agent: PostmanRuntime/7.32.3
Accept: */*
Postman-Token: <postman-token>
Host: <our host>
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 253

请求Body

{
    "name": "Hello-World2",
    "description": "This is your first repository",
    "homepage": "https://github.com",
    "private": false,
    "has_issues": true,
    "has_projects": true,
    "has_wiki": true,
    "visibility": "internal"
}

Octokit相关代码

创建仓库的调用代码

// 周边代码
const createResult = await octoClient.rest.repos.createInOrg({
    ...params,
    org: orga,
    team_id: params.teamId,                 
});

参数定义

const newRepoDescriptor: githubRepositoryCreationType = {
    name: 'automation-test-3',
    description: "automation-test-1",
    homepage: "",
    private: false,
    has_issues: true,
    has_projects: true,
    has_wiki: true,
}

Octokit客户端初始化代码

export const octoClientBaseParams = {
    auth: process.env.GITHUB_TOKEN,
    log: require("console-log-level")({ level: "debug" }),
    baseUrl: "https://<myhost>/api/v3",         
}

const myOctokit = Octokit.plugin(paginateRest);

export const octoClient:Octokit = new myOctokit(octoClientBaseParams);

使用的版本信息

"@octokit/plugin-paginate-rest": "^7.1.2",
"@octokit/rest": "^19.0.13",

Node版本:v16.14.0

解决方案

方法1:类型断言绕过TS检查

GitHub Enterprise本身支持visibility: "internal",只是Octokit的TypeScript定义未同步更新,可通过类型断言强行传入参数:

const createResult = await octoClient.rest.repos.createInOrg({
    ...params,
    org: orga,
    team_id: params.teamId,
    // 用类型断言绕过TS类型检查
    visibility: "internal" as unknown as "private" | "public"
});

方法2:直接发送原始API请求

如果类型断言不够稳妥,可使用Octokit的request方法直接构造请求,完全复刻Postman的调用逻辑:

const createResult = await octoClient.request("POST /orgs/{org}/repos", {
    org: orga,
    name: "automation-test-3",
    description: "automation-test-1",
    homepage: "",
    private: false,
    has_issues: true,
    has_projects: true,
    has_wiki: true,
    visibility: "internal",
    team_id: params.teamId
});

方法3:升级Octokit版本

尝试升级@octokit/rest到最新稳定版,新版本可能已更新类型定义,支持internal可见性参数:

npm install @octokit/rest@latest @octokit/plugin-paginate-rest@latest

内容的提问来源于stack exchange,提问作者Wurzelseppi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 06:32:13