-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8aae710
commit 2b75c6d
Showing
17 changed files
with
507 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
project-keeper/src/main/java/com/exasol/projectkeeper/validators/files/GitHubWorkflow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package com.exasol.projectkeeper.validators.files; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
import java.util.stream.IntStream; | ||
|
||
class GitHubWorkflow { | ||
|
||
private final Map<String, Object> rawWorkflow; | ||
|
||
GitHubWorkflow(final Map<String, Object> rawWorkflow) { | ||
this.rawWorkflow = rawWorkflow; | ||
} | ||
|
||
static GitHubWorkflow create(final Object rawWorkflow) { | ||
return new GitHubWorkflow(asMap(rawWorkflow)); | ||
} | ||
|
||
Job getJob(final String jobId) { | ||
final Map<String, Object> jobs = asMap(rawWorkflow.get("jobs")); | ||
return new Job(asMap(jobs.get(jobId))); | ||
} | ||
|
||
Map<String, Object> getOnTrigger() { | ||
return asMap(rawWorkflow.get("on")); | ||
} | ||
|
||
Object getRawObject() { | ||
return this.rawWorkflow; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static Map<String, Object> asMap(final Object rawYaml) { | ||
return (Map<String, Object>) rawYaml; | ||
} | ||
|
||
static class Job { | ||
private final Map<String, Object> rawJob; | ||
|
||
Job(final Map<String, Object> rawJob) { | ||
this.rawJob = rawJob; | ||
} | ||
|
||
Step getStep(final String id) { | ||
return getSteps().stream() // | ||
.filter(hasId(id)) // | ||
.findFirst() // | ||
.orElseThrow( | ||
() -> new IllegalStateException("Job has no step with ID '" + id + "': " + getRawSteps())); | ||
} | ||
|
||
List<Step> getSteps() { | ||
return getRawSteps().stream().map(Step::new).toList(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private List<Map<String, Object>> getRawSteps() { | ||
final Object steps = rawJob.get("steps"); | ||
if (steps == null) { | ||
return emptyList(); | ||
} | ||
return (List<Map<String, Object>>) steps; | ||
} | ||
|
||
String getRunnerOS() { | ||
return (String) rawJob.get("runs-on"); | ||
} | ||
|
||
Map<String, Object> getConcurrency() { | ||
return asMap(rawJob.get("concurrency")); | ||
} | ||
|
||
Map<String, Object> getStrategy() { | ||
return asMap(rawJob.get("strategy")); | ||
} | ||
|
||
Map<String, Object> getEnv() { | ||
return asMap(rawJob.get("env")); | ||
} | ||
|
||
private Predicate<? super Step> hasId(final String id) { | ||
return step -> step.getId().isPresent() && step.getId().get().equals(id); | ||
} | ||
|
||
public void replaceStep(final String stepId, final Map<String, Object> rawStep) { | ||
final List<Map<String, Object>> allSteps = getRawSteps(); | ||
final int index = findStepIndex(stepId); | ||
allSteps.set(index, rawStep); | ||
} | ||
|
||
private int findStepIndex(final String stepId) { | ||
final List<Step> steps = getSteps(); | ||
return IntStream.range(0, steps.size()) // | ||
.filter(index -> steps.get(index).getId().get().equals(stepId)) // | ||
.findFirst() // | ||
.orElseThrow(() -> new IllegalStateException("No step found for id '" + stepId + "' in " + rawJob)); | ||
} | ||
} | ||
|
||
static class Step { | ||
private final Map<String, Object> rawStep; | ||
|
||
Step(final Map<String, Object> rawStep) { | ||
this.rawStep = rawStep; | ||
} | ||
|
||
Optional<String> getId() { | ||
return getOptionalString("id"); | ||
} | ||
|
||
Optional<String> getOptionalString(final String key) { | ||
return Optional.ofNullable((String) rawStep.get(key)); | ||
} | ||
|
||
String getString(final String key) { | ||
return getOptionalString(key) | ||
.orElseThrow(() -> new IllegalStateException("Step has no field '" + key + "': " + rawStep)); | ||
} | ||
|
||
String getName() { | ||
return getString("name"); | ||
} | ||
|
||
String getRunCommand() { | ||
return getString("run"); | ||
} | ||
|
||
String getIfCondition() { | ||
return getString("if"); | ||
} | ||
} | ||
} |
55 changes: 16 additions & 39 deletions
55
...src/main/java/com/exasol/projectkeeper/validators/files/GitHubWorkflowStepCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,37 @@ | ||
package com.exasol.projectkeeper.validators.files; | ||
|
||
import org.yaml.snakeyaml.*; | ||
import org.yaml.snakeyaml.DumperOptions.LineBreak; | ||
import org.yaml.snakeyaml.DumperOptions.NonPrintableStyle; | ||
import org.yaml.snakeyaml.constructor.Constructor; | ||
import org.yaml.snakeyaml.representer.Representer; | ||
import java.util.Optional; | ||
|
||
import com.exasol.projectkeeper.shared.config.BuildOptions; | ||
import com.exasol.projectkeeper.shared.config.WorkflowStep; | ||
|
||
class GitHubWorkflowStepCustomizer implements ContentCustomizingTemplate.ContentCustomizer { | ||
|
||
private final BuildOptions buildOptions; | ||
private final Yaml yaml; | ||
private final YamlIO yaml; | ||
private final String jobId; | ||
|
||
GitHubWorkflowStepCustomizer(final BuildOptions buildOptions) { | ||
this(configureYaml(), buildOptions); | ||
GitHubWorkflowStepCustomizer(final BuildOptions buildOptions, final String jobId) { | ||
this(YamlIO.create(), buildOptions, jobId); | ||
} | ||
|
||
GitHubWorkflowStepCustomizer(final Yaml yaml, final BuildOptions buildOptions) { | ||
GitHubWorkflowStepCustomizer(final YamlIO yaml, final BuildOptions buildOptions, final String jobId) { | ||
this.yaml = yaml; | ||
this.buildOptions = buildOptions; | ||
this.jobId = jobId; | ||
} | ||
|
||
@Override | ||
public String customizeContent(final String content) { | ||
final Object object = yaml.load(content); | ||
return yaml.dump(customizeWorkflow(object)); | ||
final GitHubWorkflow workflow = yaml.loadGitHubWorkflow(content); | ||
customizeWorkflow(workflow); | ||
return yaml.dumpWorkflow(workflow); | ||
} | ||
|
||
private Object customizeWorkflow(final Object object) { | ||
return object; | ||
} | ||
|
||
private static Yaml configureYaml() { | ||
final DumperOptions dumperOptions = new DumperOptions(); | ||
dumperOptions.setIndent(2); | ||
dumperOptions.setCanonical(false); | ||
dumperOptions.setExplicitEnd(false); | ||
dumperOptions.setExplicitStart(false); | ||
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO); // Remove quotes | ||
dumperOptions.setPrettyFlow(true); // Remove curly brackets | ||
dumperOptions.setLineBreak(LineBreak.UNIX); | ||
dumperOptions.setSplitLines(false); // Remove line breaks | ||
dumperOptions.setWidth(400); | ||
dumperOptions.setProcessComments(true); | ||
dumperOptions.setAllowUnicode(true); | ||
dumperOptions.setIndentWithIndicator(false); | ||
dumperOptions.setIndicatorIndent(2); | ||
dumperOptions.setMaxSimpleKeyLength(128); | ||
dumperOptions.setNonPrintableStyle(NonPrintableStyle.ESCAPE); | ||
|
||
final LoaderOptions loaderConfig = new LoaderOptions(); | ||
loaderConfig.setProcessComments(true); | ||
loaderConfig.setAllowDuplicateKeys(false); | ||
loaderConfig.setAllowRecursiveKeys(false); | ||
loaderConfig.setWrappedToRootException(true); | ||
return new Yaml(new Constructor(loaderConfig), new Representer(dumperOptions)); | ||
private void customizeWorkflow(final GitHubWorkflow workflow) { | ||
final Optional<WorkflowStep> customStep = buildOptions.getBuildStep(); | ||
if (customStep.isPresent()) { | ||
workflow.getJob(jobId).replaceStep("build-pk-verify", customStep.get().getRawStep()); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
project-keeper/src/main/java/com/exasol/projectkeeper/validators/files/YamlIO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.exasol.projectkeeper.validators.files; | ||
|
||
import org.snakeyaml.engine.v2.api.*; | ||
import org.snakeyaml.engine.v2.common.*; | ||
|
||
class YamlIO { | ||
|
||
private final Load loader; | ||
private final Dump dumper; | ||
|
||
private YamlIO(final Load loader, final Dump dumper) { | ||
this.loader = loader; | ||
this.dumper = dumper; | ||
} | ||
|
||
static YamlIO create() { | ||
final LoadSettings loadSettings = LoadSettings.builder().setParseComments(true).build(); | ||
final DumpSettings dumpSettings = DumpSettings.builder().setBestLineBreak("\n").setCanonical(false) | ||
.setDefaultFlowStyle(FlowStyle.AUTO).setDefaultScalarStyle(ScalarStyle.PLAIN).setDumpComments(true) | ||
.setExplicitEnd(false).setExplicitStart(false) // | ||
.setIndent(2).setIndentWithIndicator(true).setIndicatorIndent(2) // | ||
.setWidth(200) // | ||
.setMultiLineFlow(true).setNonPrintableStyle(NonPrintableStyle.ESCAPE).setUseUnicodeEncoding(true) | ||
.setSplitLines(false).build(); | ||
return new YamlIO(new Load(loadSettings), new Dump(dumpSettings)); | ||
} | ||
|
||
GitHubWorkflow loadGitHubWorkflow(final String content) { | ||
return GitHubWorkflow.create(load(content)); | ||
} | ||
|
||
Object load(final String content) { | ||
return this.loader.loadFromString(content); | ||
} | ||
|
||
String dumpWorkflow(final GitHubWorkflow workflow) { | ||
return dump(workflow.getRawObject()); | ||
} | ||
|
||
String dump(final Object object) { | ||
return this.dumper.dumpToString(object); | ||
} | ||
} |
Oops, something went wrong.