make_release_notes.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/usr/bin/env python
  2. # Copyright 2019 Google
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Converts github flavored markdown changelogs to release notes.
  16. """
  17. import argparse
  18. import re
  19. import subprocess
  20. import string
  21. NO_HEADING = 'PRODUCT HAS NO HEADING'
  22. PRODUCTS = {
  23. 'FirebaseABTesting/CHANGELOG.md': '{{ab_testing}}',
  24. 'Firebase/Auth/CHANGELOG.md': '{{auth}}',
  25. 'FirebaseCore/CHANGELOG.md': NO_HEADING,
  26. 'Crashlytics/CHANGELOG.md': '{{crashlytics}}',
  27. 'Firebase/Database/CHANGELOG.md': '{{database}}',
  28. 'FirebaseDynamicLinks/CHANGELOG.md': '{{ddls}}',
  29. 'FirebaseInAppMessaging/CHANGELOG.md': '{{inapp_messaging}}',
  30. 'FirebaseInstallations/CHANGELOG.md': 'Installations',
  31. 'Firebase/InstanceID/CHANGELOG.md': 'InstanceID',
  32. 'Firebase/Messaging/CHANGELOG.md': '{{messaging}}',
  33. 'FirebaseStorage/CHANGELOG.md': '{{storage}}',
  34. 'Firestore/CHANGELOG.md': '{{firestore}}',
  35. 'Functions/CHANGELOG.md': '{{cloud_functions}}',
  36. 'FirebaseRemoteConfig/CHANGELOG.md': '{{remote_config}}',
  37. # 'GoogleDataTransport/CHANGELOG.md': '?',
  38. # 'GoogleDataTransportCCTSupport/CHANGELOG.md': '?',
  39. }
  40. def main():
  41. local_repo = find_local_repo()
  42. parser = argparse.ArgumentParser(description='Create release notes.')
  43. parser.add_argument('--repo', '-r', default=local_repo,
  44. help='Specify which GitHub repo is local.')
  45. parser.add_argument('--only', metavar='VERSION',
  46. help='Convert only a specific version')
  47. parser.add_argument('--all', action='store_true',
  48. help='Emits entries for all versions')
  49. parser.add_argument('changelog',
  50. help='The CHANGELOG.md file to parse')
  51. args = parser.parse_args()
  52. if args.all:
  53. text = read_file(args.changelog)
  54. else:
  55. text = read_changelog_section(args.changelog, args.only)
  56. product = None
  57. if not args.all:
  58. product = PRODUCTS.get(args.changelog)
  59. renderer = Renderer(args.repo, product)
  60. translator = Translator(renderer)
  61. result = translator.translate(text)
  62. print(result)
  63. def find_local_repo():
  64. url = subprocess.check_output(['git', 'config', '--get', 'remote.origin.url'])
  65. # ssh or https style URL
  66. m = re.match(r'^(?:git@github\.com:|https://github\.com/)(.*)\.git$', url)
  67. if m:
  68. return m.group(1)
  69. raise LookupError('Can\'t figure local repo from remote URL %s' % url)
  70. CHANGE_TYPE_MAPPING = {
  71. 'added': 'feature'
  72. }
  73. class Renderer(object):
  74. def __init__(self, local_repo, product):
  75. self.local_repo = local_repo
  76. self.product = product
  77. def heading(self, heading):
  78. if self.product:
  79. if self.product == NO_HEADING:
  80. return ''
  81. else:
  82. return '### %s\n' % self.product
  83. return heading
  84. def bullet(self, spacing):
  85. """Renders a bullet in a list.
  86. All bulleted lists in devsite are '*' style.
  87. """
  88. return '%s* ' % spacing
  89. def change_type(self, tag):
  90. """Renders a change type tag as the appropriate double-braced macro.
  91. That is "[fixed]" is rendered as "{{fixed}}".
  92. """
  93. tag = CHANGE_TYPE_MAPPING.get(tag, tag)
  94. return '{{%s}}' % tag
  95. def url(self, url):
  96. m = re.match(r'^(?:https:)?(//github.com/(.*)/issues/(\d+))$', url)
  97. if m:
  98. link = m.group(1)
  99. repo = m.group(2)
  100. issue = m.group(3)
  101. if repo == self.local_repo:
  102. text = '#' + issue
  103. else:
  104. text = repo + '#' + issue
  105. return '[%s](%s)' % (text, link)
  106. return url
  107. def local_issue_link(self, issues):
  108. """Renders a local issue link as a proper markdown URL.
  109. Transforms (#1234, #1235) into
  110. ([#1234](//github.com/firebase/firebase-ios-sdk/issues/1234),
  111. [#1235](//github.com/firebase/firebase-ios-sdk/issues/1235)).
  112. """
  113. issue_link_list = []
  114. issue_list = issues.split(", ")
  115. for issue in issue_list:
  116. issue = issue.translate(None, string.punctuation)
  117. link = '//github.com/%s/issues/%s' % (self.local_repo, issue)
  118. issue_link_list.append('[#%s](%s)' % (issue, link))
  119. return "(" + ", ".join(issue_link_list) + ")"
  120. def text(self, text):
  121. """Passes through any other text."""
  122. return text
  123. class Translator(object):
  124. def __init__(self, renderer):
  125. self.renderer = renderer
  126. def translate(self, text):
  127. result = ''
  128. while text:
  129. for key in self.rules:
  130. rule = getattr(self, key)
  131. m = rule.match(text)
  132. if not m:
  133. continue
  134. callback = getattr(self, 'parse_' + key)
  135. callback_result = callback(m)
  136. result += callback_result
  137. text = text[len(m.group(0)):]
  138. break
  139. return result
  140. heading = re.compile(
  141. r'^#{1,6} .*'
  142. )
  143. def parse_heading(self, m):
  144. return self.renderer.heading(m.group(0))
  145. bullet = re.compile(
  146. r'^(\s*)[*+-] '
  147. )
  148. def parse_bullet(self, m):
  149. return self.renderer.bullet(m.group(1))
  150. change_type = re.compile(
  151. r'\[' # opening square bracket
  152. r'(\w+)' # tag word (like "feature" or "changed")
  153. r'\]' # closing square bracket
  154. r'(?!\()' # not followed by opening paren (that would be a link)
  155. )
  156. def parse_change_type(self, m):
  157. return self.renderer.change_type(m.group(1))
  158. url = re.compile(r'^(https?://[^\s<]+[^<.,:;"\')\]\s])')
  159. def parse_url(self, m):
  160. return self.renderer.url(m.group(1))
  161. local_issue_link = re.compile(
  162. r'\(' # opening paren
  163. r'(#(\d+)(, )?)+' # list of hash and issue number, comma-delimited
  164. r'\)' # closing paren
  165. )
  166. def parse_local_issue_link(self, m):
  167. return self.renderer.local_issue_link(m.group(0))
  168. text = re.compile(
  169. r'^[\s\S]+?(?=[(\[\n]|https?://|$)'
  170. )
  171. def parse_text(self, m):
  172. return self.renderer.text(m.group(0))
  173. rules = [
  174. 'heading', 'bullet', 'change_type', 'url', 'local_issue_link', 'text'
  175. ]
  176. def read_file(filename):
  177. """Reads the contents of the file as a single string."""
  178. with open(filename, 'r') as fd:
  179. return fd.read()
  180. def read_changelog_section(filename, single_version=None):
  181. """Reads a single section of the changelog from the given filename.
  182. If single_version is None, reads the first section with a number in its
  183. heading. Otherwise, reads the first section with single_version in its
  184. heading.
  185. Args:
  186. - single_version: specifies a string to look for in headings.
  187. Returns:
  188. A string containing the heading and contents of the heading.
  189. """
  190. with open(filename, 'r') as fd:
  191. # Discard all lines until we see a heading that either has the version the
  192. # user asked for or any version.
  193. if single_version:
  194. initial_heading = re.compile(r'^#{1,6} .*%s' % re.escape(single_version))
  195. else:
  196. initial_heading = re.compile(r'^#{1,6} ([^\d]*)\d')
  197. heading = re.compile(r'^#{1,6} ')
  198. initial = True
  199. result = []
  200. for line in fd:
  201. if initial:
  202. if initial_heading.match(line):
  203. initial = False
  204. result.append(line)
  205. else:
  206. if heading.match(line):
  207. break
  208. result.append(line)
  209. # Prune extra newlines
  210. while result and result[-1] == '\n':
  211. result.pop()
  212. return ''.join(result)
  213. if __name__ == '__main__':
  214. main()