From 8606fd30b81e3c8149ddf4b6230a7f99449f1174 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 17 2018 12:52:06 +0000 Subject: 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 dd142aa..779120f 100644 --- a/pagure/lib/lib_ci.py +++ b/pagure/lib/lib_ci.py @@ -24,8 +24,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), } @@ -50,17 +52,20 @@ def process_jenkins_build( 'Could not find build %s at: %s' % (build_id, jenkins_name)) 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) @@ -81,18 +86,26 @@ def process_jenkins_build( raise 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 = project.ci_hook.ci_type + " #" + str(build_id) - + 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, @@ -100,8 +113,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,