def delete_branches(request, project_id):
    """ Delete all the branches associated with a project """
    if request.method != 'POST':
        return res.get_response(400, 'Only post allowed', {})

    project_entry = GitProjectEntry.objects.filter(id=project_id).first()
    if project_entry is None:
        return res.get_response(404, 'project not found', {})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        LogEntry.error(
            request.user,
            'Json parser failed.\n{0}'.format(json.dumps(post_info)))
        return res.get_json_parser_failed({})

    (obj_validated, val_resp_obj) = val.validate_obj_schema(
        post_info, GitFeederSchemas.GIT_FEED_DELETE_BRANCHES_SCHEMA)
    if not obj_validated:
        LogEntry.error(
            request.user,
            'Json schema failed.\n{0}'.format(json.dumps(val_resp_obj)))
        return res.get_schema_failed(val_resp_obj)

    for name in val_resp_obj['branch_names']:
        GitFeederController.delete_branch(project_entry, name)

    return res.get_response(200, 'Branches removed correctly', {})
Example #2
0
def login_worker_info(request):
    """ Makes login with worker credentials """

    if request.method != 'POST':
        return res.get_only_post_allowed({})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        return res.get_json_parser_failed({})

    (obj_validated, obj) = val.validate_obj_schema(post_info, BluesteelWorkerSchemas.LOGIN_WORKER_SCHEMA)
    if not obj_validated:
        return res.get_schema_failed(obj)

    user = authenticate(username=obj['username'], password=obj['password'])
    if user is not None:
        # the password verified for the user
        if user.is_active:
            login(request, user)
            return res.get_response(200, 'Access granted!', {})

        return res.get_response(200, 'The password is valid, but the account has been disabled!', {})

    # the authentication system was unable to verify the username and password
    return res.get_response(401, 'Access denied!', {})
def post_feed_reports(request, project_id):
    """ Insert feed reports to a given git project """
    if request.method != 'POST':
        return res.get_response(400, 'Only post allowed', {})

    project_entry = GitProjectEntry.objects.filter(id=project_id).first()
    if project_entry is None:
        return res.get_response(404, 'project not found', {})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        LogEntry.error(
            request.user,
            'Json parser failed.\n{0}'.format(json.dumps(post_info)))
        return res.get_json_parser_failed({})

    (obj_validated, val_resp_obj) = val.validate_obj_schema(
        post_info, GitFeederSchemas.GIT_FEED_REPORTS_SCHEMA)
    if not obj_validated:
        LogEntry.error(
            request.user,
            'Json schema failed.\n{0}'.format(json.dumps(val_resp_obj)))
        return res.get_schema_failed(val_resp_obj)

    GitFeederController.insert_reports(request.user, val_resp_obj['reports'],
                                       project_entry)

    if request.user.is_authenticated() and not request.user.is_anonymous():
        worker_entry = WorkerEntry.objects.filter(user=request.user).first()

        if worker_entry:
            GitFeederController.purge_old_reports(
                worker_entry.id, worker_entry.max_feed_reports)

    return res.get_response(200, 'Reports added correctly', {})
Example #4
0
def save_project(request, project_id):
    """ Save project properties """
    if request.method != 'POST':
        return res.get_only_post_allowed({})

    project_entry = BluesteelProjectEntry.objects.filter(id=project_id).first()
    if project_entry is None:
        return res.get_response(404, 'Bluesteel project not found', {})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        return res.get_json_parser_failed({})

    (obj_validated, val_resp_obj) = val.validate_obj_schema(post_info, BluesteelSchemas.SAVE_PROJECT)

    if not obj_validated:
        return res.get_schema_failed(val_resp_obj)

    CommandSetEntry.objects.filter(group=project_entry.command_group).delete()

    local_search_path = filter_folder_path(val_resp_obj['git_project_folder_search_path'])

    project_entry.name = val_resp_obj['name']
    project_entry.git_project_folder_search_path = local_search_path
    project_entry.save()

    CommandController.add_full_command_set(project_entry.command_group, 'CLONE', 0, val_resp_obj['clone'])
    CommandController.add_full_command_set(project_entry.command_group, 'FETCH', 1, val_resp_obj['fetch'])

    return res.get_response(200, 'Project saved', {})
