Skip to content

Commit

Permalink
RAP-110 Add new endpoint for post-release task
Browse files Browse the repository at this point in the history
  • Loading branch information
QuyenLy87 committed Oct 29, 2024
1 parent b24c04d commit 5fde112
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public interface PublishService {
void publishAdHocFile(ReleaseCenter releaseCenter, InputStream inputStream, String originalFilename, long size, boolean publishComponentIds) throws BusinessServiceException;

boolean exists(ReleaseCenter releaseCenter, String previouslyPublishedPackageName);

void copyReleaseFileToPublishedStore(Build build) throws BusinessServiceException;

void copyReleaseFileToVersionedContentStore(Build build) throws BusinessServiceException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,41 @@ public Map<String, Object> getBranchMetadataIncludeInherited(String path) throws
return mergedMetadata;
}

@Override
public void copyReleaseFileToPublishedStore(Build build) throws BusinessServiceException {
String pkgOutPutDir = s3PathHelper.getBuildOutputFilesPath(build).toString();
List<String> filesFound = srsFileHelper.listFiles(pkgOutPutDir);
ReleaseFile releaseFiles = getReleaseFiles(filesFound);

String publishFilePath = s3PathHelper.getPublishJobFilePath(build.getReleaseCenterKey(), releaseFiles.releaseFileName());
String releaseFileName = releaseFiles.releaseFileName();
String outputFileFullPath = s3PathHelper.getBuildOutputFilePath(build, releaseFileName);

srsFileHelper.copyFile(outputFileFullPath, publishFilePath);
LOGGER.info("Release file: {} is copied to the published path: {}", releaseFileName, publishFilePath);
try {
publishExtractedVersionOfPackage(publishFilePath, srsFileHelper.getFileStream(publishFilePath));
} catch (IOException | DecoderException e) {
throw new BusinessServiceException(e);
}

// copy MD5 file if available
if (releaseFiles.md5FileName() != null) {
copyMd5FileToPublishedDirectory(build, releaseFiles.md5FileName());
}
}

@Override
public void copyReleaseFileToVersionedContentStore(Build build) throws BusinessServiceException {
String pkgOutPutDir = s3PathHelper.getBuildOutputFilesPath(build).toString();
List<String> filesFound = srsFileHelper.listFiles(pkgOutPutDir);
ReleaseFile releaseFiles = getReleaseFiles(filesFound);
String releaseFileName = releaseFiles.releaseFileName();

String outputFileFullPath = s3PathHelper.getBuildOutputFilePath(build, releaseFileName);
copyBuildToVersionedContentsStore(outputFileFullPath, releaseFileName, null);
}

