Skip to content

Commit

Permalink
Merge pull request #115 from opendatamesh-initiative/114-blueprint-en…
Browse files Browse the repository at this point in the history
…hancements

minor fixes
  • Loading branch information
mattia155 authored Nov 14, 2023
2 parents 443a26a + 203b296 commit 7412433
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,48 @@ public abstract class BlueprintAbstractController {
// TODO: add SEARCH endpoint
// TODO: edit examples, add one for Azure, change urls to SSH

private static final String EXAMPLE_BLUEPRINT = "{\n" + //
private static final String EXAMPLE_BLUEPRINT_GITHUB = "{\n" + //
" \"name\": \"blueprint1\",\n" + //
" \"version\": \"1.0.0\",\n" + //
" \"displayName\": \"first blueprint\",\n" + //
" \"description\": \"description of blueprint\",\n" + //
" \"repositoryProvider\": \"GITHUB\",\n" + //
" \"repositoryUrl\": \"[email protected]:opendatamesh-initiative/blueprint.git\",\n" + //
" \"blueprintDirectory\": \"blueprint-dir\",\n" + //
" \"organization\": \"opendatamesh-initiative\",\n" + //
"}";

private static final String EXAMPLE_BLUEPRINT_AZUREDEVOPS = "{\n" + //
" \"name\": \"blueprint1\",\n" + //
" \"version\": \"1.0.0\",\n" + //
" \"displayName\": \"first blueprint\",\n" + //
" \"description\": \"description of blueprint\",\n" + //
" \"repositoryProvider\": \"AZURE_DEVOPS\",\n" + //
" \"repositoryUrl\": \"https://github.com/opendatamesh-initiative/blueprint1\",\n" + //
" \"repositoryUrl\": \"[email protected]:v3/opendatamesh-initiative/project1/blueprint1\",\n" + //
" \"blueprintDirectory\": \"blueprint-dir\",\n" + //
" \"organization\": \"opendatamesh-initiative\",\n" + //
" \"projectId\": \"123-a61\"\n" + //
"}";
private static final String EXAMPLE_BLUEPRINT_UPDATE = "{\n" + //
private static final String EXAMPLE_BLUEPRINT_UPDATE_GITHUB = "{\n" + //
" \"id\": 1,\n" + //
" \"name\": \"blueprint1 - updated\",\n" + //
" \"version\": \"1.0.1\",\n" + //
" \"displayName\": \"first blueprint\",\n" + //
" \"description\": \"description of blueprint\",\n" + //
" \"repositoryProvider\": \"AZURE_DEVOPS\",\n" + //
" \"repositoryBaseUrl\": \"https://github.com/opendatamesh-initiative\",\n" + //
" \"blueprintRepo\": \"/blueprints1\",\n" + //
" \"targetRepo\": \"/project1.0.1\"\n" + //
" \"repositoryProvider\": \"GITHUB\",\n" + //
" \"repositoryUrl\": \"git@github.com:opendatamesh-initiative/blueprint.git\",\n" + //
" \"blueprintDirectory\": \"blueprint1.0.1-dir\",\n" + //
" \"organization\": \"opendatamesh-initiative\"\n" + //
"}";

private static final String EXAMPLE_CONFIG = "{\n" + //
" \"parameter1\": \"value of parameter 2\",\n" + //
" \"parameter2\": \"value of parameter 2\",\n" + //
" \"parameter3\": \"value of parameter 3\"\n" + //
" \"targetRepo\": \"projectFromBlueprint\",\n" + //
" \"createRepo\": true,\n" + //
" \"config\": {\n" + //
" \"parameter1\": \"value of parameter 2\",\n" + //
" \"parameter2\": \"value of parameter 2\",\n" + //
" \"parameter3\": \"value of parameter 3\"\n" + //
" }\n" + //
"}";

// @see https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#arrayschema
Expand Down Expand Up @@ -181,7 +198,8 @@ public BlueprintResource readBlueprintEndpoint(
mediaType = "application/json",
schema = @Schema(implementation = BlueprintResource.class),
examples = {
@ExampleObject(name = "one", value = EXAMPLE_BLUEPRINT)
@ExampleObject(name = "github", value = EXAMPLE_BLUEPRINT_GITHUB),
@ExampleObject(name = "azuredevops", value = EXAMPLE_BLUEPRINT_AZUREDEVOPS)
}
)
),
Expand Down Expand Up @@ -227,9 +245,14 @@ public BlueprintResource createBlueprintEndpoint(
content = @Content(
examples = {
@ExampleObject(
name = "blueprint",
description = "description of blueprint example",
value = EXAMPLE_BLUEPRINT
name = "github-blueprint",
description = "Example of a blueprint for GitHub",
value = EXAMPLE_BLUEPRINT_GITHUB
),
@ExampleObject(
name = "azuredevops-blueprint",
description = "Example of a blueprint for Azure DevOps",
value = EXAMPLE_BLUEPRINT_AZUREDEVOPS
)
}
)
Expand Down Expand Up @@ -262,7 +285,7 @@ public abstract BlueprintResource createBlueprint(
mediaType = "application/json",
schema = @Schema(implementation = BlueprintResource.class),
examples = {
@ExampleObject(name = "blueprintUpdate", value = EXAMPLE_BLUEPRINT_UPDATE)
@ExampleObject(name = "blueprintUpdate", value = EXAMPLE_BLUEPRINT_UPDATE_GITHUB)
}
)
),
Expand Down Expand Up @@ -314,8 +337,8 @@ public BlueprintResource updateBlueprintEndpoint(
examples = {
@ExampleObject(
name = "blueprintUpdate",
description = "description of blueprint update example",
value = EXAMPLE_BLUEPRINT_UPDATE
description = "Example of object to update an existing blueprint",
value = EXAMPLE_BLUEPRINT_UPDATE_GITHUB
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ public class VelocityConfig {
public VelocityEngine velocityEngine() {
VelocityEngine velocityEngine = new VelocityEngine();
Properties velocityProperties = new Properties();
velocityProperties.setProperty("resource.loader.file.path", velocityTemplatePath);
velocityProperties.setProperty("resource.loader.file.path", velocityTemplatePath + "/projects");
velocityEngine.init(velocityProperties);
//velocityEngine.init();
return velocityEngine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void instanceBlueprint(Long blueprintId, ConfigResource configResource) {

// Clean the repository to consider only the template
logger.info("Initializing new Git target repository ...");
gitService.initTargetRepository(
gitRepo = gitService.initTargetRepository(
gitRepo,
blueprint.getBlueprintDirectory(),
configResource.getCreateRepo(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public abstract class GitService {
//TODO : timeouts on operations?

@Value("${git.templates.path}")
private String targetPath;
private String templatesPath;

private String tmpTargetRepo = "tmpTargetRepo";
private String targetPathSuffix = "/projects";

private String sourcePathSuffix = "/blueprints";

private String paramsFileJson = "params.json";

Expand Down Expand Up @@ -54,7 +56,6 @@ public GitCheckResource checkGitRepository(String repositoryUrl, String blueprin
);

if(gitCheckResource.getParamsDescriptionCheck()) {
// Remove exception from signature and refactor this method in a File utils class (used also in TemplatingService)
String paramsFileContent = CustomFileUtils.readFileAsString(new File(repoToCheckFile, paramsFileJson));
gitCheckResource.setParamsJsonFileContent(paramsFileContent);
}
Expand All @@ -72,7 +73,7 @@ public GitCheckResource checkGitRepository(String repositoryUrl, String blueprin
// ======================================================================================

public Git cloneRepo(String sourceUrl) {
return cloneRepo(sourceUrl, targetPath);
return cloneRepo(sourceUrl, templatesPath + sourcePathSuffix);
}

private Git cloneRepo(String sourceUrl, String destinationPath) {
Expand Down Expand Up @@ -102,13 +103,13 @@ public Git initTargetRepository(Git oldGitRepo, String blueprintDir, Boolean cre
File oldRepo = oldGitRepo.getRepository().getWorkTree();
if (createRepoFlag) {
newGitRepo = Git.init()
.setDirectory(new File(tmpTargetRepo))
.setDirectory(new File(templatesPath + targetPathSuffix))
.call();
// Set origin to new Repo
newGitRepo = setOrigin(newGitRepo, targetOrigin);
} else {
// Clone old repo
newGitRepo = cloneRepo(targetOrigin, tmpTargetRepo);
newGitRepo = cloneRepo(targetOrigin, templatesPath + targetPathSuffix);
// Remove all repo content
CustomFileUtils.cleanDirectoryExceptOneDir(
newGitRepo.getRepository().getWorkTree(),
Expand All @@ -119,11 +120,8 @@ public Git initTargetRepository(Git oldGitRepo, String blueprintDir, Boolean cre
File newRepoFile = newGitRepo.getRepository().getWorkTree();
File blueprintDirectoryFile = new File(oldRepo, blueprintDir);
CustomFileUtils.copyDirectory(blueprintDirectoryFile, newRepoFile);
// Remove the old repository
oldGitRepo.close(); // Close git connection
deleteLocalRepository();
// Move new repo to "targetPath" as it was the old repo
CustomFileUtils.renameFile(newRepoFile, targetPath);
// Close Git connection to old repository
oldGitRepo.close();
// Return new Repo
return newGitRepo;
} catch (Throwable t) {
Expand All @@ -140,7 +138,7 @@ public Git initTargetRepository(Git oldGitRepo, String blueprintDir, Boolean cre
// COMMIT & PUSH Repository
// ======================================================================================

public void commitAndPushRepo(Git gitRepo, String message) {
public Iterable<PushResult> commitAndPushRepo(Git gitRepo, String message) {
try {
// Remove renamed files/directories
gitRepo.add()
Expand All @@ -159,6 +157,7 @@ public void commitAndPushRepo(Git gitRepo, String message) {
Iterable<PushResult> pushResults = gitRepo.push()
.setTransportConfigCallback(getSshTransportConfigCallback())
.call();
return pushResults; // Needed for Windows to wait the end of the push command
} catch (Throwable t) {
throw new InternalServerException(
BlueprintApiStandardErrors.SC500_01_GIT_ERROR,
Expand All @@ -177,7 +176,7 @@ public void commitAndPushRepo(Git gitRepo, String message) {
// ======================================================================================

public void deleteLocalRepository() {
CustomFileUtils.removeDirectory(new File(targetPath));
CustomFileUtils.removeDirectory(new File(templatesPath));
}


Expand Down

0 comments on commit 7412433

Please sign in to comment.