def select_reviewer_by_repo_stats(reviewers, ubers, issue): print('select_reviewer_by_repo_stats', reviewers, ubers, issue.author) uri = GITHUB_API_URI + REPO_CONTRIBUTORS_PATH.format(issue.repository) r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: return select_reviewer_by_random(reviewers, ubers, issue) utils.cache_response(r) contributions = 0 contributors = {} for contributor in json.loads(r.text): contributions += contributor['contributions'] contributors[contributor['login']] = contributor['contributions'] if len(contributors) == 0: return select_reviewer_by_random(reviewers, ubers, issue) threshold = contributions / len(contributors) * config.repo_lottery_factor eligible_reviewers = {} for reviewer, score in reviewers.items(): if reviewer not in contributors or reviewer == issue.author: continue if contributors[reviewer] >= threshold: eligible_reviewers[reviewer] = score print(eligible_reviewers) if len(eligible_reviewers) == 0: eligible_reviewers = {u: reviewers[u] for u in ubers} ubers = {} return select_reviewer_by_random(eligible_reviewers, ubers, issue)
def select_reviewer_by_repo_stats(reviewers, ubers, issue): print('select_reviewer_by_repo_stats', reviewers, ubers, issue.author) uri = GITHUB_API_URI + REPO_CONTRIBUTORS_PATH.format(issue.repository) r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: return select_reviewer_by_random(reviewers, ubers, issue) utils.cache_response(r) contributions = 0 contributors = {} for contributor in json.loads(r.text): contributions += contributor['contributions'] contributors[contributor['login']] = contributor['contributions'] if len(contributors) == 0: return select_reviewer_by_random(reviewers, ubers, issue) threshold = contributions / len(contributors) * config.repo_lottery_factor eligible_reviewers = {} for reviewer, score in reviewers.items(): if reviewer not in contributors or reviewer == issue.author: continue if contributors[reviewer] >= threshold: eligible_reviewers[reviewer] = score print(eligible_reviewers) if len(eligible_reviewers) == 0: eligible_reviewers = {u: reviewers[u] for u in ubers} ubers = {} return select_reviewer_by_random(eligible_reviewers, ubers, issue)
def current_user_name(): uri = GITHUB_API_URI + USER_PATH r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) user = json.loads(r.text) return user['login'] if user else None
def update_teams(self): uri = GITHUB_API_URI + REPO_TEAMS_PATH.format(self._name) r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) if r.status_code != 200 and r.status_code != 304: denied_repositories[self._name] = time.time() utils.remove_response_from_cache(uri) self._teams = [] return if r.status_code == 200: utils.cache_response(r) self._teams = list(map(lambda r: r['name'], json.loads(r.text)))
def update_teams(self): uri = GITHUB_API_URI + REPO_TEAMS_PATH.format(self._name) r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) if r.status_code != 200 and r.status_code != 304: denied_repositories[self._name] = time.time() utils.remove_response_from_cache(uri) self._teams = [] return if r.status_code == 200: utils.cache_response(r) self._teams = list(map(lambda r: r['name'], json.loads(r.text)))
def current_user_name(): uri = GITHUB_API_URI + USER_PATH r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) user = json.loads(r.text) return user['login'] if user else None
def team_repositories(team_id): uri = GITHUB_API_URI + TEAM_REPOS_PATH.format(team_id) all_repos = [] while uri is not None: r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) all_repos += json.loads(r.text) uri = utils.next_page_url(r) return map(lambda r: r['full_name'], filter(lambda r: not r['fork'], all_repos))
def team_members(team_id): uri = GITHUB_API_URI + TEAM_MEMBERS_PATH.format(team_id) all_users = [] while uri is not None: r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) all_users += json.loads(r.text) uri = utils.next_page_url(r) current_user = users.current_user_name() return map(lambda u: u['login'], filter(lambda u: u['login'] != current_user, all_users))
def find_team_by_name(team_name): uri = GITHUB_API_URI + USER_TEAMS_PATH while uri is not None: r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) for team in json.loads(r.text): if team_name == team['name']: return team['id'] uri = utils.next_page_url(r) return None
def team_repositories(team_id): uri = GITHUB_API_URI + TEAM_REPOS_PATH.format(team_id) all_repos = [] while uri is not None: r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) all_repos += json.loads(r.text) uri = utils.next_page_url(r) return map(lambda r: r['full_name'], filter(lambda r: not r['fork'], all_repos))
def find_team_by_name(team_name): uri = GITHUB_API_URI + USER_TEAMS_PATH while uri is not None: r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) for team in json.loads(r.text): if team_name == team['name']: return team['id'] uri = utils.next_page_url(r) return None
def create_labels_if_needed(repository): labels_uri = GITHUB_API_URI + LABELS_PATH.format(repository) all_labels = [] while labels_uri is not None: r = requests.get(labels_uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(labels_uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return False utils.cache_response(r) all_labels += json.loads(r.text) labels_uri = utils.next_page_url(r) labels_uri = GITHUB_API_URI + LABELS_PATH.format(repository) in_review_label_found = False reviewed_label_found = False for label in all_labels: label_name = label['name'] if label_name == IN_REVIEW_LABEL: in_review_label_found = True elif label_name == REVIEWED_LABEL: reviewed_label_found = True if in_review_label_found and reviewed_label_found: break if not in_review_label_found: label_data = {'name': IN_REVIEW_LABEL, 'color': 'eb6420'} r = requests.post(labels_uri, auth = (config.api_token, 'x-oauth-basic'), data = json.dumps(label_data)) if r.status_code != 201: print('Something went wrong with in review label', r.status_code) return False if not reviewed_label_found: label_data = {'name': REVIEWED_LABEL, 'color': '00aa00'} r = requests.post(labels_uri, auth = (config.api_token, 'x-oauth-basic'), data = json.dumps(label_data)) if r.status_code != 201: print('Something went wrong with reviewed label', r.status_code) return False return True
def team_members(team_id): uri = GITHUB_API_URI + TEAM_MEMBERS_PATH.format(team_id) all_users = [] while uri is not None: r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return None utils.cache_response(r) all_users += json.loads(r.text) uri = utils.next_page_url(r) current_user = users.current_user_name() return map(lambda u: u['login'], filter(lambda u: u['login'] != current_user, all_users))
def issue_contains_review_done_comment(issue): if issue.comments_count == 0: return False comments_headers = {} uri = GITHUB_API_URI + COMMENTS_PATH.format(issue.repository, issue.number) try: r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) except requests.exceptions.RequestException: return False if r.status_code == 304: r = utils.cached_response(uri) if r.status_code == 200: utils.cache_response(r) for comment in json.loads(r.text): if comment['user']['login'] == issue.assignee and comment['body'].strip() == REVIEW_DONE_COMMENT: return True else: print('Something went wrong', r.status_code) return False
def issue_contains_review_done_comment(issue): if issue.comments_count == 0: return False comments_headers = {} uri = GITHUB_API_URI + COMMENTS_PATH.format(issue.repository, issue.number) try: r = requests.get(uri, auth=(config.api_token, 'x-oauth-basic'), headers=utils.caching_request_headers(uri)) except requests.exceptions.RequestException: return False if r.status_code == 304: r = utils.cached_response(uri) if r.status_code == 200: utils.cache_response(r) for comment in json.loads(r.text): if comment['user']['login'] == issue.assignee and comment[ 'body'].strip() == REVIEW_DONE_COMMENT: return True else: print('Something went wrong', r.status_code) return False
def fetch_opened_pull_requests(): uri = GITHUB_API_URI + (SUBSCRIBED_ISSUES_PATH if config.only_subscribed_issues else ALL_ISSUES_PATH) all_issues = [] while uri is not None: try: r = requests.get(uri, auth = (config.api_token, 'x-oauth-basic'), headers = utils.caching_request_headers(uri)) except requests.exceptions.RequestException: return [] if r.status_code == 304: r = utils.cached_response(uri) if r.status_code != 200: print('Something went wrong', r.status_code) return [] utils.cache_response(r) all_issues += json.loads(r.text) uri = utils.next_page_url(r) issues_filler = lambda issue: Issue(issue) filtered_issues = filter(lambda issue: issue['state'] == 'open' and 'pull_request' in issue and not issue['repository']['fork'], all_issues) return map(issues_filler, filtered_issues)