Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from PrefectHQ/add_client
Browse files Browse the repository at this point in the history
Add client
  • Loading branch information
desertaxle authored Dec 28, 2022
2 parents 2c94a03 + 3384331 commit 4542732
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `get_client` method and `url` field to `GitLabCredentials` - [#9](https://github.com/PrefectHQ/prefect-gitlab/pull/9)

### Changed

### Deprecated
Expand All @@ -25,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)
19 changes: 18 additions & 1 deletion prefect_gitlab/credentials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module used to enable authenticated interactions with GitLab"""

from gitlab import Gitlab
from prefect.blocks.core import Block
from pydantic import Field, HttpUrl, SecretStr

Expand All @@ -10,7 +11,8 @@ class GitLabCredentials(Block):
repositories.
Attributes:
token: personal access token to authenticate with GitLab.
token: The personal access token to authenticate with GitLab.
url: URL to self-hosted GitLab instances.
Examples:
Load stored GitLab credentials:
Expand All @@ -31,3 +33,18 @@ class GitLabCredentials(Block):
default=None,
description="A GitLab Personal Access Token with read_repository 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.get_secret_value())
gitlab.auth()
return gitlab
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
prefect>=2.3.0
pydantic
prefect>=2.7.0
python-gitlab>=3.12.0
17 changes: 17 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from unittest.mock import MagicMock

from prefect_gitlab.credentials import GitLabCredentials


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.get_secret_value(),
)
mock_gitlab.assert_called_once()

0 comments on commit 4542732

Please sign in to comment.