|
30 | 30 | RefreshMixin, |
31 | 31 | SaveMixin, |
32 | 32 | UpdateMixin, |
| 33 | + UploadMixin, |
33 | 34 | ) |
34 | 35 | from gitlab.types import RequiredOptional |
35 | 36 |
|
@@ -158,8 +159,11 @@ class ProjectGroupManager(ListMixin, RESTManager): |
158 | 159 | _types = {"skip_groups": types.ArrayAttribute} |
159 | 160 |
|
160 | 161 |
|
161 | | -class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTObject): |
| 162 | +class Project( |
| 163 | + RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, UploadMixin, RESTObject |
| 164 | +): |
162 | 165 | _repr_attr = "path_with_namespace" |
| 166 | + _upload_path = "/projects/{id}/uploads" |
163 | 167 |
|
164 | 168 | access_tokens: ProjectAccessTokenManager |
165 | 169 | accessrequests: ProjectAccessRequestManager |
@@ -437,59 +441,6 @@ def housekeeping(self, **kwargs: Any) -> None: |
437 | 441 | path = f"/projects/{self.encoded_id}/housekeeping" |
438 | 442 | self.manager.gitlab.http_post(path, **kwargs) |
439 | 443 |
|
440 | | - # see #56 - add file attachment features |
441 | | - @cli.register_custom_action("Project", ("filename", "filepath")) |
442 | | - @exc.on_http_error(exc.GitlabUploadError) |
443 | | - def upload( |
444 | | - self, |
445 | | - filename: str, |
446 | | - filedata: Optional[bytes] = None, |
447 | | - filepath: Optional[str] = None, |
448 | | - **kwargs: Any, |
449 | | - ) -> Dict[str, Any]: |
450 | | - """Upload the specified file into the project. |
451 | | -
|
452 | | - .. note:: |
453 | | -
|
454 | | - Either ``filedata`` or ``filepath`` *MUST* be specified. |
455 | | -
|
456 | | - Args: |
457 | | - filename: The name of the file being uploaded |
458 | | - filedata: The raw data of the file being uploaded |
459 | | - filepath: The path to a local file to upload (optional) |
460 | | -
|
461 | | - Raises: |
462 | | - GitlabConnectionError: If the server cannot be reached |
463 | | - GitlabUploadError: If the file upload fails |
464 | | - GitlabUploadError: If ``filedata`` and ``filepath`` are not |
465 | | - specified |
466 | | - GitlabUploadError: If both ``filedata`` and ``filepath`` are |
467 | | - specified |
468 | | -
|
469 | | - Returns: |
470 | | - A ``dict`` with the keys: |
471 | | - * ``alt`` - The alternate text for the upload |
472 | | - * ``url`` - The direct url to the uploaded file |
473 | | - * ``markdown`` - Markdown for the uploaded file |
474 | | - """ |
475 | | - if filepath is None and filedata is None: |
476 | | - raise exc.GitlabUploadError("No file contents or path specified") |
477 | | - |
478 | | - if filedata is not None and filepath is not None: |
479 | | - raise exc.GitlabUploadError("File contents and file path specified") |
480 | | - |
481 | | - if filepath is not None: |
482 | | - with open(filepath, "rb") as f: |
483 | | - filedata = f.read() |
484 | | - |
485 | | - url = f"/projects/{self.encoded_id}/uploads" |
486 | | - file_info = {"file": (filename, filedata)} |
487 | | - data = self.manager.gitlab.http_post(url, files=file_info, **kwargs) |
488 | | - |
489 | | - if TYPE_CHECKING: |
490 | | - assert isinstance(data, dict) |
491 | | - return {"alt": data["alt"], "url": data["url"], "markdown": data["markdown"]} |
492 | | - |
493 | 444 | @cli.register_custom_action("Project") |
494 | 445 | @exc.on_http_error(exc.GitlabRestoreError) |
495 | 446 | def restore(self, **kwargs: Any) -> None: |
|
0 commit comments