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', {})
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', {})
def test_log_critical_anonymous(self): LogEntry.critical(AnonymousUser(), 'message-5') entry = LogEntry.objects.all().first() self.assertEqual(None, entry.user) self.assertEqual(LogEntry.CRITICAL, entry.log_type) self.assertEqual('message-5', entry.message)
def test_log_error_anonymous(self): LogEntry.error(AnonymousUser(), 'message-4') entry = LogEntry.objects.all().first() self.assertEqual(None, entry.user) self.assertEqual(LogEntry.ERROR, entry.log_type) self.assertEqual('message-4', entry.message)
def test_log_critical(self): LogEntry.critical(self.user1, 'message-5') entry = LogEntry.objects.all().first() self.assertEqual(self.user1.id, entry.user.id) self.assertEqual(LogEntry.CRITICAL, entry.log_type) self.assertEqual('message-5', entry.message)
def test_log_warning_anonymous(self): LogEntry.warning(AnonymousUser(), 'message-3') entry = LogEntry.objects.all().first() self.assertEqual(None, entry.user) self.assertEqual(LogEntry.WARNING, entry.log_type) self.assertEqual('message-3', entry.message)
def test_log_error(self): LogEntry.error(self.user1, 'message-4') entry = LogEntry.objects.all().first() self.assertEqual(self.user1.id, entry.user.id) self.assertEqual(LogEntry.ERROR, entry.log_type) self.assertEqual('message-4', entry.message)
def test_log_warning(self): LogEntry.warning(self.user1, 'message-3') entry = LogEntry.objects.all().first() self.assertEqual(self.user1.id, entry.user.id) self.assertEqual(LogEntry.WARNING, entry.log_type) self.assertEqual('message-3', entry.message)
def test_log_info_anonymous(self): LogEntry.info(AnonymousUser(), 'message-2') entry = LogEntry.objects.all().first() self.assertEqual(None, entry.user) self.assertEqual(LogEntry.INFO, entry.log_type) self.assertEqual('message-2', entry.message)
def test_log_info(self): LogEntry.info(self.user1, 'message-2') entry = LogEntry.objects.all().first() self.assertEqual(self.user1.id, entry.user.id) self.assertEqual(LogEntry.INFO, entry.log_type) self.assertEqual('message-2', entry.message)
def test_log_debug_anonymous(self): LogEntry.debug(AnonymousUser(), 'message-1') entry = LogEntry.objects.all().first() self.assertEqual(None, entry.user) self.assertEqual(LogEntry.DEBUG, entry.log_type) self.assertEqual('message-1', entry.message)
def test_log_debug(self): LogEntry.debug(self.user1, 'message-1') entry = LogEntry.objects.all().first() self.assertEqual(self.user1.id, entry.user.id) self.assertEqual(LogEntry.DEBUG, entry.log_type) self.assertEqual('message-1', entry.message)
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({})
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', {})