def process_deps_file_from_origin(context,
                                  ecosystem,
                                  manifest,
                                  origin,
                                  version=3):
    """Test stack analyses of an ecosystem specific dependencies file from an integration point."""
    endpoint = stack_analysis_endpoint(context, version)
    test_stack_analyses_with_deps_file(context, ecosystem.lower(), manifest,
                                       origin, endpoint)
def npm_manifest_stack_analysis(context, manifest, version=3, token="without"):
    """Send the NPM package manifest file to the stack analysis."""
    endpoint = stack_analysis_endpoint(context, version)
    use_token = parse_token_clause(token)
    send_manifest_to_stack_analysis(context,
                                    manifest,
                                    'package.json',
                                    endpoint,
                                    use_token,
                                    ecosystem='npm',
                                    origin='vscode')
def maven_new_manifest_stack_analysis(context,
                                      manifest,
                                      version=3,
                                      token="without"):
    """Send the Maven package manifest file to the stack analysis."""
    endpoint = stack_analysis_endpoint(context, version)
    use_token = parse_token_clause(token)
    send_manifest_to_stack_analysis(context,
                                    manifest,
                                    'dependencies.txt',
                                    endpoint,
                                    use_token,
                                    ecosystem='maven',
                                    origin='vscode')
def wait_for_stack_analysis_completion(context, version=3, token="without"):
    """Try to wait for the stack analysis to be finished.

    This step assumes that stack analysis has been started previously and
    thus that the job ID is known

    Current API implementation returns just three HTTP codes:
    200 OK : analysis is already finished
    202 Accepted: analysis is started or is in progress (or other state!)
    401 UNAUTHORIZED : missing or improper authorization token
    """
    context.duration = None
    start_time = time.time()

    timeout = context.stack_analysis_timeout  # in seconds
    sleep_amount = 10  # we don't have to overload the API with too many calls
    use_token = parse_token_clause(token)

    id = context.response.json().get("id")
    context.stack_analysis_id = id
    # log.info("REQUEST ID: {}\n\n".format(context.stack_analysis_id))
    url = urljoin(stack_analysis_endpoint(context, version), id)
    # log.info("RECOMMENDER API URL: {}\n\n".format(url))

    for _ in range(timeout // sleep_amount):
        if use_token:
            context.response = requests.get(url,
                                            headers=authorization(context))
        else:
            context.response = requests.get(url)
        status_code = context.response.status_code
        # log.info("%r" % context.response.json())
        if status_code == 200:
            json_resp = context.response.json()
            if contains_alternate_node(json_resp):
                # log.info('Recommendation found')
                break
        # 401 code should be checked later
        elif status_code == 401:
            break
        elif status_code != 202:
            raise Exception('Bad HTTP status code {c}'.format(c=status_code))
        time.sleep(sleep_amount)
    else:
        raise Exception('Timeout waiting for the stack analysis results')
    end_time = time.time()
    # compute the duration
    # plase note that duration==None in case of any errors (which is to be expected)
    context.duration = end_time - start_time