Skip to content
Permalink
Browse files
[DONE JENKINS-10964] global configuration
  • Loading branch information
escoem committed May 29, 2018
1 parent 08ac7a0 commit 1c2da98f9e5a7e1268f0bb4db1c8ee306eab75e3
39 pom.xml
@@ -31,11 +31,11 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.13</version>
<version>3.5</version>
</parent>

<artifactId>job-import-plugin</artifactId>
<version>2.2-SNAPSHOT</version>
<version>3.0-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>Job Import Plugin</name>
@@ -45,8 +45,8 @@
<url>http://wiki.jenkins-ci.org/display/JENKINS/Job+Import+Plugin</url>

<properties>
<jenkins.version>1.625.3</jenkins.version>
<java.level>7</java.level>
<jenkins.version>2.107.3</jenkins.version>
<java.level>8</java.level>
<findbugs.failOnError>false</findbugs.failOnError>
</properties>

@@ -92,12 +92,43 @@
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>3.0</compatibleSinceVersion>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.io7m.xom/xom -->
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.io7m.xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
@@ -39,16 +39,19 @@
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkins.ci.plugins.jobimport.client.RestApiClient;
import org.jenkins.ci.plugins.jobimport.model.JenkinsSite;
import org.jenkins.ci.plugins.jobimport.model.RemoteFolder;
import org.jenkins.ci.plugins.jobimport.model.RemoteItem;
import org.jenkins.ci.plugins.jobimport.utils.Constants;
import org.jenkins.ci.plugins.jobimport.utils.CredentialsUtils;
import org.jenkins.ci.plugins.jobimport.utils.CredentialsUtils.NullSafeCredentials;
import org.jenkins.ci.plugins.jobimport.utils.RemoteItemUtils;
import org.jenkins.ci.plugins.jobimport.utils.URLUtils;
import org.kohsuke.stapler.ForwardToView;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
@@ -58,6 +61,7 @@
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
@@ -76,43 +80,89 @@ public final class JobImportAction implements RootAction, Describable<JobImportA

private static final Logger LOG = Logger.getLogger(JobImportAction.class.getName());

private String remoteUrl;
private String localFolder;
private String credentialId;
private String recursiveSearch;

private final SortedSet<RemoteItem> remoteJobs = new TreeSet<RemoteItem>();
private final SortedMap<RemoteItem, RemoteItemImportStatus> remoteJobsImportStatus = new TreeMap<RemoteItem, RemoteItemImportStatus>();

