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

时间:2019-10-07 15:40:56

标签: git jq bitbucket-api

我试图在我的团队的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
}

有可能吗?我该怎么做?

谢谢

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

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