Skip to content

Commit

Permalink
[DONE JENKINS-51119] now we can override
Browse files Browse the repository at this point in the history
  • Loading branch information
escoem committed May 30, 2018
1 parent a356f5b commit bb75953
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Expand Up @@ -31,6 +31,7 @@
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.Extension;
import hudson.PluginManager;
import hudson.model.AbstractItem;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Item;
Expand Down Expand Up @@ -61,6 +62,8 @@
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
Expand Down Expand Up @@ -124,7 +127,7 @@ public void doImport(final StaplerRequest request, final StaplerResponse respons
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, credentialId, shouldInstallPlugins(request.getParameter("plugins")), remoteJobs, remoteJobsImportStatus);
doImportInternal(jobUrl, localFolder, credentialId, shouldInstallPlugins(request.getParameter("plugins")), shouldUpdate(request.getParameter("update")), remoteJobs, remoteJobsImportStatus);
}
}
}
Expand Down Expand Up @@ -173,6 +176,7 @@ public void doQuery(final StaplerRequest request, final StaplerResponse response
private void doImportInternal(String jobUrl, String localPath,
String credentialId,
boolean installPlugins,
boolean update,
SortedSet<RemoteItem> remoteJobs,
SortedMap<RemoteItem, RemoteItemImportStatus> remoteJobsImportStatus) throws IOException {
final RemoteItem remoteJob = RemoteItemUtils.getRemoteJob(remoteJobs, jobUrl);
Expand All @@ -183,9 +187,9 @@ private void doImportInternal(String jobUrl, String localPath,

// ---

if (StringUtils.isNotEmpty(localPath) && Jenkins.get().getItemByFullName(localPath + remoteJob.getName()) != null) {
if (!update && StringUtils.isNotEmpty(localPath) && Jenkins.get().getItemByFullName(localPath + remoteJob.getName()) != null) {
remoteJobsImportStatus.get(remoteJob).setStatus(MessagesUtils.formatFailedDuplicateJobName());
} else if (StringUtils.isEmpty(localPath) && Jenkins.get().getItem(remoteJob.getName()) != null) {
} else if (!update && StringUtils.isEmpty(localPath) && Jenkins.get().getItem(remoteJob.getName()) != null) {
remoteJobsImportStatus.get(remoteJob).setStatus(MessagesUtils.formatFailedDuplicateJobName());
} else {
InputStream inputStream = null;
Expand All @@ -197,11 +201,24 @@ private void doImportInternal(String jobUrl, String localPath,

final Item newItem;
if (StringUtils.isNotEmpty(localPath) && !StringUtils.equals("/", localPath.trim())) {
newItem = Jenkins.get().getItemByFullName(localPath, com.cloudbees.hudson.plugins.folder.Folder.class).
createProjectFromXML(remoteJob.getFullName(), inputStream);
String fullName = localPath.endsWith("/") ? localPath + remoteJob.getFullName() : localPath + "/" + remoteJob.getFullName();
Item currentItem = Jenkins.get().getItemByFullName(localPath);
if (update && currentItem instanceof AbstractItem) {
((AbstractItem)currentItem).updateByXml((Source)new StreamSource(inputStream));
newItem = currentItem;
} else {
newItem = Jenkins.get().getItemByFullName(localPath, com.cloudbees.hudson.plugins.folder.Folder.class).
createProjectFromXML(remoteJob.getFullName(), inputStream);
}
} else {
newItem = Jenkins.get().
createProjectFromXML(remoteJob.getFullName(), inputStream);
Item currentItem = Jenkins.get().getItemByFullName(remoteJob.getFullName());
if (update && currentItem instanceof AbstractItem) {
((AbstractItem)currentItem).updateByXml((Source)new StreamSource(inputStream));
newItem = currentItem;
} else {
newItem = Jenkins.get().
createProjectFromXML(remoteJob.getFullName(), inputStream);
}
}

if (newItem != null) {
Expand All @@ -220,7 +237,7 @@ private void doImportInternal(String jobUrl, String localPath,

if (remoteJob.isFolder() && ((RemoteFolder)remoteJob).hasChildren()) {
for (RemoteItem childJob : ((RemoteFolder)remoteJob).getChildren()) {
doImportInternal(childJob.getUrl(), newItem.getFullName(), credentialId, installPlugins, remoteJobs, remoteJobsImportStatus);
doImportInternal(childJob.getUrl(), newItem.getFullName(), credentialId, installPlugins, update, remoteJobs, remoteJobsImportStatus);
}
}
} catch (final Exception e) {
Expand Down Expand Up @@ -256,6 +273,9 @@ private boolean isRecursive(String param) {
private boolean shouldInstallPlugins(String param) {
return StringUtils.equals("on", param);
}
private boolean shouldUpdate(String param) {
return StringUtils.equals("on", param);
}

public String getRootUrl() {
return Jenkins.get().getRootUrl();
Expand Down
Expand Up @@ -69,6 +69,9 @@
<f:checkbox name="plugins" value="${plugins}" default="false"/>
</f:entry>
</j:if>
<f:entry title="${%Override exisitng jobs}" field="update">
<f:checkbox name="update" value="${update}" default="false"/>
</f:entry>
<table class="jobImportPlugin" cellpadding="3" cellspacing="3">
<tr>
<th>Import?</th>
Expand Down

0 comments on commit bb75953

Please sign in to comment.