Exemple #1
0
    def upload(self, pid, *args, **kwargs):
        """Upload action for repostiories.

        Can upload a repository or its single file and/or create a webhook.
        Expects json with params:
        * `url`
        * `webhook` (true|false)
        * `event_type` (release|push)'.
        """
        with UpdateDepositPermission(self).require(403):
            if request:
                _, rec = request.view_args.get('pid_value').data
                record_uuid = str(rec.id)
                data = request.get_json()
                webhook = data.get('webhook', False)
                event_type = data.get('event_type', 'release')

                try:
                    url = data['url']
                except KeyError:
                    raise FileUploadError('Missing url parameter.')

                try:
                    host, owner, repo, branch, filepath = parse_git_url(url)
                    api = create_git_api(host, owner, repo, branch,
                                         current_user.id)

                    if filepath:
                        if webhook:
                            raise FileUploadError(
                                'You cannot create a webhook on a file')

                        download_repo_file(
                            record_uuid,
                            f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}/{filepath}',  # noqa
                            *api.get_file_download(filepath),
                            api.auth_headers,
                        )
                    elif webhook:
                        if event_type == 'release':
                            if branch:
                                raise FileUploadError(
                                    'You cannot create a release webhook'
                                    ' for a specific branch or sha.')

                        if event_type == 'push' and \
                                api.branch is None and api.sha:
                            raise FileUploadError(
                                'You cannot create a push webhook'
                                ' for a specific sha.')

                        create_webhook(record_uuid, api, event_type)
                    else:
                        download_repo.delay(
                            record_uuid,
                            f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}.tar.gz',  # noqa
                            api.get_repo_download(),
                            api.auth_headers)

                except GitError as e:
                    raise FileUploadError(str(e))

            return self
Exemple #2
0
def test_importer_with_scrambled_url():
    with pytest.raises(GitURLParsingError):
        create_git_api(parse_git_url('https://hubgit.com/cernanalysis/test'))
Exemple #3
0
def test_importer_with_wrong_url():
    with pytest.raises(GitURLParsingError):
        create_git_api(parse_git_url('https://google.com'))
Exemple #4
0
def test_parse_git_url_with_wrong_url():
    with pytest.raises(GitURLParsingError):
        parse_git_url('https://google.com')
Exemple #5
0
def test_parse_git_url_pr_throws_exception(url):
    """Test the different url parsing combinations"""
    with pytest.raises(GitPRParsingError):
        parse_git_url(url)
Exemple #6
0
def test_parse_git_url_attrs(url, parsed):
    """Test the different url parsing combinations"""
    assert parsed == parse_git_url(url)