githubgithub-graphqlgithub-api-v4

How to get the zipballUrl of a branch using github graphQL API?


I am trying to programmatically download a zip file of a repository which is already checked out to a specific branch. For that I need to obtain the zipballUrl of the head of the branch I specify through github graphQL API.

This answer specifies how to get the zipballUrl of the default branch but I could not alter it to my requirement. Appreciate if someone can help out.

PS: Running a git clone command and checking out to branch is not an option because the programming language I use (ballerina) does not support shell commands yet.


Solution

  • You can use the reference field of the repository object.

    For example, considering the same google gson project

    {
      repository(owner: "google", name: "gson") {
    
        ref(qualifiedName: "722"){
          target {
            ... on Commit {
              tarballUrl
              zipballUrl
            }
          }
        }
      }
    }
    

    In this query ref(qualifiedName) will basically look for the reference or branch name with the specified value and return the tar content of the branch.

    Response

    {
      "data": {
        "repository": {
          "ref": {
            "target": {
              "tarballUrl": "https://codeload.github.com/google/gson/legacy.tar.gz/2725be440147a71030ece93683b4424e849c59ed",
              "zipballUrl": "https://codeload.github.com/google/gson/legacy.zip/2725be440147a71030ece93683b4424e849c59ed"
            }
          }
        }
      }
    }
    

    you can verify the sha of the branch reference "722" https://github.com/google/gson/commit/2725be440147a71030ece93683b4424e849c59ed