Skip to content

Commit

Permalink
Fix JENKINS-16130 and JENKINS-14681
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Aug 2, 2014
1 parent cef0206 commit fc46701
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 210 deletions.
@@ -0,0 +1,50 @@
package org.jenkinsci.plugins.artifactdeployer;

import hudson.model.AbstractBuild;
import hudson.model.Action;
import org.kohsuke.stapler.StaplerProxy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Gregory Boissinot
*/
public class ArtifactDeployerBuildAction implements Action, StaplerProxy {

private static final String URL_NAME = "deployedArtifacts";

private AbstractBuild<?, ?> owner;

private Map<Integer, List<ArtifactDeployerVO>> deployedArtifactsInfo = new HashMap<Integer, List<ArtifactDeployerVO>>();

public void setArtifactsInfo(AbstractBuild<?, ?> owner, Map<Integer, List<ArtifactDeployerVO>> deployedArtifactsInfo) {
this.owner = owner;
this.deployedArtifactsInfo.putAll(deployedArtifactsInfo);
}

public AbstractBuild<?, ?> getOwner() {
return owner;
}

public Map<Integer, List<ArtifactDeployerVO>> getDeployedArtifactsInfo() {
return this.deployedArtifactsInfo;
}

public String getIconFileName() {
return "package.gif";
}

public String getDisplayName() {
return "Deployed Build Artifacts";
}

public String getUrlName() {
return URL_NAME;
}

public Object getTarget() {
return new DeployedArtifactsResult(this);
}
}
Expand Up @@ -45,7 +45,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

listener.getLogger().println("[ArtifactDeployer] - Starting deployment from the build step ...");
DeployedArtifactsActionManager deployedArtifactsService = DeployedArtifactsActionManager.getInstance();
DeployedArtifacts deployedArtifactsAction = deployedArtifactsService.getOrCreateAction(build);
ArtifactDeployerBuildAction artifactDeployerBuildActionAction = deployedArtifactsService.getOrCreateAction(build);
final FilePath workspace = build.getWorkspace();
Map<Integer, List<ArtifactDeployerVO>> deployedArtifacts;
try {
Expand All @@ -59,7 +59,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return false;
}

deployedArtifactsAction.addDeployedArtifacts(deployedArtifacts);
artifactDeployerBuildActionAction.setArtifactsInfo(build, deployedArtifacts);
listener.getLogger().println("[ArtifactDeployer] - Stopping deployment from the build step ...");

