Esempio n. 1
0
    def test_assign_cmd(self):
        plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub)
        plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab)
        labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)
        labhub.activate()

        mock_issue = create_autospec(GitHubIssue)
        self.mock_repo.get_issue.return_value = mock_issue

        labhub.REPOS = {'a': self.mock_repo}

        mock_dev_team = create_autospec(github3.orgs.Team)
        mock_maint_team = create_autospec(github3.orgs.Team)
        mock_dev_team.is_member.return_value = False
        mock_maint_team.is_member.return_value = False

        labhub.TEAMS = {
            'coala newcomers': self.mock_team,
            'coala developers': mock_dev_team,
            'coala maintainers': mock_maint_team
        }

        cmd = '!assign https://github.com/{}/{}/issues/{}'
        # no assignee, not newcomer
        mock_issue.assignees = tuple()
        self.mock_team.is_member.return_value = False

        testbot.assertCommand(cmd.format('coala', 'a', '23'),
                              'You\'ve been assigned to the issue')

        # no assignee, newcomer, difficulty/low
        mock_issue.labels = PropertyMock()
        mock_issue.labels = ('difficulty/low', )
        mock_issue.assignees = tuple()
        self.mock_team.is_member.return_value = True

        testbot.assertCommand(cmd.format('coala', 'a', '23'),
                              'You\'ve been assigned to the issue')

        # no assignee, newcomer, no labels
        self.mock_team.is_member.return_value = True
        mock_issue.labels = tuple()
        mock_issue.assignees = tuple()
        testbot.assertCommand(cmd.format('coala', 'a', '23'),
                              'not eligible to be assigned to this issue')
        testbot.pop_message()

        # no assignee, newcomer, difficulty medium
        mock_issue.labels = ('difficulty/medium', )
        testbot.assertCommand(cmd.format('coala', 'a', '23'),
                              'not eligible to be assigned to this issue')
        testbot.pop_message()

        # no assignee, newcomer, difficulty medium
        labhub.GH_ORG_NAME = 'not-coala'
        testbot.assertCommand(cmd.format('coala', 'a', '23'), 'assigned')
        labhub.GH_ORG_NAME = 'coala'

        # newcomer, developer, difficulty/medium
        mock_dev_team.is_member.return_value = True
        mock_maint_team.is_member.return_value = False
        testbot.assertCommand(cmd.format('coala', 'a', '23'), 'assigned')

        # has assignee
        mock_issue.assignees = ('somebody', )
        testbot.assertCommand(cmd.format('coala', 'a', '23'),
                              'already assigned to someone')

        # has assignee same as user
        mock_issue.assignees = (None, )
        testbot.assertCommand(cmd.format('coala', 'a', '23'),
                              'already assigned to you')

        # non-existent repository
        testbot.assertCommand(cmd.format('coala', 'c', '23'),
                              'Repository doesn\'t exist.')

        # unknown org
        testbot.assertCommand(cmd.format('coa', 'a', '23'),
                              'Repository not owned by our org.')
Esempio n. 2
0
    def test_invite_cmd(self):
        mock_team_newcomers = create_autospec(github3.orgs.Team)
        mock_team_developers = create_autospec(github3.orgs.Team)
        mock_team_maintainers = create_autospec(github3.orgs.Team)

        teams = {
            'coala maintainers': mock_team_maintainers,
            'coala newcomers': mock_team_newcomers,
            'coala developers': mock_team_developers
        }

        labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)
        labhub.activate()
        labhub._teams = teams

        plugins.labhub.os.environ['GH_TOKEN'] = 'patched?'

        self.assertEqual(labhub.TEAMS, teams)

        labhub.is_room_member = MagicMock(return_value=False)
        testbot.assertCommand('!invite meet to newcomers',
                              '@meet is not a member of this room.')

        labhub.is_room_member = MagicMock(return_value=True)

        # invite by maintainer
        mock_team_newcomers.is_member.return_value = True
        mock_team_developers.is_member.return_value = True
        mock_team_maintainers.is_member.return_value = True

        testbot.assertCommand(
            '!invite meet to newcomers',
            'To get started, please follow our [newcomers guide]')
        testbot.assertCommand('!invite meet to developers',
                              '@meet, you are a part of developers')
        testbot.assertCommand('!invite meet to maintainers',
                              '@meet you seem to be awesome!')

        # invite by developer
        mock_team_maintainers.is_member.return_value = False
        labhub.is_room_member = MagicMock(return_value=True)

        testbot.assertCommand(
            '!invite meet to newcomers',
            'To get started, please follow our [newcomers guide]')
        testbot.assertCommand('!invite meet to developers', ':poop:')
        testbot.assertCommand('!invite meet to maintainers', ':poop:')

        # invite by newcomer
        mock_team_developers.is_member.return_value = False

        testbot.assertCommand('!invite meet to newcomers', ':poop')
        testbot.assertCommand('!invite meet to developers', ':poop:')
        testbot.assertCommand('!invite meet to maintainers', ':poop:')

        # invalid team
        testbot.assertCommand('!invite meet to something',
                              'select from one of the valid')

        #invalid command
        testbot.assertCommand('!invite meetto newcomers',
                              'Command "invite" / "invite meetto" not found.')
