Skip to content

Commit

Permalink
#553: Fix unwanted large diff in pom.xml (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada authored Mar 20, 2024
1 parent f1f164d commit 283f550
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 117 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.generate.finalModifiers": "explicit",
Expand All @@ -11,7 +12,7 @@
"java.test.config": {
"vmArgs": [
"-Djava.util.logging.config.file=src/test/resources/logging.properties",
"-Dcom.exasol.projectkeeper.ownVersion=4.0.0"
"-Dcom.exasol.projectkeeper.ownVersion=4.2.0"
]
},
"sonarlint.connectedMode.project": {
Expand Down
1 change: 1 addition & 0 deletions doc/changes/changes_4.2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Code name:
* #545: Fix parsing of Go version numbers with a `v` prefix
* #548: Skip release build when preconditions are not fulfilled
* #540: Improved speed of validating mentioned issues in changes file
* #553: Reduced diff in `pom.xml` for mode `update-dependencies`

## Dependency Updates

Expand Down
7 changes: 4 additions & 3 deletions doc/requirements/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,14 @@ Covers:
Needs: dsn

#### Incrementing the Project Version
`dsn~dependency-updater.increment-version~1`
`dsn~dependency-updater.increment-version~2`

PK increments the project's patch version. PK does not modify the version if the current version was not yet released (i.e. there is not release in the latest changelog file).
PK increments the project's patch version. PK does not modify the version if the current version was not yet released (i.e. there is not release in the latest changelog file). If PK fails to increment the version it will log a warning and continue with the process.

Rationale:

Leaving the version unchanged when it was not yet released avoids surprises when running this locally.
* Leaving the version unchanged when it was not yet released avoids surprises when running this locally.
* PK could fail to increment the version in multi-module projects, because it's not located in the root `pom.xml`. Instead of failing, PK should continue with the update. It's more helpful if the user can manually increment the version in an existing pull request than to do the complete process themselves.

Covers:
* [`dsn~update-dependencies-mode~1`](#update-dependencies-mode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,20 @@ void testVerifyRelease() throws VerificationException, IOException {
containsString("E-PK-CORE-182: Release date '" + thisYear + "-??-??' has invalid format in "));
}

