Ejemplo n.º 1
0
def get_project(project_name):
    """Perform a lookup to return the actual project record"""
    if is_uuid(project_name):
        project = Project.query.get(project_name)
    else:
        project = Project.query.filter(Project.name == project_name).first()
    return project
Ejemplo n.º 2
0
def get_project(id_, token_info=None, user=None):
    """Get a single project by ID

    :param id: ID of test project
    :type id: str

    :rtype: Project
    """
    if not is_uuid(id_):
        id_ = convert_objectid_to_uuid(id_)
    project = Project.query.filter(Project.name == id_).first()
    if not project:
        project = Project.query.get(id_)
    if project and not project_has_user(project, user):
        return "Unauthorized", 401
    if not project:
        return "Project not found", 404
    return project.to_dict()
Ejemplo n.º 3
0
def _create_result(tar,
                   run_id,
                   result,
                   artifacts,
                   project_id=None,
                   metadata=None):
    """Create a result with artifacts, used in the archive importer"""
    old_id = None
    result_id = result.get("id")
    if is_uuid(result_id):
        result_record = session.query(Result).get(result_id)
    else:
        result_record = None
    if result_record:
        result_record.run_id = run_id
    else:
        old_id = result["id"]
        if "id" in result:
            result.pop("id")
        result["run_id"] = run_id
        if project_id:
            result["project_id"] = project_id
        if metadata:
            result["metadata"] = result.get("metadata", {})
            result["metadata"].update(metadata)
        result["env"] = result.get("metadata", {}).get("env")
        result["component"] = result.get("metadata", {}).get("component")
        result_record = Result.from_dict(**result)
    session.add(result_record)
    session.commit()
    result = result_record.to_dict()
    for artifact in artifacts:
        session.add(
            Artifact(
                filename=artifact.name.split("/")[-1],
                result_id=result["id"],
                data={
                    "contentType": "text/plain",
                    "resultId": result["id"]
                },
                content=tar.extractfile(artifact).read(),
            ))
    session.commit()
    return old_id
Ejemplo n.º 4
0
def admin_update_project(id_, project=None, token_info=None, user=None):
    """Update a project

    :param id: ID of test project
    :type id: str
    :param body: Project
    :type body: dict | bytes

    :rtype: Project
    """
    check_user_is_admin(user)
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    if not is_uuid(id_):
        id_ = convert_objectid_to_uuid(id_)
    project = Project.query.get(id_)

    if not project:
        abort(404)

    # Grab the fields from the request
    project_dict = connexion.request.get_json()

    # If the "owner" field is set, ignore it
    project_dict.pop("owner", None)

    # handle updating users separately
    for username in project_dict.pop("users", []):
        user_to_add = User.query.filter_by(email=username).first()
        if user_to_add and user_to_add not in project.users:
            project.users.append(user_to_add)

    # Make sure the project owner is in the list of users
    if project_dict.get("owner_id"):
        owner = User.query.get(project_dict["owner_id"])
        if owner and owner not in project.users:
            project.users.append(owner)

    # update the rest of the project info
    project.update(project_dict)
    session.add(project)
    session.commit()
    return project.to_dict()
Ejemplo n.º 5
0
def update_project(id_, project=None, token_info=None, user=None):
    """Update a project

    :param id: ID of test project
    :type id: str
    :param body: Project
    :type body: dict | bytes

    :rtype: Project
    """
    if not connexion.request.is_json:
        return "Bad request, JSON required", 400
    if not is_uuid(id_):
        id_ = convert_objectid_to_uuid(id_)
    project = Project.query.get(id_)

    if not project:
        return "Project not found", 404

    user = User.query.get(user)
    if not user.is_superadmin and (not project.owner
                                   or project.owner.id != user.id):
        return "Forbidden", 403

    # handle updating users separately
    updates = connexion.request.get_json()
    for username in updates.pop("users", []):
        user_to_add = User.query.filter_by(email=username).first()
        if user_to_add and user_to_add not in project.users:
            project.users.append(user_to_add)

    # update the rest of the project info
    project.update(updates)
    session.add(project)
    session.commit()
    return project.to_dict()
Ejemplo n.º 6
0
def run_archive_import(import_):
    """Import a test run from an Ibutsu archive file"""
    # Update the status of the import
    import_record = Import.query.get(str(import_["id"]))
    metadata = {}
    if import_record.data.get("metadata"):
        # metadata is expected to be a json dict
        metadata = import_record.data["metadata"]
    _update_import_status(import_record, "running")
    # Fetch the file contents
    import_file = ImportFile.query.filter(
        ImportFile.import_id == import_["id"]).first()
    if not import_file:
        _update_import_status(import_record, "error")
        return

    # First open the tarball and pull in the results
    run = None
    run_artifacts = []
    results = []
    result_artifacts = {}
    start_time = None
    file_object = BytesIO(import_file.content)
    with tarfile.open(fileobj=file_object) as tar:
        for member in tar.getmembers():
            # We don't care about directories, skip them
            if member.isdir():
                continue
            # Grab the run id
            run_id, rest = member.name.split("/", 1)
            if "/" not in rest:
                if member.name.endswith("run.json"):
                    run = json.loads(tar.extractfile(member).read())
                else:
                    run_artifacts.append(member)
                continue
            result_id, file_name = rest.split("/")
            if member.name.endswith("result.json"):
                result = json.loads(tar.extractfile(member).read())
                result_start_time = result.get("start_time")
                if not start_time or start_time > result_start_time:
                    start_time = result_start_time
                results.append(result)
            else:
                try:
                    result_artifacts[result_id].append(member)
                except KeyError:
                    result_artifacts[result_id] = [member]
        run_dict = run or {
            "duration": 0,
            "summary": {
                "errors": 0,
                "failures": 0,
                "skips": 0,
                "xfailures": 0,
                "xpasses": 0,
                "tests": 0,
            },
        }
        # patch things up a bit, if necessary
        run_dict["metadata"] = run_dict.get("metadata", {})
        run_dict["metadata"].update(metadata)
        _populate_metadata(run_dict, import_record)
        _populate_created_times(run_dict, start_time)

        # If this run has a valid ID, check if this run exists
        if is_uuid(run_dict.get("id")):
            run = session.query(Run).get(run_dict["id"])
        if run:
            run.update(run_dict)
        else:
            run = Run.from_dict(**run_dict)
        session.add(run)
        session.commit()
        import_record.run_id = run.id
        import_record.data["run_id"] = [run.id]
        # Loop through any artifacts associated with the run and upload them
        for artifact in run_artifacts:

            session.add(
                Artifact(
                    filename=artifact.name.split("/")[-1],
                    run_id=run.id,
                    data={
                        "contentType": "text/plain",
                        "runId": run.id
                    },
                    content=tar.extractfile(artifact).read(),
                ))
        # Now loop through all the results, and create or update them
        for result in results:
            artifacts = result_artifacts.get(result["id"], [])
            _create_result(
                tar,
                run.id,
                result,
                artifacts,
                project_id=run_dict.get("project_id")
                or import_record.data.get("project_id"),
                metadata=metadata,
            )
    # Update the import record
    _update_import_status(import_record, "done")
    if run:
        update_run.delay(run.id)