make_release_notes.py 7.4 KB

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