def create_contributions( # noqa: C901,R701,WPS231 repo, contrib_data, user_field=None, id_field=None, type_=None, ): """Create a contribution record.""" for contrib in contrib_data: contrib_author = contrib[user_field] if contrib_author is None or contrib_author['type'] == 'Bot': continue contrib_author_login = contrib_author['login'] if contrib_author_login in IGNORED_CONTRIBUTORS: continue if not type_: pr_or_iss = 'pr' if 'pull_request' in contrib else 'iss' if type_ == 'cit': datetime = contrib['commit']['author']['date'] else: datetime = contrib['created_at'] contribution, created = Contribution.objects.get_or_create( id=contrib[id_field], defaults={ 'repository': repo, 'contributor': get_or_create_contributor(contrib_author_login, ), 'type': type_ or pr_or_iss, 'html_url': contrib['html_url'], 'created_at': dateparse.parse_datetime(datetime), }, ) if created and type_ == 'cit': commit_data = github.get_commit_data( repo.organization, repo, contribution.id, session, ) commit_stats = commit_data['stats'] CommitStats.objects.create( commit=contribution, additions=commit_stats['additions'], deletions=commit_stats['deletions'], ) with suppress(NameError): if pr_or_iss: IssueInfo.objects.update_or_create( issue=contribution, defaults={ 'title': contrib['title'], 'is_open': contrib['state'] == 'open', }, )
def create_contributions( # noqa: C901,R701 repo, contrib_data, user_field=None, id_field=None, type_=None, ): """Create a contribution record.""" for contrib in contrib_data: contrib_author = contrib[user_field] if contrib_author is None or contrib_author['type'] == 'Bot': continue contrib_author_login = contrib_author['login'] if contrib_author_login in IGNORED_CONTRIBUTORS: continue if not type_: pr_or_iss = 'pr' if 'pull_request' in contrib else 'iss' if type_ == 'cit': datetime = contrib['commit']['author']['date'] else: datetime = contrib['created_at'] with suppress(IntegrityError): contribution = Contribution.objects.create( repository=repo, contributor=get_or_create_contributor( contrib_author_login, ), id=contrib[id_field], type=type_ or pr_or_iss, html_url=contrib['html_url'], created_at=dateparse.parse_datetime(datetime), ) if type_ == 'cit': commit_data = github.get_commit_data( repo.organization, repo, contribution.id, session, ) CommitStats.objects.create( commit=contribution, additions=commit_data['stats']['additions'], deletions=commit_data['stats']['deletions'], )
def update_database(event_type, payload): # noqa: WPS210 """Update the database with an event's data.""" action = payload.get('action', 'created') if action not in {'created', 'opened', 'edited', 'closed', 'reopened'}: return sender = payload['sender'] if sender['type'] == 'Bot': return repo = payload['repository'] org = repo['owner'] organization, _ = Organization.objects.get_or_create( id=org['id'], defaults={ 'name': org['login'], 'html_url': org['html_url'], }, ) repository, _ = Repository.objects.get_or_create( id=repo['id'], defaults={ 'name': repo['name'], 'full_name': repo['full_name'], 'html_url': repo['html_url'], 'organization': organization, }, ) contributor, _ = Contributor.objects.get_or_create( id=sender['id'], defaults={ 'login': sender['login'], 'name': github.get_user_name(sender['url']), 'avatar_url': sender['avatar_url'], 'html_url': sender['html_url'], }, ) event_type_to_data_field_mapping = { 'push': 'commits', 'pull_request': 'pull_request', 'issues': 'issue', 'commit_comment': 'comment', 'issue_comment': 'comment', 'pull_request_review_comment': 'comment', } event_type_to_db_type_mapping = { 'push': 'cit', 'pull_request': 'pr', 'issues': 'iss', 'commit_comment': 'cnt', 'issue_comment': 'cnt', 'pull_request_review_comment': 'cnt', } contrib_data = payload[event_type_to_data_field_mapping[event_type]] contrib_type = event_type_to_db_type_mapping[event_type] # Special case for commits # push event commit_created_at = timezone.localtime( dateparse.parse_datetime(payload['commits'][0]['timestamp'], ), timezone.utc, ).strftime('%Y-%m-%dT%H:%M:%SZ') # noqa: WPS323 if event_type == 'push': for gh_commit in github.get_repo_commits_except_merges( # noqa: WPS352 organization, repository, {'since': commit_created_at}, ): commit = Contribution.objects.create( repository=repository, contributor=contributor, id=gh_commit['sha'], type=contrib_type, html_url=gh_commit['html_url'], created_at=dateparse.parse_datetime( gh_commit['commit']['author']['date'], ), ) commit_extra_data = github.get_commit_data( organization, repository, commit.id, ) commit_stats = commit_extra_data['stats'] CommitStats.objects.create( commit=commit, additions=commit_stats['additions'], deletions=commit_stats['deletions'], ) # Actions for other types of events: # commit_comment, issue_comment, pull_request_review_comment # issues, pull_request else: contrib, _ = Contribution.objects.get_or_create( id=contrib_data['id'], defaults={ 'repository': repository, 'contributor': contributor, 'type': contrib_type, 'html_url': contrib_data['html_url'], 'created_at': dateparse.parse_datetime(contrib_data['created_at'], ), }, ) if contrib.type == 'iss': IssueInfo.objects.update_or_create( issue=contrib, defaults={ 'title': contrib_data['title'], 'is_open': contrib_data['state'] == 'open', }, )