Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # READ ME FIRST
- # if you are using jenkins to run this script, push commands will not work and you may want to use
- # git publish to push the changes and tags
- #
- # for the tag name to be dynamic inject the version.txt file as environment variables
- # use LAST_RELEASE_VERSION as your tag after the changelog.py script executes.
- # In jenkins skip the commits that start with "(?s).*\[JENKINS\].*" pattern in additional SCM behaviors.
- #
- # Please create a file named version.txt at the root and add following content to it (remove one hash # per line)
- # START COPYING NEXT 7 LINES
- # # SPECIFY MAJOR AND MINOR VERSION FOR YOUR APPLICATION. DO NOT CHANGE THE KEYS.
- # MAJOR_VERSION=0
- # MINOR_VERSION=0
- #
- # # DO NOT EDIT BELOW THIS LINE. THIS IS AUTOMATICALLY POPULATED BY JENKINS DURING DEPLOYMENT TO PRODUCTION
- # PATCH_VERSION=0
- # LAST_RELEASE_VERSION=0.0.0
- #!/usr/bin/python
- import os
- import sys
- import subprocess
- from datetime import datetime
- 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)):
- print("""usage:
- 1. to build released changelog
- python changelog.py <path to version.txt file> <to_commit_id> release
- 2. to build unreleased changelog
- python changelog.py <path to version.txt file> <to_commit_id>
- """)
- sys.exit(1)
- is_release = True if len(sys.argv) == 4 and sys.argv[3] == "release" else False
- version_file_path = sys.argv[1]
- to_commit_id = sys.argv[2]
- timestamp = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
- next_version_holder = [0, 0, 0] # dummy data
- prev_version_holder = [0, 0, 0] # dummy data
- # generate next version from data in version.txt
- try:
- with open(version_file_path, "r") as version_file:
- lines = version_file.read().strip().splitlines()
- for line in lines:
- if 'MAJOR_VERSION' in line:
- values = line.split("=")
- next_version_holder[0] = str(int(values[1].strip()))
- if 'MINOR_VERSION' in line:
- values = line.split("=")
- next_version_holder[1] = str(int(values[1].strip()))
- if 'PATCH_VERSION' in line:
- values = line.split("=")
- next_version_holder[2] = str(int(values[1].strip()) + 1)
- if 'LAST_RELEASE_VERSION' in line:
- values = line.split("=")[1].split('.')
- prev_version_holder = values
- if prev_version_holder[0] != next_version_holder[0]:
- next_version_holder[1] = "0"
- if prev_version_holder[1] != next_version_holder[1]:
- next_version_holder[2] = "0"
- next_version = '.'.join(next_version_holder)
- prev_version = '.'.join(prev_version_holder)
- # save the file back with updated PATCH_VERSION and LAST_RELEASE_VERSION
- if is_release:
- with open('version.txt', "w") as version_file:
- version_file.write("""# SPECIFY MAJOR AND MINOR VERSION FOR YOUR APPLICATION. DO NOT CHANGE THE KEYS.
- MAJOR_VERSION=""" + next_version_holder[0] + """
- MINOR_VERSION=""" + next_version_holder[1] + """
- # DO NOT EDIT BELOW THIS LINE. THIS IS AUTOMATICALLY POPULATED BY JENKINS DURING DEPLOYMENT TO PRODUCTION
- PATCH_VERSION=""" + next_version_holder[2] + """
- LAST_RELEASE_VERSION=""" + next_version + """
- """)
- except Exception as e:
- print(e)
- print("""ERROR! version.txt file might be absent or incorrectly formatted.
- Please ensure a version.txt file at root of the code with content in following format:
- # SPECIFY MAJOR AND MINOR VERSION FOR YOUR APPLICATION. DO NOT CHANGE THE KEYS.
- MAJOR_VERSION=0
- MINOR_VERSION=0
- # DO NOT EDIT BELOW THIS LINE. THIS IS AUTOMATICALLY POPULATED BY JENKINS DURING DEPLOYMENT TO PRODUCTION
- PATCH_VERSION=0
- LAST_RELEASE_VERSION=0.0.0
- """)
- sys.exit(2)
- # get the commit logs between two commits
- git_logs_command = "git log v" + prev_version + ".." + to_commit_id + " --pretty=format:\"%s\""
- print(git_logs_command)
- command_execution = subprocess.Popen(git_logs_command, shell=True, stdout=subprocess.PIPE)
- logs_stdout = command_execution.stdout.read().decode('utf8').strip()
- logs = logs_stdout.split("\n")
- changelog = {}
- start_commit_message_tags = ['release', 'merge', 'added', 'changed', 'deprecated', 'removed', 'fixed', 'security']
- # categorize the logs into above tags
- for log in logs:
- if not log.strip():
- continue
- log_categorized = False
- for word in start_commit_message_tags:
- if word in log.lower():
- if word not in changelog.keys():
- changelog[word] = []
- changelog[word].append(log)
- log_categorized = True
- if not log_categorized:
- if 'others' not in changelog.keys():
- changelog['others'] = []
- changelog['others'].append(log)
- # set the file name and path for changelog
- changelog_folder_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'changelog')
- if is_release:
- changelog_file_path = os.path.join(changelog_folder_path, 'released_v' + next_version + "_" + timestamp + '.txt')
- open(os.path.join(changelog_folder_path, 'unreleased.txt'), 'w').write(
- "NO UNRELEASED CHANGES. LAST RELEASED VERSION: v" + next_version
- )
- else:
- changelog_file_path = os.path.join(changelog_folder_path, 'unreleased.txt')
- # dump the changelog to the file
- if len(changelog.keys()):
- if not os.path.exists(changelog_folder_path):
- os.makedirs(changelog_folder_path)
- with open(changelog_file_path, "w+") as changelog_file:
- for word in start_commit_message_tags + ['others']:
- if word in changelog.keys():
- log_title = "[" + word.capitalize() + "]"
- print(log_title)
- changelog_file.write(log_title + "\n")
- for log in changelog[word]:
- print(log)
- changelog_file.write(log + "\n")
- print("\n")
- changelog_file.write("\n")
- # commit and push the changelog
- commands = [
- "git add version.txt",
- "git add changelog/.",
- "git commit -m \"[JENKINS] " + ("adding new" if is_release else "updating unreleased") + " changelog at " + timestamp + "\"",
- "git push"
- ]
- print("Adding changelog to repository")
- for cmd in commands:
- print(cmd)
- subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
- # update the tags on git
- # updates the latest tag and creates a new one
- if is_release:
- commands = [
- # "git tag -d latest",
- # "git push --delete origin latest",
- "git tag -a v" + next_version + " " + to_commit_id + " -m \"sent to production at " + timestamp + "\"",
- # "git tag -a latest " + to_commit_id + " -m \"sent to production at " + timestamp + "\"",
- "git push origin --tags"
- ]
- print("Updating tags in github")
- for cmd in commands:
- print(cmd)
- subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement