Ejemplo n.º 1
0
 def test_combined_status(self):
     self.assertEqual(self.commit.combined_status, Status.FAILED)
     commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                           'a37bb904b39a4aabee24f52ff98a0a988a41a24a')
     self.assertEqual(commit.combined_status, Status.PENDING)
     commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                           '9ba5b704f5866e468ec2e639fa893ae4c129f2ad')
     self.assertEqual(commit.combined_status, Status.SUCCESS)
Ejemplo n.º 2
0
 def test_set_status(self):
     commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                           '3fc4b860e0a2c17819934d678decacd914271e5c')
     status = CommitStatus(Status.FAILED, 'Theres a problem',
                           'gitmate/test')
     self.commit.set_status(status)
     self.assertEqual(commit.get_statuses().pop().description,
                      'Theres a problem')
Ejemplo n.º 3
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.º 4
0
 def setUp(self):
     super().setUpWithPlugin('ack')
     self.gh_comment_data = {
         'repository': {'full_name': self.repo.full_name, 'id': 49558751},
         'issue': {'number': 7, 'pull_request': {}},
         'comment': {'id': 0},
         'action': 'created'
     }
     self.gh_pr_data = {
         'repository': {'full_name': self.repo.full_name, 'id': 49558751},
         'pull_request': {'number': 7},
         'action': 'opened'
     }
     self.gl_comment_data = {
         'project': {'path_with_namespace': self.gl_repo.full_name},
         'object_attributes': {
             'action': 'open',
             'id': 25,
             'iid': 0,
             'noteable_type': 'MergeRequest'
         },
         'merge_request': {'iid': 25}
     }
     self.gl_pr_data = {
         'object_attributes': {
             'target': {'path_with_namespace': self.gl_repo.full_name},
             'action': 'update',
             'oldrev': 'areallylongrandomshayoudontneedtocareabout',
             'iid': 25
         }
     }
     self.gh_token = GitHubToken(environ['GITHUB_TEST_TOKEN'])
     self.gl_token = GitLabOAuthToken(environ['GITLAB_TEST_TOKEN'])
     self.gh_commit = GitHubCommit(
         self.gh_token, self.repo.full_name,
         'f6d2b7c66372236a090a2a74df2e47f42a54456b')
     self.gh_commit_2 = GitHubCommit(
         self.gh_token, self.repo.full_name,
         '3f2a3b37a2943299c589004c2a5be132e9440cba')
     self.gl_commit = GitLabCommit(
         self.gl_token, self.gl_repo.full_name,
         'f6d2b7c66372236a090a2a74df2e47f42a54456b')
     self.gl_commit_2 = GitLabCommit(
         self.gl_token, self.gl_repo.full_name,
         '3f2a3b37a2943299c589004c2a5be132e9440cba')
Ejemplo n.º 5
0
 def test_comment(self):
     self.commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                                '3fc4b860e0a2c17819934d678decacd914271e5c')
     # commit comments cannot be retrieved
     self.assertIsNone(self.commit.comment('An issue is here'))
     self.assertIsNone(
         self.commit.comment("Here in line 4, there's a spelling mistake!",
                             'README.md', 4))
     self.assertIsNone(
         self.commit.comment("Here in line 4, there's a spelling mistake!",
                             'README.md',
                             4,
                             mr_number=6))
     self.assertIsNone(self.commit.comment('test comment', 'READNOT.md', 4))
     # MR comments can be retrieved
     self.assertEqual(
         self.commit.comment('test comment', 'READNOT.md',
                             mr_number=6).body,
         'Comment on 3fc4b860e0a2c17819934d678decacd914271e5c, file READNOT.md.\n\n'
         'test comment')
Ejemplo n.º 6
0
    def commits(self):
        """
        Retrieves the set of commits in this repository.

        :return: A set of GitLabCommit objects.
        """
        # Don't move to module, leads to circular imports
        from IGitt.GitLab.GitLabCommit import GitLabCommit

        return {
            GitLabCommit.from_data(commit, self._token, self.full_name,
                                   commit['id'])
            for commit in get(self._token, self._url + '/repository/commits')
        }
