Search code examples
pythonpygithub

Get specific tag from repository using PyGithub


I have this very simple code using PyGithub

from github import Github

g = Github('<offuscated token>')
repo = g.get_repo("telefonicaid/fiware-orion")
repo.get_git_tag("3.8.0")

The repository is public (in fact, maybe the token is not needed...) and the tag exists, it can be checked here: https://github.com/telefonicaid/fiware-orion/tree/3.8.0

However, if I run that code I get at repo.get_git_tag("3.8.0") the following exception:

github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/git#get-a-tag"}

Maybe this is not the right way of getting a tag using PyGithub? How should be done in that case, please?

Thanks in advance!


Solution

  • The API call you're using expects a SHA hash not a named identifier. This is how to get information for a named tag as you probably want:

    from github import Github
    
    g = Github('thar_be_dragons_here')
    repo = g.get_repo('telefonicaid/fiware-orion')
    tag = next((x for x in repo.get_tags() if x.name == '3.8.0'))
    
    print(tag)
    

    Which gives:

    Tag(name="3.8.0", commit=Commit(sha="18d3634afeb366c62af90b9219c5ffac8e894cad"))
    

    From there you can use the commit object or the SHA it contains to poke around further. You'll also need the token because GitHub needs to know the permissions scope of what your code should be doing (repo scope in this case).