Exemplo n.º 1
0
class GitHubContentTest(IGittTestCase):
    def setUp(self):
        self.token = GitHubToken(os.environ.get('GITHUB_TEST_TOKEN', ''))
        self.repo = GitHubRepository(self.token, 'gitmate-test-user/test')

    def test_get_content(self):
        file = GitHubContent(self.token,
                             'gitmate-test-user/test',
                             path='README.md')
        self.assertIsNone(file.get_content())

    def test_delete_content(self):
        self.repo.create_file(path='deleteme',
                              message='hello',
                              content='hello',
                              branch='master')
        file = GitHubContent(self.token,
                             'gitmate-test-user/test',
                             path='deleteme')
        self.assertIsNone(file.delete(message='Delete file'))

    def test_update_content(self):
        file = GitHubContent(self.token,
                             'gitmate-test-user/test',
                             path='README.md')
        self.assertIsNone(
            file.update(message='Update README',
                        content='I am a test repo! Updated content!'))
Exemplo n.º 2
0
 def repository(self):
     """
     Returns the GitHub repository this comment was posted in, as a
     GitHubRepository instance.
     """
     from IGitt.GitHub.GitHubRepository import GitHubRepository
     return GitHubRepository(self._token, self._repository)
Exemplo n.º 3
0
 def repository(self) -> GitHubRepository:
     """
     Returns the repository this notification is related to.
     """
     return GitHubRepository.from_data(self.data['repository'],
                                       self._token,
                                       self.data['repository']['full_name'])
Exemplo n.º 4
0
 def repository(self):
     """
     Returns the GitHub repository this issue is linked with as a
     GitHubRepository instance.
     """
     from IGitt.GitHub.GitHubRepository import GitHubRepository
     return GitHubRepository(self._token, self._repository)
Exemplo n.º 5
0
    def _handle_webhook_installation(self, data):
        """Handles 'installation' event."""
        installation = data['installation']
        installation_obj = GitHubInstallation.from_data(
            installation, self._token, installation['id'])
        trigger_event = {
            'created': InstallationActions.CREATED,
            'deleted': InstallationActions.DELETED
        }[data['action']]

        # sender is the user who made this installation and has access to it
        sender = GitHubUser.from_data(data['sender'], self._token,
                                      data['sender']['login'])

        # When a new installation is created, it will be installed on at
        # least one repository which will be forwarded through
        # `repositories` key.
        if 'repositories' in data:
            repos = [
                GitHubRepository.from_data(repo, self._token, repo['id'])
                for repo in data['repositories']
            ]
            yield trigger_event, [installation_obj, sender, repos]
        else:
            yield trigger_event, [installation_obj, sender]
Exemplo n.º 6
0
def get_identifier(repo):
    if repo.provider == Providers.GITHUB.value:
        return GitHubRepository(get_token(repo), repo.full_name).identifier
    elif repo.provider == Providers.GITLAB.value:
        return GitLabRepository(get_token(repo), repo.full_name).identifier

    # Other providers aren't implemented yet.
    raise NotImplementedError
Exemplo n.º 7
0
 def master_repositories(self):
     """
     Retrieves repositories the user has admin access to.
     """
     repo_list = get(self._token, '/user/repos')
     return {
         GitHubRepository.from_data(repo, self._token, repo['full_name'])
         for repo in repo_list if repo['permissions']['admin']
     }
Exemplo n.º 8
0
 def repositories(self) -> Set[GitHubRepository]:
     """
     Returns the set of repositories this installation has access to.
     """
     data = get(self._api_token, '/installation/repositories')
     return {
         GitHubRepository.from_data(repo, self._api_token, repo['id'])
         for repo in data['repositories']
     }
Exemplo n.º 9
0
    def repositories(self) -> Set[Repository]:
        """
        Returns the list of repositories contained in this organization.
        """
        from IGitt.GitHub.GitHubRepository import GitHubRepository

        return {
            GitHubRepository.from_data(repo, self._token, repo['id'])
            for repo in get(self._token, self._url + '/repos')
        }
