Skip to content

Commit a27b9b3

Browse files
committed
fix the build failure logic
1 parent bccaca2 commit a27b9b3

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/main/java/hudson/plugins/jira/VersionCreator.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,27 @@ protected boolean perform(
4242
List<ExtendedVersion> existingVersions =
4343
Optional.ofNullable(session.getVersions(realProjectKey)).orElse(Collections.emptyList());
4444

45+
// past logic to fail the build if the version already exists
4546
if (existingVersions.stream().anyMatch(v -> v.getName().equals(finalRealVersion))) {
4647
listener.getLogger().println(Messages.JiraVersionCreator_VersionExists(realVersion, realProjectKey));
47-
} else {
48-
listener.getLogger().println(Messages.JiraVersionCreator_CreatingVersion(realVersion, realProjectKey));
49-
addVersion(realVersion, realProjectKey, session);
48+
if (listener instanceof BuildListener) {
49+
((BuildListener) listener).finished(Result.FAILURE);
50+
}
51+
return false;
5052
}
5153

54+
listener.getLogger().println(Messages.JiraVersionCreator_CreatingVersion(realVersion, realProjectKey));
55+
addVersion(realVersion, realProjectKey, session);
56+
return true;
5257
} catch (Exception e) {
5358
e.printStackTrace(
5459
listener.fatalError("Unable to add version %s to Jira project %s", realVersion, realProjectKey, e));
55-
if (listener instanceof BuildListener) {
56-
((BuildListener) listener).finished(Result.FAILURE);
57-
}
58-
return false;
5960
}
60-
return true;
61+
62+
if (listener instanceof BuildListener) {
63+
((BuildListener) listener).finished(Result.FAILURE);
64+
}
65+
return false;
6166
}
6267

6368
/**

src/test/java/hudson/plugins/jira/VersionCreatorTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.hamcrest.Matchers.is;
55
import static org.mockito.ArgumentMatchers.any;
66
import static org.mockito.Mockito.doReturn;
7+
import static org.mockito.Mockito.reset;
78
import static org.mockito.Mockito.spy;
89
import static org.mockito.Mockito.times;
910
import static org.mockito.Mockito.verify;
@@ -90,24 +91,41 @@ void createMocks() {
9091
void callsJiraWithSpecifiedParameters() throws InterruptedException, IOException {
9192
when(build.getEnvironment(listener)).thenReturn(env);
9293
when(site.getSession(any())).thenReturn(session);
93-
when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion));
9494

95+
// for new version, verify the addVersion method is called
96+
when(session.getVersions(JIRA_PRJ)).thenReturn(null);
9597
versionCreator.perform(project, JIRA_VER, JIRA_PRJ, build, listener);
96-
verify(session).addVersion(versionCaptor.capture(), projectCaptor.capture());
98+
verify(session, times(1)).addVersion(versionCaptor.capture(), projectCaptor.capture());
9799
assertThat(projectCaptor.getValue(), is(JIRA_PRJ));
98100
assertThat(versionCaptor.getValue(), is(JIRA_VER));
101+
verify(logger, times(1)).println(Messages.JiraVersionCreator_CreatingVersion(JIRA_VER, JIRA_PRJ));
102+
103+
// for existing version, verify the addVersion method is not called
104+
reset(session);
105+
when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion));
106+
versionCreator.perform(project, JIRA_VER, JIRA_PRJ, build, listener);
107+
verify(session, times(0)).addVersion(versionCaptor.capture(), projectCaptor.capture());
108+
verify(logger, times(1)).println(Messages.JiraVersionCreator_VersionExists(JIRA_VER, JIRA_PRJ));
99109
}
100110

101111
@Test
102112
void expandsEnvParameters() throws InterruptedException, IOException {
103113
when(build.getEnvironment(listener)).thenReturn(env);
104114
when(site.getSession(any())).thenReturn(session);
105-
when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion));
106115

116+
// for new version, verify the addVersion method is called
117+
when(session.getVersions(JIRA_PRJ)).thenReturn(null);
107118
versionCreator.perform(project, JIRA_VER_PARAM, JIRA_PRJ_PARAM, build, listener);
108-
verify(session).addVersion(versionCaptor.capture(), projectCaptor.capture());
119+
verify(session, times(1)).addVersion(versionCaptor.capture(), projectCaptor.capture());
109120
assertThat(projectCaptor.getValue(), is(JIRA_PRJ));
110121
assertThat(versionCaptor.getValue(), is(JIRA_VER));
122+
123+
// for existing version, verify the addVersion method is called
124+
reset(session);
125+
when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion));
126+
versionCreator.perform(project, JIRA_VER_PARAM, JIRA_PRJ_PARAM, build, listener);
127+
verify(session, times(0)).addVersion(versionCaptor.capture(), projectCaptor.capture());
128+
verify(logger, times(1)).println(Messages.JiraVersionCreator_VersionExists(JIRA_VER, JIRA_PRJ));
111129
}
112130

113131
@Test

0 commit comments

Comments
 (0)