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.py

38 lines
1 KiB
Python
Raw 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
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
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)
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)