def view_save_benchmark_definition(request, benchmark_definition_id):
    """ Save benchmark definition properties """
    if request.method != 'POST':
        return res.get_only_post_allowed({})

    (json_valid_value, post_info) = val.validate_json_string(request.body)
    if not json_valid_value:
        return res.get_json_parser_failed({})

    (obj_validated, val_resp_obj) = val.validate_obj_schema(
        post_info, BenchmarkDefinitionSchemas.SAVE_BENCHMARK_DEFINITION)

    if not obj_validated:
        return res.get_schema_failed(val_resp_obj)

    bench_entry = BenchmarkDefinitionController.save_benchmark_definition(
        benchmark_definition_id=benchmark_definition_id,
        name=val_resp_obj['name'],
        layout_id=val_resp_obj['layout_id'],
        project_id=val_resp_obj['project_id'],
        priority=val_resp_obj['priority'],
        active=val_resp_obj['active'],
        command_list=val_resp_obj['command_list'],
        max_fluctuation_percent=val_resp_obj['max_fluctuation_percent'],
        overrides=val_resp_obj['overrides'],
        max_weeks_old_notify=val_resp_obj['max_weeks_old_notify'],
        work_passes=val_resp_obj['work_passes'],
    )

    if bench_entry is None:
        return res.get_response(404, 'Benchmark Defintion save error', {})

    return res.get_response(200, 'Benchmark Definition saved', {})
Example #6
0
def save_layout(request, layout_id):
    """ Save layout properties """
    if request.method != 'POST':
        return res.get_only_post_allowed({})

    layout_entry = BluesteelLayoutEntry.objects.filter(id=layout_id).first()
    if layout_entry is None:
        return res.get_response(404, 'Bluesteel layout not found', {})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        return res.get_json_parser_failed({})

    (obj_validated,
     val_resp_obj) = val.validate_obj_schema(post_info,
                                             BluesteelSchemas.SAVE_LAYOUT)

    if not obj_validated:
        return res.get_schema_failed(val_resp_obj)

    layout_entry.name = val_resp_obj['name']
    layout_entry.active = val_resp_obj['active']
    layout_entry.project_index_path = val_resp_obj['project_index_path']
    # Check if change of path in case we need to purge other services like 'performance tests service'
    layout_entry.clamp_project_index_path()
    layout_entry.check_active_state()
    layout_entry.save()
    return res.get_response(200, 'Layout saved', {})
Example #7
0
def create_worker_info(request):
    """ Creates a worker given its info """

    if request.method != 'POST':
        return res.get_only_post_allowed({})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        return res.get_json_parser_failed(
            {}
        )

    (obj_validated, val_resp_obj) = val.validate_obj_schema(
        post_info,
        BluesteelWorkerSchemas.CREATE_WORKER_INFO_SCHEMA
    )
    if not obj_validated:
        return res.get_schema_failed(val_resp_obj)

    obj = val_resp_obj
    username_trimmed = obj['uuid'][:30]

    worker = WorkerEntry.objects.filter(uuid=username_trimmed).first()
    if worker is not None:
        return res.get_response(405, 'Worker already created', {})

    user = User.objects.filter(username=username_trimmed).first()
    if not user:
        user = User.objects.create_user(
            username=username_trimmed,
            email=None,
            password=obj['uuid']
        )
        user.save()

    worker_count = WorkerEntry.objects.all().count()

    new_worker = WorkerEntry.objects.create(
        uuid=obj['uuid'],
        name=obj['host_name'],
        operative_system=obj['operative_system'],
        user=user,
        git_feeder=(worker_count == 0)
    )
    new_worker.save()

    BenchmarkDefinitionController.populate_worker_passes_all_definitions()

    ret = {}
    ret['worker'] = new_worker.as_object()
    ret['worker']['url'] = get_worker_urls(request.get_host(), ret['worker']['id'], ret['worker']['uuid'])
    ret['worker']['last_update'] = str(ret['worker']['last_update'])

    return res.get_response(200, 'Worker created succesfuly!', ret)