public void doClear(final StaplerRequest request, final StaplerResponse response) throws ServletException,
IOException {
remoteUrl = null;
credentialId = null;
localFolder = null;
recursiveSearch = null;
remoteJobs.clear();
remoteJobsImportStatus.clear();
response.sendRedirect(Jenkins.getActiveInstance().getRootUrl());
public void doClear(final StaplerRequest request, final StaplerResponse response)
throws ServletException, IOException {
response.sendRedirect(Jenkins.get().getRootUrl() + getUrlName());
}

public void doImport(final StaplerRequest request, final StaplerResponse response) throws ServletException,
IOException {
remoteJobsImportStatus.clear();
public void doImport(final StaplerRequest request, final StaplerResponse response)
throws ServletException, IOException {
final SortedMap<RemoteItem, RemoteItemImportStatus> remoteJobsImportStatus = new TreeMap<RemoteItem, RemoteItemImportStatus>();

localFolder = request.getParameter(Constants.LOCAL_FOLDER_PARAM);
final String localFolder = request.getParameter(Constants.LOCAL_FOLDER_PARAM);
final String remoteJobsAvailable = (String)request.getParameter("remoteJobsAvailable");

if (isRemoteJobsAvailable()) {
final String site = (String)request.getParameter("remoteJenkins");

JenkinsSite remoteJenkins = new JenkinsSite("", "");
for (JenkinsSite js : JobImportGlobalConfig.get().getSites()) {
if ((js.getName() + "-" + js.getUrl() + "-" + js.getDefaultCredentialsId()).equals(site)) {
remoteJenkins = js;
break;
}
}

final String credentialId = remoteJenkins.getDefaultCredentialsId();

final SortedSet<RemoteItem> remoteJobs = new TreeSet<RemoteItem>();
final String remoteFolder = request.getParameter("remoteFolder");
final String remoteUrl = remoteJenkins.getUrl() + remoteFolder;
final String recursiveSearch = request.getParameter(Constants.RECURSIVE_PARAM);
doQueryInternal(null, remoteUrl, CredentialsUtils.getCredentials(credentialId), recursiveSearch, remoteJobs);

if (remoteJobsAvailable != null && remoteJobsAvailable.equalsIgnoreCase("true")) {
if (request.hasParameter(Constants.JOB_URL_PARAM)) {
for (final String jobUrl : Arrays.asList(request.getParameterValues(Constants.JOB_URL_PARAM))) {
doImportInternal(jobUrl, localFolder);
doImportInternal(jobUrl, localFolder, credentialId, remoteJobs, remoteJobsImportStatus);
}
}
}

response.forwardToPreviousPage(request);
new ForwardToView(this, "index")
.with("step2", "true")
.with("remoteJobsAvailable", remoteJobsAvailable)
.with("remoteJobsImportStatus", remoteJobsImportStatus)
.with("remoteJobsImportStatusAvailable", remoteJobsImportStatus.size()>0)
.generateResponse(request, response, this);
}

public void doQuery(final StaplerRequest request, final StaplerResponse response)
throws ServletException, IOException {
final SortedSet<RemoteItem> remoteJobs = new TreeSet<RemoteItem>();

final String remoteFolder = request.getParameter("remoteFolder");

final String site = request.getParameter("_.jenkinsSites");

JenkinsSite remoteJenkins = new JenkinsSite("", "");
for (JenkinsSite js : JobImportGlobalConfig.get().getSites()) {
if ((js.getName() + "-" + js.getUrl() + "-" + js.getDefaultCredentialsId()).equals(site)) {
remoteJenkins = js;
break;
}
}

final String credentialId = remoteJenkins.getDefaultCredentialsId();
final String remoteUrl = remoteJenkins.getUrl() + remoteFolder;
final String recursiveSearch = request.getParameter(Constants.RECURSIVE_PARAM);

doQueryInternal(null, remoteUrl, CredentialsUtils.getCredentials(credentialId), recursiveSearch, remoteJobs);

new ForwardToView(this, "index")
.with("step1", "true")
.with("remoteJenkins", remoteJenkins.getName() + "-" + remoteJenkins.getUrl() + "-" + remoteJenkins.getDefaultCredentialsId())
.with("remoteJobs", remoteJobs)
.with("remoteFolder", remoteFolder)
.with("recursiveSearch", recursiveSearch)
.with("remoteJobsAvailable", remoteJobs.size()>0)
.generateResponse(request, response, this);
}

private void doImportInternal(String jobUrl, String localPath) throws IOException {

private void doImportInternal(String jobUrl, String localPath,
String credentialId,
SortedSet<RemoteItem> remoteJobs,
SortedMap<RemoteItem, RemoteItemImportStatus> remoteJobsImportStatus) throws IOException {
final RemoteItem remoteJob = RemoteItemUtils.getRemoteJob(remoteJobs, jobUrl);
if (remoteJob != null) {
if (!remoteJobsImportStatus.containsKey(remoteJob)) {
@@ -121,9 +171,9 @@ private void doImportInternal(String jobUrl, String localPath) throws IOExceptio

// ---

if (StringUtils.isNotEmpty(localPath) && Jenkins.getActiveInstance().getItemByFullName(localPath + remoteJob.getName()) != null) {
if (StringUtils.isNotEmpty(localPath) && Jenkins.get().getItemByFullName(localPath + remoteJob.getName()) != null) {
remoteJobsImportStatus.get(remoteJob).setStatus(MessagesUtils.formatFailedDuplicateJobName());
} else if (StringUtils.isEmpty(localPath) && Jenkins.getActiveInstance().getItem(remoteJob.getName()) != null) {
} else if (StringUtils.isEmpty(localPath) && Jenkins.get().getItem(remoteJob.getName()) != null) {
remoteJobsImportStatus.get(remoteJob).setStatus(MessagesUtils.formatFailedDuplicateJobName());
} else {
InputStream inputStream = null;
@@ -135,10 +185,10 @@ private void doImportInternal(String jobUrl, String localPath) throws IOExceptio

final Item newItem;
if (StringUtils.isNotEmpty(localPath)) {
newItem = Jenkins.getActiveInstance().getItemByFullName(localPath, com.cloudbees.hudson.plugins.folder.Folder.class).
newItem = Jenkins.get().getItemByFullName(localPath, com.cloudbees.hudson.plugins.folder.Folder.class).
createProjectFromXML(remoteJob.getFullName(), inputStream);
} else {
newItem = Jenkins.getActiveInstance().
newItem = Jenkins.get().
createProjectFromXML(remoteJob.getFullName(), inputStream);
}

@@ -150,74 +200,42 @@ private void doImportInternal(String jobUrl, String localPath) throws IOExceptio

if (remoteJob.isFolder() && ((RemoteFolder)remoteJob).hasChildren()) {
for (RemoteItem childJob : ((RemoteFolder)remoteJob).getChildren()) {
doImportInternal(childJob.getUrl(), newItem.getFullName());
doImportInternal(childJob.getUrl(), newItem.getFullName(), credentialId, remoteJobs, remoteJobsImportStatus);
}
}
/*
try{
Jenkins instance= Jenkins.getActiveInstance();
instance.checkPermission(instance.ADMINISTER);
Jenkins.getActiveInstance().doReload();
} catch(AccessDeniedException2 ex2){
remoteJobsImportStatus.get(remoteJob).setStatus(MessagesUtils.formatSuccessNotReloaded());
LOG.log(Level.INFO, "Failed to reload Jenkins config because the user lacks the Overall Administer permission");
}
*/
}

catch (final Exception e) {
} catch (final Exception e) {
LOG.warning("Job Import Failed: " + e.getMessage());
if (LOG.isLoggable(Level.INFO)) {
LOG.log(Level.INFO, e.getMessage(), e);
}
remoteJobsImportStatus.get(remoteJob).setStatus(MessagesUtils.formatFailedException(e));

try {
TopLevelItem created = Jenkins.getActiveInstance().getItem(remoteJob.getName());
TopLevelItem created = Jenkins.get().getItem(remoteJob.getName());
if (created != null) {
created.delete();
}
}
catch (final InterruptedException e2) {
// do nothing
}
}

finally {
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
}

public void doQuery(final StaplerRequest request, final StaplerResponse response) throws ServletException,
IOException {
remoteJobs.clear();
remoteJobsImportStatus.clear();
remoteUrl = request.getParameter(Constants.REMOTE_URL_PARAM);
credentialId = request.getParameter("_.credentialId");

recursiveSearch = request.getParameter(Constants.RECURSIVE_PARAM);

doQueryInternal(null, remoteUrl, CredentialsUtils.getCredentials(credentialId));

response.forwardToPreviousPage(request);
}

private void doQueryInternal(RemoteFolder parent, String url, NullSafeCredentials credentials) {
private void doQueryInternal(RemoteFolder parent, String url, NullSafeCredentials credentials, String recursiveSearch, SortedSet<RemoteItem> remoteJobs) {
remoteJobs.addAll(RestApiClient.getRemoteItems(parent, url, credentials, isRecursive(recursiveSearch)));
}

private boolean isRecursive(String param) {
return StringUtils.equals("on", param);
}

public FormValidation doTestConnection(@QueryParameter("remoteUrl") final String remoteUrl) {
return FormValidation.ok();
}

public String getRootUrl() {
return Jenkins.getActiveInstance().getRootUrl();
return Jenkins.get().getRootUrl();
}

public String getDisplayName() {
@@ -228,47 +246,13 @@ public String getIconFileName() {
return "/images/32x32/setting.png";
}

public SortedSet<RemoteItem> getRemoteJobs() {
return remoteJobs;
}

public SortedMap<RemoteItem, RemoteItemImportStatus> getRemoteJobsImportStatus() {
return remoteJobsImportStatus;
}

public String getRemoteUrl() {
return remoteUrl;
}

public String getUrlName() {
return "/" + Constants.URL_NAME;
}

public boolean isRemoteJobsAvailable() {
return remoteJobs.size() > 0;
}

public boolean isRemoteJobsImportStatusAvailable() {
return remoteJobsImportStatus.size() > 0;
}

public void setRemoteUrl(final String remoteUrl) {
this.remoteUrl = remoteUrl;
}

public String getCredentialId() { return credentialId; }

public String getLocalFolder() {
return localFolder;
}

public void setLocalFolder(String localFolder) {
this.localFolder = localFolder;
}

@Override
public Descriptor<JobImportAction> getDescriptor() {
return Jenkins.getActiveInstance().getDescriptorOrDie(getClass());
return Jenkins.get().getDescriptorOrDie(getClass());
}

@Extension
@@ -282,17 +266,23 @@ public ListBoxModel doFillCredentialIdItems() {
.includeEmptyValue()
.includeMatchingAs(
Jenkins.getAuthentication(),
Jenkins.getInstance(),
Jenkins.getInstanceOrNull(),
StandardUsernamePasswordCredentials.class,
Collections.<DomainRequirement>emptyList(),
CredentialsMatchers.always()
).includeMatchingAs(
ACL.SYSTEM,
Jenkins.getInstance(),
Jenkins.getInstanceOrNull(),
StandardUsernamePasswordCredentials.class,
Collections.<DomainRequirement>emptyList(),
CredentialsMatchers.always()
);
}

public ListBoxModel doFillJenkinsSitesItems() {
final ListBoxModel listBoxModel = new ListBoxModel();
JobImportGlobalConfig.get().getSites().stream().forEach(s -> listBoxModel.add(s.getName(), s.getName() + "-" + s.getUrl() + "-" + s.getDefaultCredentialsId()));
return listBoxModel;
}
}
}

0 comments on commit 1c2da98

Please sign in to comment.