Search in sources :

Example 36 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class IssueLifecycleTest method mergeIssueFromPRIntoBranch.

@Test
public void mergeIssueFromPRIntoBranch() {
    DefaultIssue raw = new DefaultIssue().setKey("raw");
    DefaultIssue fromShort = new DefaultIssue().setKey("short").setIsNewCodeReferenceIssue(true);
    fromShort.setResolution("resolution");
    fromShort.setStatus("status");
    Date commentDate = new Date();
    fromShort.addComment(new DefaultIssueComment().setIssueKey("short").setCreatedAt(commentDate).setUserUuid("user_uuid").setMarkdownText("A comment"));
    Date diffDate = new Date();
    // file diff alone
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("file", "uuidA1", "uuidB1"));
    // file diff with another field
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("severity", "MINOR", "MAJOR").setDiff("file", "uuidA2", "uuidB2"));
    Branch branch = mock(Branch.class);
    when(branch.getName()).thenReturn("master");
    analysisMetadataHolder.setBranch(branch);
    underTest.mergeConfirmedOrResolvedFromPrOrBranch(raw, fromShort, BranchType.PULL_REQUEST, "2");
    assertThat(raw.resolution()).isEqualTo("resolution");
    assertThat(raw.status()).isEqualTo("status");
    assertThat(raw.defaultIssueComments()).extracting(DefaultIssueComment::issueKey, DefaultIssueComment::createdAt, DefaultIssueComment::userUuid, DefaultIssueComment::markdownText).containsOnly(tuple("raw", commentDate, "user_uuid", "A comment"));
    assertThat(raw.changes()).hasSize(2);
    assertThat(raw.changes().get(0).creationDate()).isEqualTo(diffDate);
    assertThat(raw.changes().get(0).userUuid()).isEqualTo("user_uuid");
    assertThat(raw.changes().get(0).issueKey()).isEqualTo("raw");
    assertThat(raw.changes().get(0).diffs()).containsOnlyKeys("severity");
    assertThat(raw.changes().get(1).userUuid()).isEqualTo("default_user_uuid");
    assertThat(raw.changes().get(1).diffs()).containsOnlyKeys(IssueFieldsSetter.FROM_BRANCH);
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).oldValue()).isEqualTo("#2");
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).newValue()).isEqualTo("master");
    assertThat(raw.isNewCodeReferenceIssue()).isTrue();
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) DateUtils.parseDate(org.sonar.api.utils.DateUtils.parseDate) DefaultIssueComment(org.sonar.core.issue.DefaultIssueComment) Test(org.junit.Test)

Example 37 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class IssueLifecycleTest method copyExistingIssuesFromSourceBranchOfPullRequest_only_works_for_pull_requests.

@Test
public void copyExistingIssuesFromSourceBranchOfPullRequest_only_works_for_pull_requests() {
    DefaultIssue raw = new DefaultIssue().setKey("raw");
    DefaultIssue from = new DefaultIssue().setKey("short");
    from.setResolution("resolution");
    from.setStatus("status");
    Branch branch = mock(Branch.class);
    when(branch.getType()).thenReturn(BranchType.BRANCH);
    analysisMetadataHolder.setBranch(branch);
    assertThatThrownBy(() -> underTest.copyExistingIssueFromSourceBranchToPullRequest(raw, from)).isInstanceOf(IllegalStateException.class).hasMessage("This operation should be done only on pull request analysis");
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 38 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class IssueLifecycleTest method copyExistingIssuesFromSourceBranchOfPullRequest.

@Test
public void copyExistingIssuesFromSourceBranchOfPullRequest() {
    String pullRequestKey = "1";
    Branch branch = mock(Branch.class);
    when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
    when(branch.getName()).thenReturn("sourceBranch-1");
    when(branch.getPullRequestKey()).thenReturn(pullRequestKey);
    analysisMetadataHolder.setBranch(branch);
    analysisMetadataHolder.setPullRequestKey(pullRequestKey);
    DefaultIssue raw = new DefaultIssue().setKey("raw");
    DefaultIssue fromShort = new DefaultIssue().setKey("short");
    fromShort.setResolution("resolution");
    fromShort.setStatus("status");
    Date commentDate = new Date();
    fromShort.addComment(new DefaultIssueComment().setIssueKey("short").setCreatedAt(commentDate).setUserUuid("user_uuid").setMarkdownText("A comment"));
    Date diffDate = new Date();
    // file diff alone
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("file", "uuidA1", "uuidB1"));
    // file diff with another field
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("severity", "MINOR", "MAJOR").setDiff("file", "uuidA2", "uuidB2"));
    underTest.copyExistingIssueFromSourceBranchToPullRequest(raw, fromShort);
    assertThat(raw.resolution()).isEqualTo("resolution");
    assertThat(raw.status()).isEqualTo("status");
    assertThat(raw.defaultIssueComments()).extracting(DefaultIssueComment::issueKey, DefaultIssueComment::createdAt, DefaultIssueComment::userUuid, DefaultIssueComment::markdownText).containsOnly(tuple("raw", commentDate, "user_uuid", "A comment"));
    assertThat(raw.changes()).hasSize(2);
    assertThat(raw.changes().get(0).creationDate()).isEqualTo(diffDate);
    assertThat(raw.changes().get(0).userUuid()).isEqualTo("user_uuid");
    assertThat(raw.changes().get(0).issueKey()).isEqualTo("raw");
    assertThat(raw.changes().get(0).diffs()).containsOnlyKeys("severity");
    assertThat(raw.changes().get(1).userUuid()).isEqualTo("default_user_uuid");
    assertThat(raw.changes().get(1).diffs()).containsOnlyKeys(IssueFieldsSetter.FROM_BRANCH);
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).oldValue()).isEqualTo("sourceBranch-1");
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).newValue()).isEqualTo("#1");
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) FieldDiffs(org.sonar.core.issue.FieldDiffs) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) DateUtils.parseDate(org.sonar.api.utils.DateUtils.parseDate) DefaultIssueComment(org.sonar.core.issue.DefaultIssueComment) Test(org.junit.Test)