Exemplo n.º 10
0
def handle_github_open_issue(issue: dict,
                             repository: dict) -> None:  # pragma: no cover
    """Will handle with care."""
    _LOGGER.info(f"An Issue has been opened: {issue['url']}")

    if issue["title"].startswith("Automatic update of dependency"):
        _LOGGER.info(
            f"{issue['url']} is an 'automatic update of dependencies', not sending notification"
        )
        return

    if issue["title"].startswith("Initial dependency lock"):
        _LOGGER.info(
            f"{issue['url']} is an 'automatic dependency lock', not sending notification"
        )
        return

    if issue["title"].startswith(
            "Failed to update dependencies to their latest version"):
        _LOGGER.info(
            f"{issue['url']} is an 'failed to update dependencies', not sending notification"
        )
        return

    notify_channel(
        "new_issue",
        f"{issue['user']['login']} just opened an issue: {issue['title']}... 🚨",
        issue["html_url"])

    analysis = analyse_github_issue(issue)

    if "flake" in analysis["status"].keys():
        _LOGGER.debug(
            f"{issue['number']} seems to be a flake: {analysis['status']['reason']}"
        )

        repo = GitHubRepository(GitHubToken(_SESHETA_GITHUB_ACCESS_TOKEN),
                                repository["full_name"])

        try:
            repo.create_label("flake", "#f3ccff")
            repo.create_label("human_intervention_required", "#f3ccff")
            repo.create_label("potential_flake", "#f3ccff")
        except IGitt.ElementAlreadyExistsError as excptn:
            _LOGGER.error(excptn)

        igitt_issue = GitHubIssue(GitHubToken(_SESHETA_GITHUB_ACCESS_TOKEN),
                                  repository["full_name"], issue["number"])
        labels = igitt_issue.labels
        labels.add("human_intervention_required")
        labels.add("potential_flake")
        igitt_issue.labels = labels
Exemplo n.º 11
0
    def installed_repositories(self, installation_id: int):
        """
        List repositories that are accessible to the authenticated user for an
        installation.
        """
        # Don't move to module code, causes circular dependencies
        from IGitt.GitHub.GitHubRepository import GitHubRepository

        repos = get(self._token, '/user/installations/{}/repositories'.format(
            installation_id), headers=PREVIEW_HEADER)['repositories']
        return {GitHubRepository.from_data(repo, self._token, repo['id'])
                for repo in repos}
Exemplo n.º 12
0
    def igitt_repo(self) -> IGittRepository:
        """
        Returns an IGitt Repository object from Repository model.
        """
        if self.provider == Providers.GITHUB.value:
            return GitHubRepository(self.token,
                                    self.identifier or self.full_name)
        if self.provider == Providers.GITLAB.value:
            return GitLabRepository(self.token,
                                    self.identifier or self.full_name)

        # Other providers aren't implemented yet.
        raise NotImplementedError
Exemplo n.º 13
0
    def get_repo(self, repository) -> GitHubRepository:
        """
        Retrieve a given repository.

        >>> from os import environ
        >>> github = GitHub(environ['GITHUB_TEST_TOKEN'])
        >>> repo = github.get_repo('gitmate-test-user/test')
        >>> isinstance(repo, GitHubRepository)
        True

        :return: A repository object.
        """
        return GitHubRepository(self._token, repository)
Exemplo n.º 14
0
    def repository(self):
        """
        Retrieves the repository that holds this commit.

        >>> from os import environ
        >>> commit = GitHubCommit(GithubToken(environ['GITHUB_TEST_TOKEN']),
        ...                       'gitmate-test-user/test', '3fc4b86')
        >>> commit.repository.full_name
        'gitmate-test-user/test'

        :return: A usable Repository instance.
        """
        return GitHubRepository(self._token, self._repository)
