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

#519: Add configuration for step customization #563

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion project-keeper/error_code_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error-tags:
PK-CORE:
packages:
- com.exasol.projectkeeper
highest-index: 197
highest-index: 202
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.exasol.projectkeeper.config;

import static java.util.Arrays.asList;

import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;

import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.config.ProjectKeeperRawConfig.Build;
import com.exasol.projectkeeper.config.ProjectKeeperRawConfig.*;
import com.exasol.projectkeeper.shared.config.*;
import com.exasol.projectkeeper.shared.config.ParentPomRef;
import com.exasol.projectkeeper.shared.config.Source;
import com.exasol.projectkeeper.shared.config.workflow.*;

/**
* This class reads {@link ProjectKeeperConfig} from file.
Expand Down Expand Up @@ -117,6 +123,68 @@ private BuildOptions convertBuildOptions(final Build build) {
.runnerOs(build.getRunnerOs()) //
.freeDiskSpace(build.shouldFreeDiskSpace()) //
.exasolDbVersions(build.getExasolDbVersions()) //
.workflows(convertWorkflows(build.workflows)) //
.build();
}

private List<WorkflowOptions> convertWorkflows(final List<Workflow> workflows) {
return Optional.ofNullable(workflows).map(List::stream) //
ckunki marked this conversation as resolved.
Show resolved Hide resolved
.orElseGet(Stream::empty) //
.map(this::convertWorkflow) //
.toList();
}

private WorkflowOptions convertWorkflow(final Workflow workflow) {
final List<String> supportedWorkflowNames = List.of("ci-build.yml");
if (workflow.name == null) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-199")
.message("Missing workflow name in {{config file name}}.", CONFIG_FILE_NAME)
.mitigation("Add a workflow name to the workflow configuration.").toString());
}
if (!supportedWorkflowNames.contains(workflow.name)) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-198")
.message("Unsupported workflow name {{workflow name}} found in {{config file name}}.",
workflow.name, CONFIG_FILE_NAME, supportedWorkflowNames)
.mitigation("Use one of the supported workflow {{supported workflow names}}",
supportedWorkflowNames)
.toString());
}
return WorkflowOptions.builder() //
.workflowName(workflow.name) //
.customizations(convertSteps(workflow.stepCustomizations)) //
.build();
}

private List<StepCustomization> convertSteps(final List<RawStepCustomization> stepCustomizations) {
kaklakariada marked this conversation as resolved.
Show resolved Hide resolved
return Optional.ofNullable(stepCustomizations) //
.map(List::stream) //
.orElseGet(Stream::empty) //
.map(this::convertStep) //
.toList();
}

private StepCustomization convertStep(final RawStepCustomization step) {
if (step.action == null) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-200")
.message("Missing action in step customization of {{config file}}.", CONFIG_FILE_NAME)
.mitigation("Add action with one of values {{available actions}}.",
asList(StepCustomization.Type.values()))
.toString());
}
if (step.stepId == null || step.stepId.isBlank()) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-201")
.message("Missing stepId in step customization of {{config file}}.", CONFIG_FILE_NAME)
.mitigation("Add stepId to the step customization.").toString());
}
if (step.content == null || step.content.isEmpty()) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-202")
.message("Missing content in step customization of {{config file}}.", CONFIG_FILE_NAME)
.mitigation("Add content to the step customization.").toString());
}
return StepCustomization.builder() //
.type(step.action) //
.stepId(step.stepId) //
.step(WorkflowStep.createStep(step.content)) //
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Map;

import com.exasol.projectkeeper.shared.config.workflow.StepCustomization;

/**
* Intermediate class for reading the config. This is used by {@link ProjectKeeperConfigReader}.
* <p>
Expand Down Expand Up @@ -337,10 +339,13 @@ public void setRelativePath(final String relativePath) {
* <p>
* SnakeYML requires this class to be public.
*/
@SuppressWarnings("java:S1104") // Only used for serialization, getter/setters not needed
public static class Build {
private String runnerOs;
private boolean freeDiskSpace = false;
private List<String> exasolDbVersions = emptyList();
/** Build workflow customizations allow adding and replacing steps in the default build workflow */
public List<Workflow> workflows = emptyList();

/**
* Get CI build runner operating system, e.g. {@code ubuntu-20.04}.
Expand Down Expand Up @@ -380,4 +385,32 @@ public void setExasolDbVersions(final List<String> exasolDbVersions) {
this.exasolDbVersions = exasolDbVersions;
}
}

/**
* Intermediate class for de-serializing workflow customizations from PK's YAML configuration file.
* <p>
* SnakeYML requires this class to be public.
*/
@SuppressWarnings("java:S1104") // Only used for serialization, getter/setters not needed
public static class Workflow {
/** Workflow name, e.g. {@code ci-build.yml} or {@code release.yml}. */
public String name;
/** List of customizations for the workflow. */
public List<RawStepCustomization> stepCustomizations;
}

/**
* Intermediate class for de-serializing workflow customizations from PK's YAML configuration file.
* <p>
* SnakeYML requires this class to be public.
*/
@SuppressWarnings("java:S1104") // Only used for serialization, getter/setters not needed
public static class RawStepCustomization {
/** Customization type (insert/replace). */
public StepCustomization.Type action;
/** ID of the step to replace or after which to insert. */
public String stepId;
/** The step content to insert/replace. */
public Map<String, Object> content;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.exasol.projectkeeper.github;

import java.util.logging.Logger;

/**
* This {@link WorkflowOutput} is used by {@link OutputPublisherFactory} when environment variable {@code GITHUB_OUTPUT}
* is not present. This class just logs published key/value pairs and does not actually publish them.
*/
class NullContentProvider implements WorkflowOutput {
private static final Logger LOG = Logger.getLogger(NullContentProvider.class.getName());

@Override
public void publish(final String key, final String value) {
LOG.finest(() -> "Publishing key/value pair '" + key + "' = '" + value + "'");
// Ignore
}

@Override
Expand Down
Loading
Loading