mseth10 commented on a change in pull request #25: URL: https://github.com/apache/incubator-mxnet-ci/pull/25#discussion_r424958208
########## File path: services/jenkins-pipeline-monitor/handler.py ########## @@ -0,0 +1,134 @@ +import os +import boto3 +import json +import logging +import secret_manager + +from jenkinsapi.jenkins import Jenkins + +logging.getLogger().setLevel(logging.INFO) +logging.getLogger('boto3').setLevel(logging.CRITICAL) +logging.getLogger('botocore').setLevel(logging.CRITICAL) + + +def get_jenkins_obj(secret): + """ + This method returns an object of Jenkins instantiated using username, password + """ + jenkins_url, jenkins_username, jenkins_password = os.environ["JENKINS_URL"], secret["jenkins_username"], secret["jenkins_password"] + return Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password) + + +def get_secret(): + """ + This method is to get secret value from Secrets Manager + """ + secret = json.loads(secret_manager.get_secret()) + return secret + + +def get_pipeline_job(jenkinsObj): + job = jenkinsObj["restricted-mxnet-cd/mxnet-cd-release-job"] + return job + + +def get_latest_build_number(job): + return job.get_last_build().get_number() + + +def get_build_from_build_number(job, build_number): + return job.get_build(build_number) + + +def get_build_timestamp(build): + return build.get_timestamp() + + +def get_build_date(timestamp): + return timestamp.date() + + +def is_latest_day_build(current_build, latest_build): + current_build_timestamp = get_build_timestamp(current_build) + latest_build_timestamp = get_build_timestamp(latest_build) + # if 2 builds are within 24 hours and on the same day + seconds_difference = (latest_build_timestamp-current_build_timestamp).total_seconds() + hour_difference = divmod(seconds_difference, 3600)[0] + + current_build_date = get_build_date(current_build_timestamp) + latest_build_date = get_build_date(latest_build_timestamp) + same_date_check = True if current_build_date == latest_build_date else False + if(hour_difference < 24 and same_date_check): + return True + return False + + +def get_latest_day_builds(job, latest_build_number): + latest_build = get_build_from_build_number(job, latest_build_number) + builds = [latest_build] + current_build_number = latest_build_number-1 + while True: + current_build = get_build_from_build_number(job, current_build_number) + if is_latest_day_build(current_build, latest_build): + builds.append(current_build) + current_build_number -= 1 + else: + break + return builds + + +def get_release_job_type(build): + return build.get_params()['RELEASE_JOB_TYPE'] + + +def filter_by_desired_release_job_type(latest_day_builds, desired_release_job_type): + filtered_builds = [] + for build in latest_day_builds: + if get_release_job_type(build) in desired_release_job_type: Review comment: can we also check for the author here? we only want the pipelines started by 'upstream pipeline' and filter out any pipeline started by a user manually. For example, I started this pipeline manually which should not be put into `filtered_builds` : http://jenkins.mxnet-ci.amazon-ml.com/blue/organizations/jenkins/restricted-mxnet-cd%2Fmxnet-cd-release-job/detail/mxnet-cd-release-job/1119/pipeline/ ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org