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

[JENKINS-26375] dont use domain when creating user #73

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 16 additions & 3 deletions src/main/java/hudson/plugins/tfs/model/TfsUserLookup.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hudson.plugins.tfs.model;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.tfs.core.clients.webservices.IIdentityManagementService;
Expand All @@ -26,13 +27,24 @@ public TfsUserLookup(IIdentityManagementService ims) {
* @param accountName Windows NT account name: domain\alias.
*/
public User find(String accountName) {
LOGGER.fine("Looking up Jenkins user for TFS account '%s'.");
final User jenkinsUser = User.get(accountName);

// split username from domain
String accountNameWithoutDomain;
String[] split = accountName.split("\\\\");
if (split.length == 2) {
accountNameWithoutDomain = split[1];
} else {
accountNameWithoutDomain = accountName;
}

LOGGER.log(Level.FINE, "Looking up Jenkins user for TFS account '%s'.", accountName);
final User jenkinsUser = User.get(accountNameWithoutDomain);
Mailer.UserProperty mailerProperty = jenkinsUser.getProperty(Mailer.UserProperty.class);
if (mailerProperty == null || mailerProperty.getAddress() == null || mailerProperty.getAddress().length() == 0) {
LOGGER.log(Level.FINE, "No Mailer.UserProperty defined for '%s', looking in TFS", accountName);
final TeamFoundationIdentity tfsUser = ims.readIdentity(
IdentitySearchFactor.ACCOUNT_NAME,
accountName,
accountName, //name WITH domain for TFS
MembershipQuery.NONE,
ReadIdentityOptions.NONE
);
Expand All @@ -41,6 +53,7 @@ public User find(String accountName) {
jenkinsUser.setFullName(displayName);
final String emailAddress = (String) tfsUser.getProperty("Mail");
if (emailAddress != null) {
LOGGER.log(Level.FINE, "Retrieved email '%s' from TFS", emailAddress);
mailerProperty = new Mailer.UserProperty(emailAddress);
try {
jenkinsUser.addProperty(mailerProperty);
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/hudson/plugins/tfs/model/ProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ public void assertConvertServerChange() throws Exception {
private UserLookup createMockUserLookup(String accountName, String displayName, String emailAddress) {
UserLookup userLookup = mock(UserLookup.class);
User user = mock(User.class);
// this portion stolen from User.get()
final String id = accountName.replace('\\', '_').replace('/', '_').replace('<','_')
.replace('>','_'); // 4 replace() still faster than regex
// end stolen portion

//should put this in a utility/getter function somewhere
Copy link
Member

Choose a reason for hiding this comment

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

Great idea! Please make it so. I recommend a static method on TfsUserLookup.

String id;
String[] split = accountName.split("\\\\");
if (split.length == 2) {
id = split[1];
} else {
id = accountName;
}

when(user.getId()).thenReturn(id);
when(user.getDisplayName()).thenReturn(displayName);
when(user.getProperty(Mailer.UserProperty.class)).thenReturn(new Mailer.UserProperty(emailAddress));
Expand Down Expand Up @@ -91,7 +97,7 @@ public void assertConvertServerChangeset() throws Exception {

final User author = actual.getAuthor();
assertEquals("The version was incorrect", "12472", actual.getVersion());
assertEquals("The author's user ID was incorrect", "EXAMPLE_ljenkins", author.getId());
assertEquals("The author's user ID was incorrect", "ljenkins", author.getId());
assertEquals("The author's display name was incorrect", userDisplayName, author.getDisplayName());
final String actualEmailAddress = author.getProperty(Mailer.UserProperty.class).getAddress();
assertEquals("The author's e-mail address was incorrect", userEmailAddress, actualEmailAddress);
Expand Down