Skip to content

Commit

Permalink
Refactor large function into smaller.
Browse files Browse the repository at this point in the history
No-Issue

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner committed Oct 15, 2023
1 parent 1f0968c commit e6d5365
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions galaxy_ng/app/api/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,58 @@
logger = logging.getLogger(__name__)


def find_real_role(github_user, github_repo):
"""
Given the github_user and github_repo attributes, find a matching
role with those matching values and then return the necessary
properties from the role needed to to do an import.
:param github_user:
The github_user as passed in to the CLI for imports.
:param github_repo:
The github_repo as passed in to the CLI for imports.
"""

# if we find a role, this will point at it
real_role = None

# figure out the actual namespace name
real_namespace_name = github_user

# figure out the actual github user
real_github_user = github_user

# figure out the actual github repo
real_github_repo = github_repo

# the role role can influence the clone url
clone_url = None

# some roles have their github_user set differently from their namespace name ...
candidates = LegacyRole.objects.filter(
full_metadata__github_user=github_user,
full_metadata__github_repo=github_repo
)
if candidates.count() > 0:
real_role = candidates.first()
logger.info(f'Using {real_role} as basis for import task')
rr_github_user = real_role.full_metadata.get('github_user')
rr_github_repo = real_role.full_metadata.get('github_repo')
real_namespace_name = real_role.namespace.name
if rr_github_user and rr_github_repo:
real_github_user = rr_github_user
real_github_repo = rr_github_repo
clone_url = f'https://github.com/{rr_github_user}/{rr_github_repo}'
elif rr_github_user:
real_github_user = rr_github_user
clone_url = f'https://github.com/{rr_github_user}/{github_repo}'
elif rr_github_repo:
real_github_repo = rr_github_repo
clone_url = f'https://github.com/{github_user}/{github_repo}'

return real_role, real_namespace_name, real_github_user, real_github_repo, clone_url


def legacy_role_import(
request_username=None,
github_user=None,
Expand Down Expand Up @@ -62,43 +114,17 @@ def legacy_role_import(
logger.info(f'ALTERNATE_ROLE_NAME:{alternate_role_name}')

clone_url = None
real_role = None
real_namespace_name = github_user
real_github_user = github_user
real_github_repo = github_repo
github_reference_is_tag = False

# prevent empty strings?
if not github_reference:
github_reference = None

# some roles have their github_user set differently from their namespace name ...
candidates = LegacyRole.objects.filter(
full_metadata__github_user=github_user,
full_metadata__github_repo=github_repo
)
if candidates.count() > 0:
real_role = candidates.first()
real_role, real_namespace_name, real_github_user, real_github_repo, clone_url = \
find_real_role(github_user, github_repo)
if real_role:
logger.info(f'Using {real_role} as basis for import task')
rr_github_user = real_role.full_metadata.get('github_user')
rr_github_repo = real_role.full_metadata.get('github_repo')
real_namespace_name = real_role.namespace.name
if rr_github_user and rr_github_repo:
real_github_user = rr_github_user
real_github_repo = rr_github_repo
logger.info(
f'using github_user:{real_github_user}'
+ f' and github_repo:{real_github_repo} from {real_role}'
)
clone_url = f'https://github.com/{rr_github_user}/{rr_github_repo}'
elif rr_github_user:
real_github_user = rr_github_user
logger.info(f'using github_user:{real_github_user} from {real_role}')
clone_url = f'https://github.com/{rr_github_user}/{github_repo}'
elif rr_github_repo:
real_github_repo = rr_github_repo
logger.info(f'using github_repo:{real_github_repo} from {real_role}')
clone_url = f'https://github.com/{github_user}/{github_repo}'

# this shouldn't happen but just in case ...
if request_username and not User.objects.filter(username=request_username).exists():
Expand Down

0 comments on commit e6d5365

Please sign in to comment.