# Path: f8a_worker/utils.py
# def parse_gh_repo(potential_url):
#     """Cover the following variety of URL forms for Github repo referencing.
# 
#     1) www.github.com/foo/bar
#     2) (same as above, but with ".git" in the end)
#     3) (same as the two above, but without "www.")
#     # all of the three above, but starting with "http://", "https://", "git://" or "git+https://"
#     4) git@github.com:foo/bar
#     5) (same as above, but with ".git" in the end)
#     6) (same as the two above but with "ssh://" in front or with "git+ssh" instead of "git")
# 
#     We return repository name in form `<username>/<reponame>` or `None` if this does not
#     seem to be a Github repo (or if someone invented yet another form that we can't parse yet...)
# 
#     Notably, the Github repo *must* have exactly username and reponame, nothing else and nothing
#     more. E.g. `github.com/<username>/<reponame>/<something>` is *not* recognized.
#     """
#     # TODO: reduce cyclomatic complexity
#     if not potential_url:
#         return None
# 
#     repo_name = None
#     # transform 4-6 to a URL-like string, so that we can handle it together with 1-3
#     if '@' in potential_url:
#         split = potential_url.split('@')
#         if len(split) == 2 and split[1].startswith('github.com:'):
#             potential_url = 'http://' + split[1].replace('github.com:', 'github.com/')
# 
#     # make it parsable by urlparse if it doesn't contain scheme
#     if not potential_url.startswith(('http://', 'https://', 'git://', 'git+https://')):
#         potential_url = 'http://' + potential_url
# 
#     # urlparse should handle it now
#     parsed = urlparse(potential_url)
#     if parsed.netloc in ['github.com', 'www.github.com'] and \
#             parsed.scheme in ['http', 'https', 'git', 'git+https']:
#         repo_name = parsed.path
#         if repo_name.endswith('.git'):
#             repo_name = repo_name[:-len('.git')]
# 
#     if repo_name:
#         repo_name = repo_name.strip('/')
#         if len(repo_name.split('/')) > 2:
#             temp_list = repo_name.split('/')
#             repo_name = temp_list[0] + '/' + temp_list[1]
#         if repo_name.count('/') != 1:
#             return None
# 
#     return repo_name


