|
1 | 1 | from gitlab.base import RESTManager, RESTObject |
2 | 2 | from gitlab.mixins import ListMixin |
| 3 | +from gitlab import cli |
| 4 | +from gitlab import exceptions as exc |
| 5 | +from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union |
| 6 | +from gitlab.types import RequiredOptional |
| 7 | + |
| 8 | +import requests |
3 | 9 |
|
4 | 10 | __all__ = [ |
5 | 11 | "ProjectMergeTrain", |
6 | 12 | "ProjectMergeTrainManager", |
| 13 | + # "ProjectMergeTrainMergeRequest", |
| 14 | + # "ProjectMergeTrainMergeRequestManager", |
7 | 15 | ] |
8 | 16 |
|
9 | 17 |
|
10 | 18 | class ProjectMergeTrain(RESTObject): |
11 | | - pass |
| 19 | + _path = "/projects/{project_id}/merge_trains" |
| 20 | + |
| 21 | + @cli.register_custom_action("ProjectMergeTrain") |
| 22 | + def add(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: |
| 23 | + """Attempt to merge changes between source and target branches into |
| 24 | + `/projects/:id/merge_trains/merge_requests/:merge_request_iid`. |
| 25 | +
|
| 26 | + Args: |
| 27 | + merge_request_iid: merge request id of the MR |
| 28 | + **kwargs: Extra options to send to the server (e.g. sudo) |
| 29 | +
|
| 30 | + Raises: |
| 31 | + GitlabGetError: If cannot be merged |
| 32 | + """ |
| 33 | + path = ( |
| 34 | + f"{self.manager.path}/merge_requests/{self.encoded_id}" |
| 35 | + ) |
| 36 | + data: Dict[str, Any] = {} |
| 37 | + data["when_pipeline_succeeds"] = True |
| 38 | + return self.manager.gitlab.http_post(path, post_data=data, **kwargs) |
| 39 | + |
| 40 | + @cli.register_custom_action("ProjectMergeTrain") |
| 41 | + def get_mr(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: |
| 42 | + """Attempt to merge changes between source and target branches into |
| 43 | + `/projects/:id/merge_trains/merge_requests/:merge_request_iid`. |
12 | 44 |
|
| 45 | + Args: |
| 46 | + merge_request_iid: merge request id of the MR |
| 47 | + **kwargs: Extra options to send to the server (e.g. sudo) |
13 | 48 |
|
14 | | -class ProjectMergeTrainManager(ListMixin, RESTManager): |
| 49 | + Raises: |
| 50 | + GitlabGetError: If cannot be merged |
| 51 | + """ |
| 52 | + path = f"{self.manager.path}/merge_requests/{self.encoded_id}" |
| 53 | + return self.manager.gitlab.http_get(path, **kwargs) |
| 54 | + |
| 55 | + |
| 56 | +# class ProjectMergeTrainMergeRequest(ListMixin, RESTObject, RESTManager): |
| 57 | +# _path = "/projects/{project_id}/merge_trains/merge_requests" |
| 58 | +# _from_parent_attrs = {"project_id": "id"} |
| 59 | +# _optional_get_attrs = ( |
| 60 | +# "when_pipeline_succeeds", |
| 61 | +# "sha", |
| 62 | +# "squash" |
| 63 | +# ) |
| 64 | + |
| 65 | +class ProjectMergeTrainManager(ListMixin, RESTManager, ProjectMergeTrain): |
15 | 66 | _path = "/projects/{project_id}/merge_trains" |
16 | 67 | _obj_cls = ProjectMergeTrain |
17 | 68 | _from_parent_attrs = {"project_id": "id"} |
18 | 69 | _list_filters = ("scope",) |
| 70 | + _update_attrs = RequiredOptional(optional=("when_pipeline_succeeds", "squash",)) |
| 71 | + _optional_get_attrs = ( |
| 72 | + "when_pipeline_succeeds", |
| 73 | + "sha", |
| 74 | + "squash" |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +# @exc.on_http_error(exc.GitlabGetError) |
| 79 | +# def get(self, id: Union[str, int], **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: |
| 80 | +# """Attempt to merge changes between source and target branches into |
| 81 | +# `/projects/:id/merge_trains/merge_requests/:merge_request_iid`. |
| 82 | +# |
| 83 | +# Args: |
| 84 | +# merge_request_iid: merge request id of the MR |
| 85 | +# **kwargs: Extra options to send to the server (e.g. sudo) |
| 86 | +# |
| 87 | +# Raises: |
| 88 | +# GitlabGetError: If cannot be merged |
| 89 | +# """ |
| 90 | +# path = f"{self.manager.path}/merge_requests/{id}" |
| 91 | +# return self.manager.gitlab.http_get(path, **kwargs) |
| 92 | +# |
| 93 | +# #@cli.register_custom_action("ProjectMergeTrain") |
| 94 | +# @exc.on_http_error(exc.GitlabGetError) |
| 95 | +# def add(self, id: Union[str, int], **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: |
| 96 | +# """Attempt to merge changes between source and target branches into |
| 97 | +# `/projects/:id/merge_trains/merge_requests/:merge_request_iid`. |
| 98 | +# |
| 99 | +# Args: |
| 100 | +# merge_request_iid: merge request id of the MR |
| 101 | +# **kwargs: Extra options to send to the server (e.g. sudo) |
| 102 | +# |
| 103 | +# Raises: |
| 104 | +# GitlabGetError: If cannot be merged |
| 105 | +# """ |
| 106 | +# path = f"{self.manager.path}/merge_requests/{id}" |
| 107 | +# return self.manager.gitlab.http_post(path, **kwargs) |
| 108 | +# |
| 109 | +# |
| 110 | +# # class ProjectMergeTrainMergeRequestManager(ProjectMergeTrainMergeRequest): |
| 111 | +# # pass |
| 112 | +# |
| 113 | +# |
| 114 | +# |
| 115 | +# # |
| 116 | +# # class ProjectMergeTrainMergeRequestManager(ListMixin, RESTManager): |
| 117 | +# # _path = "/projects/{project_id}/merge_trains/merge_request" |
| 118 | +# # |
| 119 | +# # |
| 120 | +# # |
| 121 | +# # @cli.register_custom_action("MergeTrain", ("merge_request_iid",)) |
| 122 | +# @exc.on_http_error(exc.GitlabDeleteError) |
| 123 | +# def unshare(self, group_id: int, **kwargs: Any) -> None: |
| 124 | +# """Delete a shared project link within a group. |
| 125 | +# |
| 126 | +# Args: |
| 127 | +# group_id: ID of the group. |
| 128 | +# **kwargs: Extra options to send to the server (e.g. sudo) |
| 129 | +# |
| 130 | +# Raises: |
| 131 | +# GitlabAuthenticationError: If authentication is not correct |
| 132 | +# GitlabDeleteError: If the server failed to perform the request |
| 133 | +# """ |
| 134 | +# path = f"/projects/{self.encoded_id}/share/{group_id}" |
| 135 | +# self.manager.gitlab.http_delete(path, **kwargs) |
0 commit comments