From 7143eec302748e5d366adcfe46f0ce1318e0b07a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 04 2018 08:46:32 +0000 Subject: [PATCH 1/3] Support calls from jenkins indicating the build is started Basically with this commit, if we receive a call from jenkins about a certain job, we'll wait 5 seconds to see if this job completes, if it does not we will flag the pull-request with a flag saying that this build is pending (ie: in progress). Then when the build finishes, we'll loop over all the tags of the pull-request and see if one is pending and refers to the same job, if it does we will just update this flag instead of adding a new one. Fixes https://pagure.io/pagure/issue/2833 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/lib_ci.py b/pagure/lib/lib_ci.py index 7208fda..0b98bec 100644 --- a/pagure/lib/lib_ci.py +++ b/pagure/lib/lib_ci.py @@ -26,8 +26,10 @@ from pagure.config import config as pagure_config _log = logging.getLogger(__name__) BUILD_STATS = { - 'SUCCESS': ('Build successful', 100), - 'FAILURE': ('Build failed', 0), + 'SUCCESS': ('Build successful', pagure_config['FLAG_SUCCESS'], 100), + 'FAILURE': ('Build failed', pagure_config['FLAG_FAILURE'], 0), + 'ABORTED': ('Build aborted', 'error', 0), + 'BUILDING': ('Build in progress', pagure_config['FLAG_PENDING'], 0), } @@ -47,17 +49,20 @@ def process_jenkins_build( build_info = jenk.get_build_info(jenkins_name, build_id) if build_info.get('building') is True: - _log('Build is still going, let\'s wait a sec and try again') - if iteration == 10: - raise pagure.exceptions.NoCorrespondingPR( - "We've been waiting for 10 seconds and the build is still " - "not finished.") - time.sleep(1) - return process_jenkins_build( - session, project, build_id, requestfolder, - iteration=iteration + 1) + if iteration < 5: + _log.info('Build is still going, let\'s wait a sec and try again') + time.sleep(1) + return process_jenkins_build( + session, project, build_id, requestfolder, + iteration=iteration + 1) + _log.info( + "We've been waiting for 5 seconds and the build is still " + "not finished, so let's keep going.") result = build_info.get('result') + if not result and build_info.get('building') is True: + result = 'BUILDING' + _log.info('Result from jenkins: %s', result) url = build_info['url'] _log.info('URL from jenkins: %s', url) @@ -78,18 +83,26 @@ def process_jenkins_build( pagure.exceptions.PagureException( 'Unknown build status: %s' % result) - status = result.lower() - request = pagure.lib.search_pull_requests( session, project_id=project.id, requestid=pr_id) if not request: raise pagure.exceptions.PagureException('Request not found') - comment, percent = BUILD_STATS[result] + comment, state, percent = BUILD_STATS[result] # Adding build ID to the CI type username = "%s #%s" % (project.ci_hook.ci_type, build_id) + if request.commit_stop: + comment += ' (commit: %s)' % (request.commit_stop[:8]) + + uid = None + for flag in request.flags: + if flag.status == pagure_config['FLAG_PENDING'] \ + and flag.username == username: + uid = flag.uid + break + _log.info("Flag's UID: %s", uid) pagure.lib.add_pull_request_flag( session, request=request, @@ -97,8 +110,8 @@ def process_jenkins_build( percent=percent, comment=comment, url=url, - status=status, - uid=None, + status=state, + uid=uid, user=project.user.username, token=None, requestfolder=requestfolder, From 0dde141c84813e63ee8ddfff9df68fabbc65dc76 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 04 2018 08:46:51 +0000 Subject: [PATCH 2/3] Adjust documentation about which events jenkins should notify about Now that we support receiving notifications from jenkins about ongoing jobs, we should ask jenkins to tell us about all events. Signed-off-by: Pierre-Yves Chibon --- diff --git a/doc/usage/pagure_ci_jenkins.rst b/doc/usage/pagure_ci_jenkins.rst index 622d137..0a3302b 100644 --- a/doc/usage/pagure_ci_jenkins.rst +++ b/doc/usage/pagure_ci_jenkins.rst @@ -56,7 +56,7 @@ Configure your project on Jenkins FORMAT: JSON PROTOCOL: HTTP - EVENT: Job Finalized + EVENT: All Events URL: TIMEOUT: 3000 LOG: 1 From 9592dbda806a87446316f697f15f310ce900f99b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 04 2018 08:51:25 +0000 Subject: [PATCH 3/3] Ignore the COMPLETED notifications from jenkins Jenkins sends three types of notifications: STARTED, COMPLETED and FINALIZED. In order to avoid duplicating flags, we want to discard the COMPLETED notifications and only act on STARTED and FINALIZED. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/ci/jenkins.py b/pagure/api/ci/jenkins.py index 724506e..c5aa9a8 100644 --- a/pagure/api/ci/jenkins.py +++ b/pagure/api/ci/jenkins.py @@ -72,6 +72,16 @@ def jenkins_ci_notification( _log.debug("Bad Request: No build ID retrieved") raise pagure.exceptions.APIError(400, error_code=APIERROR.EINVALIDREQ) + build_phase = data.get('build', {}).get('phase') + if not build_phase: + _log.debug("Bad Request: No build phase retrieved") + raise pagure.exceptions.APIError(400, error_code=APIERROR.EINVALIDREQ) + if build_phase not in ["STARTED", "FINALIZED"]: + _log.debug( + "Ignoring phase: %s - not in the list: STARTED, FINALIZED", + build_phase) + raise pagure.exceptions.APIError(400, error_code=APIERROR.EINVALIDREQ) + try: lib_ci.process_jenkins_build( flask.g.session,