from functools import reduce
from f8a_worker.utils import parse_gh_repo
from selinon.predicates import alwaysFalse
from selinon.predicates import alwaysTrue
from selinon.predicates import argsEmpty
from selinon.predicates import argsFieldBool
from selinon.predicates import argsFieldContain
from selinon.predicates import argsFieldDict
from selinon.predicates import argsFieldEqual
from selinon.predicates import argsFieldExist
from selinon.predicates import argsFieldFloat
from selinon.predicates import argsFieldGreater
from selinon.predicates import argsFieldGreaterEqual
from selinon.predicates import argsFieldInt
from selinon.predicates import argsFieldLenEqual
from selinon.predicates import argsFieldLenGreater
from selinon.predicates import argsFieldLenGreaterEqual
from selinon.predicates import argsFieldLenLess
from selinon.predicates import argsFieldLenNotEqual
from selinon.predicates import argsFieldLess
from selinon.predicates import argsFieldLessEqual
from selinon.predicates import argsFieldList
from selinon.predicates import argsFieldNone
from selinon.predicates import argsFieldNotEqual
from selinon.predicates import argsFieldStr
from selinon.predicates import argsFieldUrlNetloc
from selinon.predicates import argsFieldUrlPath
from selinon.predicates import argsFieldUrlScheme
from selinon.predicates import argsIsBool
from selinon.predicates import argsIsDict
from selinon.predicates import argsIsFloat
from selinon.predicates import argsIsInt
from selinon.predicates import argsIsList
from selinon.predicates import argsIsNone
from selinon.predicates import argsIsStr
from selinon.predicates import empty
from selinon.predicates import envEqual
from selinon.predicates import envExist
from selinon.predicates import fieldBool
from selinon.predicates import fieldContain
from selinon.predicates import fieldDict
from selinon.predicates import fieldEqual
from selinon.predicates import fieldExist
from selinon.predicates import fieldFloat
from selinon.predicates import fieldGreater
from selinon.predicates import fieldGreaterEqual
from selinon.predicates import fieldInt
from selinon.predicates import fieldLenEqual
from selinon.predicates import fieldLenGreater
from selinon.predicates import fieldLenGreaterEqual
from selinon.predicates import fieldLenLess
from selinon.predicates import fieldLenNotEqual
from selinon.predicates import fieldLess
from selinon.predicates import fieldLessEqual
from selinon.predicates import fieldList
from selinon.predicates import fieldNone
from selinon.predicates import fieldNotEqual
from selinon.predicates import fieldStr
from selinon.predicates import fieldUrlNetloc
from selinon.predicates import fieldUrlPath
from selinon.predicates import fieldUrlScheme
from selinon.predicates import httpStatus
from selinon.predicates import isBool
from selinon.predicates import isDict
from selinon.predicates import isFloat
from selinon.predicates import isInt
from selinon.predicates import isList
from selinon.predicates import isNone
from selinon.predicates import isStr
assert fieldGreaterEqual
assert fieldInt
assert fieldLenEqual
assert fieldLenGreater
assert fieldLenGreaterEqual
assert fieldLenLess
assert fieldLenNotEqual
assert fieldLess
assert fieldLessEqual
assert fieldList
assert fieldNone
assert fieldNotEqual
assert fieldStr
assert fieldUrlNetloc
assert fieldUrlPath
assert fieldUrlScheme
assert httpStatus
assert isBool
assert isDict
assert isFloat
assert isInt
assert isList
assert isNone
assert isStr


def isGhRepo(node_args, key):
    """Predicate if the repository is on GitHub."""
    try:
        val = reduce(lambda m, k: m[k], key if isinstance(key, list) else [key], node_args)

# Path: f8a_worker/utils.py
# def parse_gh_repo(potential_url):
#     """Cover the following variety of URL forms for Github repo referencing.
# 
#     1) www.github.com/foo/bar
#     2) (same as above, but with ".git" in the end)
#     3) (same as the two above, but without "www.")
#     # all of the three above, but starting with "http://", "https://", "git://" or "git+https://"
#     4) git@github.com:foo/bar
#     5) (same as above, but with ".git" in the end)
#     6) (same as the two above but with "ssh://" in front or with "git+ssh" instead of "git")
# 
#     We return repository name in form `<username>/<reponame>` or `None` if this does not
#     seem to be a Github repo (or if someone invented yet another form that we can't parse yet...)
# 
#     Notably, the Github repo *must* have exactly username and reponame, nothing else and nothing
#     more. E.g. `github.com/<username>/<reponame>/<something>` is *not* recognized.
#     """
#     # TODO: reduce cyclomatic complexity
#     if not potential_url:
#         return None
# 
#     repo_name = None
#     # transform 4-6 to a URL-like string, so that we can handle it together with 1-3
#     if '@' in potential_url:
#         split = potential_url.split('@')
#         if len(split) == 2 and split[1].startswith('github.com:'):
#             potential_url = 'http://' + split[1].replace('github.com:', 'github.com/')
# 
#     # make it parsable by urlparse if it doesn't contain scheme
#     if not potential_url.startswith(('http://', 'https://', 'git://', 'git+https://')):
#         potential_url = 'http://' + potential_url
# 
#     # urlparse should handle it now
#     parsed = urlparse(potential_url)
#     if parsed.netloc in ['github.com', 'www.github.com'] and \
#             parsed.scheme in ['http', 'https', 'git', 'git+https']:
#         repo_name = parsed.path
#         if repo_name.endswith('.git'):
#             repo_name = repo_name[:-len('.git')]
# 
#     if repo_name:
#         repo_name = repo_name.strip('/')
#         if len(repo_name.split('/')) > 2:
#             temp_list = repo_name.split('/')
#             repo_name = temp_list[0] + '/' + temp_list[1]
#         if repo_name.count('/') != 1:
#             return None
# 
#     return repo_name


