Search in sources :

Example 11 with ProjectLinkDto

use of org.sonar.db.component.ProjectLinkDto in project sonarqube by SonarSource.

the class CreateAction method doHandle.

private CreateWsResponse doHandle(CreateRequest createWsRequest) {
    String name = createWsRequest.getName();
    String url = createWsRequest.getUrl();
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = getProject(dbSession, createWsRequest);
        userSession.checkProjectPermission(UserRole.ADMIN, project);
        ProjectLinkDto link = new ProjectLinkDto().setUuid(uuidFactory.create()).setProjectUuid(project.getUuid()).setName(name).setHref(url).setType(nameToType(name));
        dbClient.projectLinkDao().insert(dbSession, link);
        dbSession.commit();
        return buildResponse(link);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) ProjectLinkDto(org.sonar.db.component.ProjectLinkDto)

Example 12 with ProjectLinkDto

use of org.sonar.db.component.ProjectLinkDto in project sonarqube by SonarSource.

the class PersistProjectLinksStep method execute.

@Override
public void execute(ComputationStep.Context context) {
    if (!analysisMetadataHolder.getBranch().isMain()) {
        return;
    }
    try (DbSession session = dbClient.openSession(false)) {
        Component project = treeRootHolder.getRoot();
        ScannerReport.Component batchComponent = reportReader.readComponent(project.getReportAttributes().getRef());
        List<ProjectLinkDto> previousLinks = dbClient.projectLinkDao().selectByProjectUuid(session, project.getUuid());
        mergeLinks(session, project.getUuid(), batchComponent.getLinkList(), previousLinks);
        session.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ProjectLinkDto(org.sonar.db.component.ProjectLinkDto) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) Component(org.sonar.ce.task.projectanalysis.component.Component)

Example 13 with ProjectLinkDto

use of org.sonar.db.component.ProjectLinkDto in project sonarqube by SonarSource.

the class PersistProjectLinksStep method mergeLinks.

private void mergeLinks(DbSession session, String componentUuid, List<ScannerReport.ComponentLink> links, List<ProjectLinkDto> previousLinks) {
    Set<String> linkType = new HashSet<>();
    links.forEach(link -> {
        String type = convertType(link.getType());
        checkArgument(!linkType.contains(type), "Link of type '%s' has already been declared on component '%s'", type, componentUuid);
        linkType.add(type);
        Optional<ProjectLinkDto> previousLink = previousLinks.stream().filter(input -> input != null && input.getType().equals(convertType(link.getType()))).findFirst();
        if (previousLink.isPresent()) {
            previousLink.get().setHref(link.getHref());
            dbClient.projectLinkDao().update(session, previousLink.get());
        } else {
            dbClient.projectLinkDao().insert(session, new ProjectLinkDto().setUuid(uuidFactory.create()).setProjectUuid(componentUuid).setType(type).setHref(link.getHref()));
        }
    });
    previousLinks.stream().filter(dto -> !linkType.contains(dto.getType())).filter(dto -> ProjectLinkDto.PROVIDED_TYPES.contains(dto.getType())).forEach(dto -> dbClient.projectLinkDao().delete(session, dto.getUuid()));
}
Also used : Component(org.sonar.ce.task.projectanalysis.component.Component) UuidFactory(org.sonar.core.util.UuidFactory) Set(java.util.Set) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DbSession(org.sonar.db.DbSession) ProjectLinkDto(org.sonar.db.component.ProjectLinkDto) AnalysisMetadataHolder(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder) HashSet(java.util.HashSet) ComponentLinkType(org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType) DbClient(org.sonar.db.DbClient) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ComputationStep(org.sonar.ce.task.step.ComputationStep) Map(java.util.Map) BatchReportReader(org.sonar.ce.task.projectanalysis.batch.BatchReportReader) TreeRootHolder(org.sonar.ce.task.projectanalysis.component.TreeRootHolder) Optional(java.util.Optional) ProjectLinkDto(org.sonar.db.component.ProjectLinkDto) HashSet(java.util.HashSet)

Example 14 with ProjectLinkDto

use of org.sonar.db.component.ProjectLinkDto in project sonarqube by SonarSource.

the class DeleteActionTest method fail_if_anonymous.

@Test
public void fail_if_anonymous() {
    ComponentDto project = db.components().insertPrivateProject();
    ProjectLinkDto link = db.componentLinks().insertCustomLink(project);
    userSession.anonymous();
    assertThatThrownBy(() -> deleteLink(link)).isInstanceOf(ForbiddenException.class);
}
Also used : ProjectLinkDto(org.sonar.db.component.ProjectLinkDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 15 with ProjectLinkDto

use of org.sonar.db.component.ProjectLinkDto in project sonarqube by SonarSource.

the class DeleteActionTest method no_response.

@Test
public void no_response() {
    ComponentDto project = db.components().insertPrivateProject();
    ProjectLinkDto link = db.componentLinks().insertCustomLink(project);
    logInAsProjectAdministrator(project);
    TestResponse response = deleteLink(link);
    assertThat(response.getStatus()).isEqualTo(204);
    assertThat(response.getInput()).isEmpty();
}
Also used : ProjectLinkDto(org.sonar.db.component.ProjectLinkDto) TestResponse(org.sonar.server.ws.TestResponse) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Aggregations

ProjectLinkDto (org.sonar.db.component.ProjectLinkDto)22 Test (org.junit.Test)15 ComponentDto (org.sonar.db.component.ComponentDto)13 SearchWsResponse (org.sonarqube.ws.ProjectLinks.SearchWsResponse)7 DbSession (org.sonar.db.DbSession)6 Component (org.sonar.ce.task.projectanalysis.component.Component)2 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)2 ProjectDto (org.sonar.db.project.ProjectDto)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Link (com.sonarsource.governance.projectdump.protobuf.ProjectDump.Link)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 AnalysisMetadataHolder (org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder)1 BatchReportReader (org.sonar.ce.task.projectanalysis.batch.BatchReportReader)1 TreeRootHolder (org.sonar.ce.task.projectanalysis.component.TreeRootHolder)1 ComputationStep (org.sonar.ce.task.step.ComputationStep)1