Skip to content

Commit

Permalink
update github-urllink
Browse files Browse the repository at this point in the history
  • Loading branch information
wj-Mcat committed Nov 5, 2022
1 parent fb36c49 commit a07d552
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ test-unit: pytest
.PHONY: test
test: check-python-version lint pytest

.PHONY: format
format:
yapf -q $(SOURCE_GLOB)

.PHONY: check-python-version
check-python-version:
./scripts/check_python_version.py
Expand Down
175 changes: 175 additions & 0 deletions src/wechaty/user/url_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,190 @@
Optional,
Type
)
from urllib3 import get_host
from wechaty_puppet import UrlLinkPayload, get_logger

from wechaty.utils.link import get_url_metadata

from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List
from abc import abstractmethod
from github import Github
from github.Repository import Repository

from github.PullRequest import PullRequest
from github.PullRequestComment import PullRequestComment

from github.Issue import Issue
from github.IssueComment import IssueComment

from wechaty_puppet.schemas.url_link import UrlLinkPayload


log = get_logger('UrlLink')


class UrlLinkParser:
# valid host-name of one parser
host_names: List[str] = []

@classmethod
def can_parser(cls, url: str) -> bool:
if not cls.host_names:
raise ValueError(f"please set valid host-names for parser, eg: ['github']")

_, host_name, _ = get_host(url)
return host_name in cls.host_names

@abstractmethod
def parse(self, url: str) -> UrlLinkPayload:
raise NotImplementedError("<parse> method must be overried by sub-class of <UrlLinkParser>")


class GithubUrlLinkParser(UrlLinkParser):
"""Parse Urllink by url"""

host_names: List[str] = ['github']

def __init__(self, token: Optional[str] = None):
self._github = Github(login_or_token=token)
self._repositories: Dict[str, Repository] = {}

@staticmethod
def can_parser(url: str) -> bool:
"""the source of url
Args:
url (str): github urllink
Returns:
bool: wheter is github based urll
"""
_, host_name, _ = get_host(url)
return host_name

def parse(self, url: str) -> UrlLinkPayload:
"""parse url-link as payload
Args:
url (str): the url-of link
Returns:
UrlLinkPayload: the instance of final url-link payload
"""
pass

def get_repo(self, repo_name: str) -> Repository:
"""get Repository instance which can fetch the issue/pull-request info
Args:
repo_name (str): the full name of repo, eg: wechaty/python-wechaty
Returns:
Repository: the repository instance
"""
if repo_name in self._repositories:
return self._repositories[repo_name]

repo = self._github.get_repo(repo_name)
self._repositories[repo_name] = repo
return repo

def get_pr_payload(self, repo_name: str, pr_id: int) -> UrlLinkPayload:
"""get pull-request main-body info
Args:
repo_name (str): the full name of repo
pr_id (int): the is of pull-request
Returns:
UrlLinkPayload: the UrlLink payload
"""
repo: Repository = self.get_repo(repo_name)
pull_request: PullRequest = repo.get_pull(pr_id)
payload = UrlLinkPayload(
url=pull_request.html_url,
title=pull_request.title,
description=pull_request.body,
thumbnailUrl=pull_request.user.avatar_url
)
return payload

def get_pr_comment_payload(self, repo_name: str, pr_id: int, comment_id: int, comment_type: str = 'issue') -> UrlLinkPayload:
"""get comment of pull-request, which can be issue or review comment
Args:
repo_name (str): the full name of repo
pr_id (int): the id of pr
comment_id (int): the id of target comment
comment_type (str, optional): issue/review. Defaults to 'issue'.
Returns:
UrlLinkPayload: _description_
"""
repo: Repository = self.get_repo(repo_name)
pull_request: PullRequest = repo.get_pull(pr_id)
if comment_type == 'issue':
comment: PullRequestComment = pull_request.get_issue_comment(comment_id)
else:
comment: PullRequestComment = pull_request.get_review_comment(comment_id)

payload = UrlLinkPayload(
url=comment.html_url,
title=pull_request.title,
description=comment.body,
thumbnailUrl=comment.user.avatar_url
)
return payload

def get_issue_payload(self, repo_name: str, issue_id: int) -> UrlLinkPayload:
"""get the issue body
Args:
repo_name (str): the full name of repo
issue_id (int): the id of issue
Returns:
UrlLinkPayload: the UrlLink payload instance
"""
repo: Repository = self.get_repo(repo_name)
issue: Issue = repo.get_issue(issue_id)
payload = UrlLinkPayload(
url=issue.html_url,
title=issue.title,
description=issue.body,
thumbnailUrl=issue.user.avatar_url
)
return payload

def get_issue_comment_payload(self, repo_name: str, issue_id: int, comment_id: int) -> UrlLinkPayload:
"""the issue comment payload
Args:
repo_name (str): the full-name of repo
issue_id (int): the id of target issue
comment_id (int): the comment id of the specific issue
Returns:
UrlLinkPayload: the UrlLink payload instance
"""
repo: Repository = self.get_repo(repo_name)
issue: Issue = repo.get_issue(issue_id)
comment: IssueComment = issue.get_comment(comment_id)

payload = UrlLinkPayload(
url=comment.html_url,
title=issue.title,
description=comment.body,
thumbnailUrl=comment.user.avatar_url
)
return payload





class UrlLink:
"""
url_link object which handle the url_link content
Expand Down
17 changes: 16 additions & 1 deletion tests/url_link_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
"""unit test for urllink"""
from __future__ import annotations

from wechaty.user.url_link import UrlLink
from unittest import TestCase
from wechaty.user.url_link import UrlLink, GithubUrlLinkParser




class TestUrlLink(TestCase):
def setUp(self) -> None:
self.sample_issue_link = 'https://github.com/wechaty/python-wechaty/issues/339'
self.sample_issue_comment_link = 'https://github.com/wechaty/python-wechaty/issues/339'

def test_create():
"""unit test for creating"""
UrlLink.create(
Expand All @@ -12,3 +20,10 @@ def test_create():
thumbnail_url='thu',
description='simple desc'
)


def test_github_payload():
parser = GithubUrlLinkParser()



0 comments on commit a07d552

Please sign in to comment.