-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path.gitchangelog.rc
169 lines (139 loc) · 5.08 KB
/
.gitchangelog.rc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8; mode: python -*-
import os
import re
from pathlib import Path
import chevron
import emoji
from gitchangelog.gitchangelog import GitRepos
repos = GitRepos('.')
major_emoji = {':boom:'}
minor_emoji = {':sparkles:'}
emj_re = re.compile(r'(:\w+:)')
def indent(text, chars=' ', first=None):
split_text = text.split('\n')
if first:
first_line = split_text[0]
rest = '\n'.join(split_text[1:])
return '\n'.join(((f'{first}{first_line}').rstrip(), indent(rest, chars=chars)))
return '\n'.join((f'{chars}{line}').rstrip() for line in split_text)
def mustache_chevron(template_name):
template = Path(template_name).read_text()
def stuffed_versions(versions, opts):
for version in versions:
title = f"{version['tag']} ({version['date']})" if version['tag'] else opts['unreleased_version_label']
version['label'] = title
version['label_chars'] = list(version['label'])
for section in version['sections']:
section['label_chars'] = list(section['label'])
section['display_label'] = not (section['label'] == 'Other' and len(version['sections']) == 1)
for commit in section['commits']:
commit['author_names_joined'] = ', '.join(commit['authors'])
commit['body_indented'] = indent(commit['body'])
yield version
def renderer(data, opts):
data['general_title'] = False
data['title_chars'] = []
if data['title']:
data['general_title'] = True
data['title_chars'] = list(data['title'])
data['versions'] = stuffed_versions(data['versions'], opts)
return chevron.render(template, data)
return renderer
ignore_regexps = [
r'(🔀|:twisted_rightwards_arrows:|[Mm]erge)',
r'(⏪|:rewind:|[Rr]evert)',
r'(🚨|:rotating_light:)',
r'(🚧|:construction:)',
r'(💚|:green_heart:)',
r'(👌|:ok_hand:)',
r'(📝|:memo:)\s*(Update CHANGELOG\.md)',
]
section_regexps = [
('💥 Breaking changes', [
r"""(?x)^(💥|:boom:)\s*([^\n]*)$""",
]),
('✨ New', [
r"""(?x)^(✨|:sparkles: )\s*([^\n]*)$""",
]),
('♻️ Changes', [
r"""(?x)^(
🗃|:card_file_box:
|✏️|:pencil[2]?:
|🔥|:fire:
|⚡️|:zap:
|🎨|:art:
|👽|:alien:
|🔧|:wrench:
|🏷️|:label:
|♻️|:recycle:
|🚸|:children_crossing:
)\s*([^\n]*)$""",
]),
('🐛 Bugs', [
r"""(?x)^(🐛|:bug:)\s*([^\n]*)$""",
]),
('⬆️ Dependencies', [
r"""(?x)^(
⬆️|:arrow_up:|
⬇️|:arrow_down:|
➕|:heavy_plus_sign:|
➖|:heavy_minus_sign:|
📌|:pushpin:
)\s*([^\n]*)$""",
]),
('📝 Docs', [r'^(📝|:memo:)\s*([^\n]*)$']),
('🌱 Other', None),
]
body_process = ReSub(r'((^|\n)[A-Z]\w+(-\w+)*: .*(\n\s+.*)*)+$', r'') | strip
subject_process = (strip
| TextProc(emoji.demojize)
| ReSub(r':arrow_up:', '⬆️')
| ReSub(r':arrow_down:', '⬇️')
| ReSub(r':heavy_plus_sign:', '➕')
| ReSub(r':heavy_minus_sign:', '➖')
| ReSub(r'^(:(\w+):)?\s*([^\n@]*)(@[a-z]+\s+)*$', r'\3')
| ucfirst | final_dot)
def unreleased_version_label(cache=[]):
if cache:
return cache[0]
tags = repos.tags()
major, minor, patch, *_ = tags[-1].identifier.lstrip('v').split('.') if tags else (0, 0, 0)
level_bump = {(0, 0, 1)} # patch
for commit in repos.log(excludes=tags):
primary_emoji = m.group(0) if (m := emj_re.search(commit.subject)) else None
if primary_emoji in major_emoji:
level_bump.add((1, -int(minor), -int(patch))) # major
elif primary_emoji in minor_emoji:
level_bump.add((0, 1, -int(patch))) # minor
major_bump, minor_bump, patch_bump = max(level_bump)
cache.append(f'{int(major) + major_bump}.{int(minor) + minor_bump}.{int(patch) + patch_bump}')
return cache[0]
output_engine = mustache_chevron('.github/markdown.tpl')
include_merge = True
tag_filter_regexp = r'^v?\d+\.\d+(\.\d+)?$'
OUTPUT_FILE = 'CHANGELOG.md'
INSERT_POINT_REGEX = r"""(?isx)
^
(
\s*%(release_title)s\s`%(rev)s`\s+%(date)s\s*(\n|\r\n|\r)
)
( ## Match all between Release notes and release rev
(
(?!
(?<=(\n|\r)) ## look back for newline
%(release_title)s\s`(%(rev)s)`\s+%(date)s(\n|\r\n|\r) ## release with date
)
.
)*
)
(?P<tail>%(release_title)s\s`(?P<rev>%(rev)s)`\s+%(date)s)
""" % {
'rev': tag_filter_regexp.lstrip('^').rstrip('$'),
'date': r'\([0-9]{4}-[0-9]{2}-[0-9]{2}\)',
'release_title': r'\#\#\#\#\sapiclient-pydantic-generator',
}
revs = [
Caret(FileFirstRegexMatch(OUTPUT_FILE, INSERT_POINT_REGEX)),
'HEAD',
]
publish = FileRegexSubst(OUTPUT_FILE, INSERT_POINT_REGEX, r'\3\o\g<tail>')