made a few options configurable instead of hard coded
This commit is contained in:
		@ -8,3 +8,9 @@ message = Bump version: {current_version} -> {new_version}
 | 
				
			|||||||
[bumpversion:file:VERSION]
 | 
					[bumpversion:file:VERSION]
 | 
				
			||||||
search = version={current_version}
 | 
					search = version={current_version}
 | 
				
			||||||
replace = version={new_version}
 | 
					replace = version={new_version}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[semver]
 | 
				
			||||||
 | 
					main_branches = develop, env-test, env-stage, env-prod
 | 
				
			||||||
 | 
					major_branches =
 | 
				
			||||||
 | 
					minor_branches = feature
 | 
				
			||||||
 | 
					patch_branches = hotfix, bugfix
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										42
									
								
								semver.py
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								semver.py
									
									
									
									
									
								
							@ -1,5 +1,6 @@
 | 
				
			|||||||
import re
 | 
					import re
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					from ConfigParser import ConfigParser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SemVer(object):
 | 
					class SemVer(object):
 | 
				
			||||||
@ -11,6 +12,19 @@ class SemVer(object):
 | 
				
			|||||||
        self.main_branch = None
 | 
					        self.main_branch = None
 | 
				
			||||||
        self.version_type = None
 | 
					        self.version_type = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.main_branches = self._setting_to_array('main_branches')
 | 
				
			||||||
 | 
					        self.major_branches = self._setting_to_array('major_branches')
 | 
				
			||||||
 | 
					        self.minor_branches = self._setting_to_array('minor_branches')
 | 
				
			||||||
 | 
					        self.patch_branches = self._setting_to_array('patch_branches')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _setting_to_array(self, setting):
 | 
				
			||||||
 | 
					        config = ConfigParser()
 | 
				
			||||||
 | 
					        config.read('/application_repo/.bumpversion.cfg')
 | 
				
			||||||
 | 
					        value = config.get('semver', setting)
 | 
				
			||||||
 | 
					        # filter() removes empty string which is what we get if setting is blank
 | 
				
			||||||
 | 
					        return filter(bool, [v.strip() for v in value.split(',')])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # based on commit message see what branches are involved in the merge
 | 
				
			||||||
    def get_branches(self):
 | 
					    def get_branches(self):
 | 
				
			||||||
        p = subprocess.Popen(['git', 'log', '-1'], stdout=subprocess.PIPE,
 | 
					        p = subprocess.Popen(['git', 'log', '-1'], stdout=subprocess.PIPE,
 | 
				
			||||||
                             cwd='/application_repo')
 | 
					                             cwd='/application_repo')
 | 
				
			||||||
@ -21,13 +35,23 @@ class SemVer(object):
 | 
				
			|||||||
            self.main_branch = matches.group(2)
 | 
					            self.main_branch = matches.group(2)
 | 
				
			||||||
        return bool(matches)
 | 
					        return bool(matches)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # based on branches involved see what type of versioning should be done
 | 
				
			||||||
    def get_version_type(self):
 | 
					    def get_version_type(self):
 | 
				
			||||||
        if self.merged_branch.startswith('feature/'):
 | 
					        for prefix in self.major_branches:
 | 
				
			||||||
            self.version_type = 'minor'
 | 
					            if self.merged_branch.startswith(prefix + '/'):
 | 
				
			||||||
        elif self.merged_branch.startswith('hotfix/'):
 | 
					                self.version_type = 'major'
 | 
				
			||||||
            self.version_type = 'patch'
 | 
					                return True
 | 
				
			||||||
        return bool(self.version_type)
 | 
					        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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # setup git settings so we can commit and tag
 | 
				
			||||||
    def setup_git_user(self):
 | 
					    def setup_git_user(self):
 | 
				
			||||||
        # setup git user
 | 
					        # setup git user
 | 
				
			||||||
        p = subprocess.Popen(['git', 'config', 'user.email',
 | 
					        p = subprocess.Popen(['git', 'config', 'user.email',
 | 
				
			||||||
@ -39,6 +63,7 @@ class SemVer(object):
 | 
				
			|||||||
        p.wait()
 | 
					        p.wait()
 | 
				
			||||||
        return self
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # use bumpversion to increment the appropriate version type
 | 
				
			||||||
    def version_repo(self):
 | 
					    def version_repo(self):
 | 
				
			||||||
        # version repo
 | 
					        # version repo
 | 
				
			||||||
        p = subprocess.Popen(['bumpversion', self.version_type],
 | 
					        p = subprocess.Popen(['bumpversion', self.version_type],
 | 
				
			||||||
@ -63,11 +88,14 @@ class SemVer(object):
 | 
				
			|||||||
        '''
 | 
					        '''
 | 
				
			||||||
        return self
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # 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
 | 
				
			||||||
    def run(self):
 | 
					    def run(self):
 | 
				
			||||||
        if not self.get_branches():
 | 
					        if not self.get_branches():
 | 
				
			||||||
            raise Exception('No merge found')
 | 
					            raise Exception('No merge found')
 | 
				
			||||||
        if self.main_branch not in \
 | 
					        if self.main_branch not in self.main_branches:
 | 
				
			||||||
         ['develop', 'env-test', 'env-stage', 'env-prod']:
 | 
					 | 
				
			||||||
            raise Exception('Not merging into a main branch')
 | 
					            raise Exception('Not merging into a main branch')
 | 
				
			||||||
        if not self.get_version_type():
 | 
					        if not self.get_version_type():
 | 
				
			||||||
            raise Exception('No git flow branch found')
 | 
					            raise Exception('No git flow branch found')
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user