Ejemplo n.º 1
0
 def _members(self, access_level):
     try:
         return {
             GitLabUser.from_data(user, self._token, user['id'])
             for user in self.raw_members()
             if user['access_level'] >= access_level
         }
     except RuntimeError:
         return {
             GitLabUser.from_data({'username': self.name},
                                  self._token,
                                  identifier=None)
         }
Ejemplo n.º 2
0
    def test_assignees(self):
        # test merge request with no assignees
        mr = GitLabMergeRequest(self.token, 'gitmate-test-user/test', 25)
        self.assertEqual(mr.assignees, set())

        sils = GitLabUser(self.token, 104269)
        user = GitLabUser(self.token, 707601)
        with self.assertRaises(NotImplementedError):
            self.mr.assignees = {user, sils}
        self.mr.assignees = {user}
        self.assertEqual(self.mr.assignees, {user})
        self.mr.assignees = set()
        self.assertEqual(self.mr.assignees, set())
Ejemplo n.º 3
0
    def test_get_permission_level(self):
        sils = GitLabUser(self.token, 104269)
        user = GitLabUser(self.token)
        meetmangukiya = GitLabUser(self.token, 707601)
        noman = GitLabUser(self.token, 1)

        self.assertEqual(self.repo.get_permission_level(sils),
                         AccessLevel.ADMIN)
        self.assertEqual(self.repo.get_permission_level(user),
                         AccessLevel.ADMIN)
        self.assertEqual(self.repo.get_permission_level(meetmangukiya),
                         AccessLevel.CAN_WRITE)
        self.assertEqual(self.repo.get_permission_level(noman),
                         AccessLevel.CAN_VIEW)