private List<String> getBranchPathStack(String path) {
List<String> paths = new ArrayList<>();
paths.add(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface ReleaseService {
void runReleaseBuild(Build build, Authentication authentication) throws IOException;

void startNewAuthoringCycle(String releaseCenterKey, String productKey, String effectiveTime, String productKeySource, String dependencyPackage) throws ParseException, JAXBException, IOException, BusinessServiceException;

void startNewAuthoringCycle(String releaseCenterKey, String dailyBuildProductKey, Build publishedBuild, String nextCycleEffectiveTime, String previousRelease, String dependencyPackage) throws BusinessServiceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -123,13 +125,27 @@ public void startNewAuthoringCycle(String releaseCenterKey, String productKey, S

String previousPackage = getPreviousPackage(latestPublishedBuild) + RF2Constants.ZIP_FILE_EXTENSION;
replaceManifestFile(releaseCenterKey, productKey, latestPublishedBuild, effectiveTime, configuration.getEffectiveTimeSnomedFormat());
externalMaintainedRefsetsService.copyExternallyMaintainedFiles(releaseCenterKey, configuration.getEffectiveTimeSnomedFormat(), effectiveTime.replaceAll("-", ""), true);
externalMaintainedRefsetsService.copyExternallyMaintainedFiles(releaseCenterKey, configuration.getEffectiveTimeSnomedFormat(), effectiveTime.replace("-", ""), true);
updateProduct(releaseCenterKey, productKey, effectiveTime, dependencyPackage, previousPackage);
} else {
LOGGER.info("The product {} does not have any build which has been published yet", productKeySource);
}
}

@Override
public void startNewAuthoringCycle(String releaseCenterKey, String dailyBuildProductKey, Build publishedBuild, String nextCycleEffectiveTime, String previousRelease, String dependencyPackage) throws BusinessServiceException {
try {
replaceManifestFile(releaseCenterKey, dailyBuildProductKey, publishedBuild, nextCycleEffectiveTime.replace("-", ""), publishedBuild.getConfiguration().getEffectiveTimeSnomedFormat());
externalMaintainedRefsetsService.copyExternallyMaintainedFiles(releaseCenterKey, publishedBuild.getConfiguration().getEffectiveTimeSnomedFormat(), nextCycleEffectiveTime.replace("-", ""), true);

SimpleDateFormat rf2DateFormat = new SimpleDateFormat(Product.SNOMED_DATE_FORMAT);
Date effectiveDate = rf2DateFormat.parse(nextCycleEffectiveTime.replace("-", ""));
updateProduct(releaseCenterKey, dailyBuildProductKey, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(effectiveDate), dependencyPackage, previousRelease);
} catch (IOException | ParseException e) {
throw new BusinessServiceException(e);
}
}

private void updateProduct(String releaseCenterKey, String productKey, String effectiveTime, String dependencyPackage, String previousPackage) throws BusinessServiceException {
Map<String, String> changes = new HashMap<>();
changes.put(ProductService.DAILY_BUILD, ProductService.TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.ihtsdo.buildcloud.core.entity.Build;
import org.ihtsdo.buildcloud.core.service.BuildService;
import org.ihtsdo.buildcloud.core.service.ProductService;
import org.ihtsdo.buildcloud.core.service.PublishService;
import org.ihtsdo.buildcloud.core.service.ReleaseService;
import org.ihtsdo.buildcloud.core.service.build.RF2Constants;
import org.ihtsdo.buildcloud.rest.pojo.PostReleaseRequest;
import org.ihtsdo.buildcloud.rest.security.IsAuthenticatedAsAdmin;
import org.ihtsdo.buildcloud.rest.security.IsAuthenticatedAsAdminOrReleaseManager;
import org.ihtsdo.otf.rest.exception.BadRequestException;
Expand All @@ -14,55 +19,85 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@ConditionalOnProperty(name = "srs.manager", havingValue = "true")
@Controller
@RequestMapping("/centers/{releaseCenterKey}/products")
@RestController
@RequestMapping("/centers/{releaseCenterKey}")
@Tag(name = "Admin", description = "-")
public class AdminController {

@Autowired
private ProductService productService;
@Autowired
private ProductService productService;

@Autowired
private ReleaseService releaseService;
@Autowired
private ReleaseService releaseService;

@PostMapping(value = "/{productKey}/new-authoring-cycle")
@IsAuthenticatedAsAdmin
@ResponseBody
@Operation(summary = "Start new authoring cycle",
description = "This API is for Daily Build only")
public ResponseEntity<Void> startNewAuthoringCycle(@PathVariable String releaseCenterKey,
@PathVariable String productKey,
@Parameter(description = "New effective time. Required format: yyyy-MM-dd", required = true)
@RequestParam String effectiveTime,
@Parameter(description = "The product that contains the latest published build. This param requires a product key", required = true)
@RequestParam String productSource,
@Parameter(description = "New dependency package if needed.")
@RequestParam(required = false) String dependencyPackage)
throws BusinessServiceException, IOException, ParseException, JAXBException {
try {
DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse(effectiveTime);
} catch (final ParseException e) {
throw new BadRequestException("Invalid effectiveTime format. Expecting format " + DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern() + ".", e);
}
releaseService.startNewAuthoringCycle(releaseCenterKey.trim(), productKey.trim(), effectiveTime, productSource.trim(), dependencyPackage != null ? dependencyPackage.trim() : null);
return new ResponseEntity<>(HttpStatus.OK);
}
@Autowired
private PublishService publishService;

@Autowired
private BuildService buildService;

@PostMapping(value = "/products/{productKey}/new-authoring-cycle")
@IsAuthenticatedAsAdmin
@Operation(summary = "Start new authoring cycle. However, this endpoint will be replaced by the new one '/post-release'",
description = "This API is for Daily Build only",
deprecated = true)
public ResponseEntity<Void> startNewAuthoringCycle(@PathVariable String releaseCenterKey,
@PathVariable String productKey,
@Parameter(description = "New effective time. Required format: yyyy-MM-dd", required = true)
@RequestParam String effectiveTime,
@Parameter(description = "The product that contains the latest published build. This param requires a product key", required = true)
@RequestParam String productSource,
@Parameter(description = "New dependency package if needed.")
@RequestParam(required = false) String dependencyPackage)
throws BusinessServiceException, IOException, ParseException, JAXBException {
try {
DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse(effectiveTime);
} catch (final ParseException e) {
throw new BadRequestException("Invalid effectiveTime format. Expecting format " + DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern() + ".", e);
}
releaseService.startNewAuthoringCycle(releaseCenterKey.trim(), productKey.trim(), effectiveTime, productSource.trim(), dependencyPackage != null ? dependencyPackage.trim() : null);
return new ResponseEntity<>(HttpStatus.OK);
}

@PostMapping(value = "/products/{productKey}/upgrade-dependant-version")
@IsAuthenticatedAsAdminOrReleaseManager
@Operation(summary = "Upgrade dependant version for daily build product",
description = "This API is for Daily Build only")
public ResponseEntity<Void> upgradeDependantVersion(@PathVariable String releaseCenterKey, @PathVariable String productKey) throws BusinessServiceException {
productService.upgradeDependantVersion(releaseCenterKey.trim(), productKey.trim());
return ResponseEntity.status(HttpStatus.OK).build();
}

@PostMapping(value = "/post-release")
@IsAuthenticatedAsAdminOrReleaseManager
@Operation(summary = "Run post-release task which will update the daily build for the new authoring cycle")
public ResponseEntity<Void> doPostReleaseTask(@PathVariable String releaseCenterKey,
@RequestBody PostReleaseRequest request) throws BusinessServiceException {
Build build = buildService.find(releaseCenterKey, request.releasedSourceProductKey(), request.releasedSourceBuildKey(), true, null, null, null);

String publishedReleaseFileName = getReleaseFileName(releaseCenterKey, request.releasedSourceProductKey(), request.releasedSourceBuildKey());
publishService.copyReleaseFileToPublishedStore(build);
publishService.copyReleaseFileToVersionedContentStore(build);
releaseService.startNewAuthoringCycle(releaseCenterKey, request.dailyBuildProductKey(), build, request.nextCycleEffectiveTime(), publishedReleaseFileName, request.newDependencyPackage());
return new ResponseEntity<>(HttpStatus.OK);
}

private String getReleaseFileName(String releaseCenterKey, String productKey, String buildKey) throws BusinessServiceException {
List<String> fileNames = buildService.getOutputFilePaths(releaseCenterKey, productKey, buildKey);
for (String fileName : fileNames) {
if (fileName.endsWith(RF2Constants.ZIP_FILE_EXTENSION)) {
return fileName.substring(fileName.lastIndexOf("/" + 1));
}
}
throw new BusinessServiceException("Could not find the release package file for build " + buildKey);
}

@PostMapping(value = "/{productKey}/upgrade-dependant-version")
@IsAuthenticatedAsAdminOrReleaseManager
@ResponseBody
@Operation(summary = "Upgrade dependant version for daily build product",
description = "This API is for Daily Build only")
public ResponseEntity<Void> upgradeDependantVersion(@PathVariable String releaseCenterKey, @PathVariable String productKey) throws BusinessServiceException, IOException, ParseException, JAXBException {
productService.upgradeDependantVersion(releaseCenterKey.trim(), productKey.trim());
return ResponseEntity.status(HttpStatus.OK).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.ihtsdo.buildcloud.rest.pojo;

public record PostReleaseRequest
(String nextCycleEffectiveTime, String dailyBuildProductKey, String releasedSourceProductKey,
String releasedSourceBuildKey, String newDependencyPackage) {
}

0 comments on commit 5fde112

Please sign in to comment.