Exemplo n.º 1
0
def autolabel(pr_handler, repo_handler):

    print(f'Running auto-labeller for {pr_handler.repo}#{pr_handler.number}')

    # Note: repo_handler is the fork, we need to use the upstream repository
    # for some tasks below.
    upstream_repo = RepoHandler(pr_handler.repo,
                                installation=pr_handler.installation)

    al_config = upstream_repo.get_config_value("autolabel", {})
    if not al_config.get("enabled", True):
        print("Skipping PR autolabel, disabled in config.")
        return

    files = pr_handler.get_modified_files()

    print('  Modified files:')
    for file in files:
        print(f'    - {file}')

    all_labels = upstream_repo.get_all_labels()

    print('  All labels: ' + ', '.join(all_labels))

    pr_labels = set(pr_handler.labels)

    print('  Pull request labels: ' + ', '.join(pr_labels))

    new_labels = set()

    if al_config.get('subpackages', True):
        labels = get_subpackage_labels(files, all_labels)
        new_labels = new_labels.union(labels)

    # TODO: add other auto-labeling logic here

    if new_labels:
        final_labels = list(pr_labels.union(new_labels))
        print('  Final labels to set: ' + ', '.join(final_labels))
        pr_handler.set_labels(final_labels)

    return None
Exemplo n.º 2
0
class TestRepoHandler:
    def setup_class(self):
        self.repo = RepoHandler('fakerepo/doesnotexist', branch='awesomebot')

    @patch('requests.get')
    def test_get_issues(self, mock_get):
        # http://engineroom.trackmaven.com/blog/real-life-mocking/
        mock_response = Mock()
        mock_response.json.return_value = [{
            'number': 42,
            'state': 'open'
        }, {
            'number': 55,
            'state': 'open',
            'pull_request': {
                'diff_url': 'blah'
            }
        }]
        mock_get.return_value = mock_response

        assert self.repo.get_issues('open', 'Close?') == [42]
        assert self.repo.get_issues('open', 'Close?',
                                    exclude_pr=False) == [42, 55]

    @patch('requests.get')
    def test_get_all_labels(self, mock_get):
        mock_response = Mock()
        mock_response.json.return_value = [{
            'name': 'io.fits'
        }, {
            'name': 'Documentation'
        }]
        mock_response.headers = {}
        mock_get.return_value = mock_response

        assert self.repo.get_all_labels() == ['io.fits', 'Documentation']

    def test_urls(self):
        assert self.repo._url_contents == 'https://api.github.com/repos/fakerepo/doesnotexist/contents/'
        assert self.repo._url_pull_requests == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls'
        assert self.repo._headers == {}