Skip to content

Commit

Permalink
more code tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnord committed Dec 5, 2019
1 parent 4ec27e7 commit 62df574
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class M2ReleaseArgumentInterceptorAction implements MavenArgumentIntercep


private final transient String goalsAndOptions;
@SuppressWarnings("unused")
@Deprecated
private transient boolean isDryRun; // keep backward compatible
private final transient String scmPassword;
Expand Down Expand Up @@ -94,21 +95,18 @@ public ArgumentListBuilder intercept(ArgumentListBuilder mavenargs, MavenModuleS

ArgumentListBuilder internalIntercept(ArgumentListBuilder mavenArgumentListBuilder, boolean isIncrementalBuild) {

ArgumentListBuilder returnListBuilder = new ArgumentListBuilder();
ArgumentListBuilder returnListBuilder;
List<String> argumentList = mavenArgumentListBuilder.toList();

if (isIncrementalBuild && containsJenkinsIncrementalBuildArguments(argumentList))
{
if (isIncrementalBuild && containsJenkinsIncrementalBuildArguments(argumentList)) {
LOGGER.config("This Maven build seems to be configured as 'Incremental build'. This will be disables, as always the full project will be released");
returnListBuilder = removeAllIncrementalBuildArguments(mavenArgumentListBuilder.clone());
} else
{
} else {
returnListBuilder = mavenArgumentListBuilder.clone();
}

if (scmPassword != null) {
returnListBuilder.addMasked("-Dpassword=" + scmPassword);
}
if (scmPassword != null) {
returnListBuilder.addMasked("-Dpassword=" + scmPassword);
}

return returnListBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.Run;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.tasks.Builder;
Expand All @@ -50,10 +49,6 @@
import hudson.util.Secret;
import jenkins.model.Jenkins;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
Expand All @@ -64,7 +59,6 @@
import org.jvnet.hudson.plugins.m2release.nexus.Stage;
import org.jvnet.hudson.plugins.m2release.nexus.StageClient;
import org.jvnet.hudson.plugins.m2release.nexus.StageException;
import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -367,7 +361,8 @@ public String getDryRunGoals() {
* Evaluate if the current build should be a release build.
* @return <code>true</code> if this build is a release build.
*/
private boolean isReleaseBuild(@SuppressWarnings("rawtypes") AbstractBuild build) {
@SuppressWarnings({ "rawtypes", "unchecked" })
private boolean isReleaseBuild(AbstractBuild build) {
return (build.getCause(ReleaseCause.class) != null);
}

Expand All @@ -384,7 +379,7 @@ public Action getProjectAction(@SuppressWarnings("rawtypes") AbstractProject job
}

/**
* Hudson defines a method {@link Builder#getDescriptor()}, which returns the corresponding
* Jenkins defines a method {@link Builder#getDescriptor()}, which returns the corresponding
* {@link Descriptor} object. Since we know that it's actually {@link DescriptorImpl}, override the method
* and give a better return type, so that we can access {@link DescriptorImpl} methods more easily. This is
* not necessary, but just a coding style preference.
Expand All @@ -398,69 +393,9 @@ public DescriptorImpl getDescriptor() {
@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor {

public static final Permission CREATE_RELEASE;

static {
Permission tmpPerm = null;
try {
// Jenkins changed the security model in a non backward compatible way :-(
// JENKINS-10661
Class<?> permissionScopeClass = Class.forName("hudson.security.PermissionScope");
Object psArr = Array.newInstance(permissionScopeClass, 2);
Field f;
f = permissionScopeClass.getDeclaredField("JENKINS");
Array.set(psArr, 0, f.get(null));
f = permissionScopeClass.getDeclaredField("ITEM");
Array.set(psArr, 1, f.get(null));

Constructor<Permission> ctor = Permission.class.getConstructor(PermissionGroup.class,
String.class,
Localizable.class,
Permission.class,
// boolean.class,
permissionScopeClass);
//permissionScopes.getClass());
tmpPerm = ctor.newInstance(Item.PERMISSIONS,
"Release",
Messages._CreateReleasePermission_Description(),
Jenkins.ADMINISTER,
// true,
f.get(null));
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).info("Using new style Permission with PermissionScope");

}
// all these exceptions are Jenkins < 1.421 or Hudson
// wouldn't multicatch be nice!
catch (NoSuchMethodException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (InvocationTargetException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (IllegalArgumentException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (IllegalAccessException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (InstantiationException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (NoSuchFieldException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (ClassNotFoundException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
if (tmpPerm == null) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new style permission with PermissionScope failed");
tmpPerm = new Permission(Item.PERMISSIONS,
"Release", //$NON-NLS-1$
Messages._CreateReleasePermission_Description(),
Jenkins.ADMINISTER);
}
CREATE_RELEASE = tmpPerm;
}
public static final Permission CREATE_RELEASE = new Permission(Item.PERMISSIONS, "Release",
Messages._CreateReleasePermission_Description(),
Jenkins.ADMINISTER, PermissionScope.ITEM);

public static final String DEFAULT_RELEASE_GOALS = "-Dresume=false release:prepare release:perform"; //$NON-NLS-1$
public static final String DEFAULT_DRYRUN_GOALS = "-Dresume=false -DdryRun=true release:prepare"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

import hudson.Plugin;
import org.jvnet.hudson.plugins.m2release.M2ReleaseBuildWrapper.DescriptorImpl;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
* @author Kohsuke Kawaguchi
*/
public class PluginImpl extends Plugin {

@SuppressFBWarnings(value="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", justification="the side effect is the permission is loaded")
@Override
public void start() throws Exception {
// this permission designates a wrong parent group, which introduces a classloading problem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,20 @@ public String getUserName() {
}
}

@Override
@Override
public String getShortDescription() {
return Messages.ReleaseCause_ShortDescription(getUserName());
}

@Override
public boolean equals(Object o) {
// generally this is bad but the parent uses instanceof and does not check classes directly and we add nothing
// and it keeps spotbugs happy. We should check authenticationName but that has been deprecated since before the dinosaurs
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class M2ReleaseArgumentInterceptorActionTest {
@Before
public void setUp()
{
sut = new M2ReleaseArgumentInterceptorAction("");
sut = new M2ReleaseArgumentInterceptorAction("", null);
listBuilder = new ArgumentListBuilder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ public boolean checkCredentials(String user, String pass) {

HttpHandler handler = new HttpHandler() {

@Override
public void handle(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
if (path.equals(requestPath)) {
Expand Down

0 comments on commit 62df574

Please sign in to comment.