Skip to content

Instantly share code, notes, and snippets.

@jdtournier
Created March 14, 2018 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdtournier/d5cea2a4d17e82ea0b99c5b4a425e61d to your computer and use it in GitHub Desktop.
Save jdtournier/d5cea2a4d17e82ea0b99c5b4a425e61d to your computer and use it in GitHub Desktop.
generate changelog

This script guesses the numbers for the merged pull requests from the last tag, then fetches the corresponding information from GitHub and formats it into markdown format suitable for use in e.g. Discourse.

To use, please add create a 'fetch_changelog_settings' file in the toplevel of your repo folder, with the correct information:

repo = 'MRtrix3/mrtrix3'
user = 'jdtournier'
password = 'gngkasgndfgknldsafnfknxgkjd'

where password above should contain your personal authentication token, as generated from your GitHub acccount.

You can then invoke this script from the toplevel folder of your repo, and redirect the output to a file, or copy/paste the output as required:

$ python fetch_changelog > changelog.md
#!/usr/bin/env python
# copy/paste the following into a standalone 'fetch_changelog_settings' file,
# and amend with the appropriate information:
repo = 'MRtrix3/mrtrix3'
user = 'YOUR_GITHUB_USERNAME'
# set this to your personal access token, which can generate from GitHub at:
# https://github.com/settings/tokens
password = 'YOUR_GITHUB_TOKEN'
import os, subprocess, requests, sys
try:
with open('fetch_changelog_settings') as fd:
exec (fd.read())
except:
sys.stderr.write ('no fetch_changelog_settings file found - using built-in defaults')
def get_list_of_merges (base, branch):
list = []
cmd = ['git', 'log', '--oneline', base + '...' + branch ]
#sys.stderr.write (' '.join (cmd) + '\n')
for line in subprocess.check_output (cmd).decode(encoding='utf-8').splitlines():
if 'Merge pull request #' in line:
list.append (int (line.split()[4][1:]))
return list
def get_pull_request (number):
url = 'https://api.github.com/repos/' + repo + '/pulls/' + str(number)
resp = requests.get (url, auth= (user, password))
if resp.status_code != 200:
sys.stderr.write ('error fetching request ' + str(number) +' with response: ' + str(resp.status_code) + '\n')
sys.stderr.write ('URL was "' + url + '"\n')
print (resp.json())
sys.exit (1)
return resp
base_tag = subprocess.check_output (['git', 'describe', '--abbrev=0']).decode(encoding='utf-8').strip()
sys.stderr.write ('using base: ' + base_tag + '\n')
merges = get_list_of_merges (base_tag, 'HEAD')
sys.stderr.write ('found pull requests ' + str(merges) + '\n')
for number in merges:
sys.stderr.write (str(number) + ' ')
sys.stderr.flush()
resp = get_pull_request(number).json()
description= resp['title']
user = resp['user']['login']
body = '\n '.join (resp['body'].splitlines())
sys.stdout.write ('- [![](https://github.com/'+user+'.png?size=24)](https://github.com/'+user+') ['+description+'](https://github.com/MRtrix3/mrtrix3/pull/'+str(number)+') ' + body + '\n')
sys.stderr.write ('\ndone\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment