Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
Contributors would only be included if they have a name (and not only a
Browse files Browse the repository at this point in the history
username) on the build server. #11
  • Loading branch information
parautenbach committed Mar 25, 2014
1 parent 551e586 commit ea16a30
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<property name="reports" location="reports"/>
<property name="main-class" value="HipChatNotifier"/>
<!-- Default to 0.0.0.0 if no version specified -->
<condition property="version" value="${env.BUILD_NUMBER}" else="0.4.3">
<condition property="version" value="${env.BUILD_NUMBER}" else="0.4.4">
<and>
<isset property="env.BUILD_NUMBER"/>
<not>
Expand Down
29 changes: 18 additions & 11 deletions src/com/whatsthatlight/teamcity/hipchat/HipChatServerExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -218,17 +219,23 @@ private String createHtmlBuildEventMessage(SRunningBuild build, TeamCityEvent bu
String fullNameATag = String.format("<a href=\"%s\">%s</a>", projectUrl, build.getBuildType().getFullName());

// Contributors (committers)
try {
List<SVcsModification> changes = build.getChanges(SelectPrevBuildPolicy.SINCE_LAST_BUILD, true);
logger.debug(String.format("Number of changes: %s", changes.size()));
} catch (Exception e) {}
UserSet<SUser> users = build.getCommitters(SelectPrevBuildPolicy.SINCE_LAST_BUILD);
logger.debug(String.format("Initial contributors: %s, %s", !users.getUsers().isEmpty(), users != UserSet.EMPTY));
Collection<String> userCollection = new ArrayList<String>();
for (SUser user : users.getUsers()) {
userCollection.add(user.getName());
List<SVcsModification> 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<String> userSet = new HashSet<String>();
for (SVcsModification modification : changes) {
String name = modification.getUserName();
List<Long> 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<String> userList = new ArrayList<String>(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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<CallbackObject> callbacks = new ArrayList<CallbackObject>();
final Object waitObject = new Object();
Expand All @@ -256,17 +260,44 @@ public void testBuildStartedEventAndMessageDetails() throws URISyntaxException,
UserSet<SUser> userSet = (UserSet<SUser>) mock(UserSet.class);
Set<SUser> users = new LinkedHashSet<SUser>();
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<Long> commiterIdsList1 = new ArrayList<Long>();
commiterIdsList1.add(expectedUserId1);
List<Long> commiterIdsList2 = new ArrayList<Long>();
commiterIdsList2.add(expectedUserId2);
List<Long> commiterIdsList3 = new ArrayList<Long>();

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<SVcsModification> changes = new ArrayList<SVcsModification>();
// 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);
Expand All @@ -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);
Expand Down Expand Up @@ -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("<img"));
assertEquals(expectedDefaultRoomId, actualDefaultRoomId);
Expand Down
1 change: 1 addition & 0 deletions src/resources/buildServerResources/adminSettings.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ limitations under the License.
</td>
</tr>
<tr>
<!-- TODO: Refresh this on URL or token change. -->
<th><label for="defaultRoomId">Default room: </label></th>
<td>
<forms:select name="defaultRoomId">
Expand Down
2 changes: 1 addition & 1 deletion teamcity-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>HipChat Notifier</name>
<display-name>HipChat Notifier</display-name>
<!-- <description>Some description goes here</description> -->
<version>0.4.3</version>
<version>0.4.4</version>
<vendor>
<name>What's That Light?</name>
<url>http://www.whatsthatlight.com/</url>
Expand Down

0 comments on commit ea16a30

Please sign in to comment.