From 0f1eeb3b275e62fd150ccd36099fb1861469c058 Mon Sep 17 00:00:00 2001 From: Pawel Kozikowski Date: Tue, 10 Jan 2017 16:22:21 +0100 Subject: [PATCH] Add configurable JIRA_ISSUES_SIZE to the JiraEnvironmentVariableBuilder --- .../JiraEnvironmentContributingAction.java | 23 +++++++++++++++---- .../jira/JiraEnvironmentVariableBuilder.java | 20 +++++++++++++--- .../config.jelly | 7 +++++- .../JiraEnvironmentVariableBuilder/help.html | 1 + ...JiraEnvironmentContributingActionTest.java | 21 ++++++++++------- .../JiraEnvironmentVariableBuilderTest.java | 17 ++++++++++---- 6 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/main/java/hudson/plugins/jira/JiraEnvironmentContributingAction.java b/src/main/java/hudson/plugins/jira/JiraEnvironmentContributingAction.java index 45d656911..eb442026f 100644 --- a/src/main/java/hudson/plugins/jira/JiraEnvironmentContributingAction.java +++ b/src/main/java/hudson/plugins/jira/JiraEnvironmentContributingAction.java @@ -15,26 +15,41 @@ public class JiraEnvironmentContributingAction extends InvisibleAction implement public static final String JIRA_URL_VARIABLE_NAME = "JIRA_URL"; private final String issuesList; - + + private final Integer issuesSize; + private final String jiraUrl; - + + private final String issuesSizeVariableName; + public String getIssuesList(){ return issuesList; } - + + public Integer getNumberOfIssues(){ + return issuesSize; + } + public String getJiraUrl() { return jiraUrl; } + + public String getIssuesSizeVariableName() { + return issuesSizeVariableName; + } - public JiraEnvironmentContributingAction(String issuesList, String jiraUrl) { + public JiraEnvironmentContributingAction(String issuesList, int issuesSize, String jiraUrl, String issuesSizeVariableName) { this.issuesList = issuesList; + this.issuesSize = issuesSize; this.jiraUrl = jiraUrl; + this.issuesSizeVariableName = issuesSizeVariableName; } @Override public void buildEnvVars(AbstractBuild ab, EnvVars ev) { if (ev != null){ ev.put(ISSUES_VARIABLE_NAME, issuesList); + ev.put(getIssuesSizeVariableName(), getNumberOfIssues().toString()); ev.put(JIRA_URL_VARIABLE_NAME, getJiraUrl()); } } diff --git a/src/main/java/hudson/plugins/jira/JiraEnvironmentVariableBuilder.java b/src/main/java/hudson/plugins/jira/JiraEnvironmentVariableBuilder.java index d5ee79afc..267129b67 100644 --- a/src/main/java/hudson/plugins/jira/JiraEnvironmentVariableBuilder.java +++ b/src/main/java/hudson/plugins/jira/JiraEnvironmentVariableBuilder.java @@ -11,6 +11,8 @@ import hudson.tasks.BuildStepDescriptor; import hudson.tasks.Builder; import java.io.IOException; + +import jenkins.model.Jenkins; import org.kohsuke.stapler.DataBoundConstructor; import java.util.Set; import org.apache.commons.lang.StringUtils; @@ -21,10 +23,12 @@ public class JiraEnvironmentVariableBuilder extends Builder { private AbstractIssueSelector issueSelector; + private String issuesSizeVariableName; @DataBoundConstructor - public JiraEnvironmentVariableBuilder(AbstractIssueSelector issueSelector) { + public JiraEnvironmentVariableBuilder(AbstractIssueSelector issueSelector, String issuesSizeVariableName) { this.issueSelector = issueSelector; + this.issuesSizeVariableName = issuesSizeVariableName; } public AbstractIssueSelector getIssueSelector() { @@ -32,7 +36,11 @@ public AbstractIssueSelector getIssueSelector() { if (uis == null) uis = new DefaultIssueSelector(); return (this.issueSelector = uis); } - + + String getIssuesSizeVariableName() { + return issuesSizeVariableName; + } + JiraSite getSiteForProject(AbstractProject project) { return JiraSite.get(project); } @@ -49,10 +57,12 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen Set ids = getIssueSelector().findIssueIds(build, site, listener); String idList = StringUtils.join(ids, ","); + Integer idListSize = ids != null ? ids.size() : null; listener.getLogger().println(Messages.JiraEnvironmentVariableBuilder_Updating(JiraEnvironmentContributingAction.ISSUES_VARIABLE_NAME, idList)); + listener.getLogger().println(Messages.JiraEnvironmentVariableBuilder_Updating(getIssuesSizeVariableName(), idListSize)); - build.addAction(new JiraEnvironmentContributingAction(idList, site.getName())); + build.addAction(new JiraEnvironmentContributingAction(idList, idListSize, site.getName(), getIssuesSizeVariableName())); return true; } @@ -72,5 +82,9 @@ public boolean isApplicable(Class klass) { public String getDisplayName() { return Messages.JiraEnvironmentVariableBuilder_DisplayName(); } + + public boolean hasIssueSelectors() { + return Jenkins.getActiveInstance().getDescriptorList(AbstractIssueSelector.class).size() > 1; + } } } diff --git a/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/config.jelly b/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/config.jelly index 9ddaf030a..14246d013 100644 --- a/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/config.jelly +++ b/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/config.jelly @@ -1,5 +1,10 @@ - + + + + + + \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/help.html b/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/help.html index f38670f4c..85db86619 100644 --- a/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/help.html +++ b/src/main/resources/hudson/plugins/jira/JiraEnvironmentVariableBuilder/help.html @@ -3,6 +3,7 @@
Available variables:

diff --git a/src/test/java/hudson/plugins/jira/JiraEnvironmentContributingActionTest.java b/src/test/java/hudson/plugins/jira/JiraEnvironmentContributingActionTest.java index a1081530d..287a293c6 100644 --- a/src/test/java/hudson/plugins/jira/JiraEnvironmentContributingActionTest.java +++ b/src/test/java/hudson/plugins/jira/JiraEnvironmentContributingActionTest.java @@ -13,12 +13,14 @@ public class JiraEnvironmentContributingActionTest { private static final String JIRA_URL = "http://example.com"; private static final String JIRA_URL_PROPERTY_NAME = "JIRA_URL"; - private static final String ISSUES_PROPERTY_NAME = "JIRA_ISSUES"; - private static final String ISSUES_LIST = "ISS-1,ISS-2"; - + private static final String ISSUES_PROPERTY_NAME = "JIRA_ISSUES"; + private static final String ISSUES_SIZE_PROPERTY_NAME = "JIRA_ISSUES_SIZE"; + private static final String ISSUES_LIST = "ISS-1,ISS-2"; + private static final Integer ISSUES_SIZE = 2; + @Test public void buildEnvVarsEnvIsNull() { - JiraEnvironmentContributingAction action = new JiraEnvironmentContributingAction(ISSUES_LIST, JIRA_URL); + JiraEnvironmentContributingAction action = new JiraEnvironmentContributingAction(ISSUES_LIST, ISSUES_SIZE, JIRA_URL, ISSUES_SIZE_PROPERTY_NAME); AbstractBuild build = mock(AbstractBuild.class); action.buildEnvVars(build, null); @@ -27,7 +29,7 @@ public void buildEnvVarsEnvIsNull() { @Test public void buildEnvVarsAddVariables() { - JiraEnvironmentContributingAction action = new JiraEnvironmentContributingAction(ISSUES_LIST, JIRA_URL); + JiraEnvironmentContributingAction action = new JiraEnvironmentContributingAction(ISSUES_LIST, ISSUES_SIZE, JIRA_URL, ISSUES_SIZE_PROPERTY_NAME); AbstractBuild build = mock(AbstractBuild.class); EnvVars envVars = mock(EnvVars.class); @@ -35,12 +37,15 @@ public void buildEnvVarsAddVariables() { ArgumentCaptor keys = ArgumentCaptor.forClass(String.class); ArgumentCaptor values = ArgumentCaptor.forClass(String.class); - verify(envVars, times(2)).put(keys.capture(), values.capture()); + verify(envVars, times(3)).put(keys.capture(), values.capture()); assertThat(keys.getAllValues().get(0), is(ISSUES_PROPERTY_NAME)); assertThat(values.getAllValues().get(0), is(ISSUES_LIST)); - assertThat(keys.getAllValues().get(1), is(JIRA_URL_PROPERTY_NAME)); - assertThat(values.getAllValues().get(1), is(JIRA_URL)); + assertThat(keys.getAllValues().get(1), is(ISSUES_SIZE_PROPERTY_NAME)); + assertThat(values.getAllValues().get(1), is(ISSUES_SIZE.toString())); + + assertThat(keys.getAllValues().get(2), is(JIRA_URL_PROPERTY_NAME)); + assertThat(values.getAllValues().get(2), is(JIRA_URL)); } } diff --git a/src/test/java/hudson/plugins/jira/JiraEnvironmentVariableBuilderTest.java b/src/test/java/hudson/plugins/jira/JiraEnvironmentVariableBuilderTest.java index 0a0363ba2..9f9c03a32 100644 --- a/src/test/java/hudson/plugins/jira/JiraEnvironmentVariableBuilderTest.java +++ b/src/test/java/hudson/plugins/jira/JiraEnvironmentVariableBuilderTest.java @@ -35,7 +35,8 @@ public class JiraEnvironmentVariableBuilderTest { private static final String JIRA_URL = "http://example.com"; private static final String JIRA_URL_PROPERTY_NAME = "JIRA_URL"; - private static final String ISSUES_PROPERTY_NAME = "JIRA_ISSUES"; + private static final String ISSUES_PROPERTY_NAME = "JIRA_ISSUES"; + private static final String ISSUES_SIZE_PROPERTY_NAME = "JIRA_ISSUES_SIZE"; private static final String ISSUE_ID_1 = "ISS-1"; private static final String ISSUE_ID_2 = "ISS-2"; @@ -79,26 +80,32 @@ public void createMocks() throws IOException, InterruptedException { @Test public void testIssueSelectorDefaultsToDefault() { - final JiraEnvironmentVariableBuilder builder = new JiraEnvironmentVariableBuilder(null); + final JiraEnvironmentVariableBuilder builder = new JiraEnvironmentVariableBuilder(null, null); assertThat(builder.getIssueSelector(), instanceOf(DefaultIssueSelector.class)); } @Test public void testSetIssueSelectorPersists() { - final JiraEnvironmentVariableBuilder builder = new JiraEnvironmentVariableBuilder(issueSelector); + final JiraEnvironmentVariableBuilder builder = new JiraEnvironmentVariableBuilder(issueSelector, null); assertThat(builder.getIssueSelector(), is(issueSelector)); } + + @Test + public void testSetIssuesSizeVariableNamePersists() { + final JiraEnvironmentVariableBuilder builder = new JiraEnvironmentVariableBuilder(issueSelector, ISSUES_SIZE_PROPERTY_NAME); + assertThat(builder.getIssuesSizeVariableName(), is(ISSUES_SIZE_PROPERTY_NAME)); + } @Test(expected = AbortException.class) public void testPerformWithNoSiteFailsBuild() throws InterruptedException, IOException { - JiraEnvironmentVariableBuilder builder = spy(new JiraEnvironmentVariableBuilder(issueSelector)); + JiraEnvironmentVariableBuilder builder = spy(new JiraEnvironmentVariableBuilder(issueSelector, null)); doReturn(null).when(builder).getSiteForProject((AbstractProject) Mockito.any()); builder.perform(build, launcher, listener); } @Test public void testPerformAddsAction() throws InterruptedException, IOException { - JiraEnvironmentVariableBuilder builder = spy(new JiraEnvironmentVariableBuilder(issueSelector)); + JiraEnvironmentVariableBuilder builder = spy(new JiraEnvironmentVariableBuilder(issueSelector, null)); doReturn(site).when(builder).getSiteForProject((AbstractProject) Mockito.any()); boolean result = builder.perform(build, launcher, listener);