Ejemplo n.º 4
0
 def test_gitlab_unack(
         self, m_set_status, m_get_statuses, m_sha, m_get_perms, m_author,
         m_body, m_commits
 ):
     m_get_statuses.return_value = (
         CommitStatus(Status.FAILED, 'Terrible issues',
                      'review/gitmate/manual', 'https://gitmate.io'),
         CommitStatus(Status.SUCCESS, 'No issues',
                      'review/somewhere/else', 'https://some/url'))
     m_sha.return_value = 'f6d2b7c66372236a090a2a74df2e47f42a54456b'
     m_get_perms.return_value = AccessLevel.CAN_WRITE
     m_author.return_value = GitLabUser(self.gl_token, 0)
     m_body.return_value = 'unack f6d2b7c'
     m_commits.return_value = tuple([self.gl_commit])
     response = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                                  self.gl_pr_data)
     response = self.simulate_gitlab_webhook_call('Note Hook',
                                                  self.gl_comment_data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     args = sum([list(args) for args, _ in m_set_status.call_args_list], [])
     # 3 calls to be made as follows
     # Status.FAILED review/gitmate/manual/pr
     # Status.FAILED review/gitmate/manual
     # Status.FAILED review/gitmate/manual/pr
     self.assertEqual(m_set_status.call_count, 3)
     self.assertEqual([(arg.status, arg.context) for arg in args],
                      [(Status.FAILED, 'review/gitmate/manual/pr'),
                       (Status.FAILED, 'review/gitmate/manual'),
                       (Status.FAILED, 'review/gitmate/manual/pr')])
Ejemplo n.º 5
0
    def test_gitlab_delete_old_acks(self, m_get_perms, m_author, m_body,
                                    m_set_status, m_commits, m_head):
        # First MR Sync, create value in ack
        m_head.return_value = self.gl_commit
        m_get_perms.return_value = AccessLevel.CAN_WRITE
        m_author.return_value = GitLabUser(self.gl_token, 0)
        m_commits.return_value = tuple([self.gl_commit])
        m_body.return_value = 'unack ' + self.gl_commit.sha
        response = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                                     self.gl_pr_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        response = self.simulate_gitlab_webhook_call('Note Hook',
                                                     self.gl_comment_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        args = sum([list(args) for args, _ in m_set_status.call_args_list], [])
        self.assertEqual([(arg.status, arg.context) for arg in args],
                         [(Status.PENDING, 'review/gitmate/manual'),
                          (Status.PENDING, 'review/gitmate/manual/pr'),
                          (Status.FAILED, 'review/gitmate/manual'),
                          (Status.FAILED, 'review/gitmate/manual/pr')])
        m_set_status.reset_mock()

        # Completely new commit, SHA for old commit should be removed
        new_head = GitLabCommit(self.gl_token, self.gl_repo.full_name,
                                '9ba5b704f5866e468ec2e639fa893ae4c129f2ad')
        m_head.return_value = new_head
        m_commits.return_value = tuple([new_head])
        response = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                                     self.gl_pr_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        args = sum([list(args) for args, _ in m_set_status.call_args_list], [])

        self.assertEqual([(arg.status, arg.context) for arg in args],
                         [(Status.PENDING, 'review/gitmate/manual'),
                          (Status.PENDING, 'review/gitmate/manual/pr')])
Ejemplo n.º 6
0
    def author(self) -> GitLabUser:
        """
        Retrieves the author of the merge request.

        :return: A GitLabUser object.
        """
        return GitLabUser.from_data(self.data['author'], self._token,
                                    self.data['author']['id'])
Ejemplo n.º 7
0
    def test_get_user_if_exists(self):
        # It is none because we never created a database entry for the user:
        # `gitmate-test-user`
        gh_user = GitHubUser(self.repo.token, 'gitmate-test-user')
        self.assertIsNone(get_user_if_exists(gh_user))

        # Mocking the exact entry from database, to get proper return value
        gl_user = GitLabUser(self.repo.token, 2)
        self.assertIsNotNone(get_user_if_exists(gl_user))
Ejemplo n.º 8
0
    def assign(self, issue: Issue, assignees: typing.List[str]) -> None:
        """Assign users (by their accounts) to the given issue."""
        if self.service_type == ServiceType.GITHUB:
            users = (GitHubUser(GitHubToken(self.token), username) for username in assignees)
        elif self.service_type == ServiceType.GITLAB:
            users = (GitLabUser(GitLabPrivateToken(self.token), username) for username in assignees)
        else:
            raise NotImplementedError

        issue.assign(*users)
Ejemplo n.º 9
0
 def test_gitlab_successful_rebase(self, m_get_perm, m_author, m_comment,
                                   m_body):
     m_body.return_value = '@{} rebase'.format(self.repo.user.username)
     m_author.return_value = GitLabUser(self.repo.token, 0)
     m_get_perm.return_value = AccessLevel.CAN_READ
     subprocess.Popen = fake_popen_success
     response = self.simulate_gitlab_webhook_call('Note Hook',
                                                  self.gitlab_data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     m_comment.assert_called_with(
         'Automated rebase with [GitMate.io](https://gitmate.io) was '
         'successful! :tada:')
Ejemplo n.º 10
0
 def test_gitlab_failed_rebase(self, m_get_perm, m_author, m_comment,
                               m_body):
     m_body.return_value = '@{} rebase'.format(self.repo.user.username)
     m_author.return_value = GitLabUser(self.repo.token, 0)
     m_get_perm.return_value = AccessLevel.CAN_READ
     subprocess.Popen = fake_popen_failure
     response = self.simulate_gitlab_webhook_call('Note Hook',
                                                  self.gitlab_data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     m_comment.assert_called_with('Automated rebase failed! Please rebase '
                                  'your pull request manually via the '
                                  'command line.\n\nReason:\n'
                                  '```\nCommand \'[\'git\', \'rebase\', '
                                  '\'master\']\' returned non-zero exit '
                                  'status 128.\n```')
Ejemplo n.º 11
0
    def author(self) -> GitLabUser:
        """
        Retrieves the author of the comment.

        >>> from os import environ
        >>> note = GitLabComment(GitLabOAuthToken(environ['GITLAB_TEST_TOKEN']),
        ...                      'gitmate-test-user/test', 1,
        ...                      CommentType.ISSUE, 31500135)
        >>> note.author.username
        'gitmate-test-user'

        :return: A GitLabUser object.
        """
        return GitLabUser.from_data(self.data['author'], self._token,
                                    self.data['author']['id'])
Ejemplo n.º 12
0
    def test_gitlab_no_response_with_unrelated_comment(
        self, m_add_comment, m_commits, m_body, m_get_perms, m_sha,
        m_set_status, m_author
    ):
        m_sha.return_value = 'f6d2b7c66372236a090a2a74df2e47f42a54456b'
        m_get_perms.return_value = AccessLevel.NONE
        m_commits.return_value = tuple([self.gl_commit])
        m_body.return_value = 'something that should not be responded to'
        m_author.return_value = GitLabUser(self.gh_token, 0)

        self.simulate_gitlab_webhook_call(
            'Merge Request Hook', self.gl_pr_data)
        # reset the mock after posting status for opening the PR
        m_set_status.reset_mock()

        self.simulate_gitlab_webhook_call('Note Hook', self.gl_comment_data)

        m_add_comment.assert_not_called()
        m_set_status.assert_not_called()
Ejemplo n.º 13
0
    def test_gitlab_ack_unack(
        self, m_commits, m_body, m_get_perms, m_set_status, m_author
    ):
        m_commits.return_value = tuple([self.gl_commit, self.gl_commit_2])
        m_body.return_value = 'ack 3f2a3b3 unack f6d2b7c'
        m_get_perms.return_value = AccessLevel.CAN_WRITE

        self.gl_commit.set_status = Mock()
        self.gl_commit_2.set_status = Mock()

        m_author.return_value = GitLabUser(self.gh_token, 0)

        self.simulate_gitlab_webhook_call(
            'Merge Request Hook', self.gl_pr_data)
        self.simulate_gitlab_webhook_call('Note Hook', self.gl_comment_data)

        self.assertEqual(self.gl_commit.set_status.call_args[0][0].status,
                         Status.FAILED)
        self.assertEqual(self.gl_commit_2.set_status.call_args[0][0].status,
                         Status.SUCCESS)
Ejemplo n.º 14
0
    def assignees(self):
        """
        Retrieves the assignee of the issue:

        >>> from os import environ
        >>> issue = GitLabIssue(GitLabOAuthToken(environ['GITLAB_TEST_TOKEN']),
        ...                     'gitmate-test-user/test', 1)
        >>> issue.assignees
        {'gitmate-test-user'}

        >>> issue = GitLabIssue(GitLabOAuthToken(environ['GITLAB_TEST_TOKEN']),
        ...                     'gitmate-test-user/test', 2)
        >>> issue.assignees # Returns empty set, unassigned
        {}

        :return: A set containing the usernames of assignees.
        """
        return {
            GitLabUser.from_data(user, self._token, user['id'])
            for user in self.data['assignees']
        }
Ejemplo n.º 15
0
 def test_gitlab_ack_without_minimum_access_level(
         self, _, m_get_statuses, m_get_perms, m_username, m_body, m_author,
         m_sha, m_add_comment, m_commits
 ):
     m_get_statuses.return_value = (
         CommitStatus(Status.SUCCESS, 'No issues',
                      'review/gitmate/manual', 'https://gitmate.io'),
         CommitStatus(Status.SUCCESS, 'No issues',
                      'review/somewhere/else', 'https://some/url'))
     m_sha.return_value = 'f6d2b7c66372236a090a2a74df2e47f42a54456b'
     m_get_perms.return_value = AccessLevel.CAN_VIEW
     m_username.return_value = self.user.username
     m_body.return_value = 'unack f6d2b7c'
     m_author.return_value = GitLabUser(self.gl_token, 0)
     m_commits.return_value = tuple([self.gl_commit])
     _ = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                           self.gl_pr_data)
     response = self.simulate_gitlab_webhook_call('Note Hook',
                                                  self.gl_comment_data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     m_add_comment.assert_called_once_with(
         'Sorry @{}, you do not have the necessary permission levels to '
         'perform the action.'.format(self.user.username))
Ejemplo n.º 16
0
 def assignees(self):
     # GitLab Merge Requests do not support multiple assignees.
     user = self.data['assignee']
     if not user:
         return set()
     return {GitLabUser.from_data(user, self._token, user['id'])}
Ejemplo n.º 17
0
 def user(self) -> GitLabUser:
     """
     Retrieves the user who reacted with this reaction.
     """
     user = self.data['user']
     return GitLabUser.from_data(user, self._token, user['id'])
Ejemplo n.º 18
0
 def setUp(self):
     self.token = GitLabOAuthToken(os.environ.get('GITLAB_TEST_TOKEN', ''))
     self.user = GitLabUser(self.token)
     self.sils = GitLabUser(self.token, 104269)