Skip to content

Commit

Permalink
Deprecated User.get() replaced and clean-up (#27)
Browse files Browse the repository at this point in the history
* Code improvements

* Replace deprecated User.get()
  • Loading branch information
offa authored Oct 31, 2021
1 parent 0567882 commit 1532828
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import hudson.security.ACL;
import hudson.tasks.Mailer;
import hudson.model.User;
import hudson.model.UserProperty;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.acegisecurity.GrantedAuthority;
Expand Down Expand Up @@ -57,29 +56,29 @@ public boolean setJenkinsUserBuildVars(UserIdCause cause,
SecurityRealm realm = jenkinsInstance.getSecurityRealm();
userid = mapUserId (userid, realm);
GrantedAuthority[] authorities = realm.loadUserByUsername(originalUserid).getAuthorities();
for (int i = 0; i < authorities.length; i++) {
String authorityString = authorities[i].getAuthority();
for (GrantedAuthority authority : authorities) {
String authorityString = authority.getAuthority();
if (authorityString != null && authorityString.length() > 0) {
groupString.append(authorityString).append(",");
}
}
groupString.setLength(groupString.length() == 0 ? 0 : groupString.length() - 1);
} catch (Exception err) {
// Error
log.warning(String.format("Failed to get groups for user: %s error: %s ", userid, err.toString()));
log.warning(String.format("Failed to get groups for user: %s error: %s ", userid, err));
}
variables.put(BUILD_USER_ID, userid);
variables.put(BUILD_USER_VAR_GROUPS, groupString.toString());


User user=User.get(originalUserid);
if(null != user) {
UserProperty prop = user.getProperty(Mailer.UserProperty.class);
if(null != prop) {
String adrs = StringUtils.trimToEmpty(((Mailer.UserProperty)prop).getAddress());
variables.put(BUILD_USER_EMAIL, adrs);
}
}
User user = User.getById(originalUserid, false);
if (null != user) {
Mailer.UserProperty prop = user.getProperty(Mailer.UserProperty.class);
if (null != prop) {
String adrs = StringUtils.trimToEmpty(prop.getAddress());
variables.put(BUILD_USER_EMAIL, adrs);
}
}

return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public void testMakeUserBuildVariablesWithoutUpstream() throws Exception {
// Register non-existent build as an execution cause
Build<FreeStyleProject, FreeStyleBuild> downstreamBuild = childProject.getLastBuild();
List<CauseAction> actions = downstreamBuild.getActions(CauseAction.class);
Assert.assertTrue("CauseAction has not been created properly",
actions != null && actions.size() == 1);
Assert.assertEquals("CauseAction has not been created properly", 1, actions.size());
Cause.UpstreamCause upstreamCause = null;
List<Cause> causes = actions.get(0).getCauses();
for (Cause cause : causes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public void uiAndStorage() throws Throwable {
BuildUserVarsConfig.get().isAllBuilds());
});
sessions.then(
r -> {
assertTrue(
"still there after restart of Jenkins",
BuildUserVarsConfig.get().isAllBuilds());
});
r -> assertTrue(
"still there after restart of Jenkins",
BuildUserVarsConfig.get().isAllBuilds()));
}
}

0 comments on commit 1532828

Please sign in to comment.