def test_process_github_repos_without_annotating_commit_ids(
         self, github_mock, org_mock, pag_mock):
     # given
     scanner = GitHubScanner('url',
                             'token',
                             'org', [],
                             False,
                             annotate_latest_commit_id=False)
     repos = assemble_repos()
     create_mocks(github_mock, org_mock, pag_mock, repos)
     scanner._gh = github_mock
     # when
     findings = scanner._process_repos(None, None)
     # then
     org_mock.get_repos.assert_called_with(type='all',
                                           sort='pushed',
                                           direction='asc')
     self.assertEqual(6,
                      len(findings),
                      msg='There should be exactly 6 findings')
     self.assertFalse(findings[0]["attributes"]["archived"])
     self.assertFalse(findings[1]["attributes"]["archived"])
     self.assertTrue(findings[2]["attributes"]["archived"])
     self.assertFalse(findings[3]["attributes"]["archived"])
     self.assertFalse(findings[4]["attributes"]["archived"])
     self.assertTrue(findings[5]["attributes"]["archived"])
     for finding in findings:
         self.assertEqual(finding['name'],
                          'GitHub Repo',
                          msg=self.wrong_output_msg)
         self.assertFalse("last_commit_id" in finding['attributes'])
示例#2
0
 def test_process_github_repos_with_ignore_repos(self, github_mock, org_mock, pag_mock):
     # given
     scanner = GitHubScanner('url', 'token', 'org', [1], False)
     repos = assemble_repos()
     create_mocks(github_mock, org_mock, pag_mock, repos)
     scanner._gh = github_mock
     # when
     findings = scanner._process_repos(None, None)
     # then
     github_mock.get_organization.assert_called_with('org')
     self.assertEqual(4, len(findings), msg='There should be exactly 4 findings')
示例#3
0
 def test_process_github_repos_with_no_ignore_list(self, github_mock, org_mock, pag_mock):
     # given
     scanner = GitHubScanner('url', 'token', 'org', [], False)
     repos = assemble_repos()
     create_mocks(github_mock, org_mock, pag_mock, repos)
     scanner._gh = github_mock
     # when
     findings = scanner._process_repos(None, None)
     # then
     org_mock.get_repos.assert_called_with(type='all', sort='pushed', direction='asc')
     self.assertEqual(6, len(findings), msg='There should be exactly 6 findings')
     for finding in findings:
         self.assertEqual(finding['name'], 'GitHub Repo', msg=self.wrong_output_msg)
示例#4
0
 def test_setup_github_with_url_and_no_token_should_exit(self):
     # when
     with self.assertRaises(argparse.ArgumentError) as cm:
         GitHubScanner('url', None, 'org', [])
     # then
     self.assertEqual(cm.exception.args[1], 'Access token required for GitHab connection.',
                      msg='Process should exit')
示例#5
0
def process(args):
    scanner: AbstractScanner

    if args.git_type == 'gitlab':
        scanner = GitLabScanner(
            url=args.url,
            access_token=args.access_token,
            group=args.group,
            ignored_groups=args.ignore_groups,
            ignore_repos=args.ignore_repos,
            obey_rate_limit=args.obey_rate_limit
        )
    elif args.git_type == 'github':
        scanner = GitHubScanner(
            url=args.url,
            access_token=args.access_token,
            organization=args.organization,
            ignore_repos=args.ignore_repos,
            obey_rate_limit=args.obey_rate_limit
        )
    else:
        logger.info('Argument error: Unknown git type')
        sys.exit(1)

    try:
        return scanner.process(
            args.activity_since_duration,
            args.activity_until_duration
        )
    except argparse.ArgumentError as e:
        logger.error(f'Argument error: {e}')
        sys.exit(1)
    except gitlab.exceptions.GitlabAuthenticationError:
        logger.info('No permission. Check your access token.')
        sys.exit(1)
    except github.GithubException as e:
        logger.error(f'Github API Exception: {e.status} -> {e.data["message"]}')
        sys.exit(2)
    except gitlab.GitlabError as e:
        logger.error(f'Gitlab API Exception: {e}')
        sys.exit(2)
    except Exception as e:
        logger.error(f'Unexpected error: {e}')
        sys.exit(3)