Example 39 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class IssueLifecycleTest method copiedIssue.

@Test
public void copiedIssue() {
    DefaultIssue raw = new DefaultIssue().setNew(true).setKey("RAW_KEY").setCreationDate(parseDate("2015-10-01")).setUpdateDate(parseDate("2015-10-02")).setCloseDate(parseDate("2015-10-03"));
    DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder().setTextRange(DbCommons.TextRange.newBuilder().setStartLine(10).setEndLine(12).build()).build();
    DefaultIssue base = new DefaultIssue().setKey("BASE_KEY").setCreationDate(parseDate("2015-01-01")).setUpdateDate(parseDate("2015-01-02")).setCloseDate(parseDate("2015-01-03")).setResolution(RESOLUTION_FIXED).setStatus(STATUS_CLOSED).setSeverity(BLOCKER).setAssigneeUuid("base assignee uuid").setAuthorLogin("base author").setTags(newArrayList("base tag")).setOnDisabledRule(true).setSelectedAt(1000L).setLine(10).setMessage("message").setGap(15d).setEffort(Duration.create(15L)).setManualSeverity(false).setLocations(issueLocations);
    when(debtCalculator.calculate(raw)).thenReturn(DEFAULT_DURATION);
    Branch branch = mock(Branch.class);
    when(branch.getName()).thenReturn("release-2.x");
    analysisMetadataHolder.setBranch(branch);
    underTest.copyExistingOpenIssueFromBranch(raw, base, "master");
    assertThat(raw.isNew()).isFalse();
    assertThat(raw.isCopied()).isTrue();
    assertThat(raw.key()).isNotNull();
    assertThat(raw.key()).isNotEqualTo(base.key());
    assertThat(raw.creationDate()).isEqualTo(base.creationDate());
    assertThat(raw.updateDate()).isEqualTo(base.updateDate());
    assertThat(raw.closeDate()).isEqualTo(base.closeDate());
    assertThat(raw.resolution()).isEqualTo(RESOLUTION_FIXED);
    assertThat(raw.status()).isEqualTo(STATUS_CLOSED);
    assertThat(raw.assignee()).isEqualTo("base assignee uuid");
    assertThat(raw.authorLogin()).isEqualTo("base author");
    assertThat(raw.tags()).containsOnly("base tag");
    assertThat(raw.effort()).isEqualTo(DEFAULT_DURATION);
    assertThat(raw.isOnDisabledRule()).isTrue();
    assertThat(raw.selectedAt()).isEqualTo(1000L);
    assertThat(raw.changes().get(0).get(IssueFieldsSetter.FROM_BRANCH).oldValue()).isEqualTo("master");
    assertThat(raw.changes().get(0).get(IssueFieldsSetter.FROM_BRANCH).newValue()).isEqualTo("release-2.x");
    verifyNoInteractions(updater);
}
Also used : DbIssues(org.sonar.db.protobuf.DbIssues) Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 40 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class IssueTrackingDelegatorTest method delegate_merge_tracker.

@Test
public void delegate_merge_tracker() {
    Branch branch = mock(Branch.class);
    when(branch.getType()).thenReturn(BranchType.BRANCH);
    when(branch.isMain()).thenReturn(false);
    when(analysisMetadataHolder.getBranch()).thenReturn(branch);
    when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
    underTest.track(component, rawInput);
    verify(mergeBranchTracker).track(component, rawInput);
    verifyNoInteractions(tracker);
    verifyNoInteractions(prBranchTracker);
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) Test(org.junit.Test)

Aggregations

Branch (org.sonar.ce.task.projectanalysis.analysis.Branch)40 Test (org.junit.Test)21 DefaultIssue (org.sonar.core.issue.DefaultIssue)7 ComponentDto (org.sonar.db.component.ComponentDto)7 Before (org.junit.Before)5 BranchType (org.sonar.db.component.BranchType)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Rule (org.junit.Rule)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.when (org.mockito.Mockito.when)4 AnalysisMetadataHolderRule (org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule)4 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)4 DbTester (org.sonar.db.DbTester)4 Project (org.sonar.server.project.Project)4 Date (java.util.Date)3 FieldDiffs (org.sonar.core.issue.FieldDiffs)3 ComponentTesting (org.sonar.db.component.ComponentTesting)3 SnapshotTesting.newAnalysis (org.sonar.db.component.SnapshotTesting.newAnalysis)3 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)3 RuleKey (org.sonar.api.rule.RuleKey)2