2020-07-11 19:15:00 +03:00
|
|
|
"""Services module."""
|
|
|
|
|
|
|
|
from github import Github
|
|
|
|
from github.Repository import Repository
|
|
|
|
from github.Commit import Commit
|
|
|
|
|
|
|
|
|
|
|
|
class SearchService:
|
|
|
|
"""Search service performs search on Github."""
|
|
|
|
|
|
|
|
def __init__(self, github_client: Github):
|
|
|
|
self._github_client = github_client
|
|
|
|
|
2020-07-20 23:58:18 +03:00
|
|
|
def search_repositories(self, query, limit):
|
2020-07-11 19:15:00 +03:00
|
|
|
"""Search for repositories and return formatted data."""
|
2020-07-20 23:58:18 +03:00
|
|
|
repositories = self._github_client.search_repositories(
|
|
|
|
query=query,
|
|
|
|
**{'in': 'name'},
|
|
|
|
)
|
2020-07-11 19:15:00 +03:00
|
|
|
return [
|
|
|
|
self._format_repo(repository)
|
|
|
|
for repository in repositories[:limit]
|
|
|
|
]
|
|
|
|
|
|
|
|
def _format_repo(self, repository: Repository):
|
2020-07-14 05:45:15 +03:00
|
|
|
commits = repository.get_commits()
|
2020-07-11 19:15:00 +03:00
|
|
|
return {
|
|
|
|
'url': repository.html_url,
|
|
|
|
'name': repository.name,
|
|
|
|
'owner': {
|
|
|
|
'login': repository.owner.login,
|
|
|
|
'url': repository.owner.html_url,
|
|
|
|
'avatar_url': repository.owner.avatar_url,
|
|
|
|
},
|
2020-07-14 05:45:15 +03:00
|
|
|
'latest_commit': self._format_commit(commits[0]) if commits else {},
|
2020-07-11 19:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def _format_commit(self, commit: Commit):
|
|
|
|
return {
|
|
|
|
'sha': commit.sha,
|
|
|
|
'url': commit.html_url,
|
|
|
|
'message': commit.commit.message,
|
|
|
|
'author_name': commit.commit.author.name,
|
|
|
|
}
|