| 
49 | 49 | from sqlalchemy.orm.exc import NoResultFound  | 
50 | 50 | from werkzeug.local import LocalProxy  | 
51 | 51 | 
 
  | 
 | 52 | +from cap.modules.auth.ext import _fetch_token  | 
52 | 53 | from cap.modules.deposit.errors import DisconnectWebhookError, FileUploadError  | 
53 | 54 | from cap.modules.deposit.validators import NoRequiredValidator  | 
54 | 55 | from cap.modules.experiments.permissions import exp_need_factory  | 
 | 
76 | 77 |                           UpdateDepositPermission)  | 
77 | 78 | 
 
  | 
78 | 79 | from .review import Reviewable  | 
 | 80 | +from .tasks import upload_to_zenodo  | 
 | 81 | +from .utils import create_zenodo_deposit  | 
79 | 82 | 
 
  | 
80 | 83 | _datastore = LocalProxy(lambda: current_app.extensions['security'].datastore)  | 
81 | 84 | 
 
  | 
@@ -256,53 +259,82 @@ def upload(self, pid, *args, **kwargs):  | 
256 | 259 |                 _, rec = request.view_args.get('pid_value').data  | 
257 | 260 |                 record_uuid = str(rec.id)  | 
258 | 261 |                 data = request.get_json()  | 
259 |  | -                webhook = data.get('webhook', False)  | 
260 |  | -                event_type = data.get('event_type', 'release')  | 
261 |  | - | 
262 |  | -                try:  | 
263 |  | -                    url = data['url']  | 
264 |  | -                except KeyError:  | 
265 |  | -                    raise FileUploadError('Missing url parameter.')  | 
266 |  | - | 
267 |  | -                try:  | 
268 |  | -                    host, owner, repo, branch, filepath = parse_git_url(url)  | 
269 |  | -                    api = create_git_api(host, owner, repo, branch,  | 
270 |  | -                                         current_user.id)  | 
271 |  | - | 
272 |  | -                    if filepath:  | 
273 |  | -                        if webhook:  | 
274 |  | -                            raise FileUploadError(  | 
275 |  | -                                'You cannot create a webhook on a file')  | 
276 |  | - | 
277 |  | -                        download_repo_file(  | 
278 |  | -                            record_uuid,  | 
279 |  | -                            f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}/{filepath}',  # noqa  | 
280 |  | -                            *api.get_file_download(filepath),  | 
281 |  | -                            api.auth_headers,  | 
282 |  | -                        )  | 
283 |  | -                    elif webhook:  | 
284 |  | -                        if event_type == 'release':  | 
285 |  | -                            if branch:  | 
286 |  | -                                raise FileUploadError(  | 
287 |  | -                                    'You cannot create a release webhook'  | 
288 |  | -                                    ' for a specific branch or sha.')  | 
 | 262 | +                target = data.get('target')  | 
 | 263 | + | 
 | 264 | +                if target == 'zenodo':  | 
 | 265 | +                    # check for token  | 
 | 266 | +                    token = _fetch_token('zenodo')  | 
 | 267 | +                    if not token:  | 
 | 268 | +                        raise FileUploadError(  | 
 | 269 | +                            'Please connect your Zenodo account '  | 
 | 270 | +                            'before creating a deposit.')  | 
 | 271 | + | 
 | 272 | +                    files = data.get('files')  | 
 | 273 | +                    bucket = data.get('bucket')  | 
 | 274 | +                    zenodo_data = data.get('zenodo_data', {})  | 
 | 275 | + | 
 | 276 | +                    if files and bucket:  | 
 | 277 | +                        zenodo_deposit = create_zenodo_deposit(token, zenodo_data)  # noqa  | 
 | 278 | +                        self.setdefault('_zenodo', []).append(zenodo_deposit)  | 
 | 279 | +                        self.commit()  | 
 | 280 | + | 
 | 281 | +                        # upload files to zenodo deposit  | 
 | 282 | +                        upload_to_zenodo.delay(  | 
 | 283 | +                            files, bucket, token,  | 
 | 284 | +                            zenodo_deposit['id'],  | 
 | 285 | +                            zenodo_deposit['links']['bucket'])  | 
 | 286 | +                    else:  | 
 | 287 | +                        raise FileUploadError(  | 
 | 288 | +                            'You cannot create an empty Zenodo deposit. '  | 
 | 289 | +                            'Please add some files.')  | 
 | 290 | +                else:  | 
 | 291 | +                    webhook = data.get('webhook', False)  | 
 | 292 | +                    event_type = data.get('event_type', 'release')  | 
