From 0680af5621da011dfeedafbbc9f11b46a081c68a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 21 2017 14:49:34 +0000 Subject: Consolidate the code to reduce the duplication --- diff --git a/pagure/api/fork.py b/pagure/api/fork.py index 3496a9c..1e5d62b 100644 --- a/pagure/api/fork.py +++ b/pagure/api/fork.py @@ -343,15 +343,11 @@ def api_pull_request_merge(repo, requestid, username=None, namespace=None): if threshold > 0 and int(request.score) < int(threshold): raise pagure.exceptions.APIError(403, error_code=APIERROR.EPRSCORE) - mandatory_flags = pagure.APP.config.get('MANDATORY_FLAGS') - if mandatory_flags: - flag_ok = False - for flag in request.flags: - if flag.token_id in mandatory_flags: - flag_ok = flag.percent == 100 - if not flag_ok: - raise pagure.exceptions.APIError( - 403, error_code=APIERROR.MISSINGFLAG) + flag_ok = request.mandatory_flags_ok( + pagure.APP.config.get('MANDATORY_FLAGS')) + if not flag_ok: + raise pagure.exceptions.APIError( + 403, error_code=APIERROR.MISSINGFLAG) try: message = pagure.lib.git.merge_pull_request( diff --git a/pagure/lib/model.py b/pagure/lib/model.py index c4ca355..3855410 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -1686,6 +1686,19 @@ class PullRequest(BASE): for comment in self.comments if not comment.notification] + def mandatory_flags_ok(self, mandatory_flags): + ''' Returns if a PR can be merged based on whether it has the + mandatory flags. + ''' + if mandatory_flags: + flag_ok = False + for flag in self.flags: + if flag.token_id in mandatory_flags: + flag_ok = flag.percent == 100 + else: + flag_ok = True + return flag_ok + def to_json(self, public=False, api=False, with_comments=True): ''' Returns a dictionnary representation of the pull-request. diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index 528789f..f9d8ae4 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -733,19 +733,15 @@ def merge_request_pull(repo, requestid, username=None, namespace=None): 'request_pull', username=username, namespace=namespace, repo=repo.name, requestid=requestid)) - mandatory_flags = pagure.APP.config.get('MANDATORY_FLAGS') - if mandatory_flags: - flag_ok = False - for flag in request.flags: - if flag.token_id in mandatory_flags: - flag_ok = flag.percent == 100 - if not flag_ok: - flask.flash( - 'This request does not have any of the mandatory flags and' - 'thus is now allowed to be merged', 'error') - return flask.redirect(flask.url_for( - 'request_pull', username=username, namespace=namespace, - repo=repo.name, requestid=requestid)) + flag_ok = request.mandatory_flags_ok( + pagure.APP.config.get('MANDATORY_FLAGS')) + if not flag_ok: + flask.flash( + 'This request does not have any of the mandatory flags and' + 'thus is now allowed to be merged', 'error') + return flask.redirect(flask.url_for( + 'request_pull', username=username, namespace=namespace, + repo=repo.name, requestid=requestid)) try: message = pagure.lib.git.merge_pull_request(