Ejemplo n.º 7
0
    def head(self) -> GitLabCommit:
        """
        Retrieves the head commit as a GitLabCommit object.

         >>> from os import environ
        >>> pr = GitLabMergeRequest(
        ...     GitLabOAuthToken(environ['GITLAB_TEST_TOKEN']),
        ...     'gitmate-test-user/test', 2
        ... )
        >>> pr.head.sha
        '99f484ae167dcfcc35008ba3b5b564443d425ee0'

        :return: A GitLabCommit object.
        """
        return GitLabCommit(self._token,
                            self.source_repository.full_name,
                            sha=None,
                            branch=quote_plus(self.head_branch_name))
Ejemplo n.º 8
0
    def base(self) -> GitLabCommit:
        """
        Retrieves the base commit as a GitLabCommit object.

        >>> from os import environ
        >>> pr = GitLabMergeRequest(
        ...     GitLabOAuthToken(environ['GITLAB_TEST_TOKEN']),
        ...     'gitmate-test-user/test', 2
        ... )
        >>> pr.base.sha
        '198dd16f8249ea98ed41876efe27d068b69fa215'

        :return: A GitLabCommit object.
        """
        return GitLabCommit(self._token,
                            self._repository,
                            sha=None,
                            branch=quote_plus(self.base_branch_name))
Ejemplo n.º 9
0
    def commits(self):
        """
        Retrieves a tuple of commit objects that are included in the PR.

        >>> from os import environ
        >>> pr = GitLabMergeRequest(
        ...     GitLabOAuthToken(environ['GITLAB_TEST_TOKEN']),
        ...     'gitmate-test-user/test', 2
        ... )
        >>> assert ([commit.sha for commit in pr.commits] == [
        ...     '99f484ae167dcfcc35008ba3b5b564443d425ee0',
        ...     'bbd11b50412d34072f1889e4cea04a32de183605'])

        :return: A tuple of commit objects.
        """
        commits = get(self._token, self._url + '/commits')
        return tuple(
            GitLabCommit(self._token, self._repository, commit['id'])
            for commit in commits)
Ejemplo n.º 10
0
    def test_gitlab_new_head(self, m_set_status, m_commits, m_head):
        # First MR Sync
        m_head.return_value = self.gl_commit
        m_commits.return_value = tuple([self.gl_commit])
        response = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                                     self.gl_pr_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # Add a commit on top, now the one before (self.gl_commit) should get a
        # passing PR state because that's outdated and shouldn't block merges.
        new_head = GitLabCommit(self.gl_token, self.gl_repo.full_name,
                                '9ba5b704f5866e468ec2e639fa893ae4c129f2ad')
        m_head.return_value = new_head
        m_commits.return_value = tuple([self.gl_commit, new_head])
        response = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                                     self.gl_pr_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        states = sum([list(args) for args, _ in m_set_status.call_args_list],
                     [])
        self.assertIn((Status.SUCCESS, 'review/gitmate/manual/pr',
                       'Outdated. Check 9ba5b70 instead.'),
                      [(state.status, state.context, state.description)
                       for state in states])
Ejemplo n.º 11
0
    def test_gitlab_unmodified_commit(
        self, m_set_status, m_get_statuses, m_sha, m_message, m_diff, m_commits
    ):
        m_get_statuses.return_value = (
            CommitStatus(Status.SUCCESS, 'Good to go!',
                         'review/gitmate/manual', 'https://gitmate.io'),)
        m_diff.return_value = ('--- a/README.md\n'
                               '+++ b/README.md\n'
                               '@@ -1,2 +1,4 @@\n'
                               ' # test\n'
                               ' a test repo\n'
                               '+\n'
                               '+a commiit that can one acknowledge')
        m_message.return_value = 'Update README.md'
        m_commits.return_value = tuple([self.gl_commit])
        m_sha.return_value = 'f6d2b7c66372236a090a2a74df2e47f42a54456b'
        response = self.simulate_gitlab_webhook_call('Merge Request Hook',
                                                     self.gl_pr_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # resyncing merge request with a new unmodified commit
        m_sha.return_value = '9ba5b704f5866e468ec2e639fa893ae4c129f2ad'
        m_commits.return_value = tuple([GitLabCommit(
            self.gl_token, self.gl_repo.full_name, m_sha.return_value)])
        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], [])
        # 3 calls to be made as follows
        # Status.SUCCESS review/gitmate/manual/pr
        # Status.SUCCESS review/gitmate/manual
        # Status.SUCCESS review/gitmate/manual/pr
        self.assertEqual([(arg.status, arg.context) for arg in args],
                         [(Status.SUCCESS, 'review/gitmate/manual/pr'),
                          (Status.SUCCESS, 'review/gitmate/manual'),
                          (Status.SUCCESS, 'review/gitmate/manual/pr')])