return true;
Expand Down Expand Up @@ -98,8 +98,8 @@ private Map<Integer, List<ArtifactDeployerVO>> processDeployment(final AbstractB

//Copying files to remote directory
DeployedArtifactsActionManager deployedArtifactsService = DeployedArtifactsActionManager.getInstance();
DeployedArtifacts deployedArtifactsAction = deployedArtifactsService.getOrCreateAction(build);
int numberOfCurrentDeployedArtifacts = deployedArtifactsAction.getDeployedArtifactsInfo().size();
ArtifactDeployerBuildAction artifactDeployerBuildActionAction = deployedArtifactsService.getOrCreateAction(build);
int numberOfCurrentDeployedArtifacts = artifactDeployerBuildActionAction.getDeployedArtifactsInfo().size();
ArtifactDeployerCopy deployerCopy =
new ArtifactDeployerCopy(listener, includes, excludes, flatten, outputFilePath, numberOfCurrentDeployedArtifacts);
ArtifactDeployerManager deployerManager = new ArtifactDeployerManager();
Expand Down Expand Up @@ -148,9 +148,9 @@ public void onDeleted(AbstractBuild build) {

//Process all ArtifactDeployerBuilder instance
for (ArtifactDeployerBuilder artifactDeployerBuilder : artifactDeployerBuilders) {
DeployedArtifacts deployedArtifacts = build.getAction(DeployedArtifacts.class);
if (deployedArtifacts != null) {
Map<Integer, List<ArtifactDeployerVO>> info = deployedArtifacts.getDeployedArtifactsInfo();
ArtifactDeployerBuildAction artifactDeployerBuildAction = build.getAction(ArtifactDeployerBuildAction.class);
if (artifactDeployerBuildAction != null) {
Map<Integer, List<ArtifactDeployerVO>> info = artifactDeployerBuildAction.getDeployedArtifactsInfo();
if (info != null) {
ArtifactDeployerEntry entry = artifactDeployerBuilder.getEntry();

Expand Down Expand Up @@ -183,7 +183,7 @@ public void onDeleted(AbstractBuild build) {
if (entry.isDeleteRemoteArtifactsByScript()) {
//Inject list artifacts as variable
Binding binding = new Binding();
if (deployedArtifacts != null) {
if (artifactDeployerBuildAction != null) {
List<ArtifactDeployerVO> listArtifacts = info.get(entry.getUniqueId());
binding.setVariable("ARTIFACTS", listArtifacts);
}
Expand Down
Expand Up @@ -4,54 +4,38 @@
import hudson.model.Action;
import hudson.model.Run;

import java.util.List;

/**
* @author Gregory Boissinot
*/
public class ArtifactDeployerProjectAction implements Action {

private static final String URL_NAME = "lastSuccessfulBuild//deployedArtifacts";

private final AbstractProject<?, ?> project;

public ArtifactDeployerProjectAction(AbstractProject<?, ?> project) {
this.project = project;
}

private Run getLastSuccessfulBuild() {
return project.getLastSuccessfulBuild();
public String getIconFileName() {
return "package.gif";
}

@SuppressWarnings("unused")
public DeployedArtifacts getLatestDeployedArtifacts() {
Run latestSuccessfulBuild = getLastSuccessfulBuild();
if (latestSuccessfulBuild == null) {
return null;
}
List<DeployedArtifacts> actions = latestSuccessfulBuild.getActions(DeployedArtifacts.class);
if (actions == null || actions.size() == 0) {
return null;
}
return actions.get(actions.size() - 1);
public String getDisplayName() {
return "Last Successful Deployed Artifacts";
}

public String getUrlName() {
return URL_NAME;
}

@SuppressWarnings("unused")
public int getLastSuccessfulNumber() {
Run latestSuccessfulBuild = getLastSuccessfulBuild();
Run latestSuccessfulBuild = project.getLastSuccessfulBuild();
if (latestSuccessfulBuild == null) {
return 0;
}
return latestSuccessfulBuild.getNumber();
}

public String getIconFileName() {
return null;
}

public String getDisplayName() {
return null;
}

public String getUrlName() {
return null;
}
}
Expand Up @@ -100,10 +100,10 @@ private boolean _perform(hudson.model.AbstractBuild<?, ?> build, hudson.Launcher

listener.getLogger().println("[ArtifactDeployer] - Starting deployment from the post-action ...");
DeployedArtifactsActionManager deployedArtifactsService = DeployedArtifactsActionManager.getInstance();
DeployedArtifacts deployedArtifactsAction = deployedArtifactsService.getOrCreateAction(build);
ArtifactDeployerBuildAction artifactDeployerBuildActionAction = deployedArtifactsService.getOrCreateAction(build);
Map<Integer, List<ArtifactDeployerVO>> deployedArtifacts;
try {
int currentTotalDeployedCounter = deployedArtifactsAction.getDeployedArtifactsInfo().size();
int currentTotalDeployedCounter = artifactDeployerBuildActionAction.getDeployedArtifactsInfo().size();
deployedArtifacts = processDeployment(build, listener, currentTotalDeployedCounter);
} catch (ArtifactDeployerException ae) {
listener.getLogger().println("[ArtifactDeployer] - [ERROR] - Failed to deploy. " + ae.getMessage());
Expand All @@ -114,7 +114,7 @@ private boolean _perform(hudson.model.AbstractBuild<?, ?> build, hudson.Launcher
return false;
}

deployedArtifactsAction.addDeployedArtifacts(deployedArtifacts);
artifactDeployerBuildActionAction.setArtifactsInfo(build, deployedArtifacts);
listener.getLogger().println("[ArtifactDeployer] - Stopping deployment from the post-action...");
}
return true;
Expand Down Expand Up @@ -242,7 +242,6 @@ public static final class DeleteRemoteArtifact extends RunListener<AbstractBuild

private final Logger logger = Logger.getLogger(DeleteRemoteArtifact.class.getName());


@Override
public void onDeleted(AbstractBuild build) {

Expand All @@ -262,9 +261,9 @@ public void onDeleted(AbstractBuild build) {
}

if (artifactDeployerPublisher != null) {
DeployedArtifacts deployedArtifacts = build.getAction(DeployedArtifacts.class);
if (deployedArtifacts != null) {
Map<Integer, List<ArtifactDeployerVO>> info = deployedArtifacts.getDeployedArtifactsInfo();
ArtifactDeployerBuildAction artifactDeployerBuildAction = build.getAction(ArtifactDeployerBuildAction.class);
if (artifactDeployerBuildAction != null) {
Map<Integer, List<ArtifactDeployerVO>> info = artifactDeployerBuildAction.getDeployedArtifactsInfo();
if (info != null) {
for (ArtifactDeployerEntry entry : artifactDeployerPublisher.getEntries()) {
//Delete output
Expand Down Expand Up @@ -302,7 +301,7 @@ public void onDeleted(AbstractBuild build) {
if (entry.isDeleteRemoteArtifactsByScript()) {
//Inject list artifacts as variable
Binding binding = new Binding();
if (deployedArtifacts != null) {
if (artifactDeployerBuildAction != null) {
List<ArtifactDeployerVO> listArtifacts = info.get(entry.getUniqueId());
binding.setVariable("ARTIFACTS", listArtifacts);
}
Expand Down Expand Up @@ -334,48 +333,6 @@ public String getDisplayName() {
return DISPLAY_NAME;
}


// private ArtifactDeployerEntry populateAndGetEntry(JSONObject element) {
// ArtifactDeployerEntry entry = new ArtifactDeployerEntry();
// entry.setIncludes(Util.fixEmpty(element.getString("includes")));
// entry.setBasedir(Util.fixEmpty(element.getString("basedir")));
// entry.setExcludes(Util.fixEmpty(element.getString("excludes")));
// entry.setRemote(Util.fixEmpty(element.getString("remote")));
// entry.setDeleteRemote(element.getBoolean("deleteRemote"));
// entry.setFlatten(element.getBoolean("flatten"));
// entry.setFailNoFilesDeploy(element.getBoolean("failNoFilesDeploy"));
// entry.setDeleteRemoteArtifacts(element.getBoolean("deleteRemoteArtifacts"));
// Object deleteRemoteArtifactsObject = element.get("deleteRemoteArtifactsByScript");
// if (deleteRemoteArtifactsObject == null) {
// entry.setDeleteRemoteArtifactsByScript(false);
// } else {
// entry.setDeleteRemoteArtifactsByScript(true);
// entry.setGroovyExpression(Util.fixEmpty(element.getJSONObject("deleteRemoteArtifactsByScript").getString("groovyExpression")));
// }
// return entry;
// }

// @Override
// public Publisher newInstance(StaplerRequest req, JSONObject formData) throws FormException {
// ArtifactDeployerPublisher pub = new ArtifactDeployerPublisher();
// List<ArtifactDeployerEntry> artifactDeployerEntries = new ArrayList<ArtifactDeployerEntry>();
// Object entries = formData.get("deployedArtifact");
// if (entries != null) {
// if (entries instanceof JSONObject) {
// artifactDeployerEntries.add(populateAndGetEntry((JSONObject) entries));
// } else {
// JSONArray jsonArray = (JSONArray) entries;
// Iterator it = jsonArray.iterator();
// while (it.hasNext()) {
// artifactDeployerEntries.add(populateAndGetEntry((JSONObject) it.next()));
// }
// }
// }
// pub.setEntries(artifactDeployerEntries);
// pub.setDeployEvenBuildFail(formData.getBoolean("deployEvenBuildFail"));
// return pub;
// }

public FormValidation doCheckIncludes(@AncestorInPath AbstractProject project, @QueryParameter String value) throws IOException {
return FilePath.validateFileMask(project.getSomeWorkspace(), value);
}
Expand All @@ -393,6 +350,6 @@ public FormValidation doCheckRemote(@QueryParameter String value) throws IOExcep
}
return FormValidation.ok();
}

}

}
@@ -1,35 +1,18 @@
package org.jenkinsci.plugins.artifactdeployer;

import hudson.model.Action;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Gregory Boissinot
*/
public class DeployedArtifacts implements Action {

private static final String URL_NAME = "deployedArtifacts";

@SuppressWarnings("unused")
@Deprecated
/**
* Note: there is no backward compatibility for this field
* (can't delete deployed artifacts performed with artifactdeployer plugin previous v0.3)
*/
private transient List<ArtifactDeployerVO> deployedArtifacts = new LinkedList<ArtifactDeployerVO>();

private Map<Integer, List<ArtifactDeployerVO>> deployedArtifactsInfo = new HashMap<Integer, List<ArtifactDeployerVO>>();

public void addDeployedArtifacts(Map<Integer, List<ArtifactDeployerVO>> deployedArtifactsInfo) {
this.deployedArtifactsInfo.putAll(deployedArtifactsInfo);
}

public String getIconFileName() {
return null;
}
Expand All @@ -39,69 +22,19 @@ public String getDisplayName() {
}

public String getUrlName() {
return URL_NAME;
}

public Map<Integer, List<ArtifactDeployerVO>> getDeployedArtifactsInfo() {
return this.deployedArtifactsInfo;
}

@SuppressWarnings("unused")
public Collection<ArtifactDeployerVO> getAllArtifacts() {

Comparator<ArtifactDeployerVO> comparator = new Comparator<ArtifactDeployerVO>() {
public int compare(ArtifactDeployerVO artifactDeployer1, ArtifactDeployerVO artifactDeployer2) {
if (artifactDeployer1 == null || artifactDeployer2 == null) {
return 0;
}

String remotePath1 = artifactDeployer1.getRemotePath();
String remotePath2 = artifactDeployer2.getRemotePath();
if (remotePath1 == null || remotePath2 == null) {
return 0;
}
return remotePath1.compareTo(remotePath2);
}
};

SortedSet<ArtifactDeployerVO> result = new TreeSet<ArtifactDeployerVO>(comparator);
if (this.deployedArtifactsInfo != null) {
for (List<ArtifactDeployerVO> list : this.deployedArtifactsInfo.values()) {
result.addAll(list);
}
}
return result;
}


private ArtifactDeployerVO getArtifactDeployerVO(int id) {
for (List<ArtifactDeployerVO> entryList : this.deployedArtifactsInfo.values()) {
for (ArtifactDeployerVO entry : entryList) {
if (entry.getId() == id) {
return entry;
}
}
}
return null;
}

@SuppressWarnings("unused")
public void doDownload(final StaplerRequest request, final StaplerResponse response) throws IOException, ServletException {

String restOfPath = request.getRestOfPath();
if (restOfPath == null) {
return;
}

final String artifactPattenLink = "/artifact.";
if (restOfPath.startsWith(artifactPattenLink)) {
int id = Integer.parseInt(restOfPath.split(artifactPattenLink)[1]);
ArtifactDeployerVO selectedArtifact = getArtifactDeployerVO(id);
if (selectedArtifact != null) {
response.setHeader("Content-Disposition", "attachment;filename=\"" + selectedArtifact.getFileName() + "\"");
response.serveFile(request, new File(selectedArtifact.getRemotePath()).toURI().toURL());
}
}
public Map<Integer, List<ArtifactDeployerVO>> getDeployedArtifactsInfo() {
return deployedArtifactsInfo;
}

// private Object readResolve() throws ObjectStreamException {
// if (deployedArtifactsInfo != null) {
// final ArtifactDeployerBuildAction artifactDeployerBuildAction = new ArtifactDeployerBuildAction();
// artifactDeployerBuildAction.setArtifactsInfo(null, deployedArtifactsInfo);
// return artifactDeployerBuildAction;
// }
// return this;
// }
}

1 comment on commit fc46701

@hashar
Copy link

@hashar hashar commented on fc46701 Aug 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonjour,

It looks like this broke lazy-loading of projects/build history: JENKINS-24140 to 'ArtifactDeployer migration breaks lazy-load on Jenkins initialization' https://issues.jenkins-ci.org/browse/JENKINS-24140

Please sign in to comment.