Search code examples
pythonpython-3.xgithub-api

Github API: Get 'body' for all new releases in Python


I have a python script that fetches for updates from GitHub and displays patch-notes for the latest version when doing so.

I would like the script to display patch-notes for all releases ahead of the current version/release.

My github releases are versioned as so: v1.2.3

This is the code I am currently using that displays patch notes for the latest release only:

version = 2.1.5

import urllib.request, json

with urllib.request.urlopen("https://api.github.com/repos/:author/:repo/releases/latest") as url:
  data = json.loads(url.read().decode())
  latest = data['tag_name'][1:] # "v2.3.6" -> "2.3.6"
  patchNotes = data['body']

if latest > version:
    print('\nUpdate available!')
    print(f'Latest Version: v{latest}')
    print('\n'+str(patchNotes)+'\n') #display latest (v2.3.6) patch notes
    input(str('Update now? [Y/n] ')).upper()
    #code to download the latest version

This is an idea I have had to get what I need:

  1. Get all release version numbers (e.g 1.2.3)
  2. Add all version numbers that are ahead of the current into an array
  3. For version in array, get data[body] from respective github-api json page
  4. Print the patch-notes in the correct order (oldest (one version ahead of current) to newest (latest version)

I don't know how to impliment the above idea, and if there are more efficient ways of achieving what I'm going for, I'm open for suggestions.


Solution

  • You can use something like this :

    import requests
    from packaging import version
    
    maxVersion = version.parse("3.9.2")
    repoWithOwner= "labstack/echo"
    
    r = requests.get("https://api.github.com/repos/{}/releases?per_page=100".format(repoWithOwner))
    releases = [ 
        (t["tag_name"],t["body"]) 
        for t in r.json() 
        if version.parse(t["tag_name"]) >= maxVersion
    ][::-1]
    
    for r in releases:
        print("{} : {}".format(r[0],r[1]))
    

    It gets the 100 last releases & check wether it is >= {your specified version}, reverse the array and print the tag & body