使用 jq 从项目中克隆所有 BitBucket 存储库

Clone all BitBucket Repositories From a Project using jq

我正在尝试在 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 命令的帮助 select 这两个属性并传递给 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 命令 .values[].links.clones[],这里简单的关键是使用 jq 来构造它们。以下 jq 过滤器将完成这项工作:

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

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

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

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"
]