def check_empty_commit_id(flask_app): """ check commit ID for empty tracking """ with flask_app.app_context(): new_track = get_track_from_db() for item in new_track: if item.scm_commit: continue if item.version_control == "github": github_api = GitHub(item) status, result = github_api.get_latest_commit_id() if status == 'success': commit_id = result['latest_commit'] data = { 'version_control': item.version_control, 'repo': item.repo, 'branch': item.branch, 'enabled': item.enabled, 'scm_commit': commit_id, 'scm_branch': item.scm_branch, 'scm_repo': item.scm_repo } update_tracking(data) else: logger.error( 'Check empty CommitID: Fail to get latest commit id of scm_repo: %s scm_branch: %s. \ Return val: %s', item.scm_repo, item.scm_branch, result ) elif item.version_control == "git": git_api = Git(item) commit_id = git_api.git_latest_sha() if not commit_id: return None data = { 'version_control': item.version_control, 'repo': item.repo, 'branch': item.branch, 'enabled': item.enabled, 'scm_commit': commit_id, 'scm_branch': item.scm_branch, 'scm_repo': item.scm_repo } update_tracking(data) else: logger.error( 'Check empty CommitID: Fail to get latest commit id of scm_repo: %s scm_branch: %s. Return val: %s', item.scm_repo, item.scm_branch, result ) return None
def get_patch_list(self): """ get patch list """ repo = os.path.join(self.base_path, self.repo_dir_name) patch_list = list() latest_commit = self.git_latest_sha() if not latest_commit: return None if not self.track.scm_commit: data = { 'version_control': self.track.version_control, 'repo': self.track.repo, 'branch': self.track.branch, 'enabled': self.track.enabled, 'scm_commit': latest_commit, 'scm_branch': self.track.scm_branch, 'scm_repo': self.track.scm_repo } try: update_tracking(data) except SQLAlchemyError as err: logger.error( '[Patch Tracking update empty commit id] Fail to update tracking: %s. Result: %s', data, err) return None commit_list = self.get_commit_list(self.track.scm_commit, latest_commit) if not commit_list: return None for i in range(len(commit_list) - 1): patch_dict = dict() patch_dict['commit_id'] = commit_list[i] patch_dict['message'], patch_dict['time'] = self.get_commit_info( commit_list[i]) patch_ret = self.git_patch(repo, commit_list[i], commit_list[i + 1]) if patch_ret: patch_dict['patch_content'] = patch_ret else: return None patch_list.append(patch_dict) patch_list.reverse() return patch_list
def post(): """ Creates or update a tracking. """ required_params = [ 'version_control', 'scm_repo', 'scm_branch', 'scm_commit', 'repo', 'branch', 'enabled' ] input_params = request.json data = dict() for item in input_params: if item in required_params: data[item] = input_params[item] required_params.remove(item) else: return ResponseCode.ret_message( ResponseCode.INPUT_PARAMETERS_ERROR) if len(required_params) > 1 or (len(required_params) == 1 and required_params[0] != 'scm_commit'): return ResponseCode.ret_message(ResponseCode.INPUT_PARAMETERS_ERROR) if data['version_control'] not in ["github", "git"]: return ResponseCode.ret_message(ResponseCode.INPUT_PARAMETERS_ERROR) track = Tracking.query.filter_by(repo=data['repo'], branch=data['branch']).first() if track: try: update_tracking(data) logger.info('Update tracking. Data: %s.', data) except SQLAlchemyError as err: return ResponseCode.ret_message( code=ResponseCode.INSERT_DATA_ERROR, data=err) else: try: create_tracking(data) logger.info('Create tracking. Data: %s.', data) except SQLAlchemyError as err: return ResponseCode.ret_message( code=ResponseCode.INSERT_DATA_ERROR, data=err) return ResponseCode.ret_message(code=ResponseCode.SUCCESS, data=request.json)
def get_patch_list(self): """ get patch list """ all_commits_info = self.get_all_commit_list() all_commits = [item["sha"] for item in all_commits_info] if self.track.scm_commit not in all_commits: logger.error( '[Patch Tracking] Scm repo commit : %s not found in latest 100 commits of scm_repo: %s scm_branch: %s.', self.track.scm_commit, self.track.scm_repo, self.track.scm_branch) return None commit_list = list() status, result = self.get_latest_commit_id() if status != 'success': logger.error( '[Patch Tracking] Fail to get latest commit id of scm_repo: %s scm_branch: %s. Return val: %s', self.track.scm_repo, self.track.scm_branch, result) return None latest_commit = result['latest_commit'] if not self.track.scm_commit: data = { 'version_control': self.track.version_control, 'repo': self.track.repo, 'branch': self.track.branch, 'enabled': self.track.enabled, 'scm_commit': latest_commit, 'scm_branch': self.track.scm_branch, 'scm_repo': self.track.scm_repo } try: update_tracking(data) except SQLAlchemyError as err: logger.error( '[Patch Tracking update empty commit id] Fail to update tracking: %s. Result: %s', data, err) return None while self.track.scm_commit != latest_commit: status, result = self.get_commit_info(self.track.scm_repo, latest_commit) logger.debug('get_commit_info: %s %s', status, result) if status == 'success': if 'parent' in result: ret = self.get_patch(self.track.scm_repo, latest_commit, latest_commit) logger.debug('get patch api ret: %s', ret) if ret['status'] == 'success': result['patch_content'] = ret['api_ret'] # inverted insert commit_list commit_list.insert(0, result) else: logger.error( 'Get scm: %s commit: %s patch failed. Result: %s', self.track.scm_repo, latest_commit, result) latest_commit = result['parent'] else: logger.info( '[Patch Tracking] Successful get scm commit from %s to %s ID/message/time/patch.', self.track.scm_commit, latest_commit) break else: logger.error( '[Patch Tracking] Get scm: %s commit: %s ID/message/time failed. Result: %s', self.track.scm_repo, latest_commit, result) return commit_list
def create_patch_issue_pr(patch, cur_time, git_api): """ Create temporary branches, submit files, and create PR and issue :return: """ issue_dict = dict() gitee_repo = patch["repo"].replace("https://gitee.com/", "") issue_dict['repo'] = patch['repo'] issue_dict['branch'] = patch['branch'] new_branch = 'patch-tracking/' + cur_time result = create_branch(gitee_repo, patch['branch'], new_branch) if result == 'success': logger.info('[Patch Tracking %s] Successful create branch: %s', cur_time, new_branch) else: logger.error('[Patch Tracking %s] Fail to create branch: %s', cur_time, new_branch) return None patch_lst = list() for latest_commit in patch['commit_list']: scm_commit_url = '/'.join([ 'https://github.com', patch['scm_repo'], 'commit', latest_commit['commit_id'] ]) latest_commit['message'] = latest_commit['message'].replace( "\r", "").replace("\n", "<br>") patch_file_content = latest_commit['patch_content'] post_data = { 'repo': gitee_repo, 'branch': new_branch, 'latest_commit_id': latest_commit['commit_id'], 'patch_file_content': str(patch_file_content), 'cur_time': cur_time, 'commit_url': scm_commit_url } result = upload_patch(post_data) if result == 'success': logger.info( '[Patch Tracking %s] Successfully upload patch file of commit: %s', cur_time, latest_commit['commit_id']) else: logger.error( '[Patch Tracking %s] Fail to upload patch file of commit: %s', cur_time, latest_commit['commit_id']) return None patch_lst.append(str(latest_commit['commit_id'])) result = upload_spec_to_repo(patch, patch_lst, cur_time) if result == "success": logger.info('[Patch Tracking %s] Successfully upload spec file.', cur_time) else: logger.error( '[Patch Tracking %s] Fail to upload spec file. Result: %s', cur_time, result) return None issue_table = git_api.issue_table(patch['commit_list']) logger.debug(issue_table) result = create_gitee_issue(gitee_repo, patch['branch'], issue_table, cur_time) if result[0] == 'success': issue_num = result[1] logger.info('[Patch Tracking %s] Successfully create issue: %s', cur_time, issue_num) retry_count = 10 while retry_count > 0: ret = create_pull_request(gitee_repo, patch['branch'], new_branch, issue_num, cur_time) if ret == 'success': logger.info( '[Patch Tracking %s] Successfully create PR of issue: %s.', cur_time, issue_num) break logger.warning( '[Patch Tracking %s] Fail to create PR of issue: %s. Result: %s', cur_time, issue_num, ret) retry_count -= 1 time.sleep(random.random() * 5) if retry_count == 0: logger.error('[Patch Tracking %s] Fail to create PR of issue: %s.', cur_time, issue_num) return None issue_dict['issue'] = issue_num data = { 'version_control': patch['version_control'], 'repo': patch['repo'], 'branch': patch['branch'], 'enabled': patch['enabled'], 'scm_commit': patch['commit_list'][-1]['commit_id'], 'scm_branch': patch['scm_branch'], 'scm_repo': patch['scm_repo'] } try: update_tracking(data) except SQLAlchemyError as err: logger.error( '[Patch Tracking %s] Fail to update tracking: %s. Result: %s', cur_time, data, err) else: logger.error( '[Patch Tracking %s] Fail to create issue: %s. Result: %s', cur_time, issue_table, result[1]) return None return issue_dict