Example #8
0
def save_worker(request, worker_id):
    """ Saves workers info """
    if request.method != 'POST':
        return res.get_only_post_allowed({})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        return res.get_json_parser_failed({})

    (obj_validated, obj) = val.validate_obj_schema(post_info, BluesteelWorkerSchemas.SAVE_WORKER_SCHEMA)
    if not obj_validated:
        return res.get_schema_failed(obj)

    worker = WorkerEntry.objects.filter(id=worker_id).first()
    if worker is None:
        return res.get_response(400, 'Worker not found', obj)
    else:
        worker.description = obj['description']
        worker.git_feeder = obj['git_feeder']
        worker.max_feed_reports = obj['max_feed_reports']
        worker.save()

    return res.get_response(200, 'Worker Saved!', {})
Example #9
0
def set_branch_merge_target(request, project_id):
    """ Insert new commits to a given git project """
    if request.method == 'POST':
        project_entry = GitProjectEntry.objects.filter(id=project_id).first()
        if project_entry is None:
            return res.get_response(404, 'project not found', {})

        (json_is_valid, post_info) = val.validate_json_string(request.body)
        if not json_is_valid:
            return res.get_json_parser_failed({})

        (obj_validated, val_resp_obj) = val.validate_obj_schema(
            post_info, GitRepoSchemas.GIT_MERGE_TARGET_SCHEMA)
        if not obj_validated:
            return res.get_schema_failed(val_resp_obj)

        # We search for both branches involved on the merge target
        current_branch = GitBranchEntry.objects.filter(
            project=project_entry,
            name=val_resp_obj['current_branch_name']).first()

        if current_branch is None:
            return res.get_response(404, 'Current branch name not found',
                                    val_resp_obj)

        target_branch = GitBranchEntry.objects.filter(
            project=project_entry,
            name=val_resp_obj['target_branch_name']).first()

        if target_branch is None:
            return res.get_response(404, 'Target branch name not found',
                                    val_resp_obj)

        # We search for the merge target entry with current one
        merge_target = GitBranchMergeTargetEntry.objects.filter(
            project=project_entry,
            current_branch=current_branch,
        ).first()

        if merge_target is None:
            return res.get_response(404, 'Merge Target entry not found',
                                    val_resp_obj)

        if merge_target.target_branch.id == target_branch.id:
            return res.get_response(200, 'Target branch not changed',
                                    val_resp_obj)

        # We search for the fork point between current and target
        current_branch_trails = GitBranchTrailEntry.objects.filter(
            project=project_entry, branch=current_branch).order_by('-order')

        target_branch_trails = GitBranchTrailEntry.objects.filter(
            project=project_entry, branch=target_branch).order_by('-order')

        fork_point = GitController.get_fork_point(target_branch_trails,
                                                  current_branch_trails)

        # If fork point not present, we select the first and most ancient commit of the branch
        if not fork_point:
            msg = ('Fork point is None, this should never happen\n'
                   'Current branch: {0}\n'
                   'Target branch: {1}\n').format(current_branch.name,
                                                  target_branch.name)
            LogEntry.error(request.user, msg)

        merge_target.target_branch = target_branch
        merge_target.fork_point = fork_point
        merge_target.save()

        return res.get_response(200, 'Target branch changed', val_resp_obj)
    else:
        return res.get_only_post_allowed({})
