Github API:为 Python 中的所有新版本获取 'body'

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

我有一个 python 脚本,它从 GitHub 获取更新并在这样做时显示最新版本的补丁说明。

我希望脚本显示当前 version/release 之前所有版本的补丁说明。

我的 github 版本版本如下:v1.2.3

这是我目前使用的代码,它仅显示最新版本的补丁说明:

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

这是我不得不得到我需要的想法:

  1. 获取所有发布版本号(例如 1.2.3)
  2. 将当前版本号之前的所有版本号添加到一个数组中
  3. 对于数组中的版本,从相应的 github-api json 页面
  4. 获取 data[body]
  5. 以正确的顺序打印补丁说明(最旧(比当前版本早一个版本)到最新(最新版本)

我不知道如何实现上述想法,如果有更有效的方法来实现我的目标,我愿意提出建议。

你可以使用这样的东西:

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]))

它获取最后发布的 100 个并检查它是否 >= {您指定的版本},反转数组并打印标签和正文

启发,根据我的需要进行了编辑

我使用了Python3的预装库urllib.requestjson(所以它更便携),并且 f-strings 而不是 .format

import urllib.request, json

ver='3.0.0' #Current scripts version
author='labstack'
repo='echo'

release = json.loads(urllib.request.urlopen(f'https://api.github.com/repos/{author}/{repo}/releases?per_page=5').read().decode())
releases = [
    (data['tag_name'],data['body'])
    for data in release
    if data['tag_name'] > ver][::-1]
for release in releases:
    print(f'{release[0]}:\n{release[1]}\n')