From ea16a3027adc92095d4c11581b614c3c28cde0fc Mon Sep 17 00:00:00 2001 From: parautenbach Date: Wed, 26 Mar 2014 00:31:21 +0200 Subject: [PATCH] Contributors would only be included if they have a name (and not only a username) on the build server. #11 --- CHANGELOG | 3 ++ README.md | 10 ++++- build.xml | 2 +- .../hipchat/HipChatServerExtension.java | 29 +++++++----- .../test/HipChatServerExtensionTest.java | 45 ++++++++++++++++--- .../buildServerResources/adminSettings.jsp | 1 + teamcity-plugin.xml | 2 +- 7 files changed, 72 insertions(+), 20 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index dd76af3..91f61b1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +Version 0.4.4 +* Bug: Contributors would only be included if they have a name (and not only a username) on the build server. + Version 0.4.3 * Bug: If emoticons can't be retrieved during plugin initialisation, the server extension's registration will fail. diff --git a/README.md b/README.md index 6bf9a40..d2894ae 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,12 @@ directory (as explained by [Jetbrains](http://www.jetbrains.com/teamcity/plugins [Visit](http://www.whatsthatlight.com/index.php/projects/teamcity-hipchat-plugin/) my website for more detailled instructions and information. -Note: I've tested the plugin with TeamCity 8. Support for older versions are uncertain, but I would gladly provide information and experiences by others here. +Compatibility: +* Releases v0.1.0 to v0.4.3 was tested against TeamCity 8.0.5. +* Release v0.4.4 was tested against TeamCity 8.1.1. +* Support for older TeamCity versions is uncertain. + +Note: I would gladly provide information and experiences by others here. # Configuration @@ -42,6 +47,9 @@ For debugging, add the snippets in [`teamcity-server-log4j.xml`](https://github. # Changelog +## Version 0.4.4 +* Bug: Contributors would only be included if they have a name (and not only a username) on the build server. + ## Version 0.4.3 * Bug: If emoticons can't be retrieved during plugin initialisation, the server extension's registration will fail. diff --git a/build.xml b/build.xml index f01bbb2..3a79b57 100644 --- a/build.xml +++ b/build.xml @@ -12,7 +12,7 @@ - + diff --git a/src/com/whatsthatlight/teamcity/hipchat/HipChatServerExtension.java b/src/com/whatsthatlight/teamcity/hipchat/HipChatServerExtension.java index bc9c99f..e14d506 100644 --- a/src/com/whatsthatlight/teamcity/hipchat/HipChatServerExtension.java +++ b/src/com/whatsthatlight/teamcity/hipchat/HipChatServerExtension.java @@ -18,7 +18,9 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Random; @@ -32,7 +34,6 @@ import jetbrains.buildServer.serverSide.SProject; import jetbrains.buildServer.serverSide.SRunningBuild; import jetbrains.buildServer.users.SUser; -import jetbrains.buildServer.users.UserSet; import jetbrains.buildServer.vcs.SVcsModification; import jetbrains.buildServer.vcs.SelectPrevBuildPolicy; @@ -218,17 +219,23 @@ private String createHtmlBuildEventMessage(SRunningBuild build, TeamCityEvent bu String fullNameATag = String.format("%s", projectUrl, build.getBuildType().getFullName()); // Contributors (committers) - try { - List changes = build.getChanges(SelectPrevBuildPolicy.SINCE_LAST_BUILD, true); - logger.debug(String.format("Number of changes: %s", changes.size())); - } catch (Exception e) {} - UserSet users = build.getCommitters(SelectPrevBuildPolicy.SINCE_LAST_BUILD); - logger.debug(String.format("Initial contributors: %s, %s", !users.getUsers().isEmpty(), users != UserSet.EMPTY)); - Collection userCollection = new ArrayList(); - for (SUser user : users.getUsers()) { - userCollection.add(user.getName()); + List changes = build.getChanges(SelectPrevBuildPolicy.SINCE_LAST_BUILD, true); + logger.debug(String.format("Number of changes: %s", changes.size())); + // Use a set to ensure each user is added only once + Collection userSet = new HashSet(); + for (SVcsModification modification : changes) { + String name = modification.getUserName(); + List ids = modification.getCommitterIds(); + if (!ids.isEmpty()) { + // There should be only one user ID per commit. + name = this.server.getUserModel().findUserById(ids.get(0)).getDescriptiveName(); + } + logger.debug(String.format("Adding contributor: %s", name)); + userSet.add(name); } - String contributors = Utils.join(userCollection); + List userList = new ArrayList(userSet); + Collections.sort(userList, String.CASE_INSENSITIVE_ORDER); + String contributors = Utils.join(userList); boolean hasContributors = !contributors.isEmpty(); logger.debug(String.format("Has contributors: %s", hasContributors)); diff --git a/src/com/whatsthatlight/teamcity/hipchat/test/HipChatServerExtensionTest.java b/src/com/whatsthatlight/teamcity/hipchat/test/HipChatServerExtensionTest.java index 68924dd..123a89d 100644 --- a/src/com/whatsthatlight/teamcity/hipchat/test/HipChatServerExtensionTest.java +++ b/src/com/whatsthatlight/teamcity/hipchat/test/HipChatServerExtensionTest.java @@ -19,6 +19,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; import jetbrains.buildServer.messages.Status; @@ -32,6 +33,7 @@ import jetbrains.buildServer.users.SUser; import jetbrains.buildServer.users.UserModel; import jetbrains.buildServer.users.UserSet; +import jetbrains.buildServer.vcs.SVcsModification; import jetbrains.buildServer.vcs.SelectPrevBuildPolicy; import org.apache.log4j.BasicConfigurator; @@ -232,8 +234,10 @@ public void testBuildStartedEventAndMessageDetails() throws URISyntaxException, String expectedUser1Name = "foo"; String expectedUser2Name = "bar"; String expectedUser3Name = "baz"; - String expectedContributors = String.format("%s, %s, %s", expectedUser1Name, expectedUser2Name, expectedUser3Name); - + String expectedContributors = String.format("%s, %s, %s", expectedUser2Name.toUpperCase(), expectedUser3Name, expectedUser1Name.toUpperCase()); + long expectedUserId1 = 0; + long expectedUserId2 = 1; + // Callback closure final ArrayList callbacks = new ArrayList(); final Object waitObject = new Object(); @@ -256,17 +260,44 @@ public void testBuildStartedEventAndMessageDetails() throws URISyntaxException, UserSet userSet = (UserSet) mock(UserSet.class); Set users = new LinkedHashSet(); SUser user1 = mock(SUser.class); - when(user1.getName()).thenReturn(expectedUser1Name); + when(user1.getDescriptiveName()).thenReturn(expectedUser1Name.toUpperCase()); users.add(user1); SUser user2 = mock(SUser.class); - when(user2.getName()).thenReturn(expectedUser2Name); + when(user2.getDescriptiveName()).thenReturn(expectedUser2Name.toUpperCase()); users.add(user2); SUser user3 = mock(SUser.class); - when(user3.getName()).thenReturn(expectedUser3Name); + when(user3.getDescriptiveName()).thenReturn(expectedUser3Name.toUpperCase()); users.add(user3); when(userSet.getUsers()).thenReturn(users); + + List commiterIdsList1 = new ArrayList(); + commiterIdsList1.add(expectedUserId1); + List commiterIdsList2 = new ArrayList(); + commiterIdsList2.add(expectedUserId2); + List commiterIdsList3 = new ArrayList(); + + SVcsModification modification1 = mock(SVcsModification.class); + when(modification1.getUserName()).thenReturn(expectedUser1Name); + when(modification1.getCommitterIds()).thenReturn(commiterIdsList1); + SVcsModification modification2 = mock(SVcsModification.class); + when(modification2.getUserName()).thenReturn(expectedUser2Name); + when(modification2.getCommitterIds()).thenReturn(commiterIdsList2); + SVcsModification modification3 = mock(SVcsModification.class); + when(modification3.getUserName()).thenReturn(expectedUser3Name); + when(modification3.getCommitterIds()).thenReturn(commiterIdsList3); + + List changes = new ArrayList(); + // TODO: Test sorting + changes.add(modification1); + changes.add(modification3); + changes.add(modification2); + + when(build.getChanges(any(SelectPrevBuildPolicy.class), any(Boolean.class))).thenReturn(changes); + + UserModel userModel = mock(UserModel.class); + when(userModel.findUserById(expectedUserId1)).thenReturn(user1); + when(userModel.findUserById(expectedUserId2)).thenReturn(user2); - when(build.getCommitters(any(SelectPrevBuildPolicy.class))).thenReturn(userSet); SProject parentProject = mock(SProject.class); when(parentProject.getProjectId()).thenReturn(expectedParentProjectId); SProject project = mock(SProject.class); @@ -277,6 +308,7 @@ public void testBuildStartedEventAndMessageDetails() throws URISyntaxException, SBuildServer server = mock(SBuildServer.class); when(server.getProjectManager()).thenReturn(projectManager); when(server.getRootUrl()).thenReturn(rootUrl); + when(server.getUserModel()).thenReturn(userModel); MockHipChatNotificationProcessor processor = new MockHipChatNotificationProcessor(callback); HipChatConfiguration configuration = new HipChatConfiguration(); configuration.setNotifyStatus(expectedNotificationStatus); @@ -304,6 +336,7 @@ public void testBuildStartedEventAndMessageDetails() throws URISyntaxException, assertTrue(actualNotification.message.contains(expectedTriggerBy)); assertTrue(actualNotification.message.contains(String.format("buildId=%s", expectedBuildId))); assertTrue(actualNotification.message.contains(String.format("buildTypeId=%s", expectedBuildTypeId))); + System.out.println(String.format("Expected: %s", expectedContributors)); assertTrue(actualNotification.message.contains(expectedContributors)); assertTrue(actualNotification.message.contains(" + diff --git a/teamcity-plugin.xml b/teamcity-plugin.xml index ad20d3c..d958031 100644 --- a/teamcity-plugin.xml +++ b/teamcity-plugin.xml @@ -5,7 +5,7 @@ HipChat Notifier HipChat Notifier - 0.4.3 + 0.4.4 What's That Light? http://www.whatsthatlight.com/