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

Regex fix #31

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/gitcomp/gitcomp_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
import sys

from .user import User
from .repository import Repository
from typing import List, Dict
from typing import List, Dict, Pattern
from .net_mod import NetMod


Expand All @@ -10,8 +12,14 @@ class GitComp:
repos: List[str]
user_data: Dict[str, User] = None
repo_data: Dict[str, Repository] = None
__username_regex = r'^[a-zA-Z0-9]+'
__repo_regex = r'^[A-Za-z0-9]+/[A-Za-z0-9]+'
# GitHub usernames can be alpha numerical characters with at-most 1 - except in the beginning and the end
# https://regex101.com/r/sYIEPE/2
__username_regex: Pattern = re.compile(r'^([A-Za-z0-9])([A-Za-z0-9]*)(-)?([A-Za-z0-9]*)([A-Za-z0-9]$)')
# Repository names are currently limited to 100 characters, some special names like . are reserved
# https://regex101.com/r/ibfDFk/2
__repo_regex: Pattern = re.compile(
r'^([A-Za-z0-9])([A-Za-z0-9]*)(-)?([A-Za-z0-9]*)([A-Za-z0-9])(/)((?=[^.])([^!#@%^&+=()]*)$|((\.+)(['
r'^!#@%^&+=()]+)$))')

def __init__(self, users: List[str] = None, repos: List[str] = None):
self.users = users
Expand All @@ -25,27 +33,34 @@ def __init__(self, users: List[str] = None, repos: List[str] = None):
self.__fetch_repo_data()

def __fetch_user_data(self):
self.__validate_user_names()
try:
self.__validate_user_names()
except ValueError as e:
sys.exit(e)
response = NetMod().fetch_users_data(self.users)
for user in response:
self.user_data[user] = User(response[user])

def __validate_user_names(self):
for user in self.users:
if not re.match(GitComp.__username_regex, user):
if not GitComp.__username_regex.fullmatch(user):
raise ValueError(f"""
Improper username {user}
""")

def __fetch_repo_data(self):
self.__validate_repo_string()
try:
self.__validate_repo_string()
except ValueError as e:
sys.exit(e)

response = NetMod().fetch_repos_data(self.repos)
for repo in response:
self.repo_data[repo] = Repository(response[repo])

def __validate_repo_string(self):
for repo in self.repos:
if not re.match(GitComp.__repo_regex, repo):
if not GitComp.__repo_regex.fullmatch(repo):
raise ValueError("""
Improper repository format.
Provide the repository name as: <user-name>/<repository-name>
Expand Down