summaryrefslogtreecommitdiff
path: root/scripts/git-release-notes.py
blob: 6655b61ce8cf1b685e6bf76ed89835f51d2ba270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python

import datetime
import re
import subprocess

RELEASE = re.compile(r'[0-9]+\.[0-9]+\.[0-9]+')
ISSUE = re.compile(r'#([0-9]+)')
REVLIST = 'git rev-list develop --abbrev-commit --format="parents %p%n%B%n~~~" --max-count=200 develop'
TEMPLATE = """
boto v{version}
===========

:date: {date}

Description goes here.


Changes
-------
{changes}
"""

revisions = subprocess.check_output(REVLIST, shell=True, stderr=subprocess.STDOUT)

commit_list = []
for hunk in revisions.split('~~~')[:-1]:
    lines = hunk.strip().splitlines()
    commit = lines[0].split(' ', 1)[1]
    parents = lines[1].split(' ', 1)[1].split(' ')
    message = ' '.join(lines[2:])

    #print(commit, parents)

    if RELEASE.search(message):
        print('Found release commit, stopping:')
        print(message)
        break

    if len(parents) > 1:
        commit_list.append([commit, message])

removals = [
    re.compile(r'merge pull request #[0-9]+ from [a-z0-9/_-]+', re.I),
    re.compile(r"merge branch '[a-z0-9/_-]+' into [a-z0-9/_-]+", re.I),
    re.compile(r'fix(es)? [#0-9, ]+.?', re.I)
]

changes = ''
for commit, message in commit_list:
    append = []
    issues = set()
    for issue in ISSUE.findall(message):
        if issue not in issues:
            append.append(':issue:`{issue}`'.format(issue=issue))
            issues.add(issue)
    append.append(':sha:`{commit}`'.format(commit=commit))
    append = ' (' + ', '.join(append) + ')'

    original = message
    for removal in removals:
        message = removal.sub('', message)

    message = message.strip()

    if not message:
        message = original.strip()

    changes += '* ' + message + append + '\n'

print(TEMPLATE.format(
    version='?.?.?',
    date=datetime.datetime.now().strftime('%Y/%m/%d'),
    changes=changes
))