Skip to content

Commit

Permalink
[DONE JENKINS-10964] global configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
escoem committed May 29, 2018
1 parent 08ac7a0 commit 1c2da98
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 129 deletions.
39 changes: 35 additions & 4 deletions pom.xml
Expand Up @@ -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>
Expand All @@ -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>

Expand Down Expand Up @@ -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>
Expand Down
198 changes: 94 additions & 104 deletions src/main/java/org/jenkins/ci/plugins/jobimport/JobImportAction.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)) {
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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.