def test_get_issues_for_org_merges_issues_pull_requests(github, requests_mock): """ The '_get_issues_for_org' helper should merge both issues with pull requests and treat them both as issues """ repo1 = fixtures.repository( issues=[fixtures.issue(), fixtures.issue(), fixtures.issue()], pull_requests=[fixtures.issue(pr=True)], private=False ) repo2 = fixtures.repository( issues=[fixtures.issue(), fixtures.issue()], pull_requests=[fixtures.issue(pr=True), fixtures.issue(pr=True)], private=False ) expected_titles_are_pr = {} for repo in [repo1, repo2]: for issue in repo['issues']['nodes']: expected_titles_are_pr[issue['title']] = False for pull_request in repo['pullRequests']['nodes']: expected_titles_are_pr[pull_request['title']] = True api_response_body = fixtures.api_response_body(repos=[repo1, repo2]) requests_mock.add_github_graphql(json=api_response_body) issues = github._get_issues_for_org(requests.Session(), 'org') titles_are_pr = {i['title']: i['is_pull_request'] for i in issues} assert titles_are_pr == expected_titles_are_pr
def test_format_issue_labels(github): """The '_format_issue' helper should be able to process labels""" issue = fixtures.issue(labels=[{'name': 'bug'}, {'name': 'feature'}]) formatted_issue = github._format_issue('org', fixtures.repository(), issue) assert formatted_issue['labels'] == ['bug', 'feature'] assert formatted_issue['coach'] is False
def test_sort_issues_activity(github): """ Issues with most user activity should go first if there's no 'coach' issue and no reactions. User activity is comments count + participants count """ issue1 = fixtures.issue(labels=[], reactions_counts={}) issue1['comments']['totalCount'] = 3 issue1['participants']['totalCount'] = 2 issue2 = fixtures.issue(labels=[], reactions_counts={}) issue2['comments']['totalCount'] = 1 issue2['participants']['totalCount'] = 9 issue3 = fixtures.issue(labels=[], reactions_counts={}) issue3['comments']['totalCount'] = 0 issue3['participants']['totalCount'] = 4 repository = fixtures.repository() sorted_issues = github._sort_issues([ github._format_issue('org', repository, issue) for issue in [issue1, issue2, issue3] ]) assert sorted_issues[0]['comments'] == 1 # + 9 = 10 assert sorted_issues[1]['comments'] == 3 # + 2 = 7 assert sorted_issues[2]['comments'] == 0 # + 4 = 4
def test_format_issue_special_labels(github, label_name): """ The '_format_issue' helper should be able to process the 'coach' label and to mark the resulting formatted issue with the 'coach' flag accordingly """ issue = fixtures.issue(labels=[{'name': 'bug'}, {'name': label_name}]) formatted_issue = github._format_issue('org', fixtures.repository(), issue) assert formatted_issue['labels'] == ['bug', label_name] assert formatted_issue[label_name] is True
def test_get_issues_merges_orgs(github, requests_mock): """ Tests whether 'get_issues' makes two HTTP requests for each given GitHub organization name and whether both data gets combined in the results """ issue_org1 = fixtures.issue() requests_mock.add_github_graphql(json=fixtures.api_response_body(repos=[ fixtures.repository( issues=[issue_org1], pull_requests=[], private=False), ]), ) issue_org2 = fixtures.issue() requests_mock.add_github_graphql(json=fixtures.api_response_body(repos=[ fixtures.repository( issues=[issue_org2], pull_requests=[], private=False), ]), ) issues = github.get_issues(['org1', 'org2']) titles = set(i['title'] for i in issues) assert titles == {issue_org1['title'], issue_org2['title']}
def test_format_issue_reactions(github): """ The '_format_issue' helper should be able to calculate 'votes' from reactions """ issue = fixtures.issue(reactions_counts={ 'LAUGH': 42, 'HEART': 3, }) formatted_issue = github._format_issue('org', fixtures.repository(), issue) assert formatted_issue['votes'] == 42 + 3
def test_format_issue_missing_author(github): """ The '_format_issue' helper should be able to deal with the situation when the author is deleted/disabled user """ issue = fixtures.issue() issue['author'] = None formatted_issue = github._format_issue('org', fixtures.repository(), issue) assert formatted_issue['user'] == { 'login': None, 'html_url': 'https://github.com/ghost', }
def test_sort_issues_votes(github): """ Issues with most votes should go first if there's no 'coach' issue """ repository = fixtures.repository() issues = [github._format_issue('org', repository, issue) for issue in [ fixtures.issue(labels=[], reactions_counts={'THUMBS_UP': 1}), fixtures.issue(labels=[], reactions_counts={'THUMBS_DOWN': 1}), fixtures.issue(labels=[], reactions_counts={'THUMBS_UP': 4}), fixtures.issue(labels=[], reactions_counts={'THUMBS_UP': 3}), ]] sorted_issues = github._sort_issues(issues) assert sorted_issues[0]['votes'] == 4
def test_sort_issues_coach(github): """ Issues with coaching offer should always go first no matter what """ repository = fixtures.repository() issues = [github._format_issue('org', repository, issue) for issue in [ fixtures.issue(labels=[]), fixtures.issue(labels=[{'name': 'foo'}]), fixtures.issue(labels=[{'name': 'bar'}, {'name': 'coach'}]), fixtures.issue(labels=[]), ]] sorted_issues = github._sort_issues(issues) assert sorted_issues[0]['coach'] is True
def test_get_issues_for_org_skips_private(github, requests_mock): """ The '_get_issues_for_org' helper should skip private repositories """ public_repo = fixtures.repository(private=False) expected_titles = set() for issue in public_repo['issues']['nodes']: expected_titles.add(issue['title']) for pull_request in public_repo['pullRequests']['nodes']: expected_titles.add(pull_request['title']) api_response_body = fixtures.api_response_body(repos=[ fixtures.repository(private=True), public_repo, fixtures.repository(private=True), fixtures.repository(private=True), fixtures.repository(private=True), ]) requests_mock.add_github_graphql(json=api_response_body) issues = github._get_issues_for_org(requests.Session(), 'org') titles = set(i['title'] for i in issues) assert titles == expected_titles
def test_get_issues_merges_orgs(github, requests_mock): """ Tests whether 'get_issues' makes two HTTP requests for each given GitHub organization name and whether both data gets combined in the results """ issue_org1 = fixtures.issue() requests_mock.add_github_graphql( json=fixtures.api_response_body(repos=[ fixtures.repository(issues=[issue_org1], pull_requests=[], private=False), ]), ) issue_org2 = fixtures.issue() requests_mock.add_github_graphql( json=fixtures.api_response_body(repos=[ fixtures.repository(issues=[issue_org2], pull_requests=[], private=False), ]), ) issues = github.get_issues(['org1', 'org2']) titles = set(i['title'] for i in issues) assert titles == {issue_org1['title'], issue_org2['title']}