-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor build user vars plugin: Remove unnecessary utility class, re…
…located constants to a more generic context (#127) * Remove deprecated utility class. * Move build user variable constants to a specific class. * Refactor username utility class and tests. * Add missing override. * Fix bad html entity. Improve documentation.
- Loading branch information
1 parent
9a155a3
commit 0da9345
Showing
16 changed files
with
191 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/org/jenkinsci/plugins/builduser/utils/BuildUserVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jenkinsci.plugins.builduser.utils; | ||
|
||
public class BuildUserVariable { | ||
public static final String USERNAME = "BUILD_USER"; | ||
public static final String GROUPS = "BUILD_USER_GROUPS"; | ||
public static final String FIRST_NAME = "BUILD_USER_FIRST_NAME"; | ||
public static final String LAST_NAME = "BUILD_USER_LAST_NAME"; | ||
public static final String EMAIL = "BUILD_USER_EMAIL"; | ||
public static final String ID = "BUILD_USER_ID"; | ||
public static final String UNDEFINED = "UNDEFINED"; | ||
|
||
private BuildUserVariable() { | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
src/main/java/org/jenkinsci/plugins/builduser/utils/ClassUtils.java
This file was deleted.
Oops, something went wrong.
72 changes: 40 additions & 32 deletions
72
src/main/java/org/jenkinsci/plugins/builduser/utils/UsernameUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,65 @@ | ||
package org.jenkinsci.plugins.builduser.utils; | ||
|
||
import static org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable.BUILD_USER_FIRST_NAME_VAR_NAME; | ||
import static org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable.BUILD_USER_LAST_NAME_VAR_NAME; | ||
import static org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable.BUILD_USER_VAR_NAME; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
/** | ||
* Utility class for splitting full user name to parts. | ||
* Utility class for manipulating and extracting parts from a full username. | ||
* This class provides methods to split a full username into first and last names. | ||
* | ||
* @author GKonovalenko | ||
*/ | ||
public final class UsernameUtils { | ||
private UsernameUtils(){ | ||
|
||
private UsernameUtils() { | ||
} | ||
|
||
/** | ||
* Splits username string to first & last names and sets appropriate build variables. | ||
* @param username | ||
* string with username, usually smth. like "Chuck Norris" | ||
* @param variables | ||
* result map, where to put build variables. | ||
* Splits a full username string into first and last names and sets the appropriate build variables. | ||
* | ||
* @param username The full username string, usually in the format "First Last" | ||
* @param variables A map to store the extracted variables, where to put build variables. | ||
*/ | ||
public static void setUsernameVars(String username, Map<String, String> variables) { | ||
variables.put(BUILD_USER_VAR_NAME, username); | ||
variables.put(BUILD_USER_FIRST_NAME_VAR_NAME, getFirstName(username)); | ||
variables.put(BUILD_USER_LAST_NAME_VAR_NAME, getLastName(username)); | ||
variables.put(BuildUserVariable.USERNAME, username); | ||
variables.put(BuildUserVariable.FIRST_NAME, getFirstName(username)); | ||
variables.put(BuildUserVariable.LAST_NAME, getLastName(username)); | ||
} | ||
|
||
/** | ||
* Cuts first name (first word) out from the passed string. | ||
* @param fullName | ||
* full name -- string like "Chuck Norris" | ||
* @return | ||
* first name ("Chuck") | ||
* Extracts the first name from a full name. | ||
* | ||
* @param fullName The full name string, e.g., "First Last" | ||
* @return The first name ("First") | ||
*/ | ||
public static String getFirstName(String fullName) { | ||
String [] parts = StringUtils.trimToEmpty(fullName).split("\\s+"); | ||
return parts.length > 0 ? parts[0] : ""; | ||
if (fullName == null || fullName.trim().isEmpty()) { | ||
return ""; | ||
} | ||
String[] parts = splitName(fullName); | ||
return parts[0]; | ||
} | ||
|
||
/** | ||
* Cuts last name (second word) out from the passed string. | ||
* @param fullName | ||
* full name -- string like "Chuck Norris" | ||
* @return | ||
* last name ("Norris") | ||
* Extracts the last name from a full name. | ||
* | ||
* @param fullName The full name string, e.g., "First Last" | ||
* @return The last name ("Last") | ||
*/ | ||
public static String getLastName(String fullName) { | ||
String [] parts = StringUtils.trimToEmpty(fullName).split("\\s+"); | ||
if (fullName == null || fullName.trim().isEmpty()) { | ||
return ""; | ||
} | ||
String[] parts = splitName(fullName); | ||
return parts.length >= 2 ? parts[1] : ""; | ||
} | ||
|
||
/** | ||
* Splits a full name into its constituent parts. | ||
* | ||
* @param fullName The full name string, e.g., "First Last" | ||
* @return An array containing the first and last name | ||
*/ | ||
private static String[] splitName(String fullName) { | ||
return fullName.trim().split("\\s+"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.