Esempio n. 3
0
    def test_pr_list(self):
        git_stats, testbot = plugin_testbot(plugins.git_stats.GitStats,
                                            logging.ERROR)
        git_stats.activate()

        git_stats.REPOS = {'test': self.mock_repo}
        mock_github_mr = create_autospec(GitHubMergeRequest)
        mock_gitlab_mr = create_autospec(GitLabMergeRequest)
        mock_github_issue = create_autospec(GitHubIssue)
        mock_gitlab_issue = create_autospec(GitLabIssue)
        mock_github_mr.closes_issue = mock_github_issue
        mock_gitlab_mr.closes_issue = mock_gitlab_issue
        mock_github_mr.repository = self.mock_repo
        mock_gitlab_mr.repository = self.mock_repo
        mock_github_mr.url = 'http://www.example.com/'
        mock_gitlab_mr.url = 'http://www.example.com/'
        mock_repo_obj = create_autospec(Repo)
        cmd_github = '!mergable {}'
        cmd_gitlab = '!mergable {}'

        self.mock_repo.merge_requests.return_value = [mock_github_mr]

        # Non-existing repo
        testbot.assertCommand(cmd_github.format('b'),
                              'Repository doesn\'t exist.')
        testbot.assertCommand(cmd_gitlab.format('b'),
                              'Repository doesn\'t exist.')

        # PR is suitable
        mock_github_mr.labels = ['process/approved', 'difficulty/newcomer']
        mock_gitlab_mr.labels = ['process/approved', 'difficulty/newcomer']
        mock_github_mr.state = 'open'
        mock_gitlab_mr.state = 'open'
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        mock_repo_obj.head.commit.hexsha = '1'
        mock_github_mr.base.sha = '1'
        mock_gitlab_mr.base.sha = '1'
        testbot.assertCommand(
            cmd_github.format('test'), 'PRs ready to be merged:\n '
            'http://www.example.com/')
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(
            cmd_gitlab.format('test'), 'PRs ready to be merged:\n '
            'http://www.example.com/')

        # PR is not suitable (wrong labels)
        mock_github_mr.labels = ['process/wip', 'difficulty/newcomer']
        mock_gitlab_mr.labels = ['process/wip', 'difficulty/newcomer']
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(cmd_github.format('test'), 'No merge-ready PRs!')
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(cmd_gitlab.format('test'), 'No merge-ready PRs!')
        mock_github_mr.labels = ['process/approved', 'difficulty/newcomer']
        mock_gitlab_mr.labels = ['process/approved', 'difficulty/newcomer']

        # PR is not suitable (needs rebase)
        mock_repo_obj.head.commit.hexsha = '2'
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(cmd_github.format('test'), 'No merge-ready PRs!')
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(cmd_gitlab.format('test'), 'No merge-ready PRs!')
        mock_repo_obj.head.commit.hexsha = '1'

        # PR is not suitable (already closed)
        mock_github_mr.state = 'closed'
        mock_gitlab_mr.state = 'closed'
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(cmd_github.format('test'), 'No merge-ready PRs!')
        self.mock_repo.get_clone.return_value = (mock_repo_obj,
                                                 mkdtemp('mock_repo/'))
        testbot.assertCommand(cmd_gitlab.format('test'), 'No merge-ready PRs!')
Esempio n. 4
0
    def test_create_issue_cmd(self):
        plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub)
        plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab)
        plugins.labhub.GitHubToken = create_autospec(IGitt.GitHub.GitHubToken)
        plugins.labhub.GitLabPrivateToken = create_autospec(
            IGitt.GitLab.GitLabPrivateToken)

        labhub, testbot_private = plugin_testbot(
            plugins.labhub.LabHub, logging.ERROR, {
                'BACKEND': 'text',
                'ACCESS_CONTROLS': {
                    'create_issue_cmd': {
                        'allowprivate': False
                    }
                },
            })
        labhub.activate()
        labhub.REPOS = {'repository': self.mock_repo}
        plugins.labhub.GitHubToken.assert_called_with(None)
        plugins.labhub.GitLabPrivateToken.assert_called_with(None)

        # Start ignoring PycodestyleBear, LineLengthBear
        # TODO
        # Ignoring assertion to prevent build failure for time being
        # Creating issue in private chat
        # testbot_private.assertCommand('!new issue repository this is the title\nbo\ndy',
        #                       'You\'re not allowed')
        # Stop ignoring

        # Creating issue in public chat
        labhub, testbot_public = plugin_testbot(plugins.labhub.LabHub,
                                                logging.ERROR,
                                                {'BACKEND': 'text'})
        labhub.activate()
        labhub.REPOS = {
            'repository': self.mock_repo,
            'repository.github.io': self.mock_repo
        }

        testbot_public.assertCommand(
            textwrap.dedent('''\
                !new issue repository this is the title
                first line of body
                second line of body
            '''), 'Here you go')

        labhub.REPOS['repository'].create_issue.assert_called_once_with(
            'this is the title',
            textwrap.dedent('''\
                first line of body
                second line of body
                Opened by @None at [text]()'''))

        testbot_public.assertCommand(
            textwrap.dedent('''\
                !new issue repository.github.io another title
                body
            '''), 'Here you go')

        labhub.REPOS['repository.github.io'].create_issue.assert_called_with(
            'another title',
            textwrap.dedent('''\
                body
                Opened by @None at [text]()'''))

        testbot_public.assertCommand('!new issue coala title',
                                     'repository that does not exist')