Clone all BitBucket Repositories From a Project using jq

风格不统一 提交于 2019-12-11 06:02:32

问题


I am trying to clone all repositories in my team's project in BitBucket.

I want to extract the url and the name from the JSON returned from the REST call and use those values to clone

Below is what I have

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 

This works for the url, but what I want to do is also change the name of the directory to the Name attribute, e.g. GitRepository1, GitRepository2 etc., instead of gitrepo1, gitrepo2 that is used automatically when that parameter is not passed

So, something similar to

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

I need help with the jq command to select the two attributes and pass to the xargs command

And this is the json structure

{
  "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
}

Is it possible? How can I do it?

Thanks


回答1:


Assuming you want to execute as many git commands as there are relevant items in the stream .values[].links.clones[], the key to simplicity here is to use jq to construct them. The following jq filter will do the job:

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

(To avoid hassles with the quotation marks when using Windows, you might find it simplest to put the filter in a file.)




回答2:


The command below gives the output needed

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

The output from this command is

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


来源:https://stackoverflow.com/questions/58272893/clone-all-bitbucket-repositories-from-a-project-using-jq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!