289 | 293 | 
 
  | 
290 |  | -                        if event_type == 'push' and \  | 
291 |  | -                                api.branch is None and api.sha:  | 
292 |  | -                            raise FileUploadError(  | 
293 |  | -                                'You cannot create a push webhook'  | 
294 |  | -                                ' for a specific sha.')  | 
 | 294 | +                    try:  | 
 | 295 | +                        url = data['url']  | 
 | 296 | +                    except KeyError:  | 
 | 297 | +                        raise FileUploadError('Missing url parameter.')  | 
295 | 298 | 
 
  | 
296 |  | -                        create_webhook(record_uuid, api, event_type)  | 
297 |  | -                    else:  | 
298 |  | -                        download_repo.delay(  | 
299 |  | -                            record_uuid,  | 
300 |  | -                            f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}.tar.gz',  # noqa  | 
301 |  | -                            api.get_repo_download(),  | 
302 |  | -                            api.auth_headers)  | 
303 |  | - | 
304 |  | -                except GitError as e:  | 
305 |  | -                    raise FileUploadError(str(e))  | 
 | 299 | +                    try:  | 
 | 300 | +                        host, owner, repo, branch, filepath = parse_git_url(url)  # noqa  | 
 | 301 | +                        api = create_git_api(host, owner, repo, branch,  | 
 | 302 | +                                             current_user.id)  | 
 | 303 | + | 
 | 304 | +                        if filepath:  | 
 | 305 | +                            if webhook:  | 
 | 306 | +                                raise FileUploadError(  | 
 | 307 | +                                    'You cannot create a webhook on a file')  | 
 | 308 | + | 
 | 309 | +                            download_repo_file(  | 
 | 310 | +                                record_uuid,  | 
 | 311 | +                                f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}/{filepath}',  # noqa  | 
 | 312 | +                                *api.get_file_download(filepath),  | 
 | 313 | +                                api.auth_headers,  | 
 | 314 | +                            )  | 
 | 315 | +                        elif webhook:  | 
 | 316 | +                            if event_type == 'release':  | 
 | 317 | +                                if branch:  | 
 | 318 | +                                    raise FileUploadError(  | 
 | 319 | +                                        'You cannot create a release webhook'  | 
 | 320 | +                                        ' for a specific branch or sha.')  | 
 | 321 | + | 
 | 322 | +                            if event_type == 'push' and \  | 
 | 323 | +                                    api.branch is None and api.sha:  | 
 | 324 | +                                raise FileUploadError(  | 
 | 325 | +                                    'You cannot create a push webhook'  | 
 | 326 | +                                    ' for a specific sha.')  | 
 | 327 | + | 
 | 328 | +                            create_webhook(record_uuid, api, event_type)  | 
 | 329 | +                        else:  | 
 | 330 | +                            download_repo.delay(  | 
 | 331 | +                                record_uuid,  | 
 | 332 | +                                f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}.tar.gz',  # noqa  | 
 | 333 | +                                api.get_repo_download(),  | 
 | 334 | +                                api.auth_headers)  | 
 | 335 | + | 
 | 336 | +                    except GitError as e:  | 
 | 337 | +                        raise FileUploadError(str(e))  | 
306 | 338 | 
 
  | 
307 | 339 |             return self  | 
308 | 340 | 
 
  | 
 | 
0 commit comments