# Path: f8a_worker/dispatcher/predicates.py
from functools import reduce
from f8a_worker.utils import parse_gh_repo
from selinon.predicates import alwaysFalse
from selinon.predicates import alwaysTrue
from selinon.predicates import argsEmpty
from selinon.predicates import argsFieldBool
from selinon.predicates import argsFieldContain
from selinon.predicates import argsFieldDict
from selinon.predicates import argsFieldEqual
from selinon.predicates import argsFieldExist
from selinon.predicates import argsFieldFloat
from selinon.predicates import argsFieldGreater
from selinon.predicates import argsFieldGreaterEqual
from selinon.predicates import argsFieldInt
from selinon.predicates import argsFieldLenEqual
from selinon.predicates import argsFieldLenGreater
from selinon.predicates import argsFieldLenGreaterEqual
from selinon.predicates import argsFieldLenLess
from selinon.predicates import argsFieldLenNotEqual
from selinon.predicates import argsFieldLess
from selinon.predicates import argsFieldLessEqual
from selinon.predicates import argsFieldList
from selinon.predicates import argsFieldNone
from selinon.predicates import argsFieldNotEqual
from selinon.predicates import argsFieldStr
from selinon.predicates import argsFieldUrlNetloc
from selinon.predicates import argsFieldUrlPath
from selinon.predicates import argsFieldUrlScheme
from selinon.predicates import argsIsBool
from selinon.predicates import argsIsDict
from selinon.predicates import argsIsFloat
from selinon.predicates import argsIsInt
from selinon.predicates import argsIsList
from selinon.predicates import argsIsNone
from selinon.predicates import argsIsStr
from selinon.predicates import empty
from selinon.predicates import envEqual
from selinon.predicates import envExist
from selinon.predicates import fieldBool
from selinon.predicates import fieldContain
from selinon.predicates import fieldDict
from selinon.predicates import fieldEqual
from selinon.predicates import fieldExist
from selinon.predicates import fieldFloat
from selinon.predicates import fieldGreater
from selinon.predicates import fieldGreaterEqual
from selinon.predicates import fieldInt
from selinon.predicates import fieldLenEqual
from selinon.predicates import fieldLenGreater
from selinon.predicates import fieldLenGreaterEqual
from selinon.predicates import fieldLenLess
from selinon.predicates import fieldLenNotEqual
from selinon.predicates import fieldLess
from selinon.predicates import fieldLessEqual
from selinon.predicates import fieldList
from selinon.predicates import fieldNone
from selinon.predicates import fieldNotEqual
from selinon.predicates import fieldStr
from selinon.predicates import fieldUrlNetloc
from selinon.predicates import fieldUrlPath
from selinon.predicates import fieldUrlScheme
from selinon.predicates import httpStatus
from selinon.predicates import isBool
from selinon.predicates import isDict
from selinon.predicates import isFloat
from selinon.predicates import isInt
from selinon.predicates import isList
from selinon.predicates import isNone
from selinon.predicates import isStr

assert fieldGreaterEqual
assert fieldInt
assert fieldLenEqual
assert fieldLenGreater
assert fieldLenGreaterEqual
assert fieldLenLess
assert fieldLenNotEqual
assert fieldLess
assert fieldLessEqual
assert fieldList
assert fieldNone
assert fieldNotEqual
assert fieldStr
assert fieldUrlNetloc
assert fieldUrlPath
assert fieldUrlScheme
assert httpStatus
assert isBool
assert isDict
assert isFloat
assert isInt
assert isList
assert isNone
assert isStr


def isGhRepo(node_args, key):
    """Predicate if the repository is on GitHub."""
    try:
        val = reduce(lambda m, k: m[k], key if isinstance(key, list) else [key], node_args)

        if parse_gh_repo(val):