| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8d74b88 commit 9353f54
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,7 +89,7 @@ Remove a group:: | |||
| 89 | 89 | group.delete() | |
| 90 | 90 | ||
| 91 | 91 | Restore a Group marked for deletion (Premium only)::: | |
| 92 | - | ||
| 92 | + | ||
| 93 | 93 | group.restore() | |
| 94 | 94 | ||
| 95 | 95 | ||
@@ -368,9 +368,9 @@ SAML group links | |||
| 368 | 368 | ||
| 369 | 369 | Add a SAML group link to an existing GitLab group:: | |
| 370 | 370 | ||
| 371 | - saml_link = group.saml_group_links.create({ | ||
| 372 | - "saml_group_name": "<your_saml_group_name>", | ||
| 373 | - "access_level": <chosen_access_level> | ||
| 371 | + saml_link = group.saml_group_links.create({ | ||
| 372 | + "saml_group_name": "<your_saml_group_name>", | ||
| 373 | + "access_level": <chosen_access_level> | ||
| 374 | 374 | }) | |
| 375 | 375 | ||
| 376 | 376 | List a group's SAML group links:: | |
@@ -419,6 +419,10 @@ Update a group hook:: | |||
| 419 | 419 | hook.push_events = 0 | |
| 420 | 420 | hook.save() | |
| 421 | 421 | ||
| 422 | + Test a group hook:: | ||
| 423 | + | ||
| 424 | + hook.test("push_events") | ||
| 425 | + | ||
| 422 | 426 | Delete a group hook:: | |
| 423 | 427 | ||
| 424 | 428 | group.hooks.delete(hook_id) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -353,7 +353,7 @@ Import the project using file stored on a remote URL:: | |||
| 353 | 353 | output = gl.projects.remote_import( | |
| 354 | 354 | url="https://whatever.com/url/file.tar.gz", | |
| 355 | 355 | path="my_new_remote_project", | |
| 356 | - name="My New Remote Project", | ||
| 356 | + name="My New Remote Project", | ||
| 357 | 357 | namespace="my-group", | |
| 358 | 358 | override_params={'visibility': 'private'}, | |
| 359 | 359 | ) | |
@@ -367,7 +367,7 @@ Import the project using file stored on AWS S3:: | |||
| 367 | 367 | file_key="aws-file-key", | |
| 368 | 368 | access_key_id="aws-access-key-id", | |
| 369 | 369 | secret_access_key="secret-access-key", | |
| 370 | - name="My New Remote Project", | ||
| 370 | + name="My New Remote Project", | ||
| 371 | 371 | namespace="my-group", | |
| 372 | 372 | override_params={'visibility': 'private'}, | |
| 373 | 373 | ) | |
@@ -449,7 +449,7 @@ Get file details from headers, without fetching its entire content:: | |||
| 449 | 449 | print(headers["X-Gitlab-Size"]) | |
| 450 | 450 | ||
| 451 | 451 | Get a raw file:: | |
| 452 | - | ||
| 452 | + | ||
| 453 | 453 | raw_content = project.files.raw(file_path='README.rst', ref='main') | |
| 454 | 454 | print(raw_content) | |
| 455 | 455 | with open('/tmp/raw-download.txt', 'wb') as f: | |
@@ -689,6 +689,10 @@ Update a project hook:: | |||
| 689 | 689 | hook.push_events = 0 | |
| 690 | 690 | hook.save() | |
| 691 | 691 | ||
| 692 | + Test a project hook:: | ||
| 693 | + | ||
| 694 | + hook.test("push_events") | ||
| 695 | + | ||
| 692 | 696 | Delete a project hook:: | |
| 693 | 697 | ||
| 694 | 698 | project.hooks.delete(hook_id) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -316,6 +316,10 @@ class GitlabDeploymentApprovalError(GitlabOperationError): | |||
| 316 | 316 | pass | |
| 317 | 317 | ||
| 318 | 318 | ||
| 319 | + class GitlabHookTestError(GitlabOperationError): | ||
| 320 | + pass | ||
| 321 | + | ||
| 322 | + | ||
| 319 | 323 | # For an explanation of how these type-hints work see: | |
| 320 | 324 | # https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators | |
| 321 | 325 | # | |
@@ -370,6 +374,7 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any: | |||
| 370 | 374 | "GitlabGetError", | |
| 371 | 375 | "GitlabGroupTransferError", | |
| 372 | 376 | "GitlabHeadError", | |
| 377 | + "GitlabHookTestError", | ||
| 373 | 378 | "GitlabHousekeepingError", | |
| 374 | 379 | "GitlabHttpError", | |
| 375 | 380 | "GitlabImportError", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | from typing import Any, cast, Union | |
| 2 | 2 | ||
| 3 | + from gitlab import exceptions as exc | ||
| 3 | 4 | from gitlab.base import RESTManager, RESTObject | |
| 4 | 5 | from gitlab.mixins import CRUDMixin, NoUpdateMixin, ObjectDeleteMixin, SaveMixin | |
| 5 | 6 | from gitlab.types import RequiredOptional | |
@@ -31,6 +32,20 @@ def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Hook: | |||
| 31 | 32 | class ProjectHook(SaveMixin, ObjectDeleteMixin, RESTObject): | |
| 32 | 33 | _repr_attr = "url" | |
| 33 | 34 | ||
| 35 | + @exc.on_http_error(exc.GitlabHookTestError) | ||
| 36 | + def test(self, trigger: str) -> None: | ||
| 37 | + """ | ||
| 38 | + Test a Project Hook | ||
| 39 | + | ||
| 40 | + Args: | ||
| 41 | + trigger: Type of trigger event to test | ||
| 42 | + | ||
| 43 | + Raises: | ||
| 44 | + GitlabHookTestError: If the hook test attempt failed | ||
| 45 | + """ | ||
| 46 | + path = f"{self.manager.path}/{self.encoded_id}/test/{trigger}" | ||
| 47 | + self.manager.gitlab.http_post(path) | ||
| 48 | + | ||
| 34 | 49 | ||
| 35 | 50 | class ProjectHookManager(CRUDMixin, RESTManager): | |
| 36 | 51 | _path = "/projects/{project_id}/hooks" | |
@@ -78,6 +93,20 @@ def get( | |||
| 78 | 93 | class GroupHook(SaveMixin, ObjectDeleteMixin, RESTObject): | |
| 79 | 94 | _repr_attr = "url" | |
| 80 | 95 | ||
| 96 | + @exc.on_http_error(exc.GitlabHookTestError) | ||
| 97 | + def test(self, trigger: str) -> None: | ||
| 98 | + """ | ||
| 99 | + Test a Group Hook | ||
| 100 | + | ||
| 101 | + Args: | ||
| 102 | + trigger: Type of trigger event to test | ||
| 103 | + | ||
| 104 | + Raises: | ||
| 105 | + GitlabHookTestError: If the hook test attempt failed | ||
| 106 | + """ | ||
| 107 | + path = f"{self.manager.path}/{self.encoded_id}/test/{trigger}" | ||
| 108 | + self.manager.gitlab.http_post(path) | ||
| 109 | + | ||
| 81 | 110 | ||
| 82 | 111 | class GroupHookManager(CRUDMixin, RESTManager): | |
| 83 | 112 | _path = "/groups/{group_id}/hooks" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | import pytest | |
| 10 | 10 | import responses | |
| 11 | 11 | ||
| 12 | + import gitlab | ||
| 12 | 13 | from gitlab.v4.objects import GroupHook, Hook, ProjectHook | |
| 13 | 14 | ||
| 14 | 15 | hooks_content = [ | |
@@ -89,6 +90,58 @@ def resp_hook_update(): | |||
| 89 | 90 | yield rsps | |
| 90 | 91 | ||
| 91 | 92 | ||
| 93 | + @pytest.fixture | ||
| 94 | + def resp_hook_test(): | ||
| 95 | + with responses.RequestsMock() as rsps: | ||
| 96 | + hook_pattern = re.compile( | ||
| 97 | + r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1" | ||
| 98 | + ) | ||
| 99 | + test_pattern = re.compile( | ||
| 100 | + r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1/test/[a-z_]+" | ||
| 101 | + ) | ||
| 102 | + rsps.add( | ||
| 103 | + method=responses.GET, | ||
| 104 | + url=hook_pattern, | ||
| 105 | + json=hook_content, | ||
| 106 | + content_type="application/json", | ||
| 107 | + status=200, | ||
| 108 | + ) | ||
| 109 | + rsps.add( | ||
| 110 | + method=responses.POST, | ||
| 111 | + url=test_pattern, | ||
| 112 | + json={"message": "201 Created"}, | ||
| 113 | + content_type="application/json", | ||
| 114 | + status=201, | ||
| 115 | + ) | ||
| 116 | + yield rsps | ||
| 117 | + | ||
| 118 | + | ||
| 119 | + @pytest.fixture | ||
| 120 | + def resp_hook_test_error(): | ||
| 121 | + with responses.RequestsMock() as rsps: | ||
| 122 | + hook_pattern = re.compile( | ||
| 123 | + r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1" | ||
| 124 | + ) | ||
| 125 | + test_pattern = re.compile( | ||
| 126 | + r"http://localhost/api/v4/((groups|projects)/1/|)hooks/1/test/[a-z_]+" | ||
| 127 | + ) | ||
| 128 | + rsps.add( | ||
| 129 | + method=responses.GET, | ||
| 130 | + url=hook_pattern, | ||
| 131 | + json=hook_content, | ||
| 132 | + content_type="application/json", | ||
| 133 | + status=200, | ||
| 134 | + ) | ||
| 135 | + rsps.add( | ||
| 136 | + method=responses.POST, | ||
| 137 | + url=test_pattern, | ||
| 138 | + json={"message": "<html>error</html>"}, | ||
| 139 | + content_type="application/json", | ||
| 140 | + status=422, | ||
| 141 | + ) | ||
| 142 | + yield rsps | ||
| 143 | + | ||
| 144 | + | ||
| 92 | 145 | @pytest.fixture | |
| 93 | 146 | def resp_hook_delete(): | |
| 94 | 147 | with responses.RequestsMock() as rsps: | |
@@ -174,6 +227,17 @@ def test_delete_group_hook(group, resp_hook_delete): | |||
| 174 | 227 | group.hooks.delete(1) | |
| 175 | 228 | ||
| 176 | 229 | ||
| 230 | + def test_test_group_hook(group, resp_hook_test): | ||
| 231 | + hook = group.hooks.get(1) | ||
| 232 | + hook.test("push_events") | ||
| 233 | + | ||
| 234 | + | ||
| 235 | + def test_test_error_group_hook(group, resp_hook_test_error): | ||
| 236 | + hook = group.hooks.get(1) | ||
| 237 | + with pytest.raises(gitlab.exceptions.GitlabHookTestError): | ||
| 238 | + hook.test("push_events") | ||
| 239 | + | ||
| 240 | + | ||
| 177 | 241 | def test_list_project_hooks(project, resp_hooks_list): | |
| 178 | 242 | hooks = project.hooks.list() | |
| 179 | 243 | assert hooks[0].id == 1 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments