Search in sources :

Example 6 with Changeset

use of org.sonar.ce.task.projectanalysis.scm.Changeset in project sonarqube by SonarSource.

the class IssueAssignerTest method assign_to_author_of_the_most_recent_change_in_all_issue_locations.

@Test
public void assign_to_author_of_the_most_recent_change_in_all_issue_locations() {
    addScmUser("john", "u1");
    addScmUser("jane", "u2");
    Changeset commit1 = Changeset.newChangesetBuilder().setAuthor("john").setDate(1_000L).setRevision("rev-1").build();
    Changeset commit2 = Changeset.newChangesetBuilder().setAuthor("jane").setDate(2_000L).setRevision("rev-2").build();
    scmInfoRepository.setScmInfo(FILE_REF, commit1, commit1, commit2, commit1);
    DefaultIssue issue = newIssueOnLines(2, 3, 4);
    underTest.onIssue(FILE, issue);
    assertThat(issue.authorLogin()).isEqualTo("jane");
    assertThat(issue.assignee()).isEqualTo("u2");
}
Also used : DefaultIssue(org.sonar.core.issue.DefaultIssue) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Test(org.junit.Test)

Example 7 with Changeset

use of org.sonar.ce.task.projectanalysis.scm.Changeset in project sonarqube by SonarSource.

the class IssueAssignerTest method assign_to_default_assignee_if_no_scm_on_issue_locations.

@Test
public void assign_to_default_assignee_if_no_scm_on_issue_locations() {
    addScmUser("john", "John C");
    Changeset changeset = Changeset.newChangesetBuilder().setAuthor("john").setDate(123456789L).setRevision("rev-1").build();
    scmInfoRepository.setScmInfo(FILE_REF, changeset, changeset);
    DefaultIssue issue = newIssueOnLines(3);
    underTest.onIssue(FILE, issue);
    assertThat(issue.assignee()).isEqualTo("John C");
}
Also used : DefaultIssue(org.sonar.core.issue.DefaultIssue) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Test(org.junit.Test)

Example 8 with Changeset

use of org.sonar.ce.task.projectanalysis.scm.Changeset in project sonarqube by SonarSource.

the class IssueAssignerTest method assign_to_last_committer_of_file_if_issue_is_global_to_file.

@Test
public void assign_to_last_committer_of_file_if_issue_is_global_to_file() {
    addScmUser("henry", "Henry V");
    Changeset changeset1 = Changeset.newChangesetBuilder().setAuthor("john").setDate(1_000L).setRevision("rev-1").build();
    // Latest changeset
    Changeset changeset2 = Changeset.newChangesetBuilder().setAuthor("henry").setDate(2_000L).setRevision("rev-2").build();
    scmInfoRepository.setScmInfo(FILE_REF, changeset1, changeset2, changeset1);
    DefaultIssue issue = newIssueOnLines();
    underTest.onIssue(FILE, issue);
    assertThat(issue.assignee()).isEqualTo("Henry V");
}
Also used : DefaultIssue(org.sonar.core.issue.DefaultIssue) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Test(org.junit.Test)

Example 9 with Changeset

use of org.sonar.ce.task.projectanalysis.scm.Changeset in project sonarqube by SonarSource.

the class FileSourceDataComputerTest method compute_builds_data_object_from_lines.

@Test
public void compute_builds_data_object_from_lines() {
    int lineCount = 1 + new Random().nextInt(10);
    int randomStartPoint = new Random().nextInt(500);
    List<String> lines = IntStream.range(0, lineCount).mapToObj(i -> "line" + i).collect(toList());
    List<String> expectedLineHashes = IntStream.range(0, 1 + new Random().nextInt(12)).mapToObj(i -> "str_" + i).collect(toList());
    Changeset expectedChangeset = Changeset.newChangesetBuilder().setDate((long) new Random().nextInt(9_999)).build();
    String expectedSrcHash = computeSrcHash(lines);
    CloseableIterator<String> lineIterator = spy(CloseableIterator.from(lines.iterator()));
    DbFileSources.Data.Builder expectedLineDataBuilder = DbFileSources.Data.newBuilder();
    for (int i = 0; i < lines.size(); i++) {
        expectedLineDataBuilder.addLinesBuilder().setSource(lines.get(i)).setLine(i + 1).setScmAuthor("reader_called_" + (randomStartPoint + i));
    }
    when(sourceLinesRepository.readLines(FILE)).thenReturn(lineIterator);
    when(sourceLineReadersFactory.getLineReaders(FILE)).thenReturn(lineReaders);
    when(sourceLinesHashRepository.getLineHashesComputerToPersist(FILE)).thenReturn(lineHashesComputer);
    when(lineHashesComputer.getResult()).thenReturn(expectedLineHashes);
    when(lineReaders.getLatestChangeWithRevision()).thenReturn(expectedChangeset);
    // mocked implementation of LineReader.read to ensure changes done by it to the lineBuilder argument actually end
    // up in the FileSourceDataComputer.Data object returned
    doAnswer(new Answer() {

        int i = 0;

        @Override
        public Object answer(InvocationOnMock invocation) {
            DbFileSources.Line.Builder lineBuilder = invocation.getArgument(0);
            lineBuilder.setScmAuthor("reader_called_" + (randomStartPoint + i++));
            return null;
        }
    }).when(lineReaders).read(any(), any());
    FileSourceDataComputer.Data data = underTest.compute(FILE, fileSourceDataWarnings);
    assertThat(data.getLineHashes()).isEqualTo(expectedLineHashes);
    assertThat(data.getSrcHash()).isEqualTo(expectedSrcHash);
    assertThat(data.getLatestChangeWithRevision()).isSameAs(expectedChangeset);
    assertThat(data.getLineData()).isEqualTo(expectedLineDataBuilder.build());
    verify(lineIterator).close();
    verify(lineReaders).close();
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LineHashesComputer(org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.LineHashesComputer) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) DbFileSources(org.sonar.db.protobuf.DbFileSources) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SourceHashComputer(org.sonar.core.hash.SourceHashComputer) Random(java.util.Random) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Mockito.spy(org.mockito.Mockito.spy) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Before(org.junit.Before) Component(org.sonar.ce.task.projectanalysis.component.Component) Iterator(java.util.Iterator) CloseableIterator(org.sonar.core.util.CloseableIterator) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Random(java.util.Random) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DbFileSources(org.sonar.db.protobuf.DbFileSources) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Test(org.junit.Test)

Example 10 with Changeset

use of org.sonar.ce.task.projectanalysis.scm.Changeset in project sonarqube by SonarSource.

the class PersistFileSourcesStepTest method update_sources_when_revision_is_missing.

@Test
public void update_sources_when_revision_is_missing() {
    DbFileSources.Data sourceData = DbFileSources.Data.newBuilder().addLines(DbFileSources.Line.newBuilder().setLine(1).setSource("line").build()).build();
    dbClient.fileSourceDao().insert(dbTester.getSession(), createDto(dto -> dto.setRevision(null)));
    dbTester.getSession().commit();
    Changeset changeset = Changeset.newChangesetBuilder().setDate(1L).setRevision("revision").build();
    setComputedData(sourceData, Collections.singletonList("137f72c3708c6bd0de00a0e5a69c699b"), "29f25900140c94db38035128cb6de6a2", changeset);
    underTest.execute(new TestComputationStepContext());
    assertThat(dbTester.countRowsOfTable("file_sources")).isOne();
    FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectByFileUuid(session, FILE1_UUID);
    assertThat(fileSourceDto.getCreatedAt()).isEqualTo(PAST);
    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(NOW);
    assertThat(fileSourceDto.getRevision()).isEqualTo("revision");
    verify(fileSourceDataWarnings).commitWarnings();
}
Also used : Arrays(java.util.Arrays) DbFileSources(org.sonar.db.protobuf.DbFileSources) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) DbSession(org.sonar.db.DbSession) Uuids(org.sonar.core.util.Uuids) ComputationStep(org.sonar.ce.task.step.ComputationStep) LineHashVersion(org.sonar.db.source.LineHashVersion) Before(org.junit.Before) Component(org.sonar.ce.task.projectanalysis.component.Component) DbTester(org.sonar.db.DbTester) BaseStepTest(org.sonar.ce.task.projectanalysis.step.BaseStepTest) System2(org.sonar.api.utils.System2) FileSourceDto(org.sonar.db.source.FileSourceDto) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SequenceUuidFactory(org.sonar.core.util.SequenceUuidFactory) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) DbClient(org.sonar.db.DbClient) List(java.util.List) Rule(org.junit.Rule) FileAttributes(org.sonar.ce.task.projectanalysis.component.FileAttributes) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) TreeRootHolderRule(org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) FileSourceDto(org.sonar.db.source.FileSourceDto) DbFileSources(org.sonar.db.protobuf.DbFileSources) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) BaseStepTest(org.sonar.ce.task.projectanalysis.step.BaseStepTest) Test(org.junit.Test)

Aggregations

Changeset (org.sonar.ce.task.projectanalysis.scm.Changeset)15 Test (org.junit.Test)10 BaseStepTest (org.sonar.ce.task.projectanalysis.step.BaseStepTest)4 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)4 FileSourceDto (org.sonar.db.source.FileSourceDto)4 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 DbFileSources (org.sonar.db.protobuf.DbFileSources)3 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Before (org.junit.Before)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2 Component (org.sonar.ce.task.projectanalysis.component.Component)2 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)2 ScmInfo (org.sonar.ce.task.projectanalysis.scm.ScmInfo)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1