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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28385884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:45:10  来源:igfitidea点击:

How to create repository in github through github API?

gitcurlgithubgithub-api

提问by john

I am trying to use github api to create repositories under a particular organization. I was looking at this sitewhich talks about how to create repositories under a particular organization.

我正在尝试使用 github api 在特定组织下创建存储库。我正在查看这个站点,该站点讨论了如何在特定组织下创建存储库。

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? I have my username and password which I can use to create the repository under an organization through https url.

现在我无法理解在特定组织下创建存储库所需的完整 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 命令是什么样的?

回答by Jitendra Kumar

Step 1: Create a personal access tokenand use it in place of password

第 1 步:创建个人access token并使用它代替密码

Step 2: On command line use the given API as a POSTrequest

第 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

您可以传递其他详细信息。
更多详情:点击这里

回答by Teja Kummarikuntla

Go to Settings-> Developer Settings -> Personal Access Token Under OAuth Apps. Now, Generate a New Access token with Required privileges enabled. Ignore This if you already have one.

转到设置->开发人员设置-> OAuth 应用程序下的个人访问令牌。现在,生成一个启用了所需权限的新访问令牌。 如果你已经有了,请忽略这个。

User Account:

用户帐号:

Replace ACCESS_TOKENwith Tokenand NEW_REPO_NAMEwith your New Repository Namein the command below:

更换ACCESS_TOKENTokenNEW_REPO_NAME你的新的存储库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

回答by Orwellophile

Based on the two above answers, here's a bash >=4.2script to create a clone a repo.

基于以上两个答案,这里有一个bash >=4.2创建克隆仓库的脚本。

#!/usr/bin/env bash
TOKEN=0000000000000000000000000000000000000000

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

REPO_NAME=; 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