Exemplo n.º 15
0
    def test_clone_url(self):
        self.assertEqual(
            self.repo.clone_url,
            'https://{}@github.com/gitmate-test-user/test.git'.format(
                os.environ.get('GITHUB_TEST_TOKEN', '')))

        # testing GitHub installation token
        jwt = GitHubJsonWebToken(os.environ['GITHUB_PRIVATE_KEY'],
                                 int(os.environ['GITHUB_TEST_APP_ID']))
        itoken = GitHubInstallationToken(60731, jwt)
        repo = GitHubRepository(itoken, 'gitmate-test-user/test')
        self.assertRegex(
            repo.clone_url,
            r'https://*****:*****@github.com/gitmate-test-user/test.git'
        )
Exemplo n.º 16
0
    def _handle_webhook_installation_repositories(self, data):
        """Handles 'installation_repositories' event."""
        installation = data['installation']
        installation_obj = GitHubInstallation.from_data(
            installation, self._token, installation['id'])

        # sender is the user who has access to this installation
        sender = GitHubUser.from_data(data['sender'], self._token,
                                      data['sender']['login'])

        if data['action'] == 'added':
            trigger_event = InstallationActions.REPOSITORIES_ADDED
            repos = [
                GitHubRepository.from_data(repo, self._token, repo['id'])
                for repo in data['repositories_added']
            ]
        elif data['action'] == 'removed':
            trigger_event = InstallationActions.REPOSITORIES_REMOVED
            repos = [
                GitHubRepository.from_data(repo, self._token, repo['id'])
                for repo in data['repositories_removed']
            ]

        yield trigger_event, [installation_obj, sender, repos]
Exemplo n.º 17
0
    def write_repositories(self):
        """
        Retrieves the full names of repositories this user can write to.

        >>> from os import environ
        >>> github = GitHub(GitHubToken(environ['GITHUB_TEST_TOKEN']))
        >>> sorted(map(lambda x: x.full_name, github.write_repositories))
        ['gitmate-test-user/test', 'sils/gitmate-test']

        :return: A set of strings.
        """
        repo_list = get(self._token, '/user/repos')
        return {
            GitHubRepository.from_data(repo, self._token, repo['full_name'])
            for repo in repo_list if repo['permissions']['push']
        }
Exemplo n.º 18
0
    def owned_repositories(self):
        """
        Retrieves repositories owned by the authenticated user.

        >>> from os import environ
        >>> github = GitHub(GitHubToken(environ['GITHUB_TEST_TOKEN']))
        >>> sorted(map(lambda x: x.full_name, github.owned_repositories))
        ['gitmate-test-user/test']

        :return: A set of full repository names.
        """
        repo_list = get(self._token, '/user/repos', {'affiliation': 'owner'})
        return {
            GitHubRepository.from_data(repo, self._token, repo['full_name'])
            for repo in repo_list
        }
Exemplo n.º 19
0
    def __init__(self, service_type: ServiceType, service_url: str, token: str, slug: str):
        """Initialize source code management tools abstraction.

        Note that we are using IGitt for calls. IGitt keeps URL to services in its global context per GitHub/GitLab.
        This is global context is initialized in the manager with a hope to fix this behavior for our needs.
        """
        self.service_type = service_type
        self.slug = slug
        self.service_url = service_url
        self.token = token

        if self.service_type == ServiceType.GITHUB:
            self.repository = GitHubRepository(token=GitHubToken(token), repository=slug)
        elif self.service_type == ServiceType.GITLAB:
            self.repository = GitLabRepository(token=GitLabPrivateToken(token), repository=slug)
        else:
            raise NotImplementedError
Exemplo n.º 20
0
def unassigned_issues_activity_json(request):
    try:
        GH_TOKEN = get_api_key('GH')
    except Exception:
        return HttpResponse('[]')
    org_name = get_org_name()
    org_repo_name = org_name
    # Here 'org_repo_name' is the name of repository of a organization.
    # (assumed here, that name of repository is same as the organization name.)
    # But you can change 'org_repo_name' as per your requirement.
    repo_name = org_name + '/' + org_repo_name
    # 'repo_name' is a full name of repository e.g. `fossasia/susi_server`
    # which further used for query (assuming here 'org_name' is different from
    # 'org_repo_name')
    repo = GitHubRepository(GitHubToken(GH_TOKEN), repo_name)
    mr_requests = repo.merge_requests
    final_list = run(mr_requests)
    return HttpResponse(json.dumps(final_list))
