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 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void verifyFailsForProjectWithoutGit() throws IOException, InterruptedException
final Path projectDirRealPath = this.projectDir.toRealPath();
assertProcessFails("verify", "E-PK-CORE-90: Could not find .git directory in project-root '"
+ projectDirRealPath
+ "'. Known mitigations:\n* Run 'git init'.\n* Make sure that you run project-keeper only in the root directory of the git-repository. If you have multiple projects in that directory, define them in the '.project-keeper.yml'.");
+ "'. Known mitigations:\n* Run 'git init'.\n* Make sure that you run project-keeper only in the root directory of the git-repository. If you have multiple projects in that directory, define them in file '.project-keeper.yml'.");
}

@Test
Expand Down
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: 203
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 All @@ -23,7 +29,7 @@ public class ProjectKeeperConfigReader {
public static final String CONFIG_FILE_NAME = ".project-keeper.yml";
private static final String USER_GUIDE_URL = "https://github.com/exasol/project-keeper-maven-plugin";
private static final String CHECK_THE_USER_GUIDE = "Please check the user-guide " + USER_GUIDE_URL + ".";
private static final String INVALID_CONFIG_FILE = "Invalid " + CONFIG_FILE_NAME + ".";
private static final String INVALID_CONFIG_FILE = "Invalid file " + CONFIG_FILE_NAME + ".";

/**
* Read a {@link ProjectKeeperConfig} from file.
Expand All @@ -40,7 +46,7 @@ public ProjectKeeperConfig readConfig(final Path projectDirectory) {
return parseRawConfig(rawConfig, projectDirectory);
} catch (final IOException exception) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-82")
.message("Failed to read '" + CONFIG_FILE_NAME + "'.").toString(), exception);
.message("Failed to read file {{config file}}.", CONFIG_FILE_NAME).toString(), exception);
}
}

Expand All @@ -50,7 +56,7 @@ private void verifyWeReInProjectRoot(final Path projectDirectory) {
.message("Could not find .git directory in project-root {{root path}}.", projectDirectory)
.mitigation("Run 'git init'.")
.mitigation(
"Make sure that you run project-keeper only in the root directory of the git-repository. If you have multiple projects in that directory, define them in the '.project-keeper.yml'.")
"Make sure that you run project-keeper only in the root directory of the git-repository. If you have multiple projects in that directory, define them in file '.project-keeper.yml'.")
.toString());
}
}
Expand All @@ -68,8 +74,8 @@ private ProjectKeeperRawConfig readRawConfig(final FileReader fileReader, final
private void validateConfigFileExists(final Path configFile) {
if (!Files.exists(configFile)) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-89")
.message("Could not find '" + CONFIG_FILE_NAME + "'.")
.mitigation("Please create this configuration according to the user-guide " + USER_GUIDE_URL + ".")
.message("Could not find file {{config file}}.", CONFIG_FILE_NAME)
.mitigation("Please create this file according to the user-guide " + USER_GUIDE_URL + ".")
.toString());//
}
}
Expand Down Expand Up @@ -117,6 +123,70 @@ private BuildOptions convertBuildOptions(final Build build) {
.runnerOs(build.getRunnerOs()) //
.freeDiskSpace(build.shouldFreeDiskSpace()) //
.exasolDbVersions(build.getExasolDbVersions()) //
.workflows(convertWorkflows(build.workflows)) //
.build();
}

private List<CustomWorkflow> 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 CustomWorkflow 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 file {{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 file {{config file name}}.",
workflow.name, CONFIG_FILE_NAME, supportedWorkflowNames)
.mitigation("Please only use one of the supported workflows from {{supported workflow names}}",
supportedWorkflowNames)
.toString());
}
if (workflow.stepCustomizations == null || workflow.stepCustomizations.isEmpty()) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-203")
.message("Missing customized steps for workflow {{workflow name}} in file {{config file name}}.",
workflow.name, CONFIG_FILE_NAME)
.mitigation("Add at least one step or remove the workflow.").toString());
}
return CustomWorkflow.builder() //
.workflowName(workflow.name) //
.steps(convertSteps(workflow.stepCustomizations)) //
.build();
}

private List<StepCustomization> convertSteps(final List<RawStepCustomization> stepCustomizations) {
kaklakariada marked this conversation as resolved.
Show resolved Hide resolved
return stepCustomizations.stream().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 file {{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 file {{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 file {{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 Expand Up @@ -214,7 +284,7 @@ private Path convertPath(final Path projectDir, final String rawPath) {
private void requireProperty(final Object propertyValue, final String propertyName) {
if (propertyValue == null) {
throw new IllegalArgumentException(ExaError.messageBuilder("E-PK-CORE-86")
.message(INVALID_CONFIG_FILE + " Missing required property {{property name}}.", propertyName)
.message(INVALID_CONFIG_FILE + " Required property {{property name}} is missing.", propertyName)
.toString());
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private Optional<SimpleValidationFinding> validateTagContent(final Document docu
if (node == null) {
return Optional.of(SimpleValidationFinding
.withMessage(ExaError.messageBuilder("E-PK-CORE-123")
.message("Invalid pom file {{file}}: Missing required property {{xpath}}.",
.message("Invalid pom file {{file}}: Required property {{xpath}} is missing.",
this.projectDirectory.relativize(this.pomFilePath), parentXPath + "/" + tagName)
.mitigation("The expected value is {{expected value}}.", expectedValue).toString())
.andFix(log -> addTextElement(parent, tagName, expectedValue)).build());
Expand Down Expand Up @@ -168,7 +168,7 @@ private Optional<SimpleValidationFinding> validateGroupId(final String groupId)
private Optional<ValidationFinding> validationDescriptionExists(final Document document) {
if (runXPath(document, XPath.DESCRIPTION) == null) {
return Optional.of(SimpleValidationFinding.withMessage(ExaError.messageBuilder("E-PK-CORE-120")
.message("Invalid pom file {{file}}: Missing required property {{property|u}}.",
.message("Invalid pom file {{file}}: Required property {{property|u}} is missing.",
this.projectDirectory.relativize(this.pomFilePath), XPath.DESCRIPTION)
.mitigation("Please manually add a description.").toString()).build());
} else {
Expand Down Expand Up @@ -219,7 +219,7 @@ private List<ValidationFinding> validateAssemblyPlugin(final Node pom, final Pat
final Node finalNameProperty = runXPath(pom, XPath.FINAL_NAME);
if ((finalNameProperty == null) || finalNameProperty.getTextContent().isBlank()) {
return List.of(SimpleValidationFinding.withMessage(ExaError.messageBuilder("E-PK-CORE-105").message(
"Invalid pom file {{file}}: Missing required property finalName property in maven-assembly-plugin.",
"Invalid pom file {{file}}: Required property 'finalName' is missing in maven-assembly-plugin.",
relativePomPath).mitigation("""
Use the following template and set finalName:
<plugin>
Expand Down Expand Up @@ -359,7 +359,7 @@ private String getGroupId(final Document pom) throws InvalidPomException {
return parentGroupIdNode.getTextContent();
} else {
throw new InvalidPomException(ExaError.messageBuilder("E-PK-CORE-102")
.message("Invalid pom file {{file}}: Missing required property 'groupId'.",
.message("Invalid pom file {{file}}: Required property 'groupId' is missing.",
this.projectDirectory.relativize(this.pomFilePath))
.mitigation("Please either set {{groupId|u}} or {{parent groupId|u}}.", XPath.GROUP_ID,
XPath.PARENT_GROUP_ID)
Expand All @@ -372,7 +372,7 @@ private String getRequiredTextValue(final Node pom, final String xPath) throws I
final Node node = XPathErrorHandlingWrapper.runXPath(pom, xPath);
if (node == null) {
throw new InvalidPomException(ExaError.messageBuilder("E-PK-CORE-101")
.message("Invalid pom file {{file}}: Missing required property {{property|u}}.",
.message("Invalid pom file {{file}}: Required property {{property|u}} is missing.",
this.projectDirectory.relativize(this.pomFilePath), xPath)
.mitigation("Please set the property manually.").toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void testVerifyPhase1() throws IOException {
() -> assertThat(output,
containsString("E-PK-CORE-17: Missing required file: 'pk_generated_parent.pom'")),
() -> assertThat(output, containsString(
"E-PK-CORE-105: Invalid pom file 'pom.xml': Missing required property finalName property in maven-assembly-plugin.")));
"E-PK-CORE-105: Invalid pom file 'pom.xml': Required property 'finalName' is missing in maven-assembly-plugin.")));
}

@Test
Expand Down
Loading
Loading