Search code examples
githubgraphqlgithub-apigithub-api-v4

GitHub GraphQL API: Get tags for a commit


I don't know if it's just hard to form a useful search for this problem or if there is no solution, but I've been trying to figure out how to use the GitHub GraphQL API to fetch all tags associated with a given commit.

I'm new to GraphQL and the GitHub API, so I'm just using GitHub's explorer to try to find the mechanism to do this.

Here's what I've tried, where there is a commit with hash bfa0635104bc9a559254b5007646942ff269ae11 and message 1.0.0, tagged with v1.0.0. I'm trying to get that tag, given the commit hash.

{
  repository(owner: "bscotch", name: "stitch") {
    createdAt
    description
    name
    object(expression: "bfa0635104bc9a559254b5007646942ff269ae11") {
      ... on Commit {
        message
        oid
      }
      ... on Tag {
        tagId: id
        target {
          oid
        }
      }
    }
    ref(qualifiedName: "bfa0635104bc9a559254b5007646942ff269ae11") {
      name
    }
    refs(first: 10, refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, query: "bfa0635104bc9a559254b5007646942ff269ae11") {
      nodes {
        name
      }
    }
  }
}

The output of the above only returns data for the ... on Commit content. Everything else is completely absent:

{
  "data": {
    "repository": {
      "createdAt": "2020-09-18T18:47:33Z",
      "description": "A Gamemaker Studio 2 Pipeline Development Kit. A CLI and Node.JS API for creating GMS2 asset pipelines.",
      "name": "stitch",
      "object": {
        "message": "1.0.0",
        "oid": "bfa0635104bc9a559254b5007646942ff269ae11"
      },
      "ref": null,
      "refs": {
        "nodes": []
      }
    }
  }
}

Is there a way to do this via the API? My best guess is that, if there is, it's via the refs field.


Solution

  • I've also been struggling and this isn't the best answer, but it at least works at minimum.

    Query refs/tags/ to get all tags, then filter manually in whatever language you're using for the matching OID. You're not guaranteed to fetch all the tags in one call so that you for sure have the OID in it, but I was trying to match against what was likely a recent tag, so it worked fine.

    {
      repository(name: "dg", owner: "dgattey") {
        refs(refPrefix: "refs/tags/", last: 100) {
          nodes {
            name
            target {
              oid
            }
          }
        }
      }
    }
    

    This gets me a giant list, but in it, I have

            {
              "node": {
                "name": "v2.0.1",
                "target": {
                  "oid": "85fe69df20062aafeea7593c45c1785f44208252"
                }
              }
            }
    

    And I can use other code to check "85fe69df20062aafeea7593c45c1785f44208252" against the OID I was searching for. Not ideal!