Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JiraIssuesSizeEnvironmentVariableBuilder build step #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,18 +23,24 @@
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() {
AbstractIssueSelector uis = this.issueSelector;
if (uis == null) uis = new DefaultIssueSelector();
return (this.issueSelector = uis);
}


String getIssuesSizeVariableName() {
return issuesSizeVariableName;
}

JiraSite getSiteForProject(AbstractProject<?, ?> project) {
return JiraSite.get(project);
}
Expand All @@ -49,10 +57,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
Set<String> 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;
}
Expand All @@ -72,5 +82,9 @@ public boolean isApplicable(Class<? extends AbstractProject> klass) {
public String getDisplayName() {
return Messages.JiraEnvironmentVariableBuilder_DisplayName();
}

public boolean hasIssueSelectors() {
return Jenkins.getActiveInstance().getDescriptorList(AbstractIssueSelector.class).size() > 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<j:if test="${descriptor.hasIssueSelectors()}">
<f:dropdownDescriptorSelector field="issueSelector" title="${%Issue selector}"/>
</j:if>
</j:if>
<f:advanced>
<f:entry title="${%Issues size variable name}" field="issuesSizeVariableName">
<f:textbox default="JIRA_ISSUES_SIZE"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

better to use word COUNT instead of SIZE.
size is usually used when creating an object of specific size to show how many elements can fit in collection, not how many there are

Copy link
Contributor

Choose a reason for hiding this comment

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

Applies to all files of course :)

</f:entry>
</f:advanced>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div>Available variables:</div>
<ul>
<li>JIRA_ISSUES - A comma separated list of issues which are referenced in the version control system changelog</li>
<li>JIRA_ISSUES_SIZE - Size of the list described above</li>
<li>JIRA_URL - Primary URL for the JIRA server </li>
</ul>
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -27,20 +29,23 @@ 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);

action.buildEnvVars(build, envVars);

ArgumentCaptor<String> keys = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand Down