def handle_pull_request_review(self, data): # print(data) if data['action'] == 'submitted': if 'state' not in data['review']: # print('No state') # print(data['review'].keys()) return {'error': 'No state'}, 503 if data['review']['state'] == 'commented': # print('Review comment') return {'info': 'Only commented'} logging.info('Need repository name: {}'.format(data)) mongo_repository = mongo.db.repositories.find_one( {'full_name': data['repository']['full_name']} ) # Make sure it is configured if not mongo_repository: return token = os.getenv('GITHUB_USER_TOKEN') github_client = github.Github(token) repository = github_client.get_repo(data['repository']['id']) pull_request = repository.get_pull(data['pull_request']['number']) pr = PR(repository, pull_request, token) pr.get_contributors() pr.update_contributors_with_reviews() review = data['review'] reviewer = review['user']['login'] if reviewer not in pr.contributors: pr.contributors[reviewer] = { 'name': reviewer, 'review_date': review['submitted_at'] } value = 0 if review['state'] == 'approved': value = 1 elif review['state'] == 'changes_requested': value = -1 logging.info('Approved pullrequest {} {}'.format(reviewer, value)) pr.contributors[reviewer]['review_value'] = value pr.update_votes() pr.get_latest_dates() pr.get_merge_time() logging.info('Contributors {}'.format(pr.contributors)) pr.set_status() pull_request.create_issue_comment('''Thank you for the review. This pull request will be automatically merged by [worlddriven](https://www.worlddriven.org) in {} day(s) and {} hour(s). Current votes: {}/{}. Check the `worlddriven` status checks or the [dashboard]({}) for actual stats. '''.format(pr.days_to_merge.days, pr.days_to_merge // 3600, pr.votes, pr.votes_total, pr.url)) return {'info': 'All fine, thanks'}
def execute_opened(self): mongo_repository = mongo.db.repositories.find_one({ 'full_name': self.data['repository']['full_name'] }) # Make sure it is configured if not mongo_repository: return token = os.getenv('GITHUB_USER_TOKEN') github_client = github.Github(token) repository = github_client.get_repo(self.data['repository']['id']) pull_request = repository.get_pull(self.data['pull_request']['number']) pr = PR(repository, pull_request, token) pr.get_contributors() pr.update_contributors_with_reviews() pr.update_votes() pr.get_latest_dates() pr.get_merge_time() pr.set_status() pull_request.create_issue_comment('''This pull request will be automatically merged by [worlddriven](https://www.worlddriven.org) in {} day(s) and {} hour(s). The start date is based on the latest Commit date / Pull Request created date / (force) Push date. The time to merge is 5 days plus 5 days for each commit. Check the `worlddriven` status check or the [dashboard]({}) for actual stats. To speed up or delay the merge review the pull request: 1. ![Files changed](https://www.worlddriven.org/static/images/github-files-changed.png) 1. ![Review changes](https://www.worlddriven.org/static/images/github-review-changes.png) - Speed up: ![Approve](https://www.worlddriven.org/static/images/github-approve.png) - Delay or stop: ![Request changes](https://www.worlddriven.org/static/images/github-request-changes.png) '''.format(pr.days_to_merge.days, pr.days_to_merge.seconds // 3600, pr.url))
def execute_opened(self): print(self.data['pull_request']['title']) token = os.getenv('TOKEN') github_client = github.Github(token) repository = github_client.get_repo(self.data['repository']['full_name']) pull_request = repository.get_pull(self.data['pull_request']['number']) pr = PR(repository, pull_request) contributors = {contributor.author.login: contributor.total for contributor in pr.get_contributors()} author = self.data['pull_request']['user']['login'] possible_reviewers = [{'name': contributor, 'total': contributors[contributor]} for contributor in contributors if contributor != author and contributors[contributor] >= 10] possible_reviewers = sorted(possible_reviewers, key=lambda reviewer: -1 * reviewer['total']) reviewers = [] for i in range(2): if len(possible_reviewers) == 0: break reviewers.append(possible_reviewers.pop(0)) if len(possible_reviewers) > 0: reviewers.append(possible_reviewers[randrange(len(possible_reviewers) - 1)]) message = '''[democratic collaboration](https://github.com/TooAngel/democratic-collaboration) Approved reviews will speed up the merge, request changes will slow it down. Please review the PR to help. ''' _set_status(repostiory, pull_request, 'pending', message)
def execute_synchronize(self): mongo_repository = mongo.db.repositories.find_one( {'full_name': self.data['repository']['full_name']}) # Make sure it is configured if not mongo_repository: return token = os.getenv('GITHUB_USER_TOKEN') github_client = github.Github(token) repository = github_client.get_repo(self.data['repository']['id']) pull_request = repository.get_pull(self.data['pull_request']['number']) pr = PR(repository, pull_request, token) pr.get_contributors() pr.update_contributors_with_reviews() pr.update_votes() pr.get_latest_dates() pr.get_merge_time() pr.set_status() pull_request.create_issue_comment( '''The branch of this pull request was updated so the auto-merge time has been reset. It will be automatically merged by [worlddriven](https://www.worlddriven.org) in {} day(s) and {} hour(s). Check the `worlddriven` status check or the [dashboard]({}) for actual stats. '''.format(pr.days_to_merge.days, pr.days_to_merge.seconds // 3600, pr.url))
def handle_pull_request_review(self, data): print(data) if data['action'] == 'submitted': if 'state' not in data['review']: print('No state') print(data['review'].keys()) return {'error': 'No state'}, 503 if data['review']['state'] == 'commented': print('Review comment') return {'info': 'Only commented'} token = os.getenv('TOKEN') github_client = github.Github(token) repository = github_client.get_repo(data['repository']['id']) pull_request = repository.get_pull(data['pull_request']['number']) pr = PR(repository, pull_request) pr.get_contributors() pr.update_contributors_with_reviews() review = data['review'] reviewer = review['user']['login'] if reviewer not in pr.contributors: pr.contributors[reviewer] = {'name': reviewer, 'review_date': review['submitted_at']} value = 0 if review['state'] == 'APPROVED': value = 1 elif review['state'] == 'CHANGES_REQUESTED': value = -1 pr.contributors[reviewer]['review_value'] = value pr.update_votes() pr.get_latest_dates() pr.get_merge_time() message = '''A new review, yeah. Votes: {}/{} Coefficient: {} Merging in {} days {} hours Age {} days {} hours'''.format(pr.votes, pr.votes_total, pr.coefficient, pr.days_to_merge.days, pr.days_to_merge.seconds / 3600, pr.age.days, pr.age.seconds / 3600) print(message) status_message = '{}/{} {} Merge in {} days {}'.format(pr.votes, pr.votes_total, round(pr.coefficient, 3) * 100, pr.days_to_merge.days, pr.days_to_merge.seconds / 3600) _set_status(repository, pull_request, 'success', status_message) issue = repository.get_issue(pull_request.number) issue.create_comment(message) return {'info': 'All fine, thanks'}
def show_pull_request(org_name, project_name, pull_request_number): token = os.getenv('TOKEN') github_client = github.Github(token) repository_name = '{}/{}'.format(org_name, project_name) repository = github_client.get_repo(repository_name) pull_request = repository.get_pull(pull_request_number) pr = PR(repository, pull_request) pr.get_contributors() pr.update_contributors_with_reviews() pr.update_votes() pr.get_latest_dates() pr.get_merge_time() contributors = [ pr.contributors[contributor] for contributor in pr.contributors ] print(contributors) for contributor in contributors: if 'commits' not in contributor: contributor['commits'] = 0; contributor['time_value'] = timedelta(days=(contributor['commits'] / float(pr.votes_total)) * pr.total_merge_time) def activeFirst(value): return abs(value['review_value'] + 0.1) * value['commits'] contributors = sorted(contributors, key=activeFirst, reverse=True) return render_template( 'pull_request.html', title=pull_request.title, repository=org_name, project=project_name, pull_request_number=pull_request_number, coefficient=pr.coefficient, votes=pr.votes, votes_total=pr.votes_total, contributors=contributors, max_date=pr.max_date, unlabel_date=pr.unlabel_date, push_date=pr.push_date, commit_date=pr.commit_date, pull_request_date=pr.pull_request_date, age=pr.age, commits=pr.commits, merge_duration=pr.merge_duration, days_to_merge=pr.days_to_merge, total_merge_time=pr.total_merge_time, merge_date=pr.max_date + pr.merge_duration )