Example #10
0
def post_feed_commits(request, project_id):
    """ Insert new commits to a given git project """
    if request.method != 'POST':
        return res.get_response(400, 'Only post allowed', {})

    project_entry = GitProjectEntry.objects.filter(id=project_id).first()
    if project_entry is None:
        return res.get_response(404, 'project not found', {})

    (json_valid, post_info) = val.validate_json_string(request.body)
    if not json_valid:
        LogEntry.error(
            request.user,
            'Json parser failed.\n{0}'.format(json.dumps(post_info)))
        return res.get_json_parser_failed({})

    (obj_validated, val_resp_obj) = val.validate_obj_schema(
        post_info, GitFeederSchemas.GIT_FEED_COMMITS_SCHEMA)
    if not obj_validated:
        LogEntry.error(
            request.user,
            'Json schema failed.\n{0}'.format(json.dumps(val_resp_obj)))
        return res.get_schema_failed(val_resp_obj)

    if 'feed_data' not in val_resp_obj:
        return res.get_response(200, 'Only reports added', {})

    commits = val_resp_obj['feed_data']['commits']
    branches = val_resp_obj['feed_data']['branches']

    commit_hash_set = GitFeederController.get_unique_commit_set(commits)

    correct, msgs = GitFeederController.are_commits_unique(commits)
    if not correct:
        LogEntry.error(request.user,
                       'Commits not correct.\n{0}'.format(json.dumps(msgs)))
        return res.get_response(400, 'Commits not correct', {})

    correct, msgs = GitFeederController.are_parent_hashes_correct(
        commits, commit_hash_set, project_entry)
    if not correct:
        LogEntry.error(request.user,
                       'Parents not correct.\n{0}'.format(json.dumps(msgs)))
        return res.get_response(400, 'Parents not correct', {})

    correct, msgs = GitFeederController.are_branches_correct(
        commit_hash_set, branches, project_entry)
    if not correct:
        LogEntry.error(request.user,
                       'Branches not correct.\n{0}'.format(json.dumps(msgs)))
        return res.get_response(400, 'Branches not correct', {})

    GitFeederController.insert_commits(commits, project_entry)
    GitFeederController.insert_parents(commits, project_entry)
    GitFeederController.insert_branches(branches, project_entry)
    GitFeederController.insert_branch_trails(branches, project_entry)
    GitFeederController.update_branch_merge_target(branches, project_entry)

    commit_hashes = list(commit_hash_set)
    BenchmarkExecutionController.create_bench_executions_from_commits(
        project_entry, commit_hashes)
    BenchmarkFluctuationController.populate_fluctuation_waivers()

    return res.get_response(200, 'Commits added correctly', {})
def save_benchmark_execution(request, benchmark_execution_id):
    """ Check and save a benchmark execution data into the db """
    if request.method == 'POST':
        bench_exec_entry = BenchmarkExecutionEntry.objects.filter(
            id=benchmark_execution_id).first()
        if bench_exec_entry is None:
            return res.get_response(404, 'Bench Execution Entry not found', {})

        (is_json_valid, post_info) = val.validate_json_string(request.body)
        if not is_json_valid:
            ViewNotifications.notify_json_invalid(
                bench_exec_entry.commit.author.email, request.body)
            return res.get_json_parser_failed({})

        (obj_validated, val_resp_obj) = val.validate_obj_schema(
            post_info, BenchmarkExecutionSchemas.SAVE_BENCHMARK_EXECUTION)
        if not obj_validated:
            ViewNotifications.notify_schema_failed(
                bench_exec_entry.commit.author.email, post_info, val_resp_obj)
            report = create_false_report('schema_failed', val_resp_obj)
            BenchmarkExecutionController.save_bench_execution(
                bench_exec_entry, report)
            return res.get_schema_failed(val_resp_obj)

        for command in val_resp_obj['command_set']:
            (ids_correct,
             ids) = check_benchmark_json_ids(command['result']['out'])
            if not ids_correct:
                data = {'out': command['result']['out'], 'ids': ids}
                return res.get_response(400, 'Benchmark ids are not correct',
                                        data)

        report = val_resp_obj
        BenchmarkExecutionController.save_bench_execution(
            bench_exec_entry, report)
        young_to_notify = BenchmarkExecutionController.is_benchmark_young_for_notifications(
            bench_exec_entry)

        if not did_commands_succeed(report) and young_to_notify:
            ViewNotifications.notify_benchmark_command_failure(
                bench_exec_entry.id, bench_exec_entry.commit.author.email,
                bench_exec_entry.commit.commit_hash, report,
                request.get_host())

        allow_notifications = BenchmarkFluctuationWaiverEntry.objects.filter(
            git_user__id=bench_exec_entry.commit.author.id,
            notification_allowed=True).exists()

        fluctuation_exist = BenchmarkFluctuationController.does_benchmark_fluctuation_exist(
            bench_exec_entry)

        if fluctuation_exist[0] and young_to_notify and allow_notifications:
            ViewNotifications.notify_benchmark_fluctuation(
                bench_exec_entry.id, bench_exec_entry.commit.as_object(),
                bench_exec_entry.worker.as_object(),
                bench_exec_entry.definition.name, request.get_host(),
                fluctuation_exist[1])

        return res.get_response(200, 'Benchmark Execution saved', {})
    else:
        return res.get_response(400, 'Only post allowed', {})