11#!/usr/bin/env python3
22
33import argparse
4- import re
5- import subprocess
64import sys
75
86import utils
7+ import config
98
109#
1110# Stop immediately if version too old.
1615 f"You need Python >= { _MINIMUM_PYTHON_VERSION } , but you are running { sys .version } "
1716 )
1817
19- GITHUB_COMPARE_URL = (
20- "https://github.com/AxisCommunications/practical-react-components/compare"
21- )
22- GITHUB_COMMIT_URL = (
23- "https://github.com/AxisCommunications/practical-react-components/commit"
24- )
25-
2618GROUP_TITLES = {
2719 "build" : "👷 Build" ,
2820 "chore" : "🚧 Maintenance" ,
29- "ci" : "🚦 Continous integration" ,
21+ "ci" : "🚦 Continuous integration" ,
3022 "docs" : "📝 Documentation" ,
3123 "feat" : "✨ Features" ,
3224 "fix" : "🐛 Bug fixes" ,
4133def changelog_part (commitish_to : str , commitish_from : str , version : str ):
4234 date = utils .cmd (["git" , "log" , "-1" , "--format=%ci" , commitish_to ])
4335
44- commit_range = (
45- f"{ commitish_from } ..HEAD"
46- if commitish_to == "HEAD"
47- else f"{ commitish_from } ..{ commitish_to } ~"
48- )
36+ if commitish_from is None :
37+ commit_range = commitish_to
38+ elif commitish_to == "HEAD" :
39+ commit_range = f"{ commitish_from } ..HEAD"
40+ else :
41+ commit_range = f"{ commitish_from } ..{ commitish_to } ~"
4942
5043 commits = utils .cmd (
51- ["git" , "log" , "--no-merges" , "--date-order" , "--format=%H%x09%s" , commit_range ]
44+ [
45+ "git" ,
46+ "log" ,
47+ "--no-merges" ,
48+ "--date-order" ,
49+ "--format=%H%n%h%n%B%x1f" ,
50+ commit_range ,
51+ ]
5252 )
5353
5454 if commits == "" :
5555 return ""
5656
5757 messages = {}
5858
59- for commit in commits .split ("\n " ):
60- sha , msg = commit .split (maxsplit = 1 )
61- shortsha = utils .cmd (["git" , "log" , "-1" , "--format=%h" , sha ])
59+ for commit in commits .split ("\x1f " ):
60+ sha , shortsha , msg = commit .strip ().split ("\n " , maxsplit = 2 )
6261
6362 try :
6463 data = utils .conventional_commit_parse (msg )
64+ issues = utils .closing_issues_commit_parse (msg )
6565 messages .setdefault (data ["type" ], []).append (
66- {** data , "sha" : sha , "shortsha" : shortsha }
66+ {** data , "issues" : issues , " sha" : sha , "shortsha" : shortsha }
6767 )
6868 except :
6969 # No conventional commit
7070 pass
7171
7272 content = [
73- f"## [{ version } ]({ GITHUB_COMPARE_URL } /{ commitish_from } ...{ version } ) ({ date } )"
73+ f"## [{ version } ]({ config .GITHUB_RELEASE_URL } /{ version } )" ,
74+ f"{ date } , [Compare changes]({ config .GITHUB_COMPARE_URL } /{ commitish_from } ...{ version } )" ,
7475 ]
7576
7677 for group in GROUP_TITLES .keys ():
@@ -81,10 +82,24 @@ def changelog_part(commitish_to: str, commitish_from: str, version: str):
8182
8283 for data in messages [group ]:
8384
84- prefix = (
85- f' - **{ data ["scope" ]} **: ' if data ["scope" ] is not None else " - "
85+ prefix = " - "
86+
87+ pull_request = utils .get_github_pull_request (data ["sha" ])
88+ # pull_request[0] is id, pull_request[1] is url
89+ if pull_request is not None :
90+ prefix += f"[!{ pull_request [0 ]} ]({ pull_request [1 ]} ) - "
91+
92+ if data ["scope" ] is not None :
93+ prefix += f'**{ data ["scope" ]} **: '
94+
95+ postfix = (
96+ f' ([`{ data ["shortsha" ]} `]({ config .GITHUB_COMMIT_URL } /{ data ["sha" ]} ))'
8697 )
87- postfix = f' ([{ data ["shortsha" ]} ]({ GITHUB_COMMIT_URL } /{ data ["sha" ]} ))'
98+
99+ commit_author = utils .get_github_author (data ["sha" ])
100+ # commit_author[0] is username, commit_author[1] is url
101+ if commit_author is not None :
102+ postfix += f" ([**@{ commit_author [0 ]} **]({ commit_author [1 ]} ))"
88103
89104 if data ["breaking" ]:
90105 content .append (f'{ prefix } **BREAKING** { data ["description" ]} { postfix } ' )
@@ -96,7 +111,6 @@ def changelog_part(commitish_to: str, commitish_from: str, version: str):
96111
97112HEADER = """
98113# Changelog
99-
100114All notable changes to this project will be documented in this file.
101115
102116"""
@@ -114,6 +128,14 @@ def changelog_part(commitish_to: str, commitish_from: str, version: str):
114128 help = "Don't include a changelog header" ,
115129 )
116130
131+ parser .add_argument (
132+ "-r" ,
133+ "--release" ,
134+ type = str ,
135+ metavar = "RELEASE" ,
136+ help = "New release tag (e.g. vX.Y.Z), includes full changelog with a new entry for things not tagged" ,
137+ )
138+
117139 subparsers = parser .add_subparsers (
118140 dest = "type" , metavar = "COMMAND" , help = 'One of "single" or "full".' , required = True
119141 )
@@ -126,13 +148,6 @@ def changelog_part(commitish_to: str, commitish_from: str, version: str):
126148 full = subparsers .add_parser (
127149 "full" , description = "Generate a changelog covering entire history."
128150 )
129- full .add_argument (
130- "-r" ,
131- "--release" ,
132- type = str ,
133- metavar = "RELEASE" ,
134- help = "New release tag (e.g. vX.Y.Z), includes full changelog with a new entry for things not tagged" ,
135- )
136151
137152 args = parser .parse_args ()
138153
@@ -149,6 +164,10 @@ def changelog_part(commitish_to: str, commitish_from: str, version: str):
149164 ]
150165 ).split ()
151166
167+ if args .release is not None :
168+ tags .insert (0 , "HEAD" )
169+ tags .append (None )
170+
152171 if args .type == "single" :
153172 if args .tag is None :
154173 try :
@@ -160,23 +179,19 @@ def changelog_part(commitish_to: str, commitish_from: str, version: str):
160179 print (f"Error: tag { args .tag } not found!" )
161180 sys .exit (1 )
162181
163- if args .type == "full" and args .release is not None :
164- tags .insert (0 , "HEAD" )
165-
166- content = [HEADER ] if not args .skip_header else []
182+ if not args .skip_header :
183+ sys .stdout .write (HEADER )
167184
168185 for commitish_to , commitish_from in zip (tags [:- 1 ], tags [1 :]):
169186 if args .type == "single" and args .tag != commitish_to :
170187 continue
171-
172- content .append (
188+ sys .stdout .write (
173189 changelog_part (
174190 commitish_to ,
175191 commitish_from ,
176192 args .release if commitish_to == "HEAD" else commitish_to ,
177193 )
178194 )
179- content . append ( " " )
195+ sys . stdout . write ( " \n \n " )
180196
181- sys .stdout .write ("\n " .join (content ))
182197 sys .stdout .close ()
0 commit comments