コード例 #1
0
def register():
    """
    Endpoint for registering a new repositor.

    Registers new information and
    updates existing repo information.
    """
    resp_dict = {
        "data": [],
        "success": True,
        "summary": "{} successfully registered"
    }
    input_json = request.get_json()
    validated_data = validate_request_data(input_json)
    if not validated_data[0]:
        resp_dict["success"] = False
        resp_dict["summary"] = validated_data[1]
        return flask.jsonify(resp_dict), 404
    try:
        status = DatabaseIngestion.store_record(input_json)
        resp_dict["data"] = status
    except Exception as e:
        resp_dict["success"] = False
        resp_dict["summary"] = "Database Ingestion Failure due to" + e
        return flask.jsonify(resp_dict), 500

    status = scan_repo(input_json)
    if status is not True:
        resp_dict["success"] = False
        resp_dict["summary"] = "New Repo Scan Initializtion Failure"
        return flask.jsonify(resp_dict), 500
    rep_summary = resp_dict["summary"].format(input_json['git_url'])
    resp_dict["summary"] = rep_summary
    return flask.jsonify(resp_dict), 200
コード例 #2
0
def report(repo):
    """Endpoint for fetching generated scan report."""
    repo_info = DatabaseIngestion.get_info(repo)
    response = dict()
    if repo_info.get('is_valid'):
        git_sha = repo_info.get('data', {}).get('git_sha')
        result = retrieve_worker_result(git_sha, "ReportGenerationTask")
        if result:
            response.update({"status": "success", "result": result})
            return flask.jsonify(response), 200
        else:
            response.update({
                "status": "failure",
                "message": "No report found for this repository"
            })
            return flask.jsonify(response), 404
    else:
        response.update({
            "status": "failure",
            "message": "No registered repository found"
        })
        return flask.jsonify(response), 404
コード例 #3
0
def register():
    """
    Endpoint for registering a new repository.

    Registers new information and
    updates existing repo information.
    """
    resp_dict = {"success": True, "summary": ""}
    input_json = request.get_json()
    if request.content_type != 'application/json':
        resp_dict["success"] = False
        resp_dict["summary"] = "Set content type to application/json"
        return flask.jsonify(resp_dict), 400

    validated_data = validate_request_data(input_json)
    if not validated_data[0]:
        resp_dict["success"] = False
        resp_dict["summary"] = validated_data[1]
        return flask.jsonify(resp_dict), 404

    try:
        repo_info = DatabaseIngestion.get_info(input_json.get('git-url'))
        if repo_info.get('is_valid'):
            data = repo_info.get('data')
            # Update the record to reflect new git_sha if any.
            DatabaseIngestion.update_data(input_json)
        else:
            try:
                # First time ingestion
                DatabaseIngestion.store_record(input_json)
                status = scan_repo(input_json)
                if status is not True:
                    resp_dict["success"] = False
                    resp_dict[
                        "summary"] = "New Repo Scan Initialization Failure"
                    return flask.jsonify(resp_dict), 500

                resp_dict["summary"] = "Repository {} with commit-hash {} " \
                                       "has been successfully registered. " \
                                       "Please check back for report after some time." \
                    .format(input_json.get('git-url'),
                            input_json.get('git-sha'))

                return flask.jsonify(resp_dict), 200
            except Exception as e:
                resp_dict["success"] = False
                resp_dict["summary"] = "Database Ingestion Failure due to: {}" \
                    .format(e)
                return flask.jsonify(resp_dict), 500
    except Exception as e:
        resp_dict["success"] = False
        resp_dict["summary"] = "Cannot get information about repository {} " \
                               "due to {}" \
            .format(input_json.get('git-url'), e)
        return flask.jsonify(resp_dict), 500

    # Scan the repository irrespective of report is available or not.
    status = scan_repo(input_json)
    if status is not True:
        resp_dict["success"] = False
        resp_dict["summary"] = "New Repo Scan Initialization Failure"
        return flask.jsonify(resp_dict), 500

    resp_dict.update({
        "summary":
        "Repository {} was already registered, but no report for "
        "commit-hash {} was found. Please check back later.".format(
            input_json.get('git-url'), input_json.get('git-sha')),
        "last_scanned_at":
        data['last_scanned_at'],
        "last_scan_report":
        None
    })

    return flask.jsonify(resp_dict), 200