Exemplo n.º 21
0
 def test_commits(self):
     self.assertEqual({commit.sha
                       for commit in self.repo.commits}, {
                           '645961c0841a84c1dd2a58535aa70ad45be48c46',
                           'ff46852ae2afccfe12602420491b9b9e37cd210e',
                           '68c8743ffa9a78b31f770b6412aacb48df943c2c',
                           '27576735be020d37c015637c89a356be47cf30dd',
                           'f7e962c0066f7c7600e3a9544bc72e0dc1bfdf02',
                           'aca50e03cbd9e7285a5cf2b09a679505795a9de3',
                           'e5bf3396f339e5a8da2304ddc141c5e09c6de9a0',
                           '40a1c10f1911ccbc00aee00b35b7f398182c59b5',
                           'd9dab485d405734034508bd71af7701166702201',
                           'f78fa380ddc11504a55d16bbb1578e6e1ee3bfef',
                           '161b186a5b341e5129d7d01ef5d12b4086717d63',
                           '703c4badc774c9659a3909e203b2da96c97b44fc',
                           '500050498474c746349ccb0ac8972e77813d2e9b',
                           '674498fd415cfadc35c5eb28b8951e800f357c6f'
                       })
     repo = GitHubRepository(self.token, 'gitmate-test-user/empty')
     self.assertEqual(repo.commits, set())
Exemplo n.º 22
0
    def test_github_conditional_request():
        token = GitHubToken(os.environ.get('GITHUB_TEST_TOKEN', ''))
        repo = GitHubRepository(
            token, os.environ.get('GITHUB_TEST_REPO',
                                  'gitmate-test-user/test'))

        repo.refresh()
        prev_data = repo.data._data
        prev_count = _RESPONSES[repo.url].headers.get('X-RateLimit-Remaining')

        repo.refresh()
        new_data = repo.data._data
        new_count = _RESPONSES[repo.url].headers.get('X-RateLimit-Remaining')

        # check that no reduction in rate limit is observed
        assert prev_count == new_count

        # check that response data hasn't been modified
        assert prev_data == new_data
Exemplo n.º 23
0
 def test_id_init(self):
     repo = GitHubRepository(self.token, 49558751)
     self.assertEqual(repo.full_name, 'gitmate-test-user/test')
Exemplo n.º 24
0
 def test_parent(self):
     repo = GitHubRepository(self.token, 'nkprince007/test')
     self.assertEqual(repo.parent.full_name, 'gitmate-test-user/test')
     self.assertEqual(repo.parent.parent, None)
Exemplo n.º 25
0
 def setUp(self):
     self.token = GitHubToken(os.environ.get('GITHUB_TEST_TOKEN', ''))
     fork_token = GitHubToken(os.environ.get('GITHUB_TEST_TOKEN_2', ''))
     self.repo = GitHubRepository(self.token, 'gitmate-test-user/test')
     self.fork_repo = GitHubRepository(fork_token, 'gitmate-test-user/test')
