2017-02-27 20:58:44 +01:00
|
|
|
import re
|
2017-02-23 23:11:55 +01:00
|
|
|
import subprocess
|
2018-05-23 21:41:35 +02:00
|
|
|
try:
|
|
|
|
from configparser import ConfigParser
|
|
|
|
except ImportError:
|
|
|
|
# Python < 3
|
|
|
|
from ConfigParser import ConfigParser
|
2017-02-23 23:11:55 +01:00
|
|
|
|
2018-05-24 22:36:45 +02:00
|
|
|
version = "1.0.5"
|
2017-02-23 23:11:55 +01:00
|
|
|
|
2017-02-27 20:58:44 +01:00
|
|
|
class SemVer(object):
|
2017-02-23 23:11:55 +01:00
|
|
|
|
2018-05-24 22:36:45 +02:00
|
|
|
GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?(.+)'? (into|from) ([\w/-]+)")
|
|
|
|
#Merge pull request #1 from RightBrain-Networks/feature/PLAT-185-versioning
|
2017-02-23 23:11:55 +01:00
|
|
|
|
2017-02-27 20:58:44 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.merged_branch = None
|
|
|
|
self.main_branch = None
|
|
|
|
self.version_type = None
|
2017-02-23 23:11:55 +01:00
|
|
|
|
2018-05-24 22:36:45 +02:00
|
|
|
self.main_branches = ['development', 'env-test', 'env-stage', 'env-prod']
|
|
|
|
self.major_branches = []
|
|
|
|
self.minor_branches = ['feature', 'RightBrain-Networks/feature']
|
|
|
|
self.patch_branches = ['hotfix', 'bugfix']
|
2017-02-27 21:41:06 +01:00
|
|
|
|
2018-05-24 22:36:45 +02:00
|
|
|
#def _setting_to_array(self, setting):
|
|
|
|
# config = ConfigParser()
|
|
|
|
# config.read('./.bumpversion.cfg')
|
|
|
|
# value = config.get('semver', setting)
|
|
|
|
# print(str(value))
|
|
|
|
|
|
|
|
#return value.split(',')
|
2017-02-27 21:41:06 +01:00
|
|
|
# filter() removes empty string which is what we get if setting is blank
|
2018-05-24 22:36:45 +02:00
|
|
|
#return [v.strip() for v in value.split(',')]
|
2017-02-27 21:41:06 +01:00
|
|
|
|
|
|
|
# based on commit message see what branches are involved in the merge
|
2018-05-24 22:36:45 +02:00
|
|
|
|
2017-02-27 20:58:44 +01:00
|
|
|
def get_branches(self):
|
|
|
|
p = subprocess.Popen(['git', 'log', '-1'], stdout=subprocess.PIPE,
|
2018-05-23 21:09:14 +02:00
|
|
|
cwd='.')
|
2018-05-24 22:36:45 +02:00
|
|
|
#check current branch
|
|
|
|
b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE,
|
|
|
|
cwd='.')
|
2018-05-23 21:41:35 +02:00
|
|
|
message = str(p.stdout.read())
|
2018-05-24 22:36:45 +02:00
|
|
|
br = b.stdout.read().decode('utf-8')
|
|
|
|
#remove newline
|
|
|
|
branch=br.rstrip()
|
|
|
|
print("Main branch is "+branch)
|
2017-02-27 20:58:44 +01:00
|
|
|
matches = self.GET_COMMIT_MESSAGE.search(message)
|
|
|
|
if matches:
|
2018-05-24 22:36:45 +02:00
|
|
|
if str(matches.group(4)) == branch:
|
|
|
|
self.merged_branch = matches.group(2)
|
|
|
|
else:
|
|
|
|
self.merged_branch = matches.group(4)
|
|
|
|
self.main_branch = branch
|
|
|
|
#print("group1 "+matches.group(1))
|
|
|
|
#print("group2 "+matches.group(2))
|
|
|
|
#print("group3 "+matches.group(3))
|
|
|
|
#print("group4 "+matches.group(4))
|
|
|
|
|
2017-02-27 20:58:44 +01:00
|
|
|
return bool(matches)
|
|
|
|
|
2017-02-27 21:41:06 +01:00
|
|
|
# based on branches involved see what type of versioning should be done
|
2017-02-27 20:58:44 +01:00
|
|
|
def get_version_type(self):
|
2017-02-27 21:41:06 +01:00
|
|
|
for prefix in self.major_branches:
|
|
|
|
if self.merged_branch.startswith(prefix + '/'):
|
|
|
|
self.version_type = 'major'
|
|
|
|
return True
|
|
|
|
for prefix in self.minor_branches:
|
|
|
|
if self.merged_branch.startswith(prefix + '/'):
|
|
|
|
self.version_type = 'minor'
|
|
|
|
return True
|
|
|
|
for prefix in self.patch_branches:
|
|
|
|
if self.merged_branch.startswith(prefix + '/'):
|
|
|
|
self.version_type = 'patch'
|
|
|
|
return True
|
|
|
|
return False
|
2017-02-27 20:58:44 +01:00
|
|
|
|
2017-02-27 21:41:06 +01:00
|
|
|
# setup git settings so we can commit and tag
|
2017-02-27 20:58:44 +01:00
|
|
|
def setup_git_user(self):
|
|
|
|
# setup git user
|
|
|
|
p = subprocess.Popen(['git', 'config', 'user.email',
|
|
|
|
'"versioner@semver.com"'],
|
2018-05-23 21:09:14 +02:00
|
|
|
cwd='.')
|
2017-02-27 20:58:44 +01:00
|
|
|
p = subprocess.Popen(['git', 'config', 'user.name',
|
|
|
|
'"Semantic Versioner"'],
|
2018-05-23 21:09:14 +02:00
|
|
|
cwd='.')
|
2017-02-27 20:58:44 +01:00
|
|
|
p.wait()
|
|
|
|
return self
|
|
|
|
|
2017-02-27 21:41:06 +01:00
|
|
|
# use bumpversion to increment the appropriate version type
|
2017-02-27 20:58:44 +01:00
|
|
|
def version_repo(self):
|
|
|
|
# version repo
|
|
|
|
p = subprocess.Popen(['bumpversion', self.version_type],
|
2018-05-23 21:09:14 +02:00
|
|
|
cwd='.')
|
2017-02-27 20:58:44 +01:00
|
|
|
p.wait()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def commit_and_push(self):
|
|
|
|
# push versioning commit
|
2018-05-24 22:36:45 +02:00
|
|
|
p = subprocess.Popen(['git', 'push', 'origin', self.main_branch],
|
2018-05-23 21:09:14 +02:00
|
|
|
cwd='.')
|
2017-02-27 20:58:44 +01:00
|
|
|
p.wait()
|
|
|
|
|
|
|
|
# push versioning tag
|
|
|
|
p = subprocess.Popen(['git', 'push', 'origin', '--tags'],
|
2018-05-23 21:09:14 +02:00
|
|
|
cwd='.')
|
2017-02-27 20:58:44 +01:00
|
|
|
p.wait()
|
|
|
|
return self
|
|
|
|
|
2017-02-27 21:41:06 +01:00
|
|
|
# 1) get branches from last commit message
|
|
|
|
# 2) see if we're merging into a main branch
|
|
|
|
# 3) see what type of versioning we should do
|
|
|
|
# 4) version the repo
|
2017-02-27 20:58:44 +01:00
|
|
|
def run(self):
|
2018-05-24 22:36:45 +02:00
|
|
|
print(self.main_branches)
|
2017-02-27 20:58:44 +01:00
|
|
|
if not self.get_branches():
|
|
|
|
raise Exception('No merge found')
|
2017-02-27 21:41:06 +01:00
|
|
|
if self.main_branch not in self.main_branches:
|
2017-02-27 20:58:44 +01:00
|
|
|
raise Exception('Not merging into a main branch')
|
|
|
|
if not self.get_version_type():
|
|
|
|
raise Exception('No git flow branch found')
|
|
|
|
self.setup_git_user()
|
|
|
|
self.version_repo()
|
2017-10-27 18:41:20 +02:00
|
|
|
self.commit_and_push()
|
2017-02-27 20:58:44 +01:00
|
|
|
return self
|
2017-02-23 23:11:55 +01:00
|
|
|
|
2018-05-23 21:09:14 +02:00
|
|
|
def main():
|
2017-10-27 18:41:20 +02:00
|
|
|
try:
|
|
|
|
SemVer().run()
|
|
|
|
except Exception as e:
|
2018-05-23 21:41:35 +02:00
|
|
|
print(e)
|
2018-05-23 21:09:14 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try: main()
|
|
|
|
except: raise
|