Skip to content

Commit

Permalink
rework push goal
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz-eitco-de committed Jun 5, 2024
1 parent ca189d7 commit 6194e23
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 50 deletions.
47 changes: 47 additions & 0 deletions src/main/java/de/eitco/cicd/dotnet/AbstractDotnetMojo.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package de.eitco.cicd.dotnet;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;

import java.io.File;
import java.util.Map;

public abstract class AbstractDotnetMojo extends AbstractMojo {

Expand All @@ -16,6 +23,13 @@ public abstract class AbstractDotnetMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.build.directory}")
protected File targetDirectory;

@Parameter
protected Map<String, String> nugetSources = Map.of();
@Parameter(defaultValue = "${settings}", readonly = true)
protected Settings settings;
@Component(hint = "dotnet-security")
private SecDispatcher securityDispatcher;

protected DotnetExecutor newExecutor() {

return newExecutor(false);
Expand All @@ -32,4 +46,37 @@ protected DotnetExecutor newExecutor(boolean ignoreResult) {
}


protected String findApiKey(String serverId) throws MojoExecutionException {

if (serverId == null) {

return null;
}

Server server = findServer(serverId);

return decrypt(server.getPassword());
}

protected String decrypt(String text) throws MojoExecutionException {
try {

return securityDispatcher.decrypt(text);

} catch (SecDispatcherException e) {

throw new MojoExecutionException(e);
}
}

protected Server findServer(String serverId) throws MojoExecutionException {

Server server = settings.getServer(serverId);

if (server == null) {

throw new MojoExecutionException("server " + serverId + " not found");
}
return server;
}
}
63 changes: 53 additions & 10 deletions src/main/java/de/eitco/cicd/dotnet/DotnetExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public record DotnetExecutor(
File workingDirectory,
Expand All @@ -19,10 +22,36 @@ public record DotnetExecutor(

public int execute(String... parameters) throws MojoExecutionException {

return execute(ignoreResult, List.of(parameters));
return execute(defaultOptions().mergeIgnoreResult(ignoreResult), List.of(parameters), Set.of());
}

public int execute(boolean ignoreResult, List<String> parameters) throws MojoExecutionException {
private static class ExecutionOptions {
private boolean ignoreResult = false;
private boolean inheritIo = true;

public ExecutionOptions ignoreResult() {
ignoreResult = true;
return this;
}

public ExecutionOptions silent() {
inheritIo = false;
return this;
}
public ExecutionOptions mergeIgnoreResult(boolean ignoreResult) {

this.ignoreResult = ignoreResult || this.ignoreResult;

return this;
}
}

private static ExecutionOptions defaultOptions() {

return new ExecutionOptions();
}

private int execute(ExecutionOptions executionOptions, List<String> parameters, Set<String> obfuscation) throws MojoExecutionException {

ProcessBuilder builder = new ProcessBuilder();

Expand All @@ -32,20 +61,25 @@ public int execute(boolean ignoreResult, List<String> parameters) throws MojoExe

builder.command(command);

builder.inheritIO();
if (executionOptions.inheritIo) {

builder.inheritIO();
}

builder.environment().put("DOTNET_CLI_TELEMETRY_OPTOUT", "TRUE");

try {

log.info("Executing command: " + String.join(" ", command));

log.info("Executing command: " + presentCommand(command, obfuscation));

Process process = builder.start();

int returnCode = process.waitFor();

if (returnCode != 0 && !ignoreResult) {
if (returnCode != 0 && !executionOptions.ignoreResult) {

throw new MojoExecutionException("process " + String.join(" ", command) + " returned code " + returnCode);
throw new MojoExecutionException("process " + presentCommand(command, obfuscation) + " returned code " + returnCode);
}

return returnCode;
Expand All @@ -55,6 +89,11 @@ public int execute(boolean ignoreResult, List<String> parameters) throws MojoExe
}
}

private static String presentCommand(List<String> command, Set<String> obfuscation) {

return command.stream().map(x -> obfuscation.contains(x) ? "****" : x).collect(Collectors.joining(" "));
}

private List<String> buildCommand(List<String> parameters) {

List<String> command = new ArrayList<>();
Expand All @@ -68,7 +107,7 @@ private List<String> buildCommand(List<String> parameters) {

public void build() throws MojoExecutionException {

int returnCode = execute(true, List.of("build"));
int returnCode = execute(defaultOptions().ignoreResult(), List.of("build"), Set.of());

if (returnCode != 0) {

Expand Down Expand Up @@ -99,7 +138,7 @@ public void pack(String version, String vendor, String description, String repos
parameters.add("--output");
parameters.add(targetDirectory.getPath());

execute(false, parameters);
execute(defaultOptions(), parameters, Set.of());
}

public int test(String logger, String testResultDirectory) throws MojoExecutionException {
Expand All @@ -121,12 +160,16 @@ public void push(String apiKey, String repository) throws MojoExecutionException
parameters.add(repository);
}

execute(ignoreResult, parameters);
execute(defaultOptions().mergeIgnoreResult(ignoreResult), parameters, Optional.ofNullable(apiKey).stream().collect(Collectors.toSet()));
}

public void addNugetSource(String url, String sourceName, String username, String apiToken) throws MojoExecutionException {

execute("nuget", "add", "source", url, "--name", sourceName, "--username", username, "--password", apiToken);
// remove source in case it is already there...
execute(defaultOptions().ignoreResult().silent(), List.of("nuget", "remove", "source", sourceName), Set.of());


execute(defaultOptions(), List.of("nuget", "add", "source", url, "--name", sourceName, "--username", username, "--password", apiToken), Set.of(apiToken));
}

public void clean() throws MojoExecutionException {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/de/eitco/cicd/dotnet/InitializeMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.eitco.cicd.dotnet;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.settings.Server;

import java.util.Map;

@Mojo(name = "initialize", defaultPhase = LifecyclePhase.INITIALIZE)
public class InitializeMojo extends AbstractDotnetMojo {


@Override
public void execute() throws MojoExecutionException {

DotnetExecutor dotnetExecutor = newExecutor();

for (Map.Entry<String, String> entry : nugetSources.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();

Server server = findServer(key);

dotnetExecutor.addNugetSource(value, key, decrypt(server.getUsername()), decrypt(server.getPassword()));
}

}
}
45 changes: 5 additions & 40 deletions src/main/java/de/eitco/cicd/dotnet/NugetPushMojo.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package de.eitco.cicd.dotnet;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;

@Mojo(name = "push", defaultPhase = LifecyclePhase.DEPLOY)
public class NugetPushMojo extends AbstractDotnetMojo {

@Parameter(defaultValue = "nuget-server")
private String nugetServerId;

protected String nugetServerId;
@Parameter
private String nugetServerUrl;

@Parameter
private String nugetSnapshotServerUrl;

@Parameter(defaultValue = "${settings}", readonly = true)
protected Settings settings;

@Parameter(defaultValue = "${project}", readonly = true)
protected MavenProject project;

Expand All @@ -38,17 +29,14 @@ public class NugetPushMojo extends AbstractDotnetMojo {
@Parameter
private String repositoryName;

@Component(hint = "dotnet-security")
private SecDispatcher securityDispatcher;

@Override
public void execute() throws MojoExecutionException {

String apiKey = findApiKey();
String apiKey = findApiKey(nugetServerId);

String repositoryUrl = decideRepositoryUrl();

boolean addSource = coalesce(forceAddSource, repositoryUrl.endsWith("/index.json"));
boolean addSource = coalesce(forceAddSource, nugetSources.get(nugetServerId) == null && repositoryUrl.endsWith("/index.json"));

DotnetExecutor dotnetExecutor = newExecutor();

Expand All @@ -73,10 +61,10 @@ private String decideRepositoryUrl() {

if (isSnapshot) {

return coalesce(nugetSnapshotServerUrl, nugetServerUrl, project.getDistributionManagement().getSnapshotRepository().getUrl(), project.getDistributionManagement().getRepository().getUrl());
return coalesce(nugetSnapshotServerUrl, nugetServerUrl, project.getDistributionManagement().getSnapshotRepository().getUrl(), nugetSources.get(nugetServerId), project.getDistributionManagement().getRepository().getUrl());
}

return coalesce(nugetServerUrl, project.getDistributionManagement().getRepository().getUrl());
return coalesce(nugetServerUrl, nugetSources.get(nugetServerId), project.getDistributionManagement().getRepository().getUrl());
}

@SafeVarargs
Expand All @@ -93,27 +81,4 @@ private <Type> Type coalesce(Type... elements) {
return null;
}

private String findApiKey() throws MojoExecutionException {

if (nugetServerId == null) {

return null;
}

Server server = settings.getServer(nugetServerId);

if (server == null) {

throw new MojoExecutionException("server " + nugetServerId + " not found");
}

try {

return securityDispatcher.decrypt(server.getPassword());

} catch (SecDispatcherException e) {

throw new MojoExecutionException(e);
}
}
}
1 change: 1 addition & 0 deletions src/main/resource-templates/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<lifecycle>
<id>default</id>
<phases>
<initialize>${project.groupId}:${project.artifactId}:${project.version}:initialize</initialize>
<compile>${project.groupId}:${project.artifactId}:${project.version}:build</compile>
<test>${project.groupId}:${project.artifactId}:${project.version}:test</test>
<package>
Expand Down

0 comments on commit 6194e23

Please sign in to comment.