def process(self): if not self.__gitflow.is_release(): raise NoBranchSelected('Checkout to release branch before') if not self.__git.is_clean_working_tree(): raise NotCleanWorkingTree() self.__pull_master() if self.__git.is_branch_ahead(self.__config_handler.master(), self.__name): Log.error(""" {fg_fail}{list}{reset_fg} """.format( fg_fail=Fg.FAIL.value, list=self.__git.list_commit_diff( self.__config_handler.master(), self.__name), reset_fg=Fg.RESET.value, )) self.__checkout_current_release() raise BranchHaveDiverged(""" {fg_fail}{message}{reset_fg} """.format( fg_fail=Fg.FAIL.value, message='Oups !!! Master:' + self.__config_handler.master() + ' have commit ahead ' + self.__name + ' merge before', reset_fg=Fg.RESET.value, )) self.__pull_develop().__finish_release()
def __attach(self) -> bool: topic_number: int if self.__would_attach_topics(): for topic_number in self.__topics_number(): request_topic: FlexioTopic = FlexioTopic().with_number( topic_number) try: Log.info('waiting... from Flexio... Topic : ' + str(topic_number)) topic: FlexioTopic = self.__get_topic_with_number( request_topic) CommonTopic.print_resume_topic(topic) self.__topics.append(topic) except FileNotFoundError: Log.error(Fg.FAIL.value + 'Topic not found : retry' + Fg.RESET.value) self.__topics = [] return self.process() return True else: return False
def process(self): if not self.__git.is_clean_working_tree(): raise NotCleanWorkingTree() if not self.__gitflow.is_feature(): raise BranchNotExist(self.__config_handler.feature()) self.__pull_develop() if self.__git.is_branch_ahead(self.__config_handler.develop(), self.__current_branch_name): Log.error(""" {fg_fail}{list}{reset_fg} """.format( fg_fail=Fg.FAIL.value, list=self.__git.list_commit_diff( self.__config_handler.develop(), self.__current_branch_name), reset_fg=Fg.RESET.value, )) self.__checkout_current_feature() raise BranchHaveDiverged(""" {fg_fail}{message}{reset_fg} """.format( fg_fail=Fg.FAIL.value, message='Oups !!! Develop have commit ahead ' + self.__current_branch_name + ' merge before', reset_fg=Fg.RESET.value, )) self.__finish_feature()
def __merge_develop(self) -> Finish: self.__checkout_current_feature() message: Message = Message(message=''.join([ "'Finish feature ` ", self.__current_branch_name, " ` for dev: ", self.__state_handler.version_as_str() ]), issue=self.__issue) message_str: str = '' if self.__close_issue: message_str = message.with_close() else: message_str = message.message self.__git.commit(message_str, ['--allow-empty']).try_to_push() self.__git.checkout(self.__config_handler.develop( )).merge_with_version_message_from_branch_name( branch=self.__current_branch_name, message=Message(message='', issue=self.__issue).with_ref(), ).try_to_push() if self.__git.has_conflict(): Log.error(""" {fg_fail}CONFLICT : resolve conflict, and remove your feature branch manually{reset_fg} """.format( fg_fail=Fg.FAIL.value, reset_fg=Fg.RESET.value, )) raise GitMergeConflictError(self.__config_handler.develop(), self.__git.get_conflict()) return self
def create_branch_from(self, target_branch_name: str, source: str) -> GitCmd: source_branch_name: str = self.get_branch_name_from_git(source) self.__exec(['git', 'checkout', '-b', target_branch_name, source_branch_name]) try: self.__state_handler.load_file_config() except FileNotFoundError as e: Log.error(str(e)) return self
def is_clean_working_tree(self) -> bool: if len(self.__exec_for_stdout(['git', 'rev-parse', '--verify', 'HEAD'])) == 0: return False self.__exec(['git', 'update-index', '-q', '--ignore-submodules', '--refresh']) if len(self.__exec_for_stdout(['git', 'diff-files', '--ignore-submodules'])) > 0: Log.error("Working tree contains unstaged changes. Aborting.") return False if len(self.__exec_for_stdout( ['git', 'diff-index', '--cached', '--ignore-submodules', 'HEAD'])) > 0: Log.error("Index contains uncommited changes. Aborting.") return False return True
class UserController: def __init__(self): self.logger = Log("AdminController") self.STATES = dict() self.NEXT_STATES = dict() def update_create_user(self, chat): try: user = User.objects.filter(chat_id=chat.id).first() if user: user.last_checked_bot = timezone.now() user.state = self.NEXT_STATES[user.state] user.save(force_update=True) return user else: self.logger.info("New User start subX : \n{}".format(chat)) username = chat.username last_name = chat.last_name first_name = chat.first_name new_user = User(chat_id=chat.id, username=username, first_name=first_name, last_name=last_name, chat_type=chat.type) new_user.save(force_insert=True) return user except Exception as e: self.logger.error("update_create_user : {}".format(e)) def update_user_state(self, chat_id, user=None): if user is None: user = User.objects.filter(chat_id=chat_id).first() if not user: raise NotImplementedError( "user {} not implemented".format(chat_id)) user.state = self.NEXT_STATES[user.state] user.save(force_update=True) return user.state @staticmethod def get_user_state(chat_id): return User.filter.objects(chat_id=chat_id).first().state def set_states(self, states): self.STATES = states def set_next_states_dict(self, next_states): self.NEXT_STATES = next_states
def __merge_master(self) -> Finish: message: Message = Message(message='Merge ' + self.__name + 'into ' + self.__config_handler.master(), issue=self.__issue) message_str: str = '' if self.__close_issue: message_str = message.with_close() else: message_str = message.message self.__git.commit(message_str, ['--allow-empty']) self.__git.checkout( self.__config_handler.master()).merge_with_version_message( branch=self.__config_handler.release(), message=message_str, options=['--no-ff', '--strategy-option', 'theirs']) if self.__git.has_conflict(): raise GitMergeConflictError(self.__config_handler.master(), self.__git.get_conflict()) tag: str = self.__state_handler.version_as_str() if tag != self.__version_check: Log.error('Version have diverged during merge : ' + tag + 'should be ' + self.__version_check) raise GitMergeConflictError(self.__config_handler.master()) self.__git.tag( tag, ' '.join( ["'From Finished release : ", self.__name, 'tag : ', tag, "'"])).try_to_push_tag(tag).try_to_push() self.__git.checkout(self.__config_handler.release( )).merge_with_version_message_from_branch_name( branch=tag, message=Message(message='Merge ' + tag + ' tag into ' + self.__name, issue=self.__issue).with_ref(), options=['--no-ff']) return self
def __merge_master(self) -> Finish: self.__git.checkout(self.__config_handler.hotfix()) self.__state_handler.set_stable() self.__state_handler.write_file() UpdateSchemeVersion.from_state_handler(self.__state_handler) message: Message = Message(message=''.join([ "'Finish hotfix for master: ", self.__state_handler.version_as_str() ]), issue=self.__issue) message_str: str = '' if self.__close_issue: message_str = message.with_close() else: message_str = message.message self.__git.commit(message_str).try_to_push() self.__git.checkout( self.__config_handler.master()).merge_with_version_message( branch=self.__config_handler.hotfix(), message=Message(message='', issue=self.__issue).with_ref(), options=['--no-ff']).tag( self.__state_handler.version_as_str(), ' '.join([ "'From Finished hotfix : ", self.__git.get_branch_name_from_git( self.__config_handler.hotfix()), 'tag : ', self.__state_handler.version_as_str(), "'" ])).try_to_push_tag( self.__state_handler.version_as_str()).try_to_push() if (self.__git.has_conflict()): Log.error(""" {fg_fail}CONFLICT : resolve conflict, merge into develop and remove your hotfix branch manually{reset_fg} """.format( fg_fail=Fg.FAIL.value, reset_fg=Fg.RESET.value, )) raise GitMergeConflictError(self.__config_handler.master(), self.__git.get_conflict()) return self
def __merge_develop(self) -> Finish: self.__checkout_current_hotfix() self.__state_handler.next_dev_minor() self.__state_handler.set_dev() self.__state_handler.write_file() UpdateSchemeVersion.from_state_handler(self.__state_handler) self.__git.commit( Message(message=''.join([ "'Finish hotfix for dev: ", self.__state_handler.version_as_str() ]), issue=self.__issue).with_ref()).try_to_push() self.__git.checkout( self.__config_handler.develop()).merge_with_version_message( branch=self.__config_handler.hotfix(), message=Message(message='', issue=self.__issue).with_ref()).try_to_push() if (self.__git.has_conflict()): Log.error(""" {fg_fail}CONFLICT : resolve conflict, and remove your hotfix branch manually{reset_fg} """.format( fg_fail=Fg.FAIL.value, reset_fg=Fg.RESET.value, )) raise GitMergeConflictError(self.__config_handler.develop(), self.__git.get_conflict()) self.__git.checkout(self.__config_handler.develop()).merge_with_theirs( branch=self.__config_handler.master()).try_to_push() if (self.__git.has_conflict()): Log.error(""" {fg_fail}CONFLICT : resolve conflict, and remove your hotfix branch manually{reset_fg} """.format( fg_fail=Fg.FAIL.value, reset_fg=Fg.RESET.value, )) raise GitMergeConflictError(self.__config_handler.develop(), self.__git.get_conflict()) return self
def __attach(self) -> bool: if self.__would_attach_issue(): issue_number = self.__number_issue() issue: IssueGithub = IssueGithub().with_number(issue_number) try: Log.info('waiting... from Github... Issue : ' + str(issue_number)) r: Response = self.__read_issue(issue) self.__issue: IssueGithub = IssueGithub.from_api_dict(r.json()) CommonIssue.print_resume_issue(self.__issue) except FileNotFoundError: Log.error(Fg.FAIL.value + 'Issue not found : retry' + Fg.RESET.value) return self.process() return True else: return False
def ensure_master_branch(self) -> GitFlowCmd: Log.info('Ensure have Master branch : ' + self.__config_handler.master()) if not self.__git.local_branch_exists(self.__config_handler.master()): if not self.__git.remote_branch_exists( self.__config_handler.master()): Log.warning(self.__config_handler.master() + ' not exists') create: str = input('Create ' + self.__config_handler.master() + ' from current branch ? y/' + Fg.SUCCESS.value + 'n' + Fg.RESET.value + ' : ') if create == 'y': self.__git.create_branch_from( self.__config_handler.master(), self.__git.get_current_branch_name() ).try_to_set_upstream().try_to_push() self.ensure_master_branch() else: raise BranchNotExist(self.__config_handler.master()) else: Log.error('Remote branch : ' + self.__config_handler.master() + ' already exists, pull before') return self
def reload_state(self) -> GitCmd: try: self.__state_handler.load_file_config() except FileNotFoundError as e: Log.error(str(e)) return self
def __print_response_error(self, response: Response): Log.error('RESPONSE ERROR') print('status : ' + response.status_code) print('content :') print(response.content)