Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup GitHub #59

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
14 changes: 13 additions & 1 deletion that_is_me_on_github/config.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
VERSION = "v0.0.5"
from enum import unique

from lib.utils import StrEnum

VERSION = "v0.0.4"


@unique
class TaskNameEnum(StrEnum):
PR = "pr"
ISSUE = "issue"
USER = "single_user"
OWNED_REPOS = "owned_repos"
13 changes: 12 additions & 1 deletion that_is_me_on_github/lib/utils.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
from github.Repository import Repository
from github import Github

from enum import Enum
from concurrent.futures import ThreadPoolExecutor


@@ -79,13 +80,23 @@ def issues_and_prs(
return issues_and_prs


class StrEnum(str, Enum):
def __new__(cls, *args):
for arg in args:
if not isinstance(arg, str):
raise TypeError('Not str: {}'.format(arg))
return super(StrEnum, cls).__new__(cls, *args)


def handle_tasks(tasks):
with ThreadPoolExecutor(max_workers=5) as executor:
futures = []
for task in tasks:
fn, args = task["func"], task["args"]
kwargs = task.get("kwargs", {}) # type: dict
name = task.get("name", fn.__name__)
future = executor.submit(fn, *args, **kwargs)
setattr(future, "name", name)
futures.append(future)
results = [future.result() for future in futures]
results = {future.name: future.result() for future in futures}
return results
59 changes: 43 additions & 16 deletions that_is_me_on_github/main.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
from lib.utils import *
from urllib3.util.retry import Retry
import os
from time import time
from config import TaskNameEnum


@click.group()
@@ -75,6 +75,8 @@ def generate(
org_filter: str,
repo_filter: str,
output: str,
timeout: int,
retry: int,
):
path = os.path.expanduser(output)
try:
@@ -93,9 +95,19 @@ def generate(
"Your github password", type=str, hide_input=True
)

g = Github(auth_username, auth_password)
g = Github(
login_or_token=auth_username,
password=auth_password,
timeout=int(timeout),
retry=Retry(retry),
)
else:
g = Github()
g = Github(timeout=int(timeout), retry=Retry(retry))

# user = single_user(g, username)
# if not user:
# click.echo("User {} Not Found.".format(username))
# raise click.Abort()

click.echo("Please wait for a few seconds.")

@@ -107,26 +119,41 @@ def generate(
)

container = [
{"func": owned_repos, "args": [g, username]},
{"func": issues_and_prs, "args": [g, username], "kwargs": {'type': "issue",
'orgs': org_filter,
'repos': repo_filter}},
{"func": single_user, "args": [g, username]},
{"func": issues_and_prs, "args": [g, username], "kwargs": {'type': "pr",
'orgs': org_filter,
'repos': repo_filter}},
{"name": TaskNameEnum.OWNED_REPOS,
"func": owned_repos,
"args": [g, username]},

{"name": TaskNameEnum.ISSUE,
"func": issues_and_prs,
"args": [g, username],
"kwargs": {'type': "issue",
'orgs': org_filter,
'repos': repo_filter}},

{"name": TaskNameEnum.USER,
"func": single_user,
"args": [g, username]},

{"name": TaskNameEnum.PR,
"func": issues_and_prs,
"args": [g, username],
"kwargs": {'type': "pr",
'orgs': org_filter,
'repos': repo_filter}},
]

results = handle_tasks(container)
if not results[2]:
user_info, repo_info, pr_info, issue_info = results[TaskNameEnum.USER], results[TaskNameEnum.OWNED_REPOS], \
results[TaskNameEnum.PR], results[TaskNameEnum.ISSUE]
if not user_info:
click.echo("User {} Not Found.".format(username))
raise click.Abort()

Render().render(
results[2],
results[0],
results[3],
results[1],
user_info,
repo_info,
pr_info,
issue_info,
path,
)