Skip to content

Commit

Permalink
[JENKINS-24141] Switch to using RunWithSCM for getCulprits logic
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed May 4, 2017
1 parent 1f6a427 commit 6cce465
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
29 changes: 19 additions & 10 deletions pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.23</version>
<version>2.27</version>
<relativePath />
</parent>

Expand All @@ -30,9 +30,12 @@
<properties>
<powermock.version>1.6.4</powermock.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<workflow.version>1.10</workflow.version>
<jenkins.version>2.0</jenkins.version>
<java.level>7</java.level>
<workflow-step-api.version>2.9</workflow-step-api.version>
<workflow-aggregator.version>2.5</workflow-aggregator.version>
<workflow-job.version>2.11-20170504.162002-1</workflow-job.version> <!-- TODO: switch to release once https://github.com/jenkinsci/workflow-job-plugin/pull/35 is merged and released -->
<jenkins-core.version>2.58-20170502.192524-8</jenkins-core.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/jenkins/pull/2730 is merged and released -->
<jenkins-war.version>2.58-20170502.192544-8</jenkins-war.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/jenkins/pull/2730 is merged and released -->
<java.level>8</java.level>
<concurrency>2</concurrency>
<argLine />
</properties>
Expand Down Expand Up @@ -154,27 +157,27 @@
<!-- workflow stuff -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<version>${workflow.version}</version>
<version>${workflow-step-api.version}</version>
<artifactId>workflow-step-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<version>${workflow-job.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<version>${workflow-aggregator.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- StepConfigTester -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<classifier>tests</classifier>
<version>${workflow.version}</version>
<version>${workflow-step-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -218,6 +221,12 @@
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
Expand All @@ -228,14 +237,14 @@
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
<version>3.20.0-GA</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-war</artifactId>
<type>war</type>
<version>${jenkins.version}</version>
<version>${jenkins-war.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -8,7 +8,6 @@

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.User;
Expand All @@ -17,6 +16,7 @@
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.RecipientProviderDescriptor;
import jenkins.model.Jenkins;
import jenkins.scm.RunWithSCM;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.mail.internet.InternetAddress;
Expand Down Expand Up @@ -51,8 +51,8 @@ public void send(final String format, final Object... args) {
}
final Debug debug = new Debug();
Run<?,?> run = context.getRun();
if (run instanceof AbstractBuild) {
Set<User> users = ((AbstractBuild<?,?>)run).getCulprits();
if (run instanceof RunWithSCM) {
Set<User> users = ((RunWithSCM<?,?>)run).getCulprits();
RecipientProviderUtilities.addUsers(users, context, env, to, cc, bcc, debug);
} else {
List<Run<?, ?>> builds = new ArrayList<>();
Expand Down
Expand Up @@ -26,7 +26,6 @@
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.Item;
import hudson.model.Run;
Expand All @@ -48,6 +47,7 @@
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import jenkins.scm.RunWithSCM;
import org.acegisecurity.Authentication;
import org.acegisecurity.userdetails.UsernameNotFoundException;

Expand All @@ -66,11 +66,8 @@ public static Set<User> getChangeSetAuthors(final Collection<Run<?, ?>> runs, fi
final Set<User> users = new HashSet<>();
for (final Run<?, ?> run : runs) {
debug.send(" build: %d", run.getNumber());
if (run instanceof AbstractBuild<?,?>) {
final ChangeLogSet<?> changeLogSet = ((AbstractBuild<?,?>)run).getChangeSet();
if (changeLogSet == null) {
debug.send(" changeLogSet was null");
} else {
if (run instanceof RunWithSCM<?,?>) {
for (ChangeLogSet<? extends ChangeLogSet.Entry> changeLogSet : ((RunWithSCM<?,?>)run).getChangeSets()) {
addChangeSetUsers(changeLogSet, users, debug);
}
} else {
Expand Down
@@ -1,11 +1,13 @@
package hudson.plugins.emailext.plugins.recipients;

import hudson.model.FreeStyleBuild;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.User;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.tasks.Mailer;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -22,7 +24,9 @@
Mailer.class,
Mailer.DescriptorImpl.class,
User.class,
WorkflowRun.class
WorkflowRun.class,
WorkflowJob.class,
Job.class
})
public class CulpritsRecipientProviderTest {

Expand All @@ -46,38 +50,42 @@ public void before() throws Exception {

@Test
public void testAddRecipients1() throws Exception {
final WorkflowRun build1 = PowerMockito.mock(WorkflowRun.class);
final WorkflowJob j = PowerMockito.mock(WorkflowJob.class);
final WorkflowRun build1 = PowerMockito.spy(new WorkflowRun(j));
PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE);
MockUtilities.addChangeSet(build1, "X", "V");
PowerMockito.doReturn(null).when(build1).getPreviousBuild();

final WorkflowRun build2 = PowerMockito.mock(WorkflowRun.class);
final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j));
PowerMockito.when(build2.getResult()).thenReturn(Result.SUCCESS);
MockUtilities.addChangeSet(build2, "Z", "V");
PowerMockito.when(build2.getPreviousCompletedBuild()).thenReturn(build1);
PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild();

final WorkflowRun build3 = PowerMockito.mock(WorkflowRun.class);
final WorkflowRun build3 = PowerMockito.spy(new WorkflowRun(j));
PowerMockito.when(build3.getResult()).thenReturn(Result.UNSTABLE);
MockUtilities.addChangeSet(build3, "A");
PowerMockito.when(build3.getPreviousCompletedBuild()).thenReturn(build2);
PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild();

final WorkflowRun build4 = PowerMockito.mock(WorkflowRun.class);
final WorkflowRun build4 = PowerMockito.spy(new WorkflowRun(j));
PowerMockito.when(build4.getResult()).thenReturn(Result.UNSTABLE);
MockUtilities.addChangeSet(build4, "B");
PowerMockito.when(build4.getPreviousCompletedBuild()).thenReturn(build3);
PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild();

TestUtilities.checkRecipients(build4, new CulpritsRecipientProvider(), "A", "B");
}

@Test
public void testAddRecipients2() throws Exception {
final WorkflowRun build1 = PowerMockito.mock(WorkflowRun.class);
final WorkflowJob j = PowerMockito.mock(WorkflowJob.class);
final WorkflowRun build1 = PowerMockito.spy(new WorkflowRun(j));
PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE);
MockUtilities.addChangeSet(build1, "X", "V");
PowerMockito.doReturn(null).when(build1).getPreviousBuild();

final WorkflowRun build2 = PowerMockito.mock(WorkflowRun.class);
final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j));
PowerMockito.when(build2.getResult()).thenReturn(Result.SUCCESS);
MockUtilities.addChangeSet(build2, "Z", "V");
PowerMockito.when(build2.getPreviousCompletedBuild()).thenReturn(build1);
PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild();

TestUtilities.checkRecipients(build2, new CulpritsRecipientProvider(), "X", "V", "Z");
}
Expand Down

0 comments on commit 6cce465

Please sign in to comment.