Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #18 from abayer/jenkins-27206
[JENKINS-27206] - Move claim-plugin to use SimpleBuildStep
  • Loading branch information
ki82 committed Jan 14, 2016
2 parents 109ee7f + aec4674 commit 7e091ac
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.577</version>
<version>1.596.1</version>
</parent>

<artifactId>claim</artifactId>
Expand Down Expand Up @@ -68,5 +68,17 @@
<version>1.13.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.modules</groupId>
<artifactId>sshd</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
22 changes: 16 additions & 6 deletions src/main/java/hudson/plugins/claim/ClaimPublisher.java
@@ -1,21 +1,25 @@
package hudson.plugins.claim;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;

import java.io.IOException;

import jenkins.tasks.SimpleBuildStep;
import org.kohsuke.stapler.DataBoundConstructor;

public class ClaimPublisher extends Notifier {
public class ClaimPublisher extends Notifier implements SimpleBuildStep {

@DataBoundConstructor
public ClaimPublisher() {
Expand All @@ -33,7 +37,7 @@ public String getDisplayName() {
return Messages.ClaimPublisher_DisplayName();
}

public boolean isApplicable(Class<? extends AbstractProject> jobType) {
public boolean isApplicable(Class jobType) {
return true;
}

Expand All @@ -46,15 +50,22 @@ public DescriptorImpl getDescriptor() {

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
BuildListener listener) throws InterruptedException, IOException {
perform(build, build.getWorkspace(), launcher, listener);
return true;
}

@Override
public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {

if (build.getResult().isWorseThan(Result.SUCCESS)) {
ClaimBuildAction action = new ClaimBuildAction(build);
build.addAction(action);
build.save();

// check if previous build was claimed
AbstractBuild<?,?> previousBuild = build.getPreviousBuild();
Run<?,?> previousBuild = build.getPreviousBuild();
if (previousBuild != null) {
ClaimBuildAction c = previousBuild.getAction(ClaimBuildAction.class);
if (c != null && c.isClaimed() && c.isSticky()) {
Expand All @@ -63,11 +74,10 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
}
}

return true;
}

public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.STEP;
return BuildStepMonitor.NONE;
}

}
Expand Up @@ -24,22 +24,22 @@
alt="${b.iconColor.description}"/>
</a>
</td>
<td id="claim.build.${b.project.name}">
<a href="${rootURL}/${b.project.url}">${b.project.fullName}</a>
<td id="claim.build.${b.parent.name}">
<a href="${rootURL}/${b.parent.url}">${b.parent.fullName}</a>
<st:nbsp/>
<a href="${rootURL}/${b.url}">#${b.number}</a>
</td>
<td id="claim.date.${b.project.name}" data="${b.timestampString2}">
<td id="claim.date.${b.parent.name}" data="${b.timestampString2}">
${b.timestampString}
</td>
<j:set var="firstBad" value="${it.getFirstFail(b)}"/>
<td id="claim.firstBad.${b.project.name}" data="${firstBad.timestampString2}">
<td id="claim.firstBad.${b.parent.name}" data="${firstBad.timestampString2}">
${firstBad.timestampString}
</td>
<td id="claim.claimant.${b.project.name}">
<td id="claim.claimant.${b.parent.name}">
${it.getClaimantText(b)}
</td>
<td id="claim.desc.${b.project.name}">
<td id="claim.desc.${b.parent.name}">
${b.description}
</td>
</tr>
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/hudson/plugins/claim/ClaimWorkflowTest.java
@@ -0,0 +1,80 @@
package hudson.plugins.claim;

import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ListView;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class ClaimWorkflowTest {

@Rule
public JenkinsRule j = new JenkinsRule();

private static final String JOB_NAME = "myjob";
private WorkflowJob job;
private ListView view;

@Before
public void setUp() throws Exception {

java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.FINE);

job = createFailingJobWithName(JOB_NAME);

view = new ListView("DefaultView");
j.jenkins.addView(view);
j.jenkins.setPrimaryView(view);

}

private WorkflowJob createFailingJobWithName(String jobName) throws IOException,
InterruptedException, ExecutionException {
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, jobName);
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " catchError {\n"
+ " error('Error')\n"
+ " }\n"
+ " step([$class: 'ClaimPublisher'])\n"
+ "}"));
job.scheduleBuild2(0).get();
return job;
}

@Test
public void job_is_visible_in_claim_report() throws Exception {
// Given:
view.add(job);
//j.interactiveBreak();
// When:
HtmlPage page = j.createWebClient().goTo("claims/");
// Then:
HtmlElement element = page.getElementById("claim.build." + JOB_NAME);
assertThat(element.isDisplayed(), is(true));
}

@Test
public void job_not_present_in_default_view_is_visible_in_claim_report() throws Exception {
// When:
HtmlPage page = j.createWebClient().goTo("claims/");
// Then:
HtmlElement element = page.getElementById("claim.build." + JOB_NAME);
assertThat(element.isDisplayed(), is(true));
}

}

0 comments on commit 7e091ac

Please sign in to comment.