如何通过Github API创建仓库?

16

我想使用Github API在特定组织下创建仓库。我查看了这个网站,它介绍了如何在特定组织下创建仓库。

Create a new repository in this organization. The authenticated user must be a member of the specified organization.
POST /orgs/:org/repos

现在我无法理解我需要使用什么完整的URL来在特定组织下创建存储库?我有用户名和密码,可以通过https URL在组织下创建存储库。

我的Github实例URL是这样的- https://github.host.com

创建后,我希望我的存储库是这样的 -

https://github.host.com/Database/ClientService

在组织下创建代码仓库的 Curl 命令是什么?


1
你需要的所有信息都在这里:**https://developer.github.com/v3/guides/getting-started/#create-a-repository**,该网页是github帮助页面中描述的。 - CodeWizard
谢谢您的建议。这里的“token”是什么?我该如何获得它?抱歉问一个愚蠢的问题,因为我是第一次工作,所以不确定。 - john
1
在此处阅读如何操作:https://help.github.com/articles/creating-an-access-token-for-command-line-use - CodeWizard
7个回答

24

前往设置 -> 开发人员设置 -> 个人访问令牌在OAuth应用程序下。现在,使用所需的特权生成新的访问令牌。 如果您已经有一个,请忽略此步骤。

用户账户:

在以下命令中,将ACCESS_TOKEN替换为令牌,将NEW_REPO_NAME替换为您的新存储库名称

curl -H "Authorization: token ACCESS_TOKEN" --data '{"name":"NEW_REPO_NAME"}' https://api.github.com/user/repos

组织:

curl -H "Authorization: token ACCESS_TOKEN" --data '{"name":"NEW_REPO_NAME"}' https://api.github.com/orgs/ORGANIZATION_NAME/repos

1
创建仓库的权限是什么? - Timo

15

步骤1:创建个人访问令牌并将其用于替代密码

步骤2:在命令行中使用给定的API作为POST请求

https://api.github.com/orgs/<organisation_name>/repos?access_token=<generated token>
或者
https://api.github.com/user/<username>/repos?access_token=<generated token>

在请求体中,将这个作为有效载荷传递:

{
  "name": "<Repo Name>",
  "description": "<Some message>",
  "homepage": "https://github.com",
  "private": false,
}

您可以传递其他细节。
更多详情请点击这里


3
此方法已不再支持 - 无法在 URL 中添加访问令牌。您需要使用授权标头。 - glls
如果使用Insomnia,仍然可以使用这种类型的URL和JSON负载 - 只需将令牌放在Bearer类型身份验证中即可。 - Tom Charles Zhang

3

现在有一个来自 cli.github.com 的新解决方案。其他答案已经过时。

首先安装 GitHub CLI。运行:

brew install gh

接下来创建一个名字特定的代码库,请输入以下命令:

gh repo create --private my-project

这是创建仓库的官方文档链接


2
在GitHub企业版中创建一个新的仓库,以下方法对我有效:
执行之前,请更改尖括号中的值。在-u后面输入用户名:令牌
curl -u https://github.com/api/v3/orgs/<org-name>/repos -d '{"name":"<reponame>"}

在我的情况下,我们公司有一个自定义的 GitHub 域名;为了论证假设我们的 GitHub 域名是 "stuff.tinycorp.localdomain"。我不得不将上面的 "github.com" 替换为 "stuff.tinycorp.localdomain"。除此之外,这是一个非常有帮助的答案。 - Mike Pennington

1
根据以上两个答案,以下是一个用bash >=4.2编写的脚本,用于克隆一个仓库。
#!/usr/bin/env bash
TOKEN=0000000000000000000000000000000000000000

if [[ $# < 3 ]]; then
    echo "arguments: $0 <RepoName> <RepoDescription>"
    exit 1
fi

REPO_NAME=$1; shift
REPO_DESCRIPTION="$@"

echo "Name: $REPO_NAME"
echo "Description: $REPO_DESCRIPTION"
echo "Calling GitHub to create repo"

read -r -d '' PAYLOAD <<EOP
{
  "name": "$REPO_NAME",
  "description": "$REPO_DESCRIPTION",
  "homepage": "https://github.com/$REPO_NAME",
  "private": false
}
EOP

shopt -s lastpipe
curl -H "Authorization: token $TOKEN" --data "$PAYLOAD" \
    https://api.github.com/user/repos | readarray output

url=''
for line in "${output[@]}"; do
    echo -n "$line"
    #   "html_url": "https://github.com/sfinktah/vim-hex-sum",
    if [[ $line == *html_url* ]]; then
        l=${line#*\"}; l=${l#*\"}; l=${l#*\"}; url=${l%\"*}
    fi
done

count=0
[[ $url == http*://*github.com/* ]] &&
    until git clone $url; do 
        sleep 10; (( count++ > 5 )) && break; echo Waiting...
    done

0

看看这个答案,这是一种旧的方法, 不需要安装github cli

直接使用api在终端中创建repo

替换userName和repoName

curl -u "userNameHere" https://api.github.com/user/repos -d > '{"name":"repoNameHere","private":true}'

输入密码即可完成

有关更多信息,请参阅新的官方文档here


0
推荐使用Github SDK Octokit。创建一个代码库的方法如下:
const octokit = new Octokit({ auth: `personal-access-token123` });

// Create a repo for user.
octokit.rest.repos.createForAuthenticatedUser({
  name,
});

// Create a repo in an organization.
octokit.rest.repos.createInOrg({
  org,
  name,
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接