def _translate_pull_request_comment(cls, payload): """Translate pull request comment. :param payload: :return: """ if payload['object_attributes']['noteable_type'] != 'MergeRequest': logger.warning('Currently, non-merge-request based comments are not supported, skip') return [] event_template = { 'vendor': cls.vendor, 'type': AxEventTypes.PULL_REQUEST, 'repo': payload['merge_request']['source']['git_http_url'], 'branch': payload['merge_request']['source_branch'], 'commit': payload['merge_request']['last_commit']['id'], 'target_repo': payload['merge_request']['target']['git_http_url'], 'target_branch': payload['merge_request']['target_branch'], 'author': '{} {}'.format(payload['merge_request']['last_commit']['author']['name'], payload['merge_request']['last_commit']['author']['email']), 'committer': '{} {}'.format(payload['merge_request']['last_commit']['author']['name'], payload['merge_request']['last_commit']['author']['email']), 'title': payload['merge_request']['title'], 'description': payload['merge_request']['description'], 'date': utc(payload['object_attributes']['updated_at']) } commands = cls._parse_command(payload['object_attributes']['note']) events = [] for command in commands: event = copy.copy(event_template) for k in command: event[k] = command[k] events.append(event) return events
def _translate_push(cls, payload): """Translate push event. :param payload: :return: """ if payload['deleted']: logger.info('It is delete event, ignore it') return [] event = { 'vendor': cls.vendor, 'type': AxEventTypes.PUSH, 'repo': payload['repository']['clone_url'], 'branch': '/'.join( payload['ref'].split('/')[2:] ), # Branch is extracted from reference having form '/refs/heads/<branch_name>' 'commit': payload['head_commit']['id'], 'author': '{} {}'.format(payload['head_commit']['author']['name'], payload['head_commit']['author']['email']), 'committer': '{} {}'.format(payload['head_commit']['committer']['name'], payload['head_commit']['committer']['email']), 'description': payload['head_commit']['message'], 'date': utc(payload['head_commit']['timestamp']) } return [event]
def _translate_pull_request(cls, payload): """Translate pull request event. :param payload: :return: """ pull_request = payload.get('pullrequest') repo = pull_request['source']['repository']['links']['html']['href'] + '.git' commit_short = pull_request['source']['commit']['hash'] commit_obj = cls.client.get_commit(repo, commit_short) event = { 'vendor': cls.vendor, 'type': AxEventTypes.PULL_REQUEST, 'repo': repo, 'branch': pull_request['source']['branch']['name'], 'commit': commit_obj['hash'], 'target_repo': pull_request['destination']['repository']['links']['html']['href'] + '.git', 'target_branch': pull_request['destination']['branch']['name'], 'author': commit_obj['author']['raw'], 'committer': commit_obj['author']['raw'], # BitBucket rest api does not have committer section 'title': pull_request['title'], 'description': pull_request['description'], 'date': utc(commit_obj['date']) } return [event]
def _translate_push(cls, payload): """Translate push event. BitBucket may put multiple pushes (i.e. when the developer uses push --all) into one event. :param payload: :return: """ changes = payload['push']['changes'] events = [] for change in changes: if change.get('new'): event = { 'vendor': cls.vendor, 'type': AxEventTypes.PUSH, 'repo': payload['repository']['links']['html']['href'] + '.git', 'branch': change['new']['name'], 'commit': change['new']['target']['hash'], 'author': change['new']['target']['author']['raw'], 'committer': change['new']['target']['author']['raw'], 'description': change['new']['target']['message'], 'date': utc(change['new']['target']['date']) } events.append(event) return events
def _translate_push(cls, payload): """Translate push event. :param payload: :return: """ event = { 'vendor': cls.vendor, 'type': AxEventTypes.PUSH, 'repo': payload['project']['git_http_url'], 'branch': '/'.join(payload['ref'].split('/')[2:]), 'commit': payload['commits'][0]['id'], 'author': '{} {}'.format(payload['commits'][0]['author']['name'], payload['commits'][0]['author']['email']), 'committer': '{} {}'.format(payload['commits'][0]['author']['name'], payload['commits'][0]['author']['email']), 'description': payload['commits'][0]['message'], 'date': utc(payload['commits'][0]['timestamp']) } return [event]
def _translate_pull_request(cls, payload): """Translate pull request event. :param payload: :return: """ if payload['object_attributes']['state'] == 'merged': event_type = AxEventTypes.PULL_REQUEST_MERGE else: event_type = AxEventTypes.PULL_REQUEST event = { 'vendor': cls.vendor, 'type': event_type, 'commit': payload['object_attributes']['last_commit']['id'], 'author': '{} {}'.format( payload['object_attributes']['last_commit']['author']['name'], payload['object_attributes']['last_commit']['author'] ['email']), 'committer': '{} {}'.format( payload['object_attributes']['last_commit']['author']['name'], payload['object_attributes']['last_commit']['author'] ['email']), 'title': payload['object_attributes']['title'], 'description': payload['object_attributes']['description'], 'date': utc(payload['object_attributes']['updated_at']) } if event['type'] == AxEventTypes.PULL_REQUEST_MERGE: extra_info = { 'repo': payload['object_attributes']['target']['git_http_url'], 'branch': payload['object_attributes']['target_branch'], 'source_repo': payload['object_attributes']['source']['git_http_url'], 'source_branch': payload['object_attributes']['source_branch'] } else: extra_info = { 'repo': payload['object_attributes']['source']['git_http_url'], 'branch': payload['object_attributes']['source_branch'], 'target_repo': payload['object_attributes']['target']['git_http_url'], 'target_branch': payload['object_attributes']['target_branch'] } event.update(extra_info) return [event]
def _translate_pull_request(cls, payload): """Translate pull request event. :param payload: :return: """ event_type = AxEventTypes.PULL_REQUEST_MERGE if payload[ 'action'] == 'closed' else AxEventTypes.PULL_REQUEST commit_obj = cls.client.get_commit( payload['pull_request']['head']['repo']['clone_url'], payload['pull_request']['head']['sha']) author = '{} {}'.format(commit_obj['commit']['author']['name'], commit_obj['commit']['author']['email']) committer = '{} {}'.format(commit_obj['commit']['committer']['name'], commit_obj['commit']['committer']['email']) if payload['pull_request'].get('merged', False): _commit = payload['pull_request']['merge_commit_sha'] else: _commit = payload['pull_request']['head']['sha'] event = { 'vendor': cls.vendor, 'type': event_type, 'commit': _commit, 'author': author, 'committer': committer, 'title': payload['pull_request']['title'], 'description': payload['pull_request']['body'], 'date': utc(payload['pull_request']['updated_at']) } if event['type'] == AxEventTypes.PULL_REQUEST_MERGE: extra_info = { 'repo': payload['pull_request']['base']['repo']['clone_url'], 'branch': payload['pull_request']['base']['ref'], 'source_repo': payload['pull_request']['head']['repo']['clone_url'], 'source_branch': payload['pull_request']['head']['ref'] } else: extra_info = { 'repo': payload['pull_request']['head']['repo']['clone_url'], 'branch': payload['pull_request']['head']['ref'], 'target_repo': payload['pull_request']['base']['repo']['clone_url'], 'target_branch': payload['pull_request']['base']['ref'] } event.update(extra_info) return [event]
def _translate_pull_request_comment(cls, payload): """Translate pull request comment. :param payload: :return: """ _pull_request = payload['issue'].get('pull_request', None) if _pull_request is None: logger.warn( 'It is a general issue_comment event rather than pull_request_comment, ignore it' ) return [] pull_request_url = _pull_request['url'] repo = cls.client.url_to_repo(pull_request_url) pull_request_id = pull_request_url.split('/')[-1] pull_request = cls.client.get_pull_request(repo, pull_request_id) commit_obj = cls.client.get_commit( pull_request['head']['repo']['clone_url'], pull_request['head']['sha']) author = '{} {}'.format(commit_obj['commit']['author']['name'], commit_obj['commit']['author']['email']) committer = '{} {}'.format(commit_obj['commit']['committer']['name'], commit_obj['commit']['committer']['email']) event_template = { 'vendor': cls.vendor, 'type': AxEventTypes.PULL_REQUEST, 'repo': pull_request['head']['repo']['clone_url'], 'branch': pull_request['head']['ref'], 'commit': pull_request['head']['sha'], 'target_repo': pull_request['base']['repo']['clone_url'], 'target_branch': pull_request['base']['ref'], 'author': author, 'committer': committer, 'title': pull_request['title'], 'description': pull_request['body'], 'date': utc(pull_request['updated_at']) } commands = cls._parse_command(payload['comment']['body']) events = [] for command in commands: event = copy.copy(event_template) for k in command: event[k] = command[k] events.append(event) return events
def _translate_create(cls, payload): """Translate create event. :param payload: :return: """ if payload.get('ref_type', '') != 'tag': logger.info('It is NOT tag creation event, ignore it') return [] event = { 'vendor': cls.vendor, 'type': AxEventTypes.CREATE, 'repo': payload['repository']['clone_url'], 'branch': payload['master_branch'], 'commit': payload['ref'], 'author': payload['sender']['login'], 'committer': payload['sender']['login'], 'description': payload.get('description', ''), 'date': utc(payload['repository']['pushed_at']) } return [event]
def _construct_commit_object(self, lines_of_a_commit, having_line_break=True): """Construct a commit object. :param lines_of_a_commit: :param having_line_break: Only the last entry won't have a line break. :return: """ commit = {'repo': self.repo_url} if len(lines_of_a_commit) < 6: logger.warning( 'Illegal texts forming the contents of a commit, skip processing' ) for i in range(len(lines_of_a_commit)): print(lines_of_a_commit[i]) return for i in range(len(lines_of_a_commit)): if lines_of_a_commit[i].startswith('commit'): if '(' in lines_of_a_commit[i]: m = re.match('commit (.*) \((.*)\)', lines_of_a_commit[i]) if m: revisions, refs = m.groups() revisions = revisions.split() refs = [ v.split('/', maxsplit=1)[-1] for v in refs.split(', ') ] commit['revision'] = revisions[0] commit['branches'] = refs commit['parents'] = revisions[1:] else: logger.warning('Unable to match commit with pattern') else: m = re.match('commit (.*)', lines_of_a_commit[i]) if m: revisions = m.groups()[0] revisions = revisions.split() commit['revision'] = revisions[0] commit['branches'] = [] commit['parents'] = revisions[1:] else: logger.warning('Unable to match commit with pattern') elif lines_of_a_commit[i].startswith('Author:'): commit['author'] = lines_of_a_commit[i].split(':')[-1].strip() elif lines_of_a_commit[i].startswith('AuthorDate:'): author_date = lines_of_a_commit[i].split( ':', maxsplit=1)[-1].strip() author_date = utc(author_date, type=int) commit['author_date'] = author_date elif lines_of_a_commit[i].startswith('Commit:'): commit['committer'] = lines_of_a_commit[i].split( ':')[-1].strip() elif lines_of_a_commit[i].startswith('CommitDate:'): commit_date = lines_of_a_commit[i].split( ':', maxsplit=1)[-1].strip() commit_date = utc(commit_date, type=int) commit['commit_date'] = commit_date commit['date'] = commit_date elif lines_of_a_commit[i].startswith('Merge:'): continue else: if having_line_break: description = '\n'.join( [v.lstrip() for v in lines_of_a_commit[i + 1:-1]]) else: description = '\n'.join( [v.lstrip() for v in lines_of_a_commit[i + 1:]]) commit['description'] = description break return commit