Search in sources :

Example 21 with Project

use of org.sonar.server.project.Project in project sonarqube by SonarSource.

the class ComponentService method updateKey.

public void updateKey(DbSession dbSession, ProjectDto project, String newKey) {
    userSession.checkProjectPermission(UserRole.ADMIN, project);
    checkProjectKey(newKey);
    dbClient.componentKeyUpdaterDao().updateKey(dbSession, project.getUuid(), newKey);
    projectIndexers.commitAndIndexProjects(dbSession, singletonList(project), ProjectIndexer.Cause.PROJECT_KEY_UPDATE);
    Project newProject = new Project(project.getUuid(), newKey, project.getName(), project.getDescription(), project.getTags());
    projectLifeCycleListeners.onProjectsRekeyed(singleton(new RekeyedProject(newProject, project.getKey())));
}
Also used : RekeyedProject(org.sonar.server.project.RekeyedProject) Project(org.sonar.server.project.Project) RekeyedProject(org.sonar.server.project.RekeyedProject)

Example 22 with Project

use of org.sonar.server.project.Project in project sonarqube by SonarSource.

the class SiblingComponentsWithOpenIssuesTest method setUp.

@Before
public void setUp() {
    ComponentDto project = db.components().insertPublicProject();
    metadataHolder.setProject(new Project(project.uuid(), project.getKey(), project.name(), project.description(), Collections.emptyList()));
    branch1 = db.components().insertProjectBranch(project, b -> b.setKey("branch1"), b -> b.setBranchType(BranchType.BRANCH));
    branch1pr1 = db.components().insertProjectBranch(project, b -> b.setKey("branch1pr1"), b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(branch1.uuid()));
    ComponentDto branch1pr2 = db.components().insertProjectBranch(project, b -> b.setKey("branch1pr2"), b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(branch1.uuid()));
    fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1, null));
    RuleDefinitionDto rule = db.rules().insert();
    fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
    db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1);
    fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
    db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
    fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
    db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1);
    db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
    String fileKey = "file-x";
    fileXWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null).setDbKey(fileKey + ":BRANCH:branch1pr1"));
    db.issues().insert(rule, branch1pr1, fileXWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
    fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2, null).setDbKey(fileKey + ":BRANCH:branch1pr2"));
    db.issues().insert(rule, branch1pr2, fileXWithOneResolvedIssueOnBranch1Pr2, i -> i.setStatus("RESOLVED"));
    branch2 = db.components().insertProjectBranch(project, b -> b.setKey("branch2"), b -> b.setBranchType(BranchType.BRANCH));
    ComponentDto branch2pr1 = db.components().insertProjectBranch(project, b -> b.setKey("branch2pr1"), b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(branch2.uuid()));
    fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
    db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1);
    fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
    db.issues().insert(rule, branch2pr1, fileWithOneResolvedIssueOnBranch2Pr1, i -> i.setStatus("RESOLVED"));
    setRoot(branch1);
    underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DbTester(org.sonar.db.DbTester) BranchType(org.sonar.db.component.BranchType) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) ComponentTesting(org.sonar.db.component.ComponentTesting) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Project(org.sonar.server.project.Project) Mockito.mock(org.mockito.Mockito.mock) Project(org.sonar.server.project.Project) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Before(org.junit.Before)

Example 23 with Project

use of org.sonar.server.project.Project in project sonarqube by SonarSource.

the class ReferenceBranchComponentUuidsTest method setUp.

