use of org.jboss.pnc.facade.validation.OperationNotAllowedException in project pnc by project-ncl.
the class BrewPusherImpl method doPushBuild.
private BuildPushResult doPushBuild(Base32LongID buildId, BuildPushParameters buildPushParameters, Long buildPushResultId) throws ProcessException {
// TODO START timing event
userLog.info("Push started.");
// collect and validate input data
BuildRecord buildRecord = getLatestSuccessfullyExecutedBuildRecord(buildId);
if (buildRecord.getExecutionRootName() == null && !buildRecord.getBuildConfigurationAudited().getGenericParameters().containsKey(BREW_BUILD_NAME.name())) {
throw new InvalidEntityException("Build " + buildId + " cannot be pushed to brew, because it is missing " + Attributes.BUILD_BREW_NAME + " attribute with brew name.");
}
List<Artifact> artifacts = artifactRepository.queryWithPredicates(ArtifactPredicates.withBuildRecordId(buildRecord.getId()));
if (hasBadArtifactQuality(artifacts)) {
String message = "Build contains artifacts of insufficient quality: BLACKLISTED/DELETED.";
log.debug(message);
BuildPushResult pushResult = BuildPushResult.builder().buildId(BuildMapper.idMapper.toDto(buildId)).status(BuildPushStatus.REJECTED).id(buildPushResultId.toString()).logContext(buildPushResultId.toString()).message(message).build();
throw new OperationNotAllowedException(message, pushResult);
}
log.debug("Pushing Build.id {}.", buildRecord.getId());
BuildPushOperation buildPushOperation = new BuildPushOperation(buildRecord, buildPushResultId, buildPushParameters.getTagPrefix(), buildPushParameters.isReimport(), getCompleteCallbackUrlTemplate());
Result pushResult = buildResultPushManager.push(buildPushOperation, userService.currentUserToken());
log.info("Push Result {}.", pushResult);
BuildPushResult result = BuildPushResult.builder().id(pushResult.getId()).buildId(pushResult.getBuildId()).status(pushResult.getStatus()).logContext(pushResult.getId()).message(pushResult.getMessage()).build();
// verify operation status
switch(pushResult.getStatus()) {
case ACCEPTED:
userLog.info("Push ACCEPTED.");
return result;
case REJECTED:
userLog.warn("Push REJECTED.");
throw new AlreadyRunningException(pushResult.getMessage(), result);
case SYSTEM_ERROR:
userLog.error("Brew push failed: " + pushResult.getMessage());
throw new ProcessException(pushResult.getMessage());
default:
userLog.error("Invalid push result status.");
throw new ProcessException("Invalid push result status.");
}
}
use of org.jboss.pnc.facade.validation.OperationNotAllowedException in project pnc by project-ncl.
the class BrewPusherImpl method pushGroup.
@Override
public Set<BuildPushResult> pushGroup(int buildGroupId, String tagPrefix) {
BuildPushParameters buildPushParameters = BuildPushParameters.builder().tagPrefix(tagPrefix).reimport(false).build();
List<BuildRecord> buildRecords = buildRecordRepository.queryWithPredicates(BuildRecordPredicates.withBuildConfigSetRecordId(buildGroupId));
Set<BuildPushResult> results = new HashSet<>();
for (BuildRecord buildRecord : buildRecords) {
Long buildPushResultId = Sequence.nextId();
MDCUtils.addProcessContext(buildPushResultId.toString());
MDCUtils.addCustomContext(BUILD_ID_KEY, buildRecord.getId().getId());
try {
results.add(doPushBuild(buildRecord.getId(), buildPushParameters, buildPushResultId));
} catch (OperationNotAllowedException | AlreadyRunningException e) {
results.add(BuildPushResult.builder().status(BuildPushStatus.REJECTED).id(buildPushResultId.toString()).buildId(BuildMapper.idMapper.toDto(buildRecord.getId())).message(e.getMessage()).build());
} catch (InconsistentDataException | ProcessException e) {
results.add(BuildPushResult.builder().status(BuildPushStatus.SYSTEM_ERROR).id(buildPushResultId.toString()).buildId(BuildMapper.idMapper.toDto(buildRecord.getId())).message(e.getMessage()).build());
} finally {
MDCUtils.removeProcessContext();
MDCUtils.removeCustomContext(BUILD_ID_KEY);
}
}
return results;
}
use of org.jboss.pnc.facade.validation.OperationNotAllowedException in project pnc by project-ncl.
the class BrewPusherImpl method pushBuild.
@Override
public BuildPushResult pushBuild(String buildId, BuildPushParameters buildPushParameters) throws ProcessException {
Base32LongID id = BuildMapper.idMapper.toEntity(buildId);
BuildRecord build = buildRecordRepository.queryById(BuildMapper.idMapper.toEntity(buildId));
if (build.getStatus().equals(BuildStatus.NO_REBUILD_REQUIRED)) {
throw new OperationNotAllowedException("Build has NO_REBUILD_REQUIRED status, push last successful build or use force-rebuild.");
}
Long buildPushResultId = Sequence.nextId();
MDCUtils.addProcessContext(buildPushResultId.toString());
MDCUtils.addCustomContext(BUILD_ID_KEY, id.getId());
try {
return doPushBuild(id, buildPushParameters, buildPushResultId);
} finally {
MDCUtils.removeProcessContext();
MDCUtils.removeCustomContext(BUILD_ID_KEY);
}
}
use of org.jboss.pnc.facade.validation.OperationNotAllowedException in project pnc by project-ncl.
the class BrewPusherImpl method getLatestSuccessfullyExecutedBuildRecord.
/**
* @param buildRecordId
* @return Latest build record with status success or null if the build record does not exist.
* @throws InconsistentDataException when there is no SUCCESS status before NO_REBUILD_REQUIRED
* @throws InvalidEntityException when the status is not SUCCESS or NO_REBUILD_REQUIRED
*/
private BuildRecord getLatestSuccessfullyExecutedBuildRecord(Base32LongID buildRecordId) {
BuildRecord buildRecord = buildRecordRepository.findByIdFetchProperties(buildRecordId);
if (buildRecord == null) {
throw new EmptyEntityException("Build record not found.");
}
switch(buildRecord.getStatus()) {
case SUCCESS:
return buildRecord;
case NO_REBUILD_REQUIRED:
// if status is NO_REBUILD_REQUIRED, find the associated BuildRecord which was linked as the no rebuild
// cause
BuildRecord noRebuildCause = buildRecord.getNoRebuildCause();
if (noRebuildCause != null) {
return noRebuildCause;
} else {
String message = "There is no SUCCESS build before NO_REBUILD_REQUIRED.";
log.error(message);
throw new InconsistentDataException(message);
}
default:
// Build status is not SUCCESS or NO_REBUILD_REQUIRED.
throw new OperationNotAllowedException("Not allowed to push failed build.");
}
}
Aggregations