From 6befc06513cb6dace0ef46aae8cc1c5db445c7eb Mon Sep 17 00:00:00 2001 From: Dominik Rumian Date: Jun 30 2022 12:45:08 +0000 Subject: Refactoring of _request_branch method Nesting if-statements usually leads to worse maintainability and undestandability of a code. This commit refactors code in such a way that nested if-statements are removed. Signed-off-by: Dominik Rumian --- diff --git a/fedpkg/cli.py b/fedpkg/cli.py index 8fc8f8d..8b672b6 100644 --- a/fedpkg/cli.py +++ b/fedpkg/cli.py @@ -1097,19 +1097,44 @@ class fedpkgClient(cliClient): :return: None """ - if all_releases: - if branch: - raise rpkgError('You cannot specify a branch with the ' - '"--all-releases" option') - elif service_levels: - raise rpkgError('You cannot specify service levels with the ' - '"--all-releases" option') - elif not branch: - if active_branch: - branch = active_branch - else: - raise rpkgError('You must specify a branch if you are not in ' - 'a git repository') + def get_branch(): + """Returns the branch according to inputs.""" + if playground_match: + return playground_match.groups()[0] + if next_match: + return next_match.groups()[0] + return branch + + def check_input_parameters(): + """Checks input parameters. + + If there is not a valid combination of input parameter values + the error message will be set. + """ + if all_releases and branch: + return 'You cannot specify a branch with the ' \ + '"--all-releases" option' + if all_releases and service_levels: + return 'You cannot specify service levels with the ' \ + '"--all-releases" option' + if not (branch or all_releases) and not active_branch: + return 'You must specify a branch if you are not in ' \ + 'a git repository' + + def check_branches(): + if _branch in release_branches and service_levels: + return 'You can\'t provide SLs for release branches' + if _branch not in release_branches and re.match(RELEASE_BRANCH_REGEX, branch): + return '{0} is a current release branch'.format(branch) + if _branch not in release_branches and not service_levels: + return'You must provide SLs for non-release branches {0}'.format(branch) + + status_message = check_input_parameters() + if status_message: + raise rpkgError(status_message) + + if not (branch or all_releases) and active_branch: + branch = active_branch pdc_url = config.get('{0}.pdc'.format(name), 'url') if branch: @@ -1129,23 +1154,11 @@ class fedpkgClient(cliClient): playground_match = re.match(r'^(epel\d+)-playground$', branch) # treat epel*-next the same as epel* release branches next_match = re.match(r'^(epel\d+)-next$', branch) - if playground_match: - _branch = playground_match.groups()[0] - elif next_match: - _branch = next_match.groups()[0] - else: - _branch = branch - if _branch in release_branches: - if service_levels: - raise rpkgError( - 'You can\'t provide SLs for release branches') - else: - if re.match(RELEASE_BRANCH_REGEX, branch): - raise rpkgError('{0} is a current release branch' - .format(branch)) - elif not service_levels: - raise rpkgError( - 'You must provide SLs for non-release branches (%s)' % branch) + _branch = get_branch() + + branch_error_message = check_branches() + if branch_error_message: + raise rpkgError(branch_error_message) # If service levels were provided, verify them if service_levels: