Skip to content

Commit

Permalink
Merge pull request #97 from juliantcook/master
Browse files Browse the repository at this point in the history
JENKINS-40448: add to existing actions for a run
  • Loading branch information
Jimilian committed Jan 11, 2017
2 parents 5d17f47 + cd64fd8 commit e7cc2e4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/s3/S3ArtifactsAction.java
Expand Up @@ -109,4 +109,4 @@ private String getDownloadURL(AmazonS3Client client, int signedUrlExpirySeconds,

return client.generatePresignedUrl(request).toExternalForm();
}
}
}
22 changes: 20 additions & 2 deletions src/main/java/hudson/plugins/s3/S3BucketPublisher.java
Expand Up @@ -267,15 +267,33 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @Nonnull Launc

// don't bother adding actions if none of the artifacts are managed
if (!artifacts.isEmpty()) {
run.addAction(new S3ArtifactsAction(run, profile, artifacts));
run.addAction(new FingerprintAction(run, record));
addS3ArtifactsAction(run, profile, artifacts);
addFingerprintAction(run, record);
}
} catch (AmazonClientException|IOException e) {
e.printStackTrace(listener.error("Failed to upload files"));
run.setResult(constrainResult(Result.UNSTABLE, listener));
}
}

private void addS3ArtifactsAction(Run<?, ?> run, S3Profile profile, List<FingerprintRecord> artifacts) {
S3ArtifactsAction existingAction = run.getAction(S3ArtifactsAction.class);
if (existingAction != null) {
existingAction.getArtifacts().addAll(artifacts);
} else {
run.addAction(new S3ArtifactsAction(run, profile, artifacts));
}
}

private void addFingerprintAction(Run<?, ?> run, Map<String, String> record) {
FingerprintAction existingAction = run.getAction(FingerprintAction.class);
if (existingAction != null) {
existingAction.add(record);
} else {
run.addAction(new FingerprintAction(run, record));
}
}

private void printDiagnostics(@Nonnull FilePath ws, PrintStream console, String expanded) throws IOException {
log(Level.WARNING, console, "No file(s) found: " + expanded);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/s3/S3Profile.java
Expand Up @@ -100,7 +100,7 @@ public final int getUploadRetryTime() {
return uploadRetryTime;
}

public final String getName() {
public /*final*/ String getName() {
return this.name;
}

Expand Down
93 changes: 85 additions & 8 deletions src/test/java/hudson/plugins/s3/S3Test.java
Expand Up @@ -3,14 +3,29 @@

import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.Action;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Run;
import hudson.plugins.s3.S3BucketPublisher.DescriptorImpl;
import hudson.tasks.Builder;
import hudson.tasks.Fingerprinter.FingerprintAction;
import hudson.tasks.Shell;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.toArray;
import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertEquals;

public class S3Test {
@Rule
public JenkinsRule j = new JenkinsRule();
Expand All @@ -23,17 +38,79 @@ public void testConfigExists() throws Exception {

@Test
public void testConfigContainsProfiles() throws Exception {
final Jenkins instance = Jenkins.getInstance();
final S3BucketPublisher.DescriptorImpl s3Plugin = (S3BucketPublisher.DescriptorImpl)
instance.getDescriptor( S3BucketPublisher.class );

final S3Profile profile = new S3Profile("S3 profile random name", null, null, true, 0, "0", "0", "0", "0", true);
final List<S3Profile> profileList = new ArrayList<>();
profileList.add(profile);

s3Plugin.replaceProfiles(profileList);
replaceS3PluginProfile(profile);

HtmlPage page = j.createWebClient().goTo("configure");
WebAssert.assertTextPresent(page, "S3 profile random name");
}

@Test
public void multiplePublishersUseExistingActions() throws Exception {
String profileName = "test profile";
String fileName = "testFile";
S3BucketPublisher publisher = new S3BucketPublisher(
profileName,
newArrayList(entryForFile(fileName)),
Collections.<MetadataPair>emptyList(),
true,
"INFO",
"SUCCESS"
);
replaceS3PluginProfile(mockS3Profile(profileName));

final FreeStyleProject project = j.createFreeStyleProject("testing");
project.getBuildersList().add(stepCreatingFile(fileName));

project.getPublishersList().add(publisher);
project.getPublishersList().add(publisher);

final FreeStyleBuild build = j.buildAndAssertSuccess(project);
assertEquals(1, countActionsOfType(build, S3ArtifactsAction.class));
assertEquals(1, countActionsOfType(build, FingerprintAction.class));
}

private Entry entryForFile(String fileName) {
return new Entry("bucket", fileName, "", "", "", false, false, true, false, false, false, false, false, null);
}

private Builder stepCreatingFile(String fileName) {
return new Shell("touch " + fileName);
}

private void replaceS3PluginProfile(S3Profile s3Profile) {
final Jenkins instance = Jenkins.getInstance();
final DescriptorImpl s3Plugin = (DescriptorImpl) instance.getDescriptor(S3BucketPublisher.class);
s3Plugin.replaceProfiles(newArrayList(s3Profile));
}

private S3Profile mockS3Profile(String profileName) throws IOException, InterruptedException {
S3Profile profile = Mockito.mock(S3Profile.class);
Mockito.when(profile.getName()).thenReturn(profileName);
Mockito.when(profile.isKeepStructure()).thenReturn(true);
Mockito.when(profile.upload(
Mockito.any(Run.class),
Mockito.anyString(),
Mockito.anyList(),
Mockito.anyList(),
Mockito.anyMap(),
Mockito.anyString(),
Mockito.anyString(),
Mockito.anyBoolean(),
Mockito.anyBoolean(),
Mockito.anyBoolean(),
Mockito.anyBoolean()
)).thenReturn(newArrayList(new FingerprintRecord(true, "bucket", "path", "eu-west-1", "xxxx")));
return profile;
}

private int countActionsOfType(Run<?, ?> run, Class<?> actionClass) {
return getAllActionsOfType(run, actionClass).length;
}

private <T> T[] getAllActionsOfType(Run<?, ?> run, Class<T> actionClass) {
List<? extends Action> actions = run.getAllActions();
return toArray(filter(actions, actionClass), actionClass);
}
}

0 comments on commit e7cc2e4

Please sign in to comment.