Skip to content

Commit

Permalink
feat: add get_user
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 15, 2024
1 parent e8d264b commit c5f241b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/githarbor/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def from_url(cls, url: str, **kwargs: Any) -> BaseRepository:
msg = f"{cls.__name__} does not implement from_url"
raise FeatureNotSupportedError(msg)

def get_user(self) -> User:
msg = f"{self.__class__.__name__} does not implement get_user"
raise FeatureNotSupportedError(msg)

def get_branch(self, name: str) -> Branch:
msg = f"{self.__class__.__name__} does not implement get_branch"
raise FeatureNotSupportedError(msg)
Expand Down
8 changes: 8 additions & 0 deletions src/githarbor/core/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def default_branch(self) -> str:
"""
return self._repository.default_branch

def get_user(self) -> User:
"""Get information about the repository user.
Returns:
User information.
"""
return self._repository.get_user()

def get_branch(self, name: str) -> Branch:
"""Get information about a specific branch.
Expand Down
37 changes: 37 additions & 0 deletions src/githarbor/providers/azurerepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Issue,
PullRequest,
Release,
User,
)
from githarbor.exceptions import AuthenticationError, ResourceNotFoundError
from githarbor.providers import azuretools
Expand Down Expand Up @@ -119,6 +120,42 @@ def default_branch(self) -> str:
"""Default branch name."""
return self._repo.default_branch or "main"

@azuretools.handle_azure_errors("Failed to get user {username}")
def get_user(self, username: str | None = None) -> User:
"""Get user information.
If username is not provided, returns the authenticated user.
Args:
username: Optional username to get information for.
Returns:
User model with user information.
"""
# Get the identity client
identity_client = self._connection.clients.get_identity_client()

if username is None:
# Get the authenticated user
core_client = self._connection.clients.get_core_client()
user_info = core_client.get_authorized_user()
else:
# Search for the specific user
user_descriptor = identity_client.read_identities(
search_filter=f"General,localAccount,{username}",
)
if not user_descriptor or not user_descriptor[0]:
msg = f"User {username} not found"
raise ResourceNotFoundError(msg)
user_info = user_descriptor[0]

return User(
username=user_info.properties.get("Account", user_info.unique_name),
name=user_info.display_name or user_info.unique_name,
email=user_info.properties.get("Mail", ""),
avatar_url=user_info.properties.get("Avatar", ""),
)

@azuretools.handle_azure_errors("Failed to get branch {name}")
def get_branch(self, name: str) -> Branch:
"""Get branch information."""
Expand Down
6 changes: 6 additions & 0 deletions src/githarbor/providers/gitearepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def get_branch(self, name: str) -> Branch:
branch = self._repo_api.repo_get_branch(self._owner, self._name, name)
return giteatools.create_branch_model(branch)

@giteatools.handle_api_errors("Failed to get repository owner info")
def get_user(self) -> User:
"""Get user (repository owner) information."""
user = self._user_api.user_get_current()
return giteatools.create_user_model(user)

@giteatools.handle_api_errors("Failed to get pull request")
def get_pull_request(self, number: int) -> PullRequest:
"""Get a specific pull request by number."""
Expand Down
5 changes: 5 additions & 0 deletions src/githarbor/providers/githubrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ def iter_files(
elif not pattern or fnmatch.fnmatch(content.path, pattern):
yield content.path

@githubtools.handle_github_errors("Failed to get repository owner info")
def get_user(self) -> User:
"""Get user (repository owner) information."""
return githubtools.create_user_model(self.user)

@githubtools.handle_github_errors("Failed to get contributors")
def get_contributors(
self,
Expand Down
8 changes: 7 additions & 1 deletion src/githarbor/providers/gitlabrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def name(self) -> str:
def default_branch(self) -> str:
return self._repo.default_branch

@gitlabtools.handle_gitlab_errors("Failed to get user info")
def get_user(self) -> User:
"""Get user (repository owner) information."""
user = self._gl.users.list(username=self._owner)[0]
return gitlabtools.create_user_model(user)

@gitlabtools.handle_gitlab_errors("Branch {name} not found")
def get_branch(self, name: str) -> Branch:
branch = self._repo.branches.get(name)
Expand Down Expand Up @@ -379,4 +385,4 @@ def list_tags(self) -> list[Tag]:

if __name__ == "__main__":
repo = GitLabRepository("phil65", "test")
print(repo.list_issues())
print(repo.get_user())

0 comments on commit c5f241b

Please sign in to comment.