Skip to content

Commit

Permalink
Refactor build user vars plugin: Remove unnecessary utility class, re…
Browse files Browse the repository at this point in the history
…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
fabiodcasilva authored Aug 30, 2024
1 parent 9a155a3 commit 0da9345
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 255 deletions.
22 changes: 8 additions & 14 deletions src/main/java/org/jenkinsci/plugins/builduser/BuildUser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.builduser;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
Expand All @@ -15,29 +16,21 @@
import hudson.tasks.BuildWrapperDescriptor;
import hudson.triggers.SCMTrigger.SCMTriggerCause;
import hudson.triggers.TimerTrigger.TimerTriggerCause;

import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Logger;
import edu.umd.cs.findbugs.annotations.NonNull;

import jenkins.branch.BranchEventCause;
import jenkins.branch.BranchIndexingCause;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildWrapper;

import org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable;
import org.jenkinsci.plugins.builduser.varsetter.impl.BranchIndexingTriggerDeterminant;
import org.jenkinsci.plugins.builduser.varsetter.impl.RemoteCauseDeterminant;
import org.jenkinsci.plugins.builduser.varsetter.impl.SCMTriggerCauseDeterminant;
import org.jenkinsci.plugins.builduser.varsetter.impl.TimerTriggerCauseDeterminant;
import org.jenkinsci.plugins.builduser.varsetter.impl.UserCauseDeterminant;
import org.jenkinsci.plugins.builduser.varsetter.impl.UserIdCauseDeterminant;
import org.jenkinsci.plugins.builduser.varsetter.impl.*;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
* This plugin is used to set build user variables, see {@link IUsernameSettable}:
*
Expand All @@ -56,6 +49,7 @@ public BuildUser() {
//noop
}

@Override
public void setUp(Context context, Run<?, ?> build, FilePath workspace,
Launcher launcher, TaskListener listener, EnvVars initialEnvironment) {
Map<String, String> variables = new HashMap<>();
Expand Down
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() {
}
}