@Before
public void setUp() {
    underTest = new ReferenceBranchComponentUuids(analysisMetadataHolder, db.getDbClient());
    project = mock(Project.class);
    analysisMetadataHolder.setProject(project);
    analysisMetadataHolder.setBranch(branch);
    ComponentDto projectDto = db.components().insertPublicProject();
    when(project.getUuid()).thenReturn(projectDto.uuid());
    branch1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch1"));
    branch2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch2"));
    pr1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr1").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
    pr2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr2").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
    branch1File = ComponentTesting.newFileDto(branch1, null, "file").setUuid("branch1File");
    branch2File = ComponentTesting.newFileDto(branch2, null, "file").setUuid("branch2File");
    pr1File = ComponentTesting.newFileDto(pr1, null, "file").setUuid("file1");
    pr2File = ComponentTesting.newFileDto(pr2, null, "file").setUuid("file2");
    db.components().insertComponents(branch1File, pr1File, pr2File, branch2File);
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DbTester(org.sonar.db.DbTester) BranchType(org.sonar.db.component.BranchType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) ComponentTesting(org.sonar.db.component.ComponentTesting) Before(org.junit.Before) Project(org.sonar.server.project.Project) Mockito.mock(org.mockito.Mockito.mock) Project(org.sonar.server.project.Project) ComponentDto(org.sonar.db.component.ComponentDto) Before(org.junit.Before)

Example 24 with Project

use of org.sonar.server.project.Project in project sonarqube by SonarSource.

the class SendIssueNotificationsStepTest method send_global_new_issues_notification_only_for_non_backdated_issues.

@Test
public void send_global_new_issues_notification_only_for_non_backdated_issues() {
    Random random = new Random();
    Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
    Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
    Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
    List<DefaultIssue> issues = concat(stream(efforts).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setCreationDate(new Date(ANALYSE_DATE))), stream(backDatedEfforts).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))).collect(toList());
    shuffle(issues);
    DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
    issues.forEach(issueCache::append);
    issueCache.close();
    analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
    when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    verify(notificationService).deliver(newIssuesNotificationMock);
    ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
    verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
    verify(newIssuesNotificationMock).setDebt(expectedEffort);
    NewIssuesStatistics.Stats stats = statsCaptor.getValue();
    assertThat(stats.hasIssues()).isTrue();
    // just checking all issues have been added to the stats
    DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
    assertThat(severity.getOnCurrentAnalysis()).isEqualTo(efforts.length);
    assertThat(severity.getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
    verifyStatistics(context, 1, 0, 0);
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) Collections.shuffle(java.util.Collections.shuffle) NOTIF_TYPES(org.sonar.ce.task.projectanalysis.step.SendIssueNotificationsStep.NOTIF_TYPES) Duration(org.sonar.api.utils.Duration) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) ComputationStep(org.sonar.ce.task.step.ComputationStep) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) IssueTesting.newIssue(org.sonar.db.issue.IssueTesting.newIssue) Map(java.util.Map) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) ReportComponent.builder(org.sonar.ce.task.projectanalysis.component.ReportComponent.builder) Mockito.doReturn(org.mockito.Mockito.doReturn) NotificationService(org.sonar.server.notification.NotificationService) ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) DbTester(org.sonar.db.DbTester) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Notification(org.sonar.api.notifications.Notification) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) BRANCH(org.sonar.db.component.BranchType.BRANCH) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) DistributedMetricStatsInt(org.sonar.server.issue.notification.DistributedMetricStatsInt) RuleKey(org.sonar.api.rule.RuleKey) TreeRootHolderRule(org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule) Mockito.any(org.mockito.Mockito.any) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ComponentTesting.newBranchComponent(org.sonar.db.component.ComponentTesting.newBranchComponent) Supplier(java.util.function.Supplier) RuleTesting.newRule(org.sonar.db.rule.RuleTesting.newRule) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) Stream.concat(java.util.stream.Stream.concat) DefaultBranchImpl(org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl) Before(org.junit.Before) Component(org.sonar.ce.task.projectanalysis.component.Component) Tuple(org.assertj.core.groups.Tuple) BranchType(org.sonar.db.component.BranchType) RandomUtils.nextInt(org.apache.commons.lang.math.RandomUtils.nextInt) DefaultIssue(org.sonar.core.issue.DefaultIssue) Type(org.sonar.ce.task.projectanalysis.component.Component.Type) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) NotificationFactory(org.sonar.ce.task.projectanalysis.notification.NotificationFactory) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) TemporaryFolder(org.junit.rules.TemporaryFolder) Project(org.sonar.server.project.Project) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) Duration(org.sonar.api.utils.Duration) DefaultIssue(org.sonar.core.issue.DefaultIssue) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Date(java.util.Date) Project(org.sonar.server.project.Project) Random(java.util.Random) DistributedMetricStatsInt(org.sonar.server.issue.notification.DistributedMetricStatsInt) Test(org.junit.Test)

Example 25 with Project

use of org.sonar.server.project.Project in project sonarqube by SonarSource.

the class PersistLiveMeasuresStepTest method prepareProject.

private void prepareProject() {
    // tree of components as defined by scanner report
    Component project = ReportComponent.builder(PROJECT, REF_1).setUuid("project-uuid").addChildren(ReportComponent.builder(DIRECTORY, REF_3).setUuid("dir-uuid").addChildren(ReportComponent.builder(FILE, REF_4).setUuid("file-uuid").build()).build()).build();
    treeRootHolder.setRoot(project);
    analysisMetadataHolder.setProject(new Project(project.getUuid(), project.getDbKey(), project.getName(), project.getDescription(), emptyList()));
    // components as persisted in db
    ComponentDto projectDto = insertComponent("project-key", "project-uuid");
    ComponentDto dirDto = insertComponent("dir-key", "dir-uuid");
    ComponentDto fileDto = insertComponent("file-key", "file-uuid");
}
Also used : Project(org.sonar.server.project.Project) ComponentDto(org.sonar.db.component.ComponentDto) ViewsComponent(org.sonar.ce.task.projectanalysis.component.ViewsComponent) Component(org.sonar.ce.task.projectanalysis.component.Component) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent)

Aggregations

Project (org.sonar.server.project.Project)26 Test (org.junit.Test)21 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)14 ComponentDto (org.sonar.db.component.ComponentDto)14 Before (org.junit.Before)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 Rule (org.junit.Rule)11 Mockito.mock (org.mockito.Mockito.mock)11 Mockito.when (org.mockito.Mockito.when)11 DbTester (org.sonar.db.DbTester)11 AnalysisMetadataHolderRule (org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule)10 Branch (org.sonar.ce.task.projectanalysis.analysis.Branch)10 BranchType (org.sonar.db.component.BranchType)10 Date (java.util.Date)9 Notification (org.sonar.api.notifications.Notification)8 UserDto (org.sonar.db.user.UserDto)8 IssuesChangesNotification (org.sonar.server.issue.notification.IssuesChangesNotification)8 MyNewIssuesNotification (org.sonar.server.issue.notification.MyNewIssuesNotification)8 NewIssuesNotification (org.sonar.server.issue.notification.NewIssuesNotification)8 System2 (org.sonar.api.utils.System2)6