Ejemplo n.º 12
0
 def setUp(self):
     self.token = GitLabOAuthToken(os.environ.get('GITLAB_TEST_TOKEN', ''))
     self.commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                                '3fc4b860e0a2c17819934d678decacd914271e5c')
Ejemplo n.º 13
0
class GitLabCommitTest(IGittTestCase):
    def setUp(self):
        self.token = GitLabOAuthToken(os.environ.get('GITLAB_TEST_TOKEN', ''))
        self.commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                                   '3fc4b860e0a2c17819934d678decacd914271e5c')

    def test_sha(self):
        self.assertIn('3fc4b86', self.commit.sha)

    def test_message(self):
        self.assertEqual(self.commit.message, 'Update README.md')

    def test_repository(self):
        self.assertEqual(self.commit.repository.full_name,
                         'gitmate-test-user/test')

    def test_parent(self):
        self.assertEqual(self.commit.parent.sha,
                         '674498fd415cfadc35c5eb28b8951e800f357c6f')

    def test_set_status(self):
        commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                              '3fc4b860e0a2c17819934d678decacd914271e5c')
        status = CommitStatus(Status.FAILED, 'Theres a problem',
                              'gitmate/test')
        self.commit.set_status(status)
        self.assertEqual(commit.get_statuses().pop().description,
                         'Theres a problem')

    def test_combined_status(self):
        self.assertEqual(self.commit.combined_status, Status.FAILED)
        commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                              'a37bb904b39a4aabee24f52ff98a0a988a41a24a')
        self.assertEqual(commit.combined_status, Status.PENDING)
        commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                              '9ba5b704f5866e468ec2e639fa893ae4c129f2ad')
        self.assertEqual(commit.combined_status, Status.SUCCESS)

    def test_get_patch_for_file(self):
        patch = self.commit.get_patch_for_file('README.md')
        self.assertEqual(
            patch, '@@ -1,2 +1,4 @@\n'
            ' # test\n'
            ' a test repo\n'
            '+\n'
            '+a tst pr\n')

    def test_comment(self):
        self.commit = GitLabCommit(self.token, 'gitmate-test-user/test',
                                   '3fc4b860e0a2c17819934d678decacd914271e5c')
        # commit comments cannot be retrieved
        self.assertIsNone(self.commit.comment('An issue is here'))
        self.assertIsNone(
            self.commit.comment("Here in line 4, there's a spelling mistake!",
                                'README.md', 4))
        self.assertIsNone(
            self.commit.comment("Here in line 4, there's a spelling mistake!",
                                'README.md',
                                4,
                                mr_number=6))
        self.assertIsNone(self.commit.comment('test comment', 'READNOT.md', 4))
        # MR comments can be retrieved
        self.assertEqual(
            self.commit.comment('test comment', 'READNOT.md',
                                mr_number=6).body,
            'Comment on 3fc4b860e0a2c17819934d678decacd914271e5c, file READNOT.md.\n\n'
            'test comment')

    def test_unified_diff(self):
        self.assertEqual(
            self.commit.unified_diff, '--- a/README.md\n'
            '+++ b/README.md\n'
            '@@ -1,2 +1,4 @@\n'
            ' # test\n'
            ' a test repo\n'
            '+\n'
            '+a tst pr\n')
Ejemplo n.º 14
0
 def _handle_webhook_pipeline(self, data, repository):
     yield PipelineActions.UPDATED, [
         GitLabCommit(self._token, repository, data['commit']['id'])
     ]