Ejemplo n.º 1
0
def add_run(run=None, token_info=None, user=None):
    """Create a new run

    :param body: Run object
    :type body: dict | bytes

    :rtype: Run
    """
    if not connexion.request.is_json:
        return "Bad request, JSON is required", 400
    run = Run.from_dict(**connexion.request.get_json())

    if run.data and not (run.data.get("project") or run.project_id):
        return "Bad request, project or project_id is required", 400

    project = get_project(run.data["project"])
    if not project_has_user(project, user):
        return "Forbidden", 403
    run.project = project
    run.env = run.data.get("env") if run.data else None
    run.component = run.data.get("component") if run.data else None
    # allow start_time to be set by update_run task if no start_time present
    run.start_time = run.start_time if run.start_time else datetime.utcnow()
    # if not present, created is the time at which the run is added to the DB
    run.created = run.created if run.created else datetime.utcnow()

    session.add(run)
    session.commit()
    update_run_task.apply_async((run.id, ), countdown=5)
    return run.to_dict(), 201
def update_widget_config(id_, token_info=None, user=None):
    """Updates a single widget config

    :param id: ID of widget to update
    :type id: int
    :param body: Result
    :type body: dict

    :rtype: Result
    """
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    data = connexion.request.get_json()
    if data.get("widget") and data["widget"] not in WIDGET_TYPES.keys():
        return "Bad request, widget type does not exist", 400
    # Look up the project id
    if data.get("project"):
        project = get_project(data.pop("project"))
        if not project_has_user(project, user):
            return "Forbidden", 403
        data["project_id"] = project.id
    widget_config = WidgetConfig.query.get(id_)
    # add default weight of 10
    if not widget_config.weight:
        widget_config.weight = 10
    # default to make views navigable
    if data.get("navigable") and isinstance(data["navigable"], str):
        data["navigable"] = data["navigable"][0] in ALLOWED_TRUE_BOOLEANS
    if data.get("type") and data["type"] == "view" and data.get("navigable") is None:
        data["navigable"] = True
    widget_config.update(data)
    session.add(widget_config)
    session.commit()
    return widget_config.to_dict()
def add_widget_config(widget_config=None, token_info=None, user=None):
    """Create a new widget config

    :param widget_config: The widget_config to save
    :type widget_config: dict | bytes

    :rtype: WidgetConfig
    """
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    data = connexion.request.json
    if data["widget"] not in WIDGET_TYPES.keys():
        return "Bad request, widget type does not exist", 400
    # add default weight of 10
    if not data.get("weight"):
        data["weight"] = 10
    # Look up the project id
    if data.get("project"):
        project = get_project(data.pop("project"))
        if not project_has_user(project, user):
            return "Forbidden", 403
        data["project_id"] = project.id
    # default to make views navigable
    if data.get("navigable") and isinstance(data["navigable"], str):
        data["navigable"] = data["navigable"][0] in ALLOWED_TRUE_BOOLEANS
    if data.get("type") == "view" and data.get("navigable") is None:
        data["navigable"] = True
    widget_config = WidgetConfig.from_dict(**data)
    session.add(widget_config)
    session.commit()
    return widget_config.to_dict(), 201
Ejemplo n.º 4
0
def add_import(
    import_file: Optional[FileStorage] = None,
    project: Optional[str] = None,
    metadata: Optional[str] = None,
    source: Optional[str] = None,
    token_info: Optional[str] = None,
    user: Optional[str] = None,
):
    """Imports a JUnit XML file and creates a test run and results from it.

    :param import_file: file to upload
    :type import_file: werkzeug.datastructures.FileStorage
    :param project: the project to add this test run to
    :type project: str
    :param metadata: extra metadata to add to the run and the results, in a JSON string
    :type metadata: str
    :param source: the source of the test run
    :type source: str

    :rtype: Import
    """
    if "importFile" in connexion.request.files:
        import_file = connexion.request.files["importFile"]
    if not import_file:
        return "Bad request, no file uploaded", 400
    data = {}
    if connexion.request.form.get("project"):
        project = connexion.request.form["project"]
    if project:
        project = get_project(project)
        if not project_has_user(project, user):
            return "Forbidden", 403
        data["project_id"] = project.id
    if connexion.request.form.get("metadata"):
        metadata = json.loads(connexion.request.form.get("metadata"))
    data["metadata"] = metadata
    if connexion.request.form.get("source"):
        data["source"] = connexion.request.form["source"]
    new_import = Import.from_dict(
        **{
            "status": "pending",
            "filename": import_file.filename,
            "format": "",
            "data": data
        })
    session.add(new_import)
    session.commit()
    new_file = ImportFile(import_id=new_import.id, content=import_file.read())
    session.add(new_file)
    session.commit()
    if import_file.filename.endswith(".xml"):
        run_junit_import.delay(new_import.to_dict())
    elif import_file.filename.endswith(".tar.gz"):
        run_archive_import.delay(new_import.to_dict())
    else:
        return "Unsupported Media Type", 415
    return new_import.to_dict(), 202
Ejemplo n.º 5
0
def get_import(id_, token_info=None, user=None):
    """Get a run

    :param id: The ID of the run
    :type id: str

    :rtype: Run
    """
    import_ = Import.query.get(id_)
    if import_ and import_.data.get("project_id"):
        project = get_project(import_.data["project_id"])
        if project and not project_has_user(project, user):
            return "Forbidden", 403
    if not import_:
        return "Not Found", 404
    return import_.to_dict()
Ejemplo n.º 6
0
def bulk_update(filter_=None, page_size=1, token_info=None, user=None):
    """Updates multiple runs with common metadata

    Note: can only be used to update metadata on runs, limited to 25 runs

    :param filter_: A list of filters to apply
    :param page_size: Limit the number of runs updated, defaults to 1

    :rtype: List[Run]
    """
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400

    run_dict = connexion.request.get_json()

    if not run_dict.get("metadata"):
        return "Bad request, can only update metadata", 401

    # ensure only metadata is updated
    run_dict = {"metadata": run_dict.pop("metadata")}

    if page_size > 25:
        return "Bad request, cannot update more than 25 runs at a time", 405

    if run_dict.get("metadata", {}).get("project"):
        project = get_project(run_dict["metadata"]["project"])
        if not project_has_user(project, user):
            return "Forbidden", 403
        run_dict["project_id"] = project.id

    runs = get_run_list(filter_=filter_, page_size=page_size,
                        estimate=True).get("runs")

    if not runs:
        return f"No runs found with {filter_}", 404

    model_runs = []
    for run_json in runs:
        run = Run.query.get(run_json.get("id"))
        # update the json dict of the run with the new metadata
        merge_dicts(run_dict, run_json)
        run.update(run_json)
        session.add(run)
        model_runs.append(run)
    session.commit()

    return [run.to_dict() for run in model_runs]