make_release_notes.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. NO_HEADING = 'PRODUCT HAS NO HEADING'
  21. PRODUCTS = {
  22. 'Firebase/Auth/CHANGELOG.md': '{{auth}}',
  23. 'Firebase/Core/CHANGELOG.md': NO_HEADING,
  24. 'Firebase/Database/CHANGELOG.md': '{{database}}',
  25. 'Firebase/DynamicLinks/CHANGELOG.md': '{{ddls}}',
  26. 'Firebase/InAppMessaging/CHANGELOG.md': '{{inapp_messaging}}',
  27. 'Firebase/InstanceID/CHANGELOG.md': 'InstanceID',
  28. 'Firebase/Messaging/CHANGELOG.md': '{{messaging}}',
  29. 'Firebase/Storage/CHANGELOG.md': '{{storage}}',
  30. 'Firestore/CHANGELOG.md': '{{firestore}}',
  31. 'Functions/CHANGELOG.md': '{{cloud_functions}}',
  32. # 'Firebase/InAppMessagingDisplay/CHANGELOG.md': '?',
  33. # 'GoogleDataTransport/CHANGELOG.md': '?',
  34. # 'GoogleDataTransportCCTSupport/CHANGELOG.md': '?',
  35. }
  36. def main():
  37. local_repo = find_local_repo()
  38. parser = argparse.ArgumentParser(description='Create release notes.')
  39. parser.add_argument('--repo', '-r', default=local_repo,
  40. help='Specify which GitHub repo is local.')
  41. parser.add_argument('--only', metavar='VERSION',
  42. help='Convert only a specific version')
  43. parser.add_argument('--all', action='store_true',
  44. help='Emits entries for all versions')
  45. parser.add_argument('changelog',
  46. help='The CHANGELOG.md file to parse')
  47. args = parser.parse_args()
  48. if args.all:
  49. text = read_file(args.changelog)
  50. else:
  51. text = read_changelog_section(args.changelog, args.only)
  52. product = None
  53. if not args.all:
  54. product = PRODUCTS.get(args.changelog)
  55. renderer = Renderer(args.repo, product)
  56. translator = Translator(renderer)
  57. result = translator.translate(text)
  58. print(result)
  59. def find_local_repo():
  60. url = subprocess.check_output(['git', 'config', '--get', 'remote.origin.url'])
  61. # ssh or https style URL
  62. m = re.match(r'^(?:git@github\.com:|https://github\.com/)(.*)\.git$', url)
  63. if m:
  64. return m.group(1)
  65. raise LookupError('Can\'t figure local repo from remote URL %s' % url)
  66. CHANGE_TYPE_MAPPING = {
  67. 'added': 'feature'
  68. }
  69. class Renderer(object):
  70. def __init__(self, local_repo, product):
  71. self.local_repo = local_repo
  72. self.product = product
  73. def heading(self, heading):
  74. if self.product:
  75. if self.product == NO_HEADING:
  76. return ''
  77. else:
  78. return '### %s\n' % self.product
  79. return heading
  80. def bullet(self, spacing):
  81. """Renders a bullet in a list.
  82. All bulleted lists in devsite are '*' style.
  83. """
  84. return '%s* ' % spacing
  85. def change_type(self, tag):
  86. """Renders a change type tag as the appropriate double-braced macro.
  87. That is "[fixed]" is rendered as "{{fixed}}".
  88. """
  89. tag = CHANGE_TYPE_MAPPING.get(tag, tag)
  90. return '{{%s}}' % tag
  91. def url(self, url):
  92. m = re.match(r'^(?:https:)?(//github.com/(.*)/issues/(\d+))$', url)
  93. if m:
  94. link = m.group(1)
  95. repo = m.group(2)
  96. issue = m.group(3)
  97. if repo == self.local_repo:
  98. text = '#' + issue
  99. else:
  100. text = repo + '#' + issue
  101. return '[%s](%s)' % (text, link)
  102. return url
  103. def local_issue_link(self, issue):
  104. """Renders a local issue link as a proper markdown URL.
  105. Transforms (#1234) into
  106. ([#1234](//github.com/firebase/firebase-ios-sdk/issues/1234)).
  107. """
  108. link = '//github.com/%s/issues/%s' % (self.local_repo, issue)
  109. return '([#%s](%s))' % (issue, link)
  110. def text(self, text):
  111. """Passes through any other text."""
  112. return text
  113. class Translator(object):
  114. def __init__(self, renderer):
  115. self.renderer = renderer
  116. def translate(self, text):
  117. result = ''
  118. while text:
  119. for key in self.rules:
  120. rule = getattr(self, key)
  121. m = rule.match(text)
  122. if not m:
  123. continue
  124. callback = getattr(self, 'parse_' + key)
  125. callback_result = callback(m)
  126. result += callback_result
  127. text = text[len(m.group(0)):]
  128. break
  129. return result
  130. heading = re.compile(
  131. r'^#.*'
  132. )
  133. def parse_heading(self, m):
  134. return self.renderer.heading(m.group(0))
  135. bullet = re.compile(
  136. r'^(\s*)[*+-] '
  137. )
  138. def parse_bullet(self, m):
  139. return self.renderer.bullet(m.group(1))
  140. change_type = re.compile(
  141. r'\[' # opening square bracket
  142. r'(\w+)' # tag word (like "feature" or "changed"
  143. r'\]' # closing square bracket
  144. r'(?!\()' # not followed by opening paren (that would be a link)
  145. )
  146. def parse_change_type(self, m):
  147. return self.renderer.change_type(m.group(1))
  148. url = re.compile(r'^(https?://[^\s<]+[^<.,:;"\')\]\s])')
  149. def parse_url(self, m):
  150. return self.renderer.url(m.group(1))
  151. local_issue_link = re.compile(
  152. r'\(' # opening paren
  153. r'#(\d+)' # hash and issue number
  154. r'\)' # closing paren
  155. )
  156. def parse_local_issue_link(self, m):
  157. return self.renderer.local_issue_link(m.group(1))
  158. text = re.compile(
  159. r'^[\s\S]+?(?=[(\[\n]|https?://|$)'
  160. )
  161. def parse_text(self, m):
  162. return self.renderer.text(m.group(0))
  163. rules = [
  164. 'heading', 'bullet', 'change_type', 'url', 'local_issue_link', 'text'
  165. ]
  166. def read_file(filename):
  167. """Reads the contents of the file as a single string."""
  168. with open(filename, 'r') as fd:
  169. return fd.read()
  170. def read_changelog_section(filename, single_version=None):
  171. """Reads a single section of the changelog from the given filename.
  172. If single_version is None, reads the first section with a number in its
  173. heading. Otherwise, reads the first section with single_version in its
  174. heading.
  175. Args:
  176. - single_version: specifies a string to look for in headings.
  177. Returns:
  178. A string containing the heading and contents of the heading.
  179. """
  180. with open(filename, 'r') as fd:
  181. # Discard all lines until we see a heading that either has the version the
  182. # user asked for or any version.
  183. if single_version:
  184. initial_heading = re.compile(r'^#.*%s' % re.escape(single_version))
  185. else:
  186. initial_heading = re.compile(r'^#([^\d]*)\d')
  187. heading = re.compile(r'^#')
  188. initial = True
  189. result = []
  190. for line in fd:
  191. if initial:
  192. if initial_heading.match(line):
  193. initial = False
  194. result.append(line)
  195. else:
  196. if heading.match(line):
  197. break
  198. result.append(line)
  199. # Prune extra newlines
  200. while result and result[-1] == '\n':
  201. result.pop()
  202. return ''.join(result)
  203. if __name__ == '__main__':
  204. main()