Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. # READ ME FIRST
  2. # if you are using jenkins to run this script, push commands will not work and you may want to use
  3. # git publish to push the changes and tags
  4. #
  5. # for the tag name to be dynamic inject the version.txt file as environment variables
  6. # use LAST_RELEASE_VERSION as your tag after the changelog.py script executes.
  7. # In jenkins skip the commits that start with "(?s).*\[JENKINS\].*" pattern in additional SCM behaviors.
  8. #
  9. # Please create a file named version.txt at the root and add following content to it (remove one hash # per line)
  10. # START COPYING NEXT 7 LINES
  11.  
  12. # # SPECIFY MAJOR AND MINOR VERSION FOR YOUR APPLICATION. DO NOT CHANGE THE KEYS.
  13. # MAJOR_VERSION=0
  14. # MINOR_VERSION=0
  15. #
  16. # # DO NOT EDIT BELOW THIS LINE. THIS IS AUTOMATICALLY POPULATED BY JENKINS DURING DEPLOYMENT TO PRODUCTION
  17. # PATCH_VERSION=0
  18. # LAST_RELEASE_VERSION=0.0.0
  19.  
  20. #!/usr/bin/python
  21.  
  22. import os
  23. import sys
  24. import subprocess
  25. from datetime import datetime
  26.  
  27. if len(sys.argv) > 4 or len(sys.argv) < 3 or (len(sys.argv) == 4 and (sys.argv[3] != "release" or "help" in sys.argv)):
  28. print("""usage:
  29.  
  30. 1. to build released changelog
  31. python changelog.py <path to version.txt file> <to_commit_id> release
  32.  
  33. 2. to build unreleased changelog
  34. python changelog.py <path to version.txt file> <to_commit_id>
  35. """)
  36. sys.exit(1)
  37.  
  38. is_release = True if len(sys.argv) == 4 and sys.argv[3] == "release" else False
  39.  
  40. version_file_path = sys.argv[1]
  41. to_commit_id = sys.argv[2]
  42. timestamp = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
  43.  
  44. next_version_holder = [0, 0, 0] # dummy data
  45. prev_version_holder = [0, 0, 0] # dummy data
  46.  
  47. # generate next version from data in version.txt
  48. try:
  49. with open(version_file_path, "r") as version_file:
  50. lines = version_file.read().strip().splitlines()
  51. for line in lines:
  52. if 'MAJOR_VERSION' in line:
  53. values = line.split("=")
  54. next_version_holder[0] = str(int(values[1].strip()))
  55. if 'MINOR_VERSION' in line:
  56. values = line.split("=")
  57. next_version_holder[1] = str(int(values[1].strip()))
  58. if 'PATCH_VERSION' in line:
  59. values = line.split("=")
  60. next_version_holder[2] = str(int(values[1].strip()) + 1)
  61. if 'LAST_RELEASE_VERSION' in line:
  62. values = line.split("=")[1].split('.')
  63. prev_version_holder = values
  64.  
  65. if prev_version_holder[0] != next_version_holder[0]:
  66. next_version_holder[1] = "0"
  67. if prev_version_holder[1] != next_version_holder[1]:
  68. next_version_holder[2] = "0"
  69.  
  70. next_version = '.'.join(next_version_holder)
  71. prev_version = '.'.join(prev_version_holder)
  72.  
  73. # save the file back with updated PATCH_VERSION and LAST_RELEASE_VERSION
  74. if is_release:
  75. with open('version.txt', "w") as version_file:
  76. version_file.write("""# SPECIFY MAJOR AND MINOR VERSION FOR YOUR APPLICATION. DO NOT CHANGE THE KEYS.
  77. MAJOR_VERSION=""" + next_version_holder[0] + """
  78. MINOR_VERSION=""" + next_version_holder[1] + """
  79.  
  80. # DO NOT EDIT BELOW THIS LINE. THIS IS AUTOMATICALLY POPULATED BY JENKINS DURING DEPLOYMENT TO PRODUCTION
  81. PATCH_VERSION=""" + next_version_holder[2] + """
  82. LAST_RELEASE_VERSION=""" + next_version + """
  83. """)
  84.  
  85. except Exception as e:
  86. print(e)
  87. print("""ERROR! version.txt file might be absent or incorrectly formatted.
  88. Please ensure a version.txt file at root of the code with content in following format:
  89.  
  90. # SPECIFY MAJOR AND MINOR VERSION FOR YOUR APPLICATION. DO NOT CHANGE THE KEYS.
  91. MAJOR_VERSION=0
  92. MINOR_VERSION=0
  93.  
  94. # DO NOT EDIT BELOW THIS LINE. THIS IS AUTOMATICALLY POPULATED BY JENKINS DURING DEPLOYMENT TO PRODUCTION
  95. PATCH_VERSION=0
  96. LAST_RELEASE_VERSION=0.0.0
  97. """)
  98. sys.exit(2)
  99.  
  100. # get the commit logs between two commits
  101. git_logs_command = "git log v" + prev_version + ".." + to_commit_id + " --pretty=format:\"%s\""
  102. print(git_logs_command)
  103. command_execution = subprocess.Popen(git_logs_command, shell=True, stdout=subprocess.PIPE)
  104. logs_stdout = command_execution.stdout.read().decode('utf8').strip()
  105. logs = logs_stdout.split("\n")
  106.  
  107. changelog = {}
  108. start_commit_message_tags = ['release', 'merge', 'added', 'changed', 'deprecated', 'removed', 'fixed', 'security']
  109.  
  110. # categorize the logs into above tags
  111. for log in logs:
  112. if not log.strip():
  113. continue
  114.  
  115. log_categorized = False
  116. for word in start_commit_message_tags:
  117. if word in log.lower():
  118. if word not in changelog.keys():
  119. changelog[word] = []
  120. changelog[word].append(log)
  121. log_categorized = True
  122.  
  123. if not log_categorized:
  124. if 'others' not in changelog.keys():
  125. changelog['others'] = []
  126. changelog['others'].append(log)
  127.  
  128. # set the file name and path for changelog
  129. changelog_folder_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'changelog')
  130. if is_release:
  131. changelog_file_path = os.path.join(changelog_folder_path, 'released_v' + next_version + "_" + timestamp + '.txt')
  132. open(os.path.join(changelog_folder_path, 'unreleased.txt'), 'w').write(
  133. "NO UNRELEASED CHANGES. LAST RELEASED VERSION: v" + next_version
  134. )
  135. else:
  136. changelog_file_path = os.path.join(changelog_folder_path, 'unreleased.txt')
  137.  
  138. # dump the changelog to the file
  139. if len(changelog.keys()):
  140. if not os.path.exists(changelog_folder_path):
  141. os.makedirs(changelog_folder_path)
  142.  
  143. with open(changelog_file_path, "w+") as changelog_file:
  144. for word in start_commit_message_tags + ['others']:
  145. if word in changelog.keys():
  146. log_title = "[" + word.capitalize() + "]"
  147. print(log_title)
  148. changelog_file.write(log_title + "\n")
  149. for log in changelog[word]:
  150. print(log)
  151. changelog_file.write(log + "\n")
  152. print("\n")
  153. changelog_file.write("\n")
  154.  
  155. # commit and push the changelog
  156. commands = [
  157. "git add version.txt",
  158. "git add changelog/.",
  159. "git commit -m \"[JENKINS] " + ("adding new" if is_release else "updating unreleased") + " changelog at " + timestamp + "\"",
  160. "git push"
  161. ]
  162. print("Adding changelog to repository")
  163. for cmd in commands:
  164. print(cmd)
  165. subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
  166.  
  167. # update the tags on git
  168. # updates the latest tag and creates a new one
  169. if is_release:
  170. commands = [
  171. # "git tag -d latest",
  172. # "git push --delete origin latest",
  173. "git tag -a v" + next_version + " " + to_commit_id + " -m \"sent to production at " + timestamp + "\"",
  174. # "git tag -a latest " + to_commit_id + " -m \"sent to production at " + timestamp + "\"",
  175. "git push origin --tags"
  176. ]
  177.  
  178. print("Updating tags in github")
  179. for cmd in commands:
  180. print(cmd)
  181. subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement