Skip to content

Commit

Permalink
Merge pull request #67 from jenkinsci/parameters_fix_JENKINS-25828_JE…
Browse files Browse the repository at this point in the history
…NKINS-12578

JENKINS-12578 JENKINS-25828
  • Loading branch information
Radosław Antoniuk committed Oct 29, 2015
2 parents 05b55a7 + 0100428 commit 422af63
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 129 deletions.
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/jira/JiraSite.java
Expand Up @@ -230,7 +230,7 @@ protected JiraSession createSession() throws IOException {
LOGGER.warning("convert URL to URI error: " + e.getMessage());
throw new RuntimeException("failed to create JiraSession due to convert URI error");
}
LOGGER.info("creating Jira Session: " + uri);
LOGGER.fine("creating Jira Session: " + uri);

final JiraRestClient jiraRestClient = new AsynchronousJiraRestClientFactory()
.createWithBasicHttpAuthentication(uri, userName, password.getPlainText());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/jira/Updater.java
Expand Up @@ -341,7 +341,7 @@ static void findIssues(AbstractBuild<?, ?> build, Set<String> ids, Pattern patte
if (parameters != null) {
for (ParameterValue val : parameters.getParameters()) {
if (val instanceof JiraIssueParameterValue) {
ids.add(((JiraIssueParameterValue) val).getIssue());
ids.add(((JiraIssueParameterValue) val).getValue());
}
}
}
Expand Down
Expand Up @@ -18,46 +18,48 @@
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.ParameterValue;
import hudson.model.Run;
import hudson.util.VariableResolver;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.export.Exported;

public class JiraIssueParameterValue extends ParameterValue {
private static final long serialVersionUID = -1078274709338167211L;

private String issue;
private String value;

@DataBoundConstructor
public JiraIssueParameterValue(final String name, final String issue) {
public JiraIssueParameterValue(final String name, final String value) {
super(name);
this.issue = issue;
this.value = value;
}

@Override
public void buildEnvVars(final AbstractBuild<?, ?> build, final EnvVars env) {
env.put(getName(), getIssue());
public void buildEnvironment(final Run<?, ?> run, final EnvVars env) {
env.put(getName(), getValue());
}

@Override
public VariableResolver<String> createVariableResolver(
final AbstractBuild<?, ?> build) {
return new VariableResolver<String>() {
public String resolve(final String name) {
return JiraIssueParameterValue.this.name.equals(name) ? getIssue()
: null;
return JiraIssueParameterValue.this.name.equals(name) ? getValue() : null;
}
};
}

public void setIssue(final String issue) {
this.issue = issue;
public void setValue(final String value) {
this.value = value;
}

public String getIssue() {
return issue;
@Exported
public String getValue() {
return value;
}

@Override
public String toString() {
return "(JiraIssueParameterValue) " + getName() + "='" + issue + "'";
return "(JiraIssueParameterValue) " + getName() + "='" + value + "'";
}
}
Expand Up @@ -32,8 +32,7 @@ public VariableResolver<String> createVariableResolver(
final AbstractBuild<?, ?> build) {
return new VariableResolver<String>() {
public String resolve(final String name) {
return JiraVersionParameterValue.this.name.equals(name) ? getVersion()
: null;
return JiraVersionParameterValue.this.name.equals(name) ? getVersion() : null;
}
};
}
Expand Down
Expand Up @@ -27,7 +27,7 @@ limitations under the License.
</j:when>
<j:otherwise>
<!-- everything is fine, we can display the drop-down list to the user -->
<select name="issue">
<select name="value">
<j:forEach var="issue" items="${it.issues}">
<option value="${issue.key}">${issue.key}: ${issue.summary}</option>
</j:forEach>
Expand Down
157 changes: 45 additions & 112 deletions src/test/java/hudson/plugins/jira/UpdaterTest.java
Expand Up @@ -55,45 +55,6 @@ public String getMsg() {
}
}

@Test
public void testFindIssues() {
FreeStyleBuild build = mock(FreeStyleBuild.class);
ChangeLogSet changeLogSet = mock(ChangeLogSet.class);
BuildListener listener = mock(BuildListener.class);

when(changeLogSet.iterator()).thenReturn(Collections.EMPTY_LIST.iterator());
when(build.getChangeSet()).thenReturn(changeLogSet);

Set<String> ids = new HashSet<String>();
Updater.findIssues(build, ids, null, listener);
Assert.assertTrue(ids.isEmpty());


Set<? extends Entry> entries = Sets.newHashSet(new MockEntry("Fixed JIRA-4711"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

ids = new HashSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, listener);
Assert.assertEquals(1, ids.size());
Assert.assertEquals("JIRA-4711", ids.iterator().next());

// now test multiple ids
entries = Sets.newHashSet(
new MockEntry("Fixed BL-4711"),
new MockEntry("TR-123: foo"),
new MockEntry("[ABC-42] hallo"),
new MockEntry("#123: this one must not match"),
new MockEntry("ABC-: this one must also not match"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

ids = new TreeSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, listener);
Assert.assertEquals(3, ids.size());
Set<String> expected = Sets.newTreeSet(Sets.newHashSet(
"BL-4711", "TR-123", "ABC-42"));
Assert.assertEquals(expected, ids);
}

/**
* Tests that the JiraIssueParameters are identified as updateable JIRA
* issues.
Expand All @@ -115,8 +76,8 @@ public void testFindIssuesWithJiraParameters() {
when(build.getChangeSet()).thenReturn(changeLogSet);
when(build.getAction(ParametersAction.class)).thenReturn(action);
when(action.getParameters()).thenReturn(parameters);
when(parameter.getIssue()).thenReturn("JIRA-123");
when(parameterTwo.getIssue()).thenReturn("JIRA-321");
when(parameter.getValue()).thenReturn("JIRA-123");
when(parameterTwo.getValue()).thenReturn("JIRA-321");

Set<String> ids = new HashSet<String>();

Expand All @@ -140,61 +101,51 @@ public void testFindIssuesWithJiraParameters() {
}

@Test
@Bug(729)
public void testDigitsInProjectNameAllowed() {
@Bug(4132)
public void testProjectNamesAllowed() {
FreeStyleBuild build = mock(FreeStyleBuild.class);
ChangeLogSet changeLogSet = mock(ChangeLogSet.class);
when(build.getChangeSet()).thenReturn(changeLogSet);

Set<? extends Entry> entries = Sets.newHashSet(new MockEntry("Fixed JI123-4711"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

Set<String> ids = new HashSet<String>();
BuildListener listener = mock(BuildListener.class);
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, listener);
Assert.assertEquals(1, ids.size());
Assert.assertEquals("JI123-4711", ids.iterator().next());
}

@Test
@Bug(4092)
public void testUnderscoreInProjectNameAllowed() {
FreeStyleBuild build = mock(FreeStyleBuild.class);
ChangeLogSet changeLogSet = mock(ChangeLogSet.class);
when(build.getChangeSet()).thenReturn(changeLogSet);

Set<? extends Entry> entries = Sets.newHashSet(new MockEntry("Fixed FOO_BAR-4711"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

Set<String> ids = new HashSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, mock(BuildListener.class));
Assert.assertEquals(1, ids.size());
Assert.assertEquals("FOO_BAR-4711", ids.iterator().next());
}
Set<? extends Entry> entries = Sets.newHashSet(
new MockEntry("Fixed JI123-4711"),
new MockEntry("Fixed foo_bar-4710"),
new MockEntry("Fixed FoO_bAr-4711"),
new MockEntry("Fixed someting.\nJFoO_bAr_MULTI-4718"),
new MockEntry("TR-123: foo"),
new MockEntry("[ABC-42] hallo"),
new MockEntry("#123: this one must not match"),
new MockEntry("ABC-: this one must also not match"),
new MockEntry("ABC-: \n\nABC-127:\nthis one should match"),
new MockEntry("ABC-: \n\nABC-128:\nthis one should match"),
new MockEntry("ABC-: \n\nXYZ-10:\nXYZ-20 this one too"),
new MockEntry("Fixed DOT-4."),
new MockEntry("Fixed DOT-5. Did it right this time")
);

@Test
@Bug(4132)
public void testLowercaseProjectNameAllowed() {
FreeStyleBuild build = mock(FreeStyleBuild.class);
ChangeLogSet changeLogSet = mock(ChangeLogSet.class);
when(build.getChangeSet()).thenReturn(changeLogSet);

Set<? extends Entry> entries = Sets.newHashSet(new MockEntry("Fixed foo_bar-4711"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

Set<String> ids = new HashSet<String>();
BuildListener listener = mock(BuildListener.class);
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, listener);
Assert.assertEquals(1, ids.size());
Assert.assertEquals("FOO_BAR-4711", ids.iterator().next());
Set<String> expected = Sets.newHashSet(
"JI123-4711",
"FOO_BAR-4710",
"FOO_BAR-4711",
"JFOO_BAR_MULTI-4718",
"TR-123",
"ABC-42",
"ABC-127",
"ABC-128",
"XYZ-10",
"XYZ-20",
"DOT-4",
"DOT-5"
);

entries = Sets.newHashSet(new MockEntry("Fixed FoO_bAr-4711"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());
Set<String> result = new HashSet<String>();
Updater.findIssues(build, result, JiraSite.DEFAULT_ISSUE_PATTERN, listener);

ids = new HashSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, listener);
Assert.assertEquals(1, ids.size());
Assert.assertEquals("FOO_BAR-4711", ids.iterator().next());
Assert.assertEquals(expected.size(), result.size());
Assert.assertEquals(expected,result);
}

/**
Expand Down Expand Up @@ -263,7 +214,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
* These patterns are used e.g. by the maven release plugin.
*/
@Test
public void testDoNotMatchDotsInIssueId() {
public void testDefaultPattertNotToMatchMavenRelease() {
FreeStyleBuild build = mock(FreeStyleBuild.class);
ChangeLogSet changeLogSet = mock(ChangeLogSet.class);
when(build.getChangeSet()).thenReturn(changeLogSet);
Expand All @@ -275,24 +226,6 @@ public void testDoNotMatchDotsInIssueId() {
Set<String> ids = new HashSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, null);
Assert.assertEquals(0, ids.size());

// but ids with just a full-stop after it must still match
entries = Sets.newHashSet(new MockEntry("Fixed FOO-4. Did it right this time"));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

ids = new HashSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, null);
Assert.assertEquals(1, ids.size());
Assert.assertEquals("FOO-4", ids.iterator().next());

// as well as messages with a full-stop as last character after an issue id
entries = Sets.newHashSet(new MockEntry("Fixed FOO-4."));
when(changeLogSet.iterator()).thenReturn(entries.iterator());

ids = new HashSet<String>();
Updater.findIssues(build, ids, JiraSite.DEFAULT_ISSUE_PATTERN, null);
Assert.assertEquals(1, ids.size());
Assert.assertEquals("FOO-4", ids.iterator().next());
}

@Test
Expand Down Expand Up @@ -355,6 +288,13 @@ public void testUserPatternMatchTwoIssuesInOneComment() {
@Test
@Bug(17156)
public void testIssueIsRemovedFromCarryOverListAfterSubmission() throws RestClientException {
// mock build:
FreeStyleBuild build = mock(FreeStyleBuild.class);
FreeStyleProject project = mock(FreeStyleProject.class);
when(build.getProject()).thenReturn(project);
ChangeLogSet changeLogSet = ChangeLogSet.createEmpty(build);
when(build.getChangeSet()).thenReturn(changeLogSet);
when(build.getResult()).thenReturn(Result.SUCCESS);

final JiraIssue firstIssue = new JiraIssue("FOOBAR-1", "Title");
final JiraIssue secondIssue = new JiraIssue("ALIBA-1", "Title");
Expand Down Expand Up @@ -389,13 +329,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
doThrow(new RestClientException(new Throwable(), 404)).when(session).addComment(eq(deletedIssue.id), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
doThrow(new RestClientException(new Throwable(), 403)).when(session).addComment(eq(forbiddenIssue.id), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());

// mock build:
FreeStyleBuild build = mock(FreeStyleBuild.class);
FreeStyleProject project = mock(FreeStyleProject.class);
when(build.getProject()).thenReturn(project);
ChangeLogSet changeLogSet = ChangeLogSet.createEmpty(build);
when(build.getChangeSet()).thenReturn(changeLogSet);
when(build.getResult()).thenReturn(Result.SUCCESS);

final String groupVisibility = "";
final String roleVisibility = "";
Expand Down

0 comments on commit 422af63

Please sign in to comment.