pythongerrit

In Gerrit's REST API, how do I find the patchset ID Values? I have the url.../details JSON but can't find it in there


I am making a request.get to get the JSON, and want to do it again with the latest patchset and the extension /revisions/(patchSetNumber)/files to find all the modified files. I can't figure out how to find the patchset IDs through the requests.

I have tried searching through the url/details extension but have not been able to locate a revisions tab or a patchset tab.


Solution

  • According to the documentation, you can specify the ALL_REVISIONS option to get a list of all the revisions associated with a change, e.g for this change, we can get a list of revisions like this (the tail -c+5 is there because of a buggy REST response):

    curl -s  'https://review.opendev.org/changes/918316?o=ALL_REVISIONS' |
      tail -c+5 |
      jq -r '.revisions|keys'
    

    This produces:

    [
      "14e0f2b73995510d7bd2b0eb84f3b71b92203c78",
      "1bb00656eed24ca7de9f35868910d3c301d8114a",
      "85cebfa4775da4cff725e017e29d2bcba6691642",
      "8831400ebcfe6cfb64526ce92830f574eb826f5e",
      "9d9b03b71a4a0e87321881e5a562d995ae5cf0d9",
      "a0c5cf5ff7a2e18524bc68a10fe82b811d56868e",
      "bd657d0b31152ed3fd2b147bfb1b6e2b0a279bac",
      "cab2417cd4d4f0f7595a1d843dfb44c0410b3a75",
      "d4e653fb2b1ff54298a1262dfc397cc29e845985",
      "d75f457e0e68fbd6a31bca76143264b623ff73bf",
      "f16f39564f2c55584c4ff28abc0d4b48d796c508"
    ]
    

    We can use those revision ids to get details about each patchset:

    $ curl -s  'https://review.opendev.org/changes/918316/revisions/14e0f2b73995510d7bd2b0eb84f3b71b92203c78/files' |
      tail -c+5 |
      jq
    

    Which gets us:

    {
      "/COMMIT_MSG": {
        "status": "A",
        "lines_inserted": 12,
        "size_delta": 483,
        "size": 483
      },
      "cinder/db/migrations/versions/a8189e5afd9a_add_volume_type_metadata_table.py": {
        "status": "A",
        "new_mode": 33188,
        "lines_inserted": 53,
        "size_delta": 1601,
        "size": 1601
      },
      "cinder/db/sqlalchemy/models.py": {
        "old_mode": 33188,
        "new_mode": 33188,
        "lines_inserted": 27,
        "size_delta": 784,
        "size": 41911
      },
      "cinder/tests/unit/db/test_migrations.py": {
        "old_mode": 33188,
        "new_mode": 33188,
        "lines_inserted": 5,
        "size_delta": 224,
        "size": 19916
      }
    }
    

    In Python, that would look like:

    >>> res=requests.get('https://review.opendev.org/changes/918316/revisions/14e0f2b73995510d7bd2b0eb84f3b71b92203c78/files')
    >>> data=json.loads(res.text[5:])
    >>> list(data.keys())
    ['/COMMIT_MSG',
     'cinder/db/migrations/versions/a8189e5afd9a_add_volume_type_metadata_table.py',
     'cinder/db/sqlalchemy/models.py',
     'cinder/tests/unit/db/test_migrations.py']