简体   繁体   English

如何通过github API在github中创建存储库?

[英]How to create repository in github through github API?

I am trying to use github api to create repositories under a particular organization.我正在尝试使用 github api 在特定组织下创建存储库。 I was looking at this site which talks about how to create repositories under a particular organization.我正在查看这个站点,该站点讨论了如何在特定组织下创建存储库。

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

Now I am not able to understand what will be my full URL that I need to use to create the repository under a particular organization?现在我无法理解在特定组织下创建存储库所需的完整 URL 是什么? I have my username and password which I can use to create the repository under an organization through https url.我有我的用户名和密码,可用于通过 https url 在组织下创建存储库。

My github instance url is like this - https://github.host.com我的 github 实例 url 是这样的 - https://github.host.com

And I want my repository to be like this after getting created -我希望我的存储库在创建后像这样 -

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

What will be my curl command look like to create the repository under an organization?在组织下创建存储库时,我的 curl 命令是什么样的?

Go to Settings -> Developer Settings -> Personal Access Token Under OAuth Apps.转到设置->开发人员设置-> OAuth 应用程序下的个人访问令牌 Now, Generate a New Access token with Required privileges enabled.现在,生成一个启用了所需权限的新访问令牌。 Ignore This if you already have one.如果你已经有了,请忽略这个。

User Account:用户帐号:

Replace ACCESS_TOKEN with Token and NEW_REPO_NAME with your New Repository Name in the command below:在以下命令NEW_REPO_NAME替换为Token ,将ACCESS_TOKEN替换为您的新存储库Name

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

Organisation:组织:

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

Step 1: Create a personal access token and use it in place of password第 1 步:创建个人access token并使用它代替密码

Step 2: On command line use the given API as a POST request第 2 步:在命令行上使用给定的 API 作为POST请求

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

or要么

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

In body, pass this as a payload:在正文中,将其作为有效负载传递:

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

You can pass other details.您可以传递其他详细信息。
For more details: click here更多详情: 点击这里

There is a new solution official from cli.github.com . cli.github.com有一个新的官方解决方案。 The other answers are deprecated.其他答案已弃用。

First Install GitHub CLI.首先安装 GitHub CLI。 Run:跑步:

brew install github/gh/gh

Then to create a repository with a specific name (default: private) Enter the following command:然后创建具有特定名称的存储库(默认:私有)输入以下命令:

gh repo create my-project

Here is the official documentation link to create a repo.这是创建 repo 的官方文档链接

Based on the two above answers, here's a bash >=4.2 script to create a clone a repo.基于以上两个答案,这里有一个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

Checkout this answer this is an old method, without installing github cli结帐这个答案这是一种旧方法,无需安装 github cli

create repo in terminal directly with api直接使用 api 在终端中创建 repo

replace userName and repoName替换 userName 和 repoName

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

enter password and done输入密码并完成

for more info refer new official doc here有关更多信息,请参阅此处的新官方文档

Create repo in an enterprise hosted in github.在 github 中托管的企业中创建 repo。 Worked for me为我工作

Please change the value in angular brackets before executing.请在执行前更改尖括号中的值。 Put username:token after -uusername:token放在-u之后

curl -u https://github.com/api/v3/orgs/<org-name>/repos -d '{"name":"<reponame>"}

Recommend Github SDK Octokit .推荐Github SDK Octokit To create a repository:要创建存储库:

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,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM