【问题标题】:How to create repository in github through github API?如何通过 github API 在 github 中创建存储库?
【发布时间】:2015-04-07 18:46:56
【问题描述】:

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

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 命令是什么样的?

【问题讨论】:

标签: git curl github github-api


【解决方案1】:

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

用户帐号:

在以下命令中将ACCESS_TOKEN 替换为TokenNEW_REPO_NAME 使用您的新存储库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

【讨论】:

  • 创建repo的权限是什么?
【解决方案2】:

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

第 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,
}

您可以传递其他详细信息。
更多详情:click here

【讨论】:

  • 不再支持此方法 - 无法在 url 中添加访问令牌。您将不得不使用授权标头
  • 如果使用 Insomnia,仍然可以使用这种 url 和 JSON 负载 - 只需将令牌作为 Bearer type auth 放入。
【解决方案3】:

cli.github.com有新的解决方案官方。其他答案已弃用。

首先安装 GitHub CLI。运行:

brew install github/gh/gh

然后创建一个具有特定名称的存储库(默认:私有)输入以下命令:

gh repo create my-project

这里是官方文档link创建一个repo。

【讨论】:

    【解决方案4】:

    基于以上两个答案,这里有一个bash &gt;=4.2 脚本来创建一个克隆一个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
    

    【讨论】:

      【解决方案5】:

      检查这个答案这是一个旧方法, 无需安装 github cli

      直接使用api在终端创建repo

      替换用户名和repoName

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

      输入密码并完成

      更多信息请参考新的官方文档here

      【讨论】:

        【解决方案6】:

        在 github 上托管的企业中创建 repo。为我工作

        请在执行前更改尖括号中的值。把username:token放在-u之后

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            猜你喜欢
            • 2012-04-01
            • 1970-01-01
            • 2016-02-27
            • 2018-05-31
            • 1970-01-01
            • 2021-09-04
            • 2023-03-10
            • 2012-06-03
            • 2014-09-21
            相关资源
            最近更新 更多