Skip to content
Permalink
Browse files

Merge pull request #65 from oleg-nenashev/JENKINS-16063

[JENKINS-16063] - Inject PROMOTED_USER_ID variable + minor issue fixes
  • Loading branch information
oleg-nenashev committed Aug 20, 2015
2 parents 84bf6f9 + 4fa8d6f commit c34605266948377c7de8432e3caeef293896f171
@@ -8,6 +8,7 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Cause.UserCause;
import hudson.model.Cause.UserIdCause;
import hudson.model.Environment;
import hudson.model.Node;
import hudson.model.ParameterDefinition;
@@ -17,6 +18,7 @@
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.model.Run;
import hudson.model.User;
import hudson.plugins.promoted_builds.conditions.ManualCondition;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
@@ -38,6 +40,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import javax.annotation.Nonnull;

/**
* Records a promotion process.
@@ -103,7 +106,8 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru
e.put("PROMOTED_ID", target.getId());
e.put("PROMOTED_DISPLAY_NAME", target.getDisplayName());
e.put("PROMOTED_USER_NAME", getUserName());
EnvVars envScm = new EnvVars();
e.put("PROMOTED_USER_ID", getUserId());
EnvVars envScm = new EnvVars();
target.getProject().getScm().buildEnvVars( target, envScm );
for ( Entry<String, String> entry : envScm.entrySet() )
{
@@ -118,24 +122,76 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru


/**
*
* @return user's name who triggered the promotion, or 'anonymous'
* Get a user name of the person, who triggered the promotion.
* The method tries various sources like {@link UserIdCause} or {@link ManualCondition.Badge}.
* @return user's name who triggered the promotion, or 'anonymous' if the search fails
*/
public String getUserName(){
UserCause userClause=getCause(UserCause.class);
if (userClause!=null && userClause.getUserName()!=null){
return userClause.getUserName();
}

//fallback to badge lookup for compatibility
for (PromotionBadge badget:getStatus().getBadges()){
if (badget instanceof ManualCondition.Badge){
return ((ManualCondition.Badge) badget).getUserName();
}
}
return "anonymous";
@Nonnull
public String getUserName() {
// Deprecated, but we still want to support it in order to maintain the compatiibility
final UserCause userCause = getCause(UserCause.class);
final String nameFromUserCause = userCause != null ? userCause.getUserName() : null;
if (nameFromUserCause != null) {
return nameFromUserCause;
}

// Modern UserIdCause
final UserIdCause userIdCause = getCause(UserIdCause.class);
final String nameFromUserIdCause = userIdCause != null ? userIdCause.getUserName() : null;
if (nameFromUserIdCause != null) {
return nameFromUserIdCause;
}

//fallback to badge lookup for compatibility
for (PromotionBadge badget : getStatus().getBadges()) {
if (badget instanceof ManualCondition.Badge) {
final String nameFromBadge = ((ManualCondition.Badge) badget).getUserName();
if (nameFromBadge != null) {
return nameFromBadge;
}
}
}
return Jenkins.ANONYMOUS.getName();
}


/**
* Gets ID of the {@link User}, who triggered the promotion.
* The method tries various sources like {@link UserIdCause} or {@link ManualCondition.Badge}.
* @return ID of the user who triggered the promotion.
* If the search fails, returns ID of {@link User#getUnknown()}.
* @since 2.22
*/
@Nonnull
public String getUserId() {
// Deprecated, but we still want to support it in order to maintain the compatiibility
// We try to convert the cause to the user ID by using a search by the full name, not reliable
final UserCause userCause = getCause(UserCause.class);
final String nameFromUserCause = userCause != null ? userCause.getUserName(): null;
final User user = nameFromUserCause != null ? User.get(nameFromUserCause, false, null) : null;
if (user != null) {
return user.getId();
}

// Modern UserIdCause
final UserIdCause userIdCause = getCause(UserIdCause.class);
final String idFromUserIdCause = userIdCause != null ? userIdCause.getUserId(): null;
if (idFromUserIdCause != null) {
return idFromUserIdCause;
}

//fallback to badge lookup for compatibility
for (PromotionBadge badget : getStatus().getBadges()) {
if (badget instanceof ManualCondition.Badge) {
final String idFromBadge = ((ManualCondition.Badge) badget).getUserId();
if (idFromBadge != null) {
return idFromBadge;
}
}
}
return User.getUnknown().getId();
}

public List<ParameterValue> getParameterValues(){
List<ParameterValue> values=new ArrayList<ParameterValue>();
ParametersAction parametersAction=getParametersActions(this);
@@ -234,10 +234,19 @@ public String getUserName() {
if (authenticationName == null)
return "N/A";

User u = User.get(authenticationName, false);
User u = User.get(authenticationName, false, null);
return u != null ? u.getDisplayName() : authenticationName;
}

@Exported
public String getUserId() {
if (authenticationName == null) {
return "N/A";
}

return authenticationName;
}

@Exported
public List<ParameterValue> getParameterValues() {
return values != null ? values : Collections.EMPTY_LIST;
@@ -7,10 +7,12 @@
import hudson.model.FreeStyleProject;
import hudson.model.ParameterValue;
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.plugins.promoted_builds.conditions.ManualCondition;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import org.acegisecurity.context.SecurityContextHolder;
import org.junit.Test;
import org.junit.Rule;
import org.jvnet.hudson.test.JenkinsRule;
@@ -26,8 +28,14 @@
@Rule public JenkinsRule r = new JenkinsRule();

@Test
public void shouldSetJobAndJobFullNames() throws Descriptor.FormException, IOException, InterruptedException, ExecutionException {
public void shouldSetJobAndJobFullNames() throws Descriptor.FormException, IOException, InterruptedException, ExecutionException, Exception {
// Assemble
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
User u = User.get("foo");
u.setFullName("Foobar");

SecurityContextHolder.getContext().setAuthentication(u.impersonate());

MockFolder parent = r.createFolder("Folder");
FreeStyleProject project = parent.createProject(FreeStyleProject.class, "Project");

@@ -51,7 +59,8 @@ public void shouldSetJobAndJobFullNames() throws Descriptor.FormException, IOExc
// Assert
assertEquals("Folder/Project", env.get("PROMOTED_JOB_FULL_NAME"));
assertEquals("Project", env.get("PROMOTED_JOB_NAME"));
assertEquals("SYSTEM", env.get("PROMOTED_USER_NAME"));
assertEquals("Foobar", env.get("PROMOTED_USER_NAME"));
assertEquals("foo", env.get("PROMOTED_USER_ID"));
assertEquals("1234", env.get("PROMOTED_DISPLAY_NAME"));

project.delete();

0 comments on commit c346052

Please sign in to comment.
You can’t perform that action at this time.