This file was deleted.

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 &amp; 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+");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,20 @@
* @author GKonovalenko
*/
public interface IUsernameSettable<T extends Cause> {

/** Full name of user started build */
public static final String BUILD_USER_VAR_NAME = "BUILD_USER";
/** Groups username belongs to **/
public static final String BUILD_USER_VAR_GROUPS = "BUILD_USER_GROUPS";
/** First name of user started build */
public static final String BUILD_USER_FIRST_NAME_VAR_NAME = "BUILD_USER_FIRST_NAME";
/** Last name of user started build */
public static final String BUILD_USER_LAST_NAME_VAR_NAME = "BUILD_USER_LAST_NAME";
/** Email of user started build */
public static final String BUILD_USER_EMAIL = "BUILD_USER_EMAIL";
/** Id of user started build */
public static final String BUILD_USER_ID = "BUILD_USER_ID";
/** Optional value for variable which value couldn't be defined. */
public static final String UNDEFINED = "UNDEFINED";

/**
* Adds username build variables extracted from build cause to map of build variables.
*
* @param cause
* cause where to get username from.
* @param variables
* map of build variables, where to add username variables.
*
* @param cause cause where to get username from.
* @param variables ap of build variables, where to add username variables.
* @return
* <code>true</code> if username was determined and added to the passed map,
* <code>false</code> otherwise.
*/
boolean setJenkinsUserBuildVars(T cause, Map<String, String> variables);

/**
* Returns {@link Cause} subclass used to determine user name.
* @return
* class used to determine user name.
* Returns {@link Cause} subclass used to determine username.
* @return class used to determine username.
*/
Class<T> getUsedCauseClass();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.builduser.varsetter.impl;

import jenkins.branch.BranchIndexingCause;
import org.jenkinsci.plugins.builduser.utils.BuildUserVariable;
import org.jenkinsci.plugins.builduser.utils.UsernameUtils;
import org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable;

Expand All @@ -13,7 +14,7 @@ public class BranchIndexingTriggerDeterminant implements IUsernameSettable<Branc
public boolean setJenkinsUserBuildVars(BranchIndexingCause cause, Map<String, String> variables) {
if (cause != null) {
UsernameUtils.setUsernameVars("Branch Indexing", variables);
variables.put(BUILD_USER_ID, "branchIndexing");
variables.put(BuildUserVariable.ID, "branchIndexing");
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.jenkinsci.plugins.builduser.varsetter.impl;

import static java.lang.String.format;

import java.util.Map;

import hudson.model.Cause;
import org.jenkinsci.plugins.builduser.utils.BuildUserVariable;
import org.jenkinsci.plugins.builduser.utils.UsernameUtils;
import org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable;

import hudson.model.Cause;
import java.util.Map;

import static java.lang.String.format;

public class RemoteCauseDeterminant implements IUsernameSettable<Cause.RemoteCause> {

Expand All @@ -19,7 +19,7 @@ public boolean setJenkinsUserBuildVars(Cause.RemoteCause cause, Map<String, Stri

//As of Jenkins 2.51 remote cause is set the build was triggered using token and real user is not set
UsernameUtils.setUsernameVars(format("%s %s", cause.getAddr(), cause.getNote()), variables);
variables.put(BUILD_USER_ID, "remoteRequest");
variables.put(BuildUserVariable.ID, "remoteRequest");

Check warning on line 22 in src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/RemoteCauseDeterminant.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 22 is not covered by tests
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import hudson.triggers.SCMTrigger;
import hudson.triggers.SCMTrigger.SCMTriggerCause;

import java.util.Map;

import org.jenkinsci.plugins.builduser.utils.BuildUserVariable;
import org.jenkinsci.plugins.builduser.utils.UsernameUtils;
import org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable;

import java.util.Map;

public class SCMTriggerCauseDeterminant implements IUsernameSettable<SCMTrigger.SCMTriggerCause> {

final Class<SCMTrigger.SCMTriggerCause> causeClass = SCMTrigger.SCMTriggerCause.class;

public boolean setJenkinsUserBuildVars(SCMTriggerCause cause,
Map<String, String> variables) {

static final Class<SCMTrigger.SCMTriggerCause> causeClass = SCMTrigger.SCMTriggerCause.class;

public boolean setJenkinsUserBuildVars(SCMTriggerCause cause, Map<String, String> variables) {
if (cause != null) {
UsernameUtils.setUsernameVars("SCM Change", variables);
variables.put(BUILD_USER_ID, "scmChange");
variables.put(BuildUserVariable.ID, "scmChange");

Check warning on line 18 in src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/SCMTriggerCauseDeterminant.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 13-18 are not covered by tests

return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.jenkinsci.plugins.builduser.varsetter.impl;

import java.util.Map;

import hudson.triggers.TimerTrigger;
import org.jenkinsci.plugins.builduser.utils.BuildUserVariable;
import org.jenkinsci.plugins.builduser.utils.UsernameUtils;
import org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable;

import hudson.triggers.TimerTrigger;
import java.util.Map;

public class TimerTriggerCauseDeterminant implements IUsernameSettable<TimerTrigger.TimerTriggerCause> {

Expand All @@ -19,7 +19,7 @@ public boolean setJenkinsUserBuildVars(TimerTrigger.TimerTriggerCause cause, Map
}

UsernameUtils.setUsernameVars(TIMER_TRIGGER_DUMMY_USER_NAME, variables);
variables.put(BUILD_USER_ID, TIMER_TRIGGER_DUMMY_USER_ID);
variables.put(BuildUserVariable.ID, TIMER_TRIGGER_DUMMY_USER_ID);

Check warning on line 22 in src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/TimerTriggerCauseDeterminant.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 22 is not covered by tests
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@

import hudson.model.Cause.UserCause;
import hudson.model.Cause.UserIdCause;

import java.util.Map;

import org.jenkinsci.plugins.builduser.utils.UsernameUtils;
import org.jenkinsci.plugins.builduser.varsetter.IUsernameSettable;

import java.util.Map;

/**
* This implementation is used to determine build username variables from <b>{@link UserCause}</b>.
* This could be used with legacy version of jenkins, where {@link UserCause} is used instead of
* {@link UserIdCause} (before b1.427).
* This will let to get following set of variables:
* <ul>
* <li>{@link IUsernameSettable#BUILD_USER_VAR_NAME}</li>
* <li>{@link IUsernameSettable#BUILD_USER_FIRST_NAME_VAR_NAME}</li>
* <li>{@link IUsernameSettable#BUILD_USER_LAST_NAME_VAR_NAME}</li>
* </ul>
* This could be used with legacy version of jenkins, where {@link UserCause} is used instead of {@link UserIdCause}.
*
* @author GKonovalenko
*/
@SuppressWarnings("deprecation")
public class UserCauseDeterminant implements IUsernameSettable<UserCause> {
final Class<UserCause> causeClass = UserCause.class;

static final Class<UserCause> causeClass = UserCause.class;

Check warning on line 19 in src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserCauseDeterminant.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 19 is not covered by tests

/**
* {@inheritDoc}
Expand Down
Loading

0 comments on commit 0da9345

Please sign in to comment.