How to create repository in github through github API?

有些话、适合烂在心里 提交于 2019-12-28 05:40:10

问题


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

My github instance url is like this - 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?


回答1:


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

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

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

or

https://api.github.com/users/<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 as per the requirements.
For more details: click here




回答2:


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.

User Account:

Replace ACCESS_TOKEN with Token and NEW_REPO_NAME with your New Repository Name in the command below:

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



回答3:


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

#!/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


来源:https://stackoverflow.com/questions/28385884/how-to-create-repository-in-github-through-github-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!