【问题标题】:Clone all BitBucket Repositories From a Project using jq使用 jq 从项目中克隆所有 BitBucket 存储库
【发布时间】:2019-10-26 07:32:50
【问题描述】:

我正在尝试在 BitBucket 中克隆我团队项目中的所有存储库。

我想从 REST 调用返回的 JSON 中提取 url 和名称,并使用这些值进行克隆

以下是我所拥有的

curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?per_page=20 ^
 -u user:pass | H:\Downloads\Win64\jq-win64.exe -r ".values[] | .links.clone[] | select(.name==\"http\") | .href" ^
  H:\Utilities\Git\usr\bin\xargs.exe -L1 git clone  -b release/development 

这适用于 url,但我想要做的也是将目录的名称更改为 Name 属性,例如GitRepository1、GitRepository2 等,而不是 gitrepo1、gitrepo2 即在不传递该参数时自动使用

所以,类似于

curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?per_page=20 ^
 -u user:pass | H:\Downloads\Win64\jq-win64.exe -r ".values[] | .links.clone[] | select(.name==\"http\") | .href" ^
  H:\Utilities\Git\usr\bin\xargs.exe -L1 git clone  -b release/development git_url dir_name

我需要 jq 命令的帮助来选择这两个属性并传递给 xargs 命令

这是json结构

{
  "size": 25,
  "limit": 25,
  "isLastPage": false,
  "values": [
    {
      "slug": "gitrepo1",
      "id": 2216,
      "name": "GitRepository1",
      "scmId": "git",
      "state": "AVAILABLE",
      "statusMessage": "Available",
      "forkable": true,
      "project": {
        "key": "PROJECT_NAME",
        "id": 1369,
        "name": "PROJECT_NAME",
        "description": "ABC Team",
        "public": false,
        "type": "NORMAL",
        "links": {
          "self": [
            {
              "href": "http://bitbucketlocalserver:7990/projects/PROJECT_NAME"
            }
          ]
        }
      },
      "public": false,
      "links": {
        "clone": [
          {
            "href": "ssh://git@bitbucketlocalserver:7999/PROJECT_NAME/gitrepo1.git",
            "name": "ssh"
          },
          {
            "href": "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git",
            "name": "http"
          }
        ],
        "self": [
          {
            "href": "http://bitbucketlocalserver:7990/projects/PROJECT_NAME/repos/gitrepo1/browse"
          }
        ]
      }
    },
    {
      "slug": "gitrepo2",
      "id": 2214,
      "name": "GitRepository2",
      "scmId": "git",
      "state": "AVAILABLE",
      "statusMessage": "Available",
      "forkable": true,
      "project": {
        "key": "PROJECT_NAME",
        "id": 1369,
        "name": "PROJECT_NAME",
        "description": "ABC Team",
        "public": false,
        "type": "NORMAL",
        "links": {
          "self": [
            {
              "href": "http://bitbucketlocalserver:7990/projects/PROJECT_NAME"
            }
          ]
        }
      },
      "public": false,
      "links": {
        "clone": [
          {
            "href": "ssh://git@bitbucketlocalserver:7999/PROJECT_NAME/gitrepo2.git",
            "name": "ssh"
          },
          {
            "href": "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo2.git",
            "name": "http"
          }
        ],
        "self": [
          {
            "href": "http://bitbucketlocalserver:7990/projects/PROJECT_NAME/repos/gitrepo2/browse"
          }
        ]
      }
    }
  ],
  "start": 0,
  "nextPageStart": 25
}

有可能吗?我该怎么做?

谢谢

【问题讨论】:

    标签: git jq bitbucket-api


    【解决方案1】:

    假设您要执行的 git 命令与流 .values[].links.clones[] 中的相关项一样多,这里简单的关键是使用 jq 来构造它们。以下 jq 过滤器将完成这项工作:

     .values[]
     | .name as $name
     | .links.clone[] | select(.name=="http") 
     | "git clone -b release/development \"\(.href)\" \($name)"
    

    (为避免在使用 Windows 时使用引号引起的麻烦,您可能会发现将过滤器放在文件中最简单。)

    【讨论】:

    • 谢谢。实际上,这就是我正在尝试的 - 让 jq 构造工作。我尝试了上述但得到错误。在理解如何正确使用 jq 方面需要一些帮助。感谢任何帮助... H:\Downloads\Win64\jq-win64.exe -r ".values[] | .name as $name | .links.clone[] | select(.name==\"http\" )" | "git clone -b release/development \"(.href)\" ($name)" '"git clone -b release/development \"\' 不是内部或外部命令、可运行程序或批处理文件。
    • 正如括号中所暗示的那样,我建议使用 jq 的 -f 命令行选项。
    【解决方案2】:

    下面的命令给出了所需的输出

    curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?limit=100 ^
    -u user:pass | H:\Downloads\Win64\jq-win64.exe -r "[.values[] | ((.links.clone[] | select(.name==\"http\") |  .href)  + \" \" +  .name)]"
    

    这个命令的输出是

    [
       "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository1",
       "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository2"
    ]
    

    【讨论】:

      猜你喜欢
      • 2017-03-18
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 2019-12-26
      • 2021-02-22
      • 2016-02-05
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多