From 25a878c34e71dff521c9987c4fa4654b28e6ab0b Mon Sep 17 00:00:00 2001 From: ahuang11 Date: Wed, 28 Dec 2022 07:49:59 -0800 Subject: [PATCH 1/8] Add client --- CHANGELOG.md | 3 +++ prefect_gitlab/credentials.py | 14 ++++++++++++++ requirements.txt | 2 +- tests/test_credentials.py | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/test_credentials.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f92325..556536d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `get_client` method to `GitLabCredentials` - [#5](https://github.com/PrefectHQ/prefect-gitlab/?????) + + ### Changed ### Deprecated diff --git a/prefect_gitlab/credentials.py b/prefect_gitlab/credentials.py index dc72910..bae695f 100644 --- a/prefect_gitlab/credentials.py +++ b/prefect_gitlab/credentials.py @@ -2,6 +2,7 @@ from prefect.blocks.core import Block from pydantic import Field, HttpUrl, SecretStr +from gitlab import Gitlab class GitLabCredentials(Block): @@ -31,3 +32,16 @@ class GitLabCredentials(Block): default=None, description="A GitLab Personal Access Token with repo scope.", ) + url: str = Field(default=None, title="URL", description="URL to self-hosted GitLab instances.") + + def get_client(self) -> Gitlab: + """ + Gets an authenticated GitLab client. + + Returns: + An authenticated GitLab client. + """ + # ref: https://python-gitlab.readthedocs.io/en/stable/ + gitlab = Gitlab(url=self.url, oauth_token=self.token) + gitlab.auth() + return gitlab diff --git a/requirements.txt b/requirements.txt index dd67205..2dba6c0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ prefect>=2.3.0 -pydantic +python-gitlab>=3.12.0 diff --git a/tests/test_credentials.py b/tests/test_credentials.py new file mode 100644 index 0000000..22966ce --- /dev/null +++ b/tests/test_credentials.py @@ -0,0 +1,15 @@ +from unittest.mock import MagicMock +from prefect_gitlab.credentials import GitLabCredentials, Gitlab + + +def test_gitlab_credentials_get_client(monkeypatch): + mock_gitlab = MagicMock() + monkeypatch.setattr("prefect_gitlab.credentials.Gitlab", mock_gitlab) + gitlab_credentials = GitLabCredentials( + url="https://gitlab.example.com", token="my-token" + ) + gitlab_credentials.get_client() + mock_gitlab.assert_called_once_with( + url=gitlab_credentials.url, oauth_token=gitlab_credentials.token + ) + mock_gitlab.assert_called_once() From 0aff2066666dda7ce404eebde18685d77c6318cc Mon Sep 17 00:00:00 2001 From: Andrew <15331990+ahuang11@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:51:16 -0800 Subject: [PATCH 2/8] Update CHANGELOG.md --- CHANGELOG.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 556536d..f43f16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- `get_client` method to `GitLabCredentials` - [#5](https://github.com/PrefectHQ/prefect-gitlab/?????) - +- `get_client` method to `GitLabCredentials` - [#9](https://github.com/PrefectHQ/prefect-gitlab/pull/9) ### Changed @@ -28,5 +27,5 @@ Released on November 3rd, 2022. ### Added -- `GitLabRepository` block - [#1](https://github.com/PrefectHQ/prefect-gitlab) -- `GitLabCredentials` block - [#1](https://github.com/PrefectHQ/prefect-gitlab) +- `GitLabRepository` block - [#1](https://github.com/PrefectHQ/prefect-gitlab/pull/1) +- `GitLabCredentials` block - [#1](https://github.com/PrefectHQ/prefect-gitlab/pull/1) From 81e92f06f1185ae7e057e8fa9aafa674eab4367f Mon Sep 17 00:00:00 2001 From: ahuang11 Date: Wed, 28 Dec 2022 07:52:04 -0800 Subject: [PATCH 3/8] Add missing docstring --- prefect_gitlab/credentials.py | 1 + 1 file changed, 1 insertion(+) diff --git a/prefect_gitlab/credentials.py b/prefect_gitlab/credentials.py index bae695f..a3faf18 100644 --- a/prefect_gitlab/credentials.py +++ b/prefect_gitlab/credentials.py @@ -12,6 +12,7 @@ class GitLabCredentials(Block): Attributes: token: The token to authenticate into GitLab. + url: URL to self-hosted GitLab instances. Examples: Load stored GitLab credentials: From 3d6410fca65655dd5878f5b46b3004ffe7c3bbfc Mon Sep 17 00:00:00 2001 From: ahuang11 Date: Wed, 28 Dec 2022 07:52:52 -0800 Subject: [PATCH 4/8] Precommit --- prefect_gitlab/credentials.py | 6 ++++-- tests/test_credentials.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/prefect_gitlab/credentials.py b/prefect_gitlab/credentials.py index a3faf18..5689277 100644 --- a/prefect_gitlab/credentials.py +++ b/prefect_gitlab/credentials.py @@ -1,8 +1,8 @@ """Module used to enable authenticated interactions with GitLab""" +from gitlab import Gitlab from prefect.blocks.core import Block from pydantic import Field, HttpUrl, SecretStr -from gitlab import Gitlab class GitLabCredentials(Block): @@ -33,7 +33,9 @@ class GitLabCredentials(Block): default=None, description="A GitLab Personal Access Token with repo scope.", ) - url: str = Field(default=None, title="URL", description="URL to self-hosted GitLab instances.") + url: str = Field( + default=None, title="URL", description="URL to self-hosted GitLab instances." + ) def get_client(self) -> Gitlab: """ diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 22966ce..7fcfbaf 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -1,5 +1,6 @@ from unittest.mock import MagicMock -from prefect_gitlab.credentials import GitLabCredentials, Gitlab + +from prefect_gitlab.credentials import GitLabCredentials def test_gitlab_credentials_get_client(monkeypatch): From 68952aabac6d26198d77bf52139e3b470b5c6513 Mon Sep 17 00:00:00 2001 From: Andrew <15331990+ahuang11@users.noreply.github.com> Date: Wed, 28 Dec 2022 08:03:13 -0800 Subject: [PATCH 5/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f43f16b..5c43ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- `get_client` method to `GitLabCredentials` - [#9](https://github.com/PrefectHQ/prefect-gitlab/pull/9) +- `get_client` method and `url` field to `GitLabCredentials` - [#9](https://github.com/PrefectHQ/prefect-gitlab/pull/9) ### Changed From 8b51f2e1909e438f22a33b9f088755862e3b9f81 Mon Sep 17 00:00:00 2001 From: Andrew <15331990+ahuang11@users.noreply.github.com> Date: Wed, 28 Dec 2022 11:13:22 -0800 Subject: [PATCH 6/8] Bump Prefect version --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2dba6c0..cbb35e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -prefect>=2.3.0 +prefect>=2.7.0 python-gitlab>=3.12.0 From b718df9e3b9e9c1ca8be91a5d9fe28deeccd1f89 Mon Sep 17 00:00:00 2001 From: Andrew <15331990+ahuang11@users.noreply.github.com> Date: Wed, 28 Dec 2022 11:51:01 -0800 Subject: [PATCH 7/8] Update prefect_gitlab/credentials.py --- prefect_gitlab/credentials.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prefect_gitlab/credentials.py b/prefect_gitlab/credentials.py index add4e5d..fed1829 100644 --- a/prefect_gitlab/credentials.py +++ b/prefect_gitlab/credentials.py @@ -45,6 +45,6 @@ def get_client(self) -> Gitlab: An authenticated GitLab client. """ # ref: https://python-gitlab.readthedocs.io/en/stable/ - gitlab = Gitlab(url=self.url, oauth_token=self.token) + gitlab = Gitlab(url=self.url, oauth_token=self.token.get_secret_value()) gitlab.auth() return gitlab From 338433152501770d6ebc7fd1cf5c4e0008436bd1 Mon Sep 17 00:00:00 2001 From: ahuang11 Date: Wed, 28 Dec 2022 11:51:59 -0800 Subject: [PATCH 8/8] Fix tests --- tests/test_credentials.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 7fcfbaf..cc811e2 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -11,6 +11,7 @@ def test_gitlab_credentials_get_client(monkeypatch): ) gitlab_credentials.get_client() mock_gitlab.assert_called_once_with( - url=gitlab_credentials.url, oauth_token=gitlab_credentials.token + url=gitlab_credentials.url, + oauth_token=gitlab_credentials.token.get_secret_value(), ) mock_gitlab.assert_called_once()