// [itest->dsn~dependency-updater.increment-version~1]
// [itest->dsn~dependency-updater.increment-version~2]
// [itest->dsn~dependency-updater.update-dependencies~1]
// [itest->dsn~dependency-updater.read-vulnerability-info~1]
// [itest->dsn~dependency-updater.update-changelog~1]
@Test
@DisabledOnOs(OS.WINDOWS) // Passing vulnerability JSONL via system property fails on Windows
@DisabledOnOs(OS.WINDOWS) // Passing multi-line vulnerability JSONL via system property fails on Windows
void testUpgradeDependencies() throws VerificationException, IOException {
writeProjectKeeperConfig("sources:\n" + //
" - type: maven\n" + //
" path: pom.xml\n" + //
" modules:\n" + //
" - jar_artifact\n");
writeProjectKeeperConfig("""
sources:
- type: maven
path: pom.xml
modules:
- jar_artifact
""");
final Path userGuidePath = projectDir.resolve("user_guide.md");
writeFile(userGuidePath, "artifact reference: dummy-0.1.0.jar");

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: 192
highest-index: 196
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static DependencyUpdater create(final ProjectKeeper projectKeeper, final
*
* @return {@code true} if the process succeeded.
*/
// [impl->dsn~dependency-updater.increment-version~1]
// [impl->dsn~dependency-updater.increment-version~2]
// [impl->dsn~dependency-updater.update-changelog~1]
public boolean updateDependencies() {
final String version = incrementProjectVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
import java.util.Objects;
import java.util.Optional;

import org.apache.maven.model.Model;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.Logger;
import com.exasol.projectkeeper.shared.config.ProjectKeeperConfig;
import com.exasol.projectkeeper.sources.analyze.generic.*;
import com.exasol.projectkeeper.validators.changesfile.ChangesFile;
import com.exasol.projectkeeper.validators.changesfile.ChangesFileIO;
import com.exasol.projectkeeper.validators.pom.PomFileIO;
import com.vdurmont.semver4j.Semver;

/**
* This class can increment the project's version.
*/
// [impl->dsn~dependency-updater.increment-version~1]
// [impl->dsn~dependency-updater.increment-version~2]
class ProjectVersionIncrementor {
private static final ZoneId UTC_ZONE = ZoneId.of("UTC");
private final ProjectKeeperConfig config;
Expand All @@ -30,23 +30,23 @@ class ProjectVersionIncrementor {
private final Clock clock;
private final Path projectDir;
private final Logger logger;
private final PomFileIO pomFileIO;
private final XmlDocumentIO xmlFileIO;
private final CommandExecutor commandExecutor;

ProjectVersionIncrementor(final ProjectKeeperConfig config, final Logger logger, final Path projectDir,
final String currentProjectVersion) {
this(config, logger, projectDir, currentProjectVersion, new ChangesFileIO(), new PomFileIO(),
this(config, logger, projectDir, currentProjectVersion, new ChangesFileIO(), new XmlDocumentIO(),
new CommandExecutor(), Clock.systemUTC());
}

ProjectVersionIncrementor(final ProjectKeeperConfig config, final Logger logger, final Path projectDir,
final String currentProjectVersion, final ChangesFileIO changesFileIO, final PomFileIO pomFileIO,
final String currentProjectVersion, final ChangesFileIO changesFileIO, final XmlDocumentIO xmlFileIO,
final CommandExecutor commandExecutor, final Clock clock) {
this.config = config;
this.logger = logger;
this.projectDir = projectDir;
this.changesFileIO = changesFileIO;
this.pomFileIO = pomFileIO;
this.xmlFileIO = xmlFileIO;
this.commandExecutor = commandExecutor;
this.clock = clock;
this.currentProjectVersion = Objects.requireNonNull(currentProjectVersion, "currentProjectVersion");
Expand Down Expand Up @@ -86,20 +86,22 @@ private LocalDate today() {
* @return the new, incremented version
*/
String incrementProjectVersion() {
final Path path = getPomPath();
final Model pom = pomFileIO.readPom(path);
if (!this.currentProjectVersion.equals(pom.getVersion())) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-174").message(
"Inconsistent project version {{version in pom file}} found in pom {{pom file path}}, expected {{expected version}}.",
pom.getVersion(), path, currentProjectVersion).ticketMitigation().toString());
}
final String nextVersion = incrementVersion(pom);
final String nextVersion = getIncrementedVersion(currentProjectVersion);
updatePomVersion(nextVersion);
if (usesReferenceCheckerPlugin()) {
updateReferences();
}
return nextVersion;
}

private void updatePomVersion(final String nextVersion) {
final Path path = getPomPath();
logger.info("Incrementing version from " + currentProjectVersion + " to " + nextVersion + " in POM " + path);
final Document pom = xmlFileIO.read(path);
incrementVersion(path, pom, nextVersion);
xmlFileIO.write(pom, path);
}

private boolean usesReferenceCheckerPlugin() {
return config.getSources().stream().anyMatch(source -> source.getModules().contains(JAR_ARTIFACT));
}
Expand All @@ -111,25 +113,31 @@ private void updateReferences() {
commandExecutor.execute(command);
}

private String incrementVersion(final Model pom) {
final String nextVersion = getIncrementedVersion(currentProjectVersion);
logger.info("Incrementing version from " + currentProjectVersion + " to " + nextVersion + " in POM "
+ pom.getPomFile());
pom.setVersion(nextVersion);
writePom(pom);
return nextVersion;
private void incrementVersion(final Path path, final Document pom, final String nextVersion) {
final Optional<Node> versionNode = findVersionNode(pom);
if (versionNode.isEmpty()) {
logger.warn(ExaError.messageBuilder("W-PK-CORE-196")
.message("No version node found in pom file {{pom file path}}.", path)
.mitigation("Please update the version to {{next version}} manually.", nextVersion).toString());
return;
}
if (!this.currentProjectVersion.equals(versionNode.get().getTextContent())) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-174").message(
"Inconsistent project version {{version in pom file}} found in pom {{pom file path}}, expected {{expected version}}.",
versionNode.get().getTextContent(), path, currentProjectVersion).ticketMitigation().toString());
}
versionNode.get().setTextContent(nextVersion);
}

private Optional<Node> findVersionNode(final Document pom) {
return xmlFileIO.runXPath(pom, "/project/version");
}

static String getIncrementedVersion(final String version) {
final Semver current = new Semver(version);
return current.nextPatch().toString();
}

private void writePom(final Model pom) {
final Path path = getPomPath();
pomFileIO.writePom(pom, path);
}

private Path getPomPath() {
return projectDir.resolve("pom.xml");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.exasol.projectkeeper.dependencyupdate;

import java.nio.file.Path;
import java.util.Optional;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.exasol.projectkeeper.validators.pom.io.PomFileReader;
import com.exasol.projectkeeper.validators.pom.io.PomFileWriter;
import com.exasol.projectkeeper.xpath.XPathErrorHandlingWrapper;

/**
* This class provides read and write access to XML documents.
* <p>
* The advantage of this class is that it can be easily mocked in tests and it preserves comments when writing XML.
*/
class XmlDocumentIO {
/**
* Read an XML document from a file.
*
* @param path path to the file
* @return XML document
*/
Document read(final Path path) {
return PomFileReader.parse(path);
}

/**
* Write an XML document to a file.
*
* @param document XML document
* @param path path to the file
*/
void write(final Document document, final Path path) {
PomFileWriter.writeFile(document, path);
}

/**
* Run an XPath query on an XML document and return the first result.
*
* @param current current node
* @param xPath XPath query
* @return first result of the query
*/
Optional<Node> runXPath(final Node current, final String xPath) {
return Optional.ofNullable(XPathErrorHandlingWrapper.runXPath(current, xPath));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static WorkflowOutput create(final Path outputPath) {
@Override
public void publish(final String key, final String value) {
final String content = formatter.format(key, value);
LOG.info(() -> "Publishing content '" + content + "'");
LOG.finest(() -> "Publishing content '" + content + "'");
write(content + "\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NullContentProvider implements WorkflowOutput {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private XPath() {
}

private final Path projectDirectory;
final Collection<ProjectKeeperModule> enabledModules;
private final Collection<ProjectKeeperModule> enabledModules;
private final Path pomFilePath;
private final ParentPomRef parentPomRef;
private final RepoInfo repoInfo;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ public IssueState getIssueState(final int issueNumber) {
final Issue issue = gitHubConnectionProvider.connect().repos().get(new Coordinates.Simple(owner, repoName))
.issues().get(issueNumber);
final Issue.Smart smartIssue = new Issue.Smart(issue);
final IssueState state = getIssueState(issueNumber, smartIssue);
LOG.fine(() -> "GitHub issue #" + issueNumber + " has state " + state);
return state;
}

private IssueState getIssueState(final int issueNumber, final Issue.Smart smartIssue) {
try {
if (!smartIssue.exists()) {
LOG.fine(() -> "GitHub issue #" + issueNumber + " does not exist.");
return IssueState.MISSING;
}
if (smartIssue.isOpen()) {
LOG.fine(() -> "GitHub issue #" + issueNumber + " is still open.");
return IssueState.OPEN;
} else {
LOG.fine(() -> "GitHub issue #" + issueNumber + " is closed.");
return IssueState.CLOSED;
}
} catch (final IOException exception) {
Expand Down
Loading

0 comments on commit 283f550

Please sign in to comment.