How can I retrieve tags from a repository?


#1

Hello

Actually we can only retrieve tag within a release:

query repos {
  repository(owner: "j0k3r", name: "f43.me") {
    releases(last: 5) {
      nodes {
        tag {
          name
        }
      }
    }
  }
}

But some people doesn’t use the release feature of Github but only tag.

I was wondering if you plan to support ability to retrieve a list of tags from a repository?

Thanks


Get releases (including tags)
#2

:wave: @j0k3r,

I believe this query is what you’re looking for:

query {
  repository(owner:"rails", name:"rails") {
    tags:refs(refPrefix:"refs/tags/", last:30) {
      edges {
        tag:node {
          name
        }
      }
    }
  }
}

#3

Oh great ! :+1:
I forgot about the refs (like on the rest api). I was looking for tag somewhere.

Any chance to retrieve the author and date of the commit attached to the tag? (like when using the repo/commits endpoint)
I can retrieve the commit but I can’t go further

query {
  repository(owner:"rails", name:"rails") {
    tags:refs(refPrefix:"refs/tags/", last:30) {
      edges {
        tag:node {
          name
          target {
            sha:oid
          }
        }
      }
    }
  }
}

#4

The target field there should return a GitObject interface which implements Tag. From Tag, you should be able to pull the tagger which I believe has the info you’re looking for:

query {
  repository(owner: "rails", name: "rails") {
    tags: refs(refPrefix: "refs/tags/", last: 30) {
      edges {
        tag: node {
          name
          target {
            ... on Tag {
              sha: oid
              tagger {
                name
                email
                date
              }
            }
          }
        }
      }
    }
  }
}

#5

Oh excellent !

One last question I hope :slight_smile:

For that particular query, it seems it can’t retrieve information about the commit. Is it a bug in the graphql implementation? Because using the rest api I can get commit information.

{
  repository(owner: "arachnys", name: "cabot") {
    tags: refs(refPrefix: "refs/tags/", first: 5, direction: DESC) {
      edges {
        tag: node {
          name
          target {
            ... on Tag {
              sha: oid
              message
              tagger {
                name
                date
              }
            }
          }
        }
      }
    }
  }
}

Here is the response:

{
  "data": {
    "repository": {
      "tags": {
        "edges": [
          {
            "tag": {
              "name": "0.8.1",
              "target": {}
            }
          },
          {
            "tag": {
              "name": "0.8.0",
              "target": {}
            }
          },
          {
            "tag": {
              "name": "0.7.0",
              "target": {}
            }
          },
          {
            "tag": {
              "name": "0.6.0",
              "target": {}
            }
          }
        ]
      }
    }
  }
}

And I got same problem for rails/rails but only for one tag, the v5.0.0.1.
Strange is it? :thinking:


#6

It looks like the targets on those refs are actually commits (for the repo that you posted):

query {
  repository(owner: "arachnys", name: "cabot") {
    tags: refs(refPrefix: "refs/tags/", first: 5, direction: DESC) {
      edges {
        tag: node {
          name
          target {
          	__typename
          }
        }
      }
    }
  }
}
{
  "data": {
    "repository": {
      "tags": {
        "edges": [
          {
            "tag": {
              "name": "0.8.1",
              "target": {
                "__typename": "Commit"
              }
            }
          },
          {
            "tag": {
              "name": "0.8.0",
              "target": {
                "__typename": "Commit"
              }
            }
          },
          {
            "tag": {
              "name": "0.7.0",
              "target": {
                "__typename": "Commit"
              }
            }
          },
          {
            "tag": {
              "name": "0.6.0",
              "target": {
                "__typename": "Commit"
              }
            }
          }
        ]
      }
    }
  }
}

So in that case, you should be able to do:

  repository(owner: "arachnys", name: "cabot") {
    tags: refs(refPrefix: "refs/tags/", first: 5, direction: DESC) {
      edges {
        tag: node {
          name
          target {
            ... on Commit {
              sha:oid
              
              author {
                name
                email
                date
              }
            }
          }
        }
      }
    }
  }
}

Or if you wanted to catch the possibility of the target being a Commit or Tag, you could do:

query {
  repository(owner: "arachnys", name: "cabot") {
    tags: refs(refPrefix: "refs/tags/", first: 5, direction: DESC) {
      edges {
        tag: node {
          name
          target {
            sha: oid
            ... on Commit {
              author {
                name
                email
                date
              }
            }
            ... on Tag {
              tagger {
                name
                email
                date
              }
            }
          }
        }
      }
    }
  }
}

#7

Wow amazing.
Thanks so much, it works like a charm.