def _get_jenkins_handle()

in services/jenkins-autoscaling/lambda_mxnet_ci/autoscaling/handler.py [0:0]


def _get_jenkins_handle() -> jenkinsapi.jenkins.Jenkins:  # pragma: no cover
    from requests.exceptions import HTTPError

    jenkins_credentials = _get_jenkins_credentials()
    try:
        server = jenkinsapi.jenkins.Jenkins(baseurl=jenkins_credentials['jenkins_url'],
                                            username=jenkins_credentials['github_username'],
                                            password=jenkins_credentials['github_token'],
                                            timeout=JENKINS_REQUEST_TIMEOUT_SECONDS)
    except HTTPError as e:
        logging.exception('Error initializing Jenkins API.')
        if e.response.status_code == 500:
            logging.error('Did you properly set up the API token? https://REDACTED/MXBLN-376')

        logging.error('HTML response - use an HTML beautifier to view it properly: %s', e.response.content)
        raise Exception('Error initializing Jenkins API', e)

    # Jenkins returns a 302 (redirect) for certain requests. That's a valid response code somehow.....
    # We do not want to follow redirects since they are very expensive and cause timeouts
    # See https://REDACTED/MXBLN-298/communication for further detailsI
    server.requester.VALID_STATUS_CODES.extend([302])
    _add_timer_to_jenkins_requester(jenkins_server=server)
    return server