How to create repository in github through github API?

后端 未结 5 822
别跟我提以往
别跟我提以往 2020-12-03 02:17

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 particu

相关标签:
5条回答
  • 2020-12-03 02:27

    There is a new solution official from cli.github.com. The other answers are deprecated.

    First Install 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.

    0 讨论(0)
  • 2020-12-03 02:28

    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
    
    0 讨论(0)
  • 2020-12-03 02:35

    Checkout this answer this is an old method, without installing github cli

    create repo in terminal directly with api

    replace userName and 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

    0 讨论(0)
  • 2020-12-03 02:38

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

    0 讨论(0)
  • 2020-12-03 02:44

    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
    
    0 讨论(0)
提交回复
热议问题