Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Modernize the plugin baseline
Browse files Browse the repository at this point in the history
1. Upgrade to Jenkins 2.346.3 baseline.
2. Fix the compilation issue which the jboss package is not used.
3. Fix the tests that failed in new Jenkins version.
4. Fix SpotBugs issues.
5. Fix stream not closed issue in tests
  • Loading branch information
topikachu authored and Gong Yi committed Sep 4, 2023
1 parent 3db11aa commit ad1fd1e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
25 changes: 17 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.50</version>
<version>4.50</version>
<relativePath />
</parent>

<properties>
<jenkins.version>1.653</jenkins.version>
<java.level>8</java.level>
<jenkins.version>2.346.3</jenkins.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<!-- https://wiki.jenkins-ci.org/display/JENKINS/Marking+a+new+plugin+version+as+incompatible+with+older+versions -->
<hpi.compatibleSinceVersion>3.1.6</hpi.compatibleSinceVersion>
</properties>
Expand Down Expand Up @@ -42,7 +44,7 @@
<artifactId>maven-hpi-plugin</artifactId>
<configuration>
<compatibleSinceVersion>3.0.4-SNAPSHOT</compatibleSinceVersion>
</configuration>
</configuration>
</plugin>
-->
</plugins>
Expand All @@ -69,27 +71,34 @@
</pluginRepository>
</pluginRepositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.346.x</artifactId>
<version>1742.vb_70478c1b_25f</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.6.1.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.75</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.13</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -845,7 +847,7 @@ protected static URL generateEffectiveRemoteBuildURL(URL remoteBuildURL, String

private String printOffsetConsoleOutput(BuildContext context, String offset, RemoteBuildInfo buildInfo)
throws IOException, InterruptedException {
if (offset.equals("-1")) {
if (offset == null || offset.equals("-1")) {
return "-1";
}
String buildUrlString = String.format("%slogText/progressiveText?start=%s", buildInfo.getBuildURL(), offset);
Expand Down Expand Up @@ -1271,10 +1273,10 @@ public ListBoxModel doFillRemoteJenkinsNameItems() {
ListBoxModel model = new ListBoxModel();

model.add("");
for (RemoteJenkinsServer site : getRemoteSites()) {
model.add(site.getDisplayName());
}

Arrays.stream(getRemoteSites())
.filter(Objects::nonNull)
.map(RemoteJenkinsServer::getDisplayName)
.forEach(model::add);
return model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import org.jboss.marshalling.util.IntKeyMap;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.BuildContext;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Optional;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNullableByDefault;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.BuildContext;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteJenkinsServer;
Expand Down Expand Up @@ -366,7 +368,10 @@ public Object readJsonFileFromBuildArchive(String filename) throws IOException,
private String getParameterFromJobMetadata(JSONObject remoteJobMetadata, String string)
{
try {
return trimToNull(remoteJobMetadata.getString("name"));
return Optional.ofNullable(remoteJobMetadata)
.map(meta->meta.getString("name"))
.map(StringUtils::trimToNull)
.orElse(null);
}
catch (JSONException e) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.mockito.Mockito.spy;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -24,6 +25,7 @@
import java.util.List;
import java.util.Map;

import jenkins.security.ApiTokenProperty;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration.DescriptorImpl;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.NullAuth;
Expand Down Expand Up @@ -77,7 +79,7 @@ private void enableAuth() throws IOException {
HudsonPrivateSecurityRealm hudsonPrivateSecurityRealm = new HudsonPrivateSecurityRealm(false, false, null);
jenkinsRule.jenkins.setSecurityRealm(hudsonPrivateSecurityRealm); //jenkinsRule.createDummySecurityRealm());
testUser = hudsonPrivateSecurityRealm.createAccount("test", "test");
testUserToken = testUser.getProperty(jenkins.security.ApiTokenProperty.class).getApiToken();
testUserToken = testUser.getProperty(ApiTokenProperty.class).generateNewToken("test").plainValue;

mockAuth.grant(Jenkins.ADMINISTER).everywhere().toAuthenticated();
}
Expand Down Expand Up @@ -150,19 +152,23 @@ private void _testRemoteBuild(boolean authenticate, boolean withParam, FreeStyle
//Check results
FreeStyleBuild lastBuild2 = project.getLastBuild();
assertNotNull(lastBuild2);
List<String> log = IOUtils.readLines(lastBuild2.getLogInputStream());
assertTrue(log.toString(), log.toString().contains("Started by user " + (authenticate ? "test" : "anonymous") + ", Building in workspace"));

FreeStyleBuild lastBuild = remoteProject.getLastBuild();
assertNotNull("lastBuild null", lastBuild);
if (withParam) {
EnvVars remoteEnv = lastBuild.getEnvironment(new LogTaskListener(null, null));
for (Map.Entry<String, String> p : params.entrySet()) {
assertEquals(p.getValue(), remoteEnv.get(p.getKey()));
try(InputStream logStream=lastBuild2.getLogInputStream()){
List<String> log = IOUtils.readLines(logStream);
assertTrue(log.toString(), log.toString().contains("Started by user " + (authenticate ? "test" : "unknown or anonymous") + ", Running as SYSTEM, Building in workspace"));

FreeStyleBuild lastBuild = remoteProject.getLastBuild();
assertNotNull("lastBuild null", lastBuild);
if (withParam) {
EnvVars remoteEnv = lastBuild.getEnvironment(new LogTaskListener(null, null));
for (Map.Entry<String, String> p : params.entrySet()) {
assertEquals(p.getValue(), remoteEnv.get(p.getKey()));
}
} else {
assertNotEquals("lastBuild should be executed no matter the result which depends on the remote job configuration.", null, lastBuild.getNumber());
}
} else {
assertNotEquals("lastBuild should be executed no matter the result which depends on the remote job configuration.", null, lastBuild.getNumber());
}

}

private void _testRemoteBuild(boolean authenticate) throws Exception {
Expand Down

0 comments on commit ad1fd1e

Please sign in to comment.