private void processEvents(DbSession session, Component component, Long analysisDate) { Function<Event, EventDto> eventToEventDto = event -> newBaseEvent(component, analysisDate) .setName(event.getName()) .setCategory(convertCategory(event.getCategory())) .setDescription(event.getDescription()) .setData(event.getData()); // FIXME bulk insert for (EventDto batchEventDto : StreamSupport.stream(eventRepository.getEvents(component).spliterator(), false).map(eventToEventDto).collect(Collectors.toList())) { dbClient.eventDao().insert(session, batchEventDto); } }
private void saveVersionEvent(DbSession session, Component component, Long analysisDate) { String version = component.getProjectAttributes().getVersion(); deletePreviousEventsHavingSameVersion(session, version, component); dbClient.eventDao().insert(session, newBaseEvent(component, analysisDate) .setName(version) .setCategory(EventDto.CATEGORY_VERSION)); }
private EventDto toDbEvent(CreateEventRequest request, SnapshotDto analysis) { checkArgument(isNotBlank(request.getName()), "A non empty name is required"); return new EventDto() .setUuid(uuidFactory.create()) .setAnalysisUuid(analysis.getUuid()) .setComponentUuid(analysis.getComponentUuid()) .setCategory(request.getCategory().getLabel()) .setName(request.getName()) .setCreatedAt(system.now()) .setDate(analysis.getCreatedAt()); }
@Test public void fail_with_WS_categories() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Event of category 'QUALITY_GATE' cannot be modified. Authorized categories: VERSION, OTHER"); EventValidator.checkModifiable().accept(newEvent().setCategory(QUALITY_GATE.getLabel())); }
@Test public void update_OTHER_event_does_not_update_analysis_version() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel())); call("E1", "6.3"); SnapshotDto updatedAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(updatedAnalysis.getVersion()).isEqualTo("5.6"); }
@Test public void limit_version_name_length_to_100_for_analysis_events() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()).setName("E1 name")); db.events().insertEvent(newEvent(analysis).setUuid("E2").setCategory(VERSION.getLabel()).setName("E2 name")); call("E1", repeat("a", 100)); call("E1", repeat("a", 101)); call("E2", repeat("a", 100)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Version length (101) is longer than the maximum authorized (100). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided"); call("E2", repeat("a", 101)); }
@Test public void update_VERSION_event_update_analysis_version() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel())); call("E1", "6.3"); SnapshotDto updatedAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(updatedAnalysis.getVersion()).isEqualTo("6.3"); }
public static EventDto newEvent(SnapshotDto analysis) { requireNonNull(analysis.getUuid()); requireNonNull(analysis.getComponentUuid()); return new EventDto() .setAnalysisUuid(analysis.getUuid()) .setComponentUuid(analysis.getComponentUuid()) .setUuid(randomAlphanumeric(40)) .setName(randomAlphanumeric(400)) .setDescription(null) .setCategory("Other") .setComponentUuid(analysis.getComponentUuid()) .setCreatedAt(System.currentTimeMillis()) .setDate(System.currentTimeMillis()); } }
@Test public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(true)); // 2008-11-11 dbTester.events().insertEvent(newEvent(analysis).setName("0.9").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(new TestComputationStepContext()); assertPeriod(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, analysis.getCreatedAt(), analysis.getUuid()); verifyDebugLogs("Resolving first analysis as new code period as there is only one existing version"); }
@Test public void fail_if_other_event_with_same_name_on_same_analysis() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()).setName("E1 name")); db.events().insertEvent(newEvent(analysis).setUuid("E2").setCategory(OTHER.getLabel()).setName("E2 name")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("An 'Other' event with the same name already exists on analysis '" + analysis.getUuid() + "'"); call("E2", "E1 name"); }
private EventDto newVersionEventDto(ComponentDto project, long date, String name) { return new EventDto().setUuid(uuidFactory.create()).setComponentUuid(project.uuid()) .setAnalysisUuid("analysis_uuid") .setCategory(CATEGORY_VERSION) .setName(name).setDate(date).setCreatedAt(date); }
@Test public void fail_if_category_other_than_other_or_version() { SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6"); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory("Profile")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Event of category 'QUALITY_PROFILE' cannot be modified. Authorized categories: VERSION, OTHER"); call("E1", "name"); }
@Test public void insert() { dbTester.prepareDbUnit(getClass(), "empty.xml"); underTest.insert(dbTester.getSession(), new EventDto() .setUuid("E1") .setAnalysisUuid("uuid_1") .setComponentUuid("ABCD") .setName("1.0") .setCategory(EventDto.CATEGORY_VERSION) .setDescription("Version 1.0") .setData("some data") .setDate(1413407091086L) .setCreatedAt(1225630680000L)); dbTester.getSession().commit(); dbTester.assertDbUnit(getClass(), "insert-result.xml", new String[] {"id"}, "events"); }
@Test public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("1.1").setLast(false)); // 2008-11-11 SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 dbTester.events().insertEvent(newEvent(analysis2).setName("1.1").setCategory(CATEGORY_VERSION)); when(system2Mock.now()).thenReturn(november30th2008.getTime()); when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false); setupRoot(project, "1.1"); settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(new TestComputationStepContext()); assertPeriod(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, analysis1.getCreatedAt(), analysis1.getUuid()); verifyDebugLogs("Resolving first analysis as new code period as there is only one existing version"); }
@Test public void delete_version_event() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setVersion("5.6.3").setLast(false)); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel())); logInAsProjectAdministrator(project); call("E1"); SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get(); assertThat(newAnalysis.getVersion()).isNull(); }
@Test public void branch() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(branch)); EventDto event = db.events().insertEvent(newEvent(analysis).setCategory(EventCategory.QUALITY_GATE.getLabel())); List<Analysis> result = call(SearchRequest.builder() .setProject(project.getKey()) .setBranch("my_branch") .build()) .getAnalysesList(); assertThat(result).extracting(Analysis::getKey).containsExactlyInAnyOrder(analysis.getUuid()); assertThat(result.get(0).getEventsList()).extracting(Event::getKey).containsExactlyInAnyOrder(event.getUuid()); }
@Test public void json_example() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setUuid("A2")); db.events().insertEvent(newEvent(analysis) .setUuid("E1") .setCategory(OTHER.getLabel()) .setName("Original Name") .setDescription("Original Description")); logInAsProjectAdministrator(project); String result = ws.newRequest() .setParam(PARAM_EVENT, "E1") .setParam(PARAM_NAME, "My Custom Event") .execute().getInput(); assertJson(result).isSimilarTo(getClass().getResource("update_event-example.json")); }
@Test public void pull_request() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST)); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(branch)); EventDto event = db.events().insertEvent(newEvent(analysis).setCategory(EventCategory.QUALITY_GATE.getLabel())); List<Analysis> result = call(SearchRequest.builder() .setProject(project.getKey()) .setPullRequest("pr-123") .build()) .getAnalysesList(); assertThat(result).extracting(Analysis::getKey).containsExactlyInAnyOrder(analysis.getUuid()); assertThat(result.get(0).getEventsList()).extracting(Event::getKey).containsExactlyInAnyOrder(event.getUuid()); }
@Test public void fail_if_category_different_than_other_and_version() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "P1"); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory("Profile")); logInAsProjectAdministrator(project); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Event of category 'QUALITY_PROFILE' cannot be modified. Authorized categories: VERSION, OTHER"); call("E1"); }
@Test public void fail_if_version_for_last_analysis() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setVersion("5.6.3").setLast(true)); db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel())); logInAsProjectAdministrator(project); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Cannot delete the version event of last analysis"); call("E1"); }