|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""changelog |
| 4 | +This script outputs a changelog markdown to stdout. |
| 5 | +""" |
| 6 | + |
| 7 | +import subprocess |
| 8 | +import re |
| 9 | + |
| 10 | +# Get all the tags for the project |
| 11 | +git_tag_result = subprocess.run( |
| 12 | + ["git", "tag"], capture_output=True, text=True |
| 13 | +) |
| 14 | + |
| 15 | +# Make a tag list |
| 16 | +tags = git_tag_result.stdout.split("\n") |
| 17 | + |
| 18 | +# Remove v from tags |
| 19 | +for i in range(len(tags)): |
| 20 | + tags[i] = tags[i].replace('v','') |
| 21 | + |
| 22 | +# Remove empty strings from tags |
| 23 | +tags[:] = [x for x in tags if x] |
| 24 | + |
| 25 | +# Sort tags by semver number |
| 26 | +tags.sort(key = lambda x: [int(y) for y in x.split('.')]) |
| 27 | + |
| 28 | +# Add v back into tags |
| 29 | +for i in range(len(tags)): |
| 30 | + tags[i] = 'v'+tags[i] |
| 31 | + |
| 32 | +# Iterate through all the tags in the tag list and get a list of commits |
| 33 | +count = 0 |
| 34 | +changelogs = {} |
| 35 | +for tag in tags: |
| 36 | + if tag: |
| 37 | + if count == 0: |
| 38 | + # If this is the first tag, get a list of the commits up to when the tag was created. |
| 39 | + git_log_result = subprocess.getoutput( |
| 40 | + f'git log --pretty=oneline {tag} | grep -v Merge | cut -d " " -f 2-' |
| 41 | + ) |
| 42 | + else: |
| 43 | + # For subsequents tags, get a list of the commits since the previous tag. |
| 44 | + git_log_result = subprocess.getoutput( |
| 45 | + f'git log --pretty=oneline {tag}...{prev_tag} | grep -v Merge | cut -d " " -f 2-' |
| 46 | + ) |
| 47 | + |
| 48 | + # Convert the commit list to a set and then back to a list to remove any duplicate commits. |
| 49 | + git_logs = list(set(git_log_result.split('\n'))) |
| 50 | + |
| 51 | + # Sort git_logs so they are in the same order each time |
| 52 | + git_logs.sort() |
| 53 | + |
| 54 | + # Add links to pull requests |
| 55 | + for i in range(len(git_logs)): |
| 56 | + if re.search(r'\(\#\d*\)',git_logs[i]): |
| 57 | + test = re.search(r'\(\#\d*\)',git_logs[i]) |
| 58 | + num = test.group() |
| 59 | + num = num[2:] |
| 60 | + num = num[:-1] |
| 61 | + sub = ' https://github.com/itential/itential.platform/pull/' + num |
| 62 | + git_logs[i] = re.sub('\(\#\d*\)',sub,git_logs[i]) |
| 63 | + |
| 64 | + # Add the commits to the changelogs dictionary using the tag as the key and the |
| 65 | + # commit list as the value |
| 66 | + changelogs[tag] = git_logs |
| 67 | + |
| 68 | + # Save the tag as the previous tag and increment the counter |
| 69 | + prev_tag = tag |
| 70 | + count = count + 1 |
| 71 | + |
| 72 | +# Create the changelog markdown output |
| 73 | +print('# Changelog\n') |
| 74 | +for release,changes in reversed(changelogs.items()): |
| 75 | + # Get the tag date in the date format we want |
| 76 | + release_date = subprocess.getoutput( |
| 77 | + f'git log -1 --format=%ad --date=format:"%B %d, %Y" {release}' |
| 78 | + ) |
| 79 | + |
| 80 | + # Print the tag (release) and the date it was created |
| 81 | + print(f'## {release} ({release_date})\n') |
| 82 | + |
| 83 | + # Print an unordered list of the commits (changes) |
| 84 | + for change in changes: |
| 85 | + print(f'* {change}') |
| 86 | + print() |
| 87 | + |
| 88 | + for i in range(len(tags)): |
| 89 | + if i > 0: |
| 90 | + if release == tags[i]: |
| 91 | + full = 'https://github.com/itential/itential.platform/compare/' + tags[i-1] + '...' + release |
| 92 | + print('Full Changelog:', full, '\n\n') |
0 commit comments