Exemplo n.º 26
0
class GitHubRepositoryTest(IGittTestCase):
    def setUp(self):
        self.token = GitHubToken(os.environ.get('GITHUB_TEST_TOKEN', ''))
        fork_token = GitHubToken(os.environ.get('GITHUB_TEST_TOKEN_2', ''))
        self.repo = GitHubRepository(self.token, 'gitmate-test-user/test')
        self.fork_repo = GitHubRepository(fork_token, 'gitmate-test-user/test')

    def test_id(self):
        self.assertEqual(self.repo.identifier, 49558751)

    def test_id_init(self):
        repo = GitHubRepository(self.token, 49558751)
        self.assertEqual(repo.full_name, 'gitmate-test-user/test')

    def test_top_level_org(self):
        self.assertEqual(self.repo.top_level_org.name, 'gitmate-test-user')

    def test_hoster(self):
        self.assertEqual(self.repo.hoster, 'github')

    def test_full_name(self):
        self.assertEqual(self.repo.full_name, 'gitmate-test-user/test')

    def test_clone_url(self):
        self.assertEqual(
            self.repo.clone_url,
            'https://{}@github.com/gitmate-test-user/test.git'.format(
                os.environ.get('GITHUB_TEST_TOKEN', '')))

        # testing GitHub installation token
        jwt = GitHubJsonWebToken(os.environ['GITHUB_PRIVATE_KEY'],
                                 int(os.environ['GITHUB_TEST_APP_ID']))
        itoken = GitHubInstallationToken(60731, jwt)
        repo = GitHubRepository(itoken, 'gitmate-test-user/test')
        self.assertRegex(
            repo.clone_url,
            r'https://*****:*****@github.com/gitmate-test-user/test.git'
        )

    def test_get_labels(self):
        self.assertEqual(sorted(self.repo.get_labels()),
                         ['a', 'b', 'c', 'dem'])

    def test_labels(self):
        with self.assertRaises(ElementAlreadyExistsError):
            self.repo.create_label('a', '000000')

        with self.assertRaises(ElementDoesntExistError):
            self.repo.delete_label('f')

        self.repo.create_label('bug', '000000')
        self.assertEqual(sorted(self.repo.get_labels()),
                         ['a', 'b', 'bug', 'c', 'dem'])
        self.repo.delete_label('bug')
        self.assertEqual(sorted(self.repo.get_labels()),
                         ['a', 'b', 'c', 'dem'])

    def test_get_issue(self):
        self.assertEqual(self.repo.get_issue(1).title, 'test issue')

    def test_get_mr(self):
        self.assertEqual(self.repo.get_mr(11).title, 'testpr closing/opening')

    def test_issues(self):
        self.assertEqual(len(self.repo.issues), 89)

    def test_merge_requests(self):
        self.assertEqual(len(self.repo.merge_requests), 18)

    def test_create_issue(self):
        self.assertEqual(
            self.repo.create_issue('title', 'body').title, 'title')

    def test_hooks(self):
        self.repo.register_hook('http://some.url/in/the/world',
                                '...',
                                events={WebhookEvents.PUSH})
        self.assertIn('http://some.url/in/the/world', self.repo.hooks)
        # won't register again
        self.repo.register_hook('http://some.url/in/the/world')
        self.repo.delete_hook('http://some.url/in/the/world')
        self.assertNotIn('http://some.url/in/the/world', self.repo.hooks)
        # events not specified, register all
        self.repo.register_hook('http://some.url/in/the/world')
        self.assertIn('http://some.url/in/the/world', self.repo.hooks)
        self.repo.delete_hook('http://some.url/in/the/world')

    def test_create_fork(self):
        self.assertIsInstance(self.fork_repo.create_fork(), GitHubRepository)

    def test_repo_delete(self):
        fork = self.fork_repo.create_fork()
        self.assertIsNone(fork.delete())

    def test_create_mr(self):
        fork = self.fork_repo.create_fork()
        try:
            fork.create_file('.coafile', 'Hello', 'Hello', 'master')
        except RuntimeError:
            fork.delete()
            fork = self.fork_repo.create_fork()
            fork.create_file('.coafile', 'Hello', 'Hello', 'master')
        self.assertIsInstance(
            self.fork_repo.create_merge_request(
                'add', head='gitmate-test-user-2:master', base='master'),
            GitHubMergeRequest)

    def test_create_file(self):
        fork = self.fork_repo.create_fork()
        try:
            file = fork.create_file('.coafile', 'Hello', 'Hello', 'master')
        except RuntimeError:
            fork.delete()
            fork = self.fork_repo.create_fork()
            file = fork.create_file('.coafile', 'Hello', 'Hello', 'master')

        self.assertIsInstance(file, GitHubContent)

    def test_search_issues(self):
        date = datetime(2017, 6, 17).date()
        issues = [
            issue for issue in self.repo.search_issues(created_before=date,
                                                       state=IssueStates.OPEN)
        ]
        self.assertEqual(len(issues), 75)
        issues = [
            issue for issue in self.repo.search_issues(created_after=date,
                                                       state=IssueStates.OPEN)
        ]
        self.assertEqual(len(issues), 16)
        issues = [
            issue
            for issue in self.repo.search_issues(created_before=date,
                                                 state=IssueStates.CLOSED)
        ]
        self.assertEqual(len(issues), 12)
        with self.assertRaises(RuntimeError):
            next(
                self.repo.search_issues(created_before=date,
                                        created_after=date))

    def test_search_mrs(self):
        date = datetime(2016, 1, 25).date()
        mrs = [
            mr for mr in self.repo.search_mrs(created_before=date,
                                              state=MergeRequestStates.OPEN)
        ]
        self.assertEqual(len(mrs), 2)
        mrs = [
            mr for mr in self.repo.search_mrs(created_after=date,
                                              state=MergeRequestStates.OPEN)
        ]
        self.assertEqual(len(mrs), 17)
        mrs = [
            mr for mr in self.repo.search_mrs(created_before=date,
                                              state=MergeRequestStates.CLOSED)
        ]
        self.assertEqual(len(mrs), 1)
        date = datetime(2017, 6, 18).date()
        mrs = [
            mr for mr in self.repo.search_mrs(updated_after=date,
                                              state=MergeRequestStates.OPEN)
        ]
        self.assertEqual(len(mrs), 17)
        mrs = [
            mr for mr in self.repo.search_mrs(updated_before=date,
                                              state=MergeRequestStates.OPEN)
        ]
        self.assertEqual(len(mrs), 2)
        date = datetime(2017, 12, 31).date()
        mrs = [
            mr for mr in self.repo.search_mrs(updated_before=date,
                                              state=MergeRequestStates.MERGED)
        ]
        self.assertEqual(len(mrs), 1)

    def test_commits(self):
        self.assertEqual({commit.sha
                          for commit in self.repo.commits}, {
                              '645961c0841a84c1dd2a58535aa70ad45be48c46',
                              'ff46852ae2afccfe12602420491b9b9e37cd210e',
                              '68c8743ffa9a78b31f770b6412aacb48df943c2c',
                              '27576735be020d37c015637c89a356be47cf30dd',
                              'f7e962c0066f7c7600e3a9544bc72e0dc1bfdf02',
                              'aca50e03cbd9e7285a5cf2b09a679505795a9de3',
                              'e5bf3396f339e5a8da2304ddc141c5e09c6de9a0',
                              '40a1c10f1911ccbc00aee00b35b7f398182c59b5',
                              'd9dab485d405734034508bd71af7701166702201',
                              'f78fa380ddc11504a55d16bbb1578e6e1ee3bfef',
                              '161b186a5b341e5129d7d01ef5d12b4086717d63',
                              '703c4badc774c9659a3909e203b2da96c97b44fc',
                              '500050498474c746349ccb0ac8972e77813d2e9b',
                              '674498fd415cfadc35c5eb28b8951e800f357c6f'
                          })
        repo = GitHubRepository(self.token, 'gitmate-test-user/empty')
        self.assertEqual(repo.commits, set())

    def test_get_permission_level(self):
        sils = GitHubUser(self.token, 'sils')
        user = GitHubUser(self.token)
        nkprince007 = GitHubUser(self.token, 'nkprince007')

        self.assertEqual(self.repo.get_permission_level(sils),
                         AccessLevel.CAN_WRITE)
        self.assertEqual(self.repo.get_permission_level(user),
                         AccessLevel.ADMIN)
        self.assertEqual(self.repo.get_permission_level(nkprince007),
                         AccessLevel.CAN_READ)

    def test_parent(self):
        repo = GitHubRepository(self.token, 'nkprince007/test')
        self.assertEqual(repo.parent.full_name, 'gitmate-test-user/test')
        self.assertEqual(repo.parent.parent, None)