1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
todoist-github/todoist_github/utils/__init__.py

48 lines
1.3 KiB
Python
Raw Permalink Normal View History

import re
2020-01-08 21:05:58 +00:00
from typing import Optional
from urllib.parse import urlparse
from github.Issue import Issue
2020-03-01 12:57:13 +00:00
from github.PullRequest import PullRequest
from urlextract import URLExtract
GITHUB_ISSUE_PR_RE = re.compile(r"\/(.+?)\/(.+?)\/(pull|issues)\/(\d+?)$")
2020-01-08 21:05:58 +00:00
extractor = URLExtract()
def get_github_task(content) -> Optional[str]:
if "github" not in content.lower():
return None
for url in extractor.gen_urls(content):
parsed_url = urlparse(url)
if parsed_url.netloc == "github.com" and GITHUB_ISSUE_PR_RE.search(
parsed_url.path
):
2020-01-08 21:05:58 +00:00
return url
2020-01-08 22:42:33 +00:00
return None
def get_github_issue_details(content):
url = get_github_task(content)
if not url:
return
parsed_url = urlparse(url)
match = GITHUB_ISSUE_PR_RE.search(parsed_url.path)
2020-01-08 22:42:33 +00:00
if not match:
return
return match.group(1), match.group(2), match.group(4)
def get_issue(me, org, repo, issue_num):
headers, data = me._requester.requestJsonAndCheck(
"GET", f"/repos/{org}/{repo}/issues/{issue_num}"
)
return Issue(me._requester, headers, data, completed=True)
2020-03-01 12:57:13 +00:00
def get_my_review(me, pr: PullRequest):
for review in pr.get_reviews().reversed:
if review.user.login == me.login:
return review