Search in sources :

Example 1 with ValidationException

use of org.jboss.pnc.spi.exception.ValidationException in project pnc by project-ncl.

the class TemporaryBuildsCleanerAsyncInvoker method deleteTemporaryBuild.

/**
 * Deletes a single temporary build.
 *
 * @param buildRecordId ID of the build to be deleted
 * @param authToken Bearer token
 * @param onComplete Operation to be executed after deletion operation
 * @return True if the build exists and deletion started otherwise, false is build doesn't exist
 * @throws ValidationException Thrown when build cannot be deleted
 */
public boolean deleteTemporaryBuild(Base32LongID buildRecordId, String authToken, Consumer<Result> onComplete) throws ValidationException {
    BuildRecord buildRecord = buildRecordRepository.findByIdFetchAllProperties(buildRecordId);
    if (buildRecord == null) {
        return false;
    }
    if (!buildRecord.isTemporaryBuild()) {
        throw new ValidationException("Only deletion of the temporary builds is allowed");
    }
    executorService.submit(() -> {
        try {
            Result result = temporaryBuildsCleaner.deleteTemporaryBuild(buildRecordId, authToken);
            onComplete.accept(result);
        } catch (ValidationException e) {
            logger.error("Failed to delete temporary buildRecord.id: " + buildRecordId + ".", e);
            onComplete.accept(new Result(buildRecordId.toString(), ResultStatus.FAILED, "Failed to delete temporary buildRecord."));
        }
    });
    return true;
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildRecord(org.jboss.pnc.model.BuildRecord) Result(org.jboss.pnc.spi.coordinator.Result)

Example 2 with ValidationException

use of org.jboss.pnc.spi.exception.ValidationException in project pnc by project-ncl.

the class TemporaryBuildsCleanerAsyncInvoker method deleteTemporaryBuildConfigSetRecord.

/**
 * Deletes a temporary BuildConfigSetRecord.
 *
 * @param buildConfigSetRecordId ID of the BuildConfigSetRecord to be deleted
 * @param authToken Bearer token
 * @param onComplete Operation to be executed after deletion operation
 * @return True if the build exists and deletion started otherwise, false is build doesn't exist
 * @throws ValidationException Thrown when build cannot be deleted
 */
public boolean deleteTemporaryBuildConfigSetRecord(Integer buildConfigSetRecordId, String authToken, Consumer<Result> onComplete) throws ValidationException {
    BuildConfigSetRecord buildConfigSetRecord = buildConfigSetRecordRepository.queryById(buildConfigSetRecordId);
    if (buildConfigSetRecord == null) {
        return false;
    }
    if (!buildConfigSetRecord.isTemporaryBuild()) {
        throw new ValidationException("Only deletion of the temporary builds is allowed");
    }
    executorService.submit(() -> {
        try {
            Result result = temporaryBuildsCleaner.deleteTemporaryBuildConfigSetRecord(buildConfigSetRecordId, authToken);
            onComplete.accept(result);
        } catch (ValidationException e) {
            logger.error("Failed to delete temporary buildConfigSetRecord.id: " + buildConfigSetRecordId + ".", e);
            onComplete.accept(new Result(buildConfigSetRecordId.toString(), ResultStatus.FAILED, "Failed to delete temporary buildConfigSetRecord."));
        }
    });
    return true;
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildConfigSetRecord(org.jboss.pnc.model.BuildConfigSetRecord) Result(org.jboss.pnc.spi.coordinator.Result)

Example 3 with ValidationException

use of org.jboss.pnc.spi.exception.ValidationException in project pnc by project-ncl.

the class TemporaryBuildsCleaner method deleteTemporaryBuildConfigSetRecord.

/**
 * Deletes a BuildConfigSetRecord and BuildRecords produced in the build
 *
 * @param buildConfigSetRecordId BuildConfigSetRecord to be deleted
 * @param authToken
 */
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Result deleteTemporaryBuildConfigSetRecord(Integer buildConfigSetRecordId, String authToken) throws ValidationException {
    BuildConfigSetRecord buildConfigSetRecord = buildConfigSetRecordRepository.queryById(buildConfigSetRecordId);
    if (buildConfigSetRecord == null) {
        throw new ValidationException("Cannot delete temporary BuildConfigSetRecord with id " + buildConfigSetRecordId + " as no BuildConfigSetRecord with this id exists");
    }
    if (!buildConfigSetRecord.isTemporaryBuild()) {
        throw new ValidationException("Only deletion of the temporary builds is allowed");
    }
    log.info("Starting deletion of a temporary build record set " + buildConfigSetRecord);
    for (BuildRecord br : buildConfigSetRecord.getBuildRecords()) {
        br.setBuildConfigSetRecord(null);
        buildRecordRepository.save(br);
    }
    buildConfigSetRecordRepository.delete(buildConfigSetRecord.getId());
    log.info("Deletion of a temporary build record set {} finished successfully.", buildConfigSetRecord);
    return new Result(buildConfigSetRecordId.toString(), ResultStatus.SUCCESS);
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildRecord(org.jboss.pnc.model.BuildRecord) BuildConfigSetRecord(org.jboss.pnc.model.BuildConfigSetRecord) Result(org.jboss.pnc.spi.coordinator.Result) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 4 with ValidationException

use of org.jboss.pnc.spi.exception.ValidationException in project pnc by project-ncl.

the class TemporaryBuildsCleaner method deleteTemporaryBuild.

private Result deleteTemporaryBuild(BuildRecord buildRecord, String authToken) throws ValidationException {
    if (!buildRecord.isTemporaryBuild()) {
        throw new ValidationException("Only deletion of the temporary builds is allowed");
    }
    // first delete BRs where this build is noRebuildCause
    List<BuildRecord> noRebuildBRs = buildRecordRepository.getBuildByCausingRecord(buildRecord.getId());
    for (BuildRecord noRebuildBR : noRebuildBRs) {
        log.info("Deleting build " + noRebuildBR.getId() + " which has noRebuildCause " + buildRecord.getId() + ".");
        deleteTemporaryBuild(noRebuildBR.getId(), authToken);
    }
    // delete the build itself
    log.info("Starting deletion of a temporary build " + buildRecord + "; Built artifacts: " + buildRecord.getBuiltArtifacts() + "; Dependencies: " + buildRecord.getDependencies());
    String externalBuildId = BuildMapper.idMapper.toDto(buildRecord.getId());
    Result result = remoteBuildsCleaner.deleteRemoteBuilds(buildRecord, authToken);
    if (!result.isSuccess()) {
        log.error("Failed to delete remote temporary builds for BR.id:{}.", buildRecord.getId());
        return new Result(externalBuildId, ResultStatus.FAILED, "Failed to delete remote temporary builds.");
    }
    removeBuiltArtifacts(buildRecord);
    buildRecordRepository.delete(buildRecord.getId());
    log.info("Deletion of the temporary build {} finished successfully.", buildRecord);
    return new Result(externalBuildId, ResultStatus.SUCCESS);
}
Also used : ValidationException(org.jboss.pnc.spi.exception.ValidationException) BuildRecord(org.jboss.pnc.model.BuildRecord) Result(org.jboss.pnc.spi.coordinator.Result)

Aggregations

Result (org.jboss.pnc.spi.coordinator.Result)4 ValidationException (org.jboss.pnc.spi.exception.ValidationException)4 BuildRecord (org.jboss.pnc.model.BuildRecord)3 BuildConfigSetRecord (org.jboss.pnc.model.BuildConfigSetRecord)2 TransactionAttribute (javax.ejb.TransactionAttribute)1