Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/azure pipeline #49

Merged
merged 6 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ gradle generateDrivenAdapter --value=[referenceNumberDrivenAdapter]
gradle gda --value [referenceNumberDrivenAdapter]
```

| Reference number driven adapter | Name |
| Reference driven adapter value | Name |
| ------------------ | ------------ |
| 1|JPA Repository |
| 2|Mongo Repository |
Expand All @@ -65,7 +65,7 @@ gradle gda --value [referenceNumberDrivenAdapter]
gradle generateEntryPoint --value=referenceNumberEntryPoint
gradle gep --value referenceNumberEntryPoint
```
| Reference number entry point | Name |
| Reference entry point value | Name |
| ------------------ | ------------ |
| 1|API REST (Spring Boot Starter Web) |
| 2|API REST (Spring Boot Starter WebFlux) |
Expand All @@ -78,6 +78,14 @@ gradle validateStructure
gradle vs
```

7 The ```generatePipeline | gpl``` task will generate CI pipeline inside the folder "./deployment/", this task have one required parameter ```value```.
```sh
gradle generatePipeline --value=referenceNumberPipeline
gradle gpl --value referenceNumberPipeline
````
| Reference pipeline value | Name |
| ------------------ | ------------ |
| 1|Azure DevOps |


How I can help?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ public void canRunTaskGenerateDrivenAdapterEventBusTest() {
assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

@Test
public void canRunTaskGeneratePipelineAzureDevOpsTest() {
canRunTaskGenerateStructureWithOutParameters();
String task = "generatePipeline";
String valuePipeline = "1";

runner.withArguments(task, "--value=" + valuePipeline);
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

assertTrue(new File("build/functionalTest/deployment/cleanArchitecture_Build.yaml").exists());

assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}

@Test
public void canRunTaskvalidateStructureWithOutParameters() {
canRunTaskGenerateStructureWithOutParameters();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/co/com/bancolombia/PluginClean.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private List<TaskModel> initTasks() {
.description("Validate that project references are not violated").group(taskGroup)
.taskAction(ValidateStructureTask.class).build());

tasksModels.add(TaskModel.builder().name("generatePipeline").shortcut("gpl")
.description("Generate CI pipeline as a code in deployment layer").group(taskGroup)
.taskAction(GeneratePipelineTask.class).build());

return tasksModels;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package co.com.bancolombia.factory;
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.drivenadapters.AsyncEventBusDrivenAdapter;
import co.com.bancolombia.models.drivenadapters.JPADrivenAdapter;
import co.com.bancolombia.models.drivenadapters.MongoDrivenAdapter;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.models.adapters.drivenadapters.AsyncEventBusDrivenAdapter;
import co.com.bancolombia.models.adapters.drivenadapters.JPADrivenAdapter;
import co.com.bancolombia.models.adapters.drivenadapters.MongoDrivenAdapter;
import co.com.bancolombia.templates.DrivenAdapterTemplate;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package co.com.bancolombia.factory;
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.entrypoints.ApiReactive;
import co.com.bancolombia.models.entrypoints.ApiRest;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.models.adapters.entrypoints.ApiReactive;
import co.com.bancolombia.models.adapters.entrypoints.ApiRest;
import co.com.bancolombia.templates.EntryPointTemplate;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package co.com.bancolombia.factory;
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.adapters.AbstractModule;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.com.bancolombia.factory.pipelines;


import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.models.pipelines.AbstractModule;

import java.io.IOException;

public interface ModuleFactory {
AbstractModule makePipeline(int pipelineCode) throws IOException, CleanException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package co.com.bancolombia.factory.pipelines;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.models.pipelines.AbstractModule;
import co.com.bancolombia.models.pipelines.AzureDevOpsPipeline;
import co.com.bancolombia.templates.PipelineTemplate;

import java.io.IOException;

import static co.com.bancolombia.templates.PipelineTemplate.Pipelines.NO_AVAILABLE;

public class PipelineFactory implements ModuleFactory {

@Override
public AbstractModule makePipeline(int pipelineCode) throws IOException, CleanException {
AbstractModule pipeline = null;
switch (getFactory(pipelineCode)) {
case AZURE_DEVOPS:
pipeline = new AzureDevOpsPipeline(pipelineCode);
break;
default:
throw new CleanException("Pipeline value invalid");
}
return pipeline;
}

private PipelineTemplate.Pipelines getFactory(int pipelineCode) {

return PipelineTemplate.Pipelines.valueOf(pipelineCode, () -> NO_AVAILABLE);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.com.bancolombia.models;
package co.com.bancolombia.models.adapters;

import co.com.bancolombia.Utils;
import co.com.bancolombia.templates.Constants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package co.com.bancolombia.models.drivenadapters;
package co.com.bancolombia.models.adapters.drivenadapters;

import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.DrivenAdapterTemplate;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.com.bancolombia.models.drivenadapters;
package co.com.bancolombia.models.adapters.drivenadapters;

import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.templates.DrivenAdapterTemplate;
import co.com.bancolombia.templates.HelperTemplate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.com.bancolombia.models.drivenadapters;
package co.com.bancolombia.models.adapters.drivenadapters;

import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.templates.DrivenAdapterTemplate;
import co.com.bancolombia.templates.HelperTemplate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.com.bancolombia.models.entrypoints;
package co.com.bancolombia.models.adapters.entrypoints;

import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.templates.EntryPointTemplate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.com.bancolombia.models.entrypoints;
package co.com.bancolombia.models.adapters.entrypoints;

import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.templates.EntryPointTemplate;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package co.com.bancolombia.models.pipelines;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
public abstract class AbstractModule {

private int code;
private String projectName;
private List<String> modulesPaths;

public AbstractModule(int code) {
this.code = code;
}

protected String getProjectName(String fileLine) {
return fileLine.replace("rootProject.name = ", "")
.replace("'", "");
}

protected List<String> getModulesNames(List<String> settingsGradleFileLines) {
List<String> modulesNames = new ArrayList<>();
settingsGradleFileLines.stream()
.filter(x -> x.contains("include"))
.forEach(x -> {
String path = x.trim();
path = path.replace("include \":", "");
path = path.replace("\"", "");
modulesNames.add(path);
});
return modulesNames;
}

protected String getProjectPath(List<String> settingsGradleFileLines, String moduleName) {
Optional<String> modulePaths = settingsGradleFileLines.stream()
.filter(x -> x.contains("project(':" + moduleName + "').projectDir"))
.findFirst();

if (modulePaths.isPresent()) {
return modulePaths.get()
.replace("project(':" + moduleName + "').projectDir = file('.", "")
.replace("')", "");
} else {
return "";
}
}

protected List<String> getSettingsAsList(String settings) {
return Stream.of(settings.split("\n"))
.map(elem -> new String(elem))
.filter(elem -> !elem.isEmpty())
.parallel()
.collect(Collectors.toList());
}

public void setPropertiesToPipeline(String settings) {
List<String> settingsList = this.getSettingsAsList(settings);
this.setProjectName(this.getProjectName(settingsList.get(0)));
modulesPaths = new ArrayList<>();
for (String name : getModulesNames(settingsList)) {
modulesPaths.add(getProjectPath(settingsList, name));
}
}

public abstract String getPipelineContent(String settings);

private void setProjectName(String projectName) {
this.projectName = projectName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package co.com.bancolombia.models.pipelines;

import co.com.bancolombia.templates.deployment.AzureDevOpsPipelineTemplate;

import java.util.stream.Collectors;

public class AzureDevOpsPipeline extends AbstractModule {

public AzureDevOpsPipeline(int code) {
super(code);
}

@Override
public String getPipelineContent(String settings) {
super.setPropertiesToPipeline(settings);
String pipelineContent = AzureDevOpsPipelineTemplate.generateAzureDevOpsPipelineTemplateContent();

String binaries = super.getModulesPaths().stream()
.map(x -> new String().concat(AzureDevOpsPipelineTemplate.BUILD_SOURCE_DIRECTORY)
.concat(x)
.concat(AzureDevOpsPipelineTemplate.BUILD_BINARIES_PATH))
.collect(Collectors.joining(","));

String test = super.getModulesPaths().stream()
.filter(x -> !x.contains("app-service"))
.map(x-> new String().concat(AzureDevOpsPipelineTemplate.BUILD_SOURCE_DIRECTORY
.concat(x)
.concat(AzureDevOpsPipelineTemplate.BUILD_TEST_PATH)))
.collect(Collectors.joining(","));

return pipelineContent.replace("[ProjectName]", super.getProjectName())
.replace("[Binaries]", binaries)
.replace("[Test]", test);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import co.com.bancolombia.templates.*;
import co.com.bancolombia.Utils;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.DrivenAdapterFactory;
import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.factory.adapters.ModuleFactory;
import co.com.bancolombia.factory.adapters.DrivenAdapterFactory;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.config.JpaConfigTemplate;
import co.com.bancolombia.templates.properties.PropertiesTemplate;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.Utils;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.EntryPointFactory;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.models.AbstractModule;
import co.com.bancolombia.factory.adapters.EntryPointFactory;
import co.com.bancolombia.factory.adapters.ModuleFactory;
import co.com.bancolombia.models.adapters.AbstractModule;
import co.com.bancolombia.templates.PluginTemplate;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/co/com/bancolombia/task/GeneratePipelineTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package co.com.bancolombia.task;

import co.com.bancolombia.Utils;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.pipelines.ModuleFactory;
import co.com.bancolombia.factory.pipelines.PipelineFactory;
import co.com.bancolombia.models.pipelines.AbstractModule;
import co.com.bancolombia.templates.Constants;
import co.com.bancolombia.templates.ScaffoldTemplate;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GeneratePipelineTask extends DefaultTask {

private int pipelineValue = -1;
private Logger logger = getProject().getLogger();
private ModuleFactory pipelineFactory = new PipelineFactory();
private AbstractModule pipeline;

@Option(option = "value", description = "Set the value to generate the pipeline")
public void setPipelineValueProject(String pipelineValue) {
this.pipelineValue = Utils.tryParse(pipelineValue);
}

@TaskAction
public void generatePipelineTask() throws IOException, CleanException {
if (pipelineValue < 0) {
throw new IllegalArgumentException("No pipeline value was set, usage: gradle generatePipeline --value 1");
}

pipeline = pipelineFactory.makePipeline(pipelineValue);
logger.lifecycle("Clean Architecture plugin version: {}", Utils.getVersionPlugin());
logger.lifecycle("Value set: {}", pipelineValue);
writeFiles();
}

private void writeFiles() throws IOException {
String settings = Utils.readFile(getProject(), Constants.SETTINGS_GRADLE).collect(Collectors.joining("\n"));
String content = pipeline.getPipelineContent(settings);
Utils.writeString(getProject(), ScaffoldTemplate.DEPLOYMENT.concat("/").concat(pipeline.getProjectName().concat("_Build").concat(".yaml")), content);
}

}
Loading