Skip to content

Commit

Permalink
avoid NPE when outsite of a job context (#413)
Browse files Browse the repository at this point in the history
Avoid NPE when outside of a job context
  • Loading branch information
olamy committed Feb 16, 2022
1 parent c5d2c85 commit d57cb34
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/main/java/hudson/plugins/jira/JiraMailAddressResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import hudson.model.User;
import hudson.tasks.MailAddressResolver;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Pattern;

Expand All @@ -32,9 +34,16 @@ public String findMailAddressFor(User u) {
}
String username = u.getId();

Job<?, ?> job = Stapler.getCurrentRequest().findAncestorObject( Job.class);
Job<?, ?> job = null;

for (JiraSite site : JiraSite.getJiraSites(job)) {
StaplerRequest req = Stapler.getCurrentRequest();
if(req != null) {
job = req.findAncestorObject(Job.class);
}

List<JiraSite> sites = job == null ? JiraGlobalConfiguration.get().getSites() : JiraSite.getJiraSites(job);

for (JiraSite site : sites) {
JiraSession session = site.getSession(job);
if (session == null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* @author schristou88
*/
public class MailResolverTest extends JenkinsRule {
public class MailResolverDisabledTest extends JenkinsRule {
@Rule
public JenkinsRule r = new JenkinsRule();

Expand Down
116 changes: 116 additions & 0 deletions src/test/java/hudson/plugins/jira/MailResolverWithExtensionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* The MIT License
*
* Copyright (c) 2015 schristou88
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.jira;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.User;
import hudson.security.HudsonPrivateSecurityRealm;
import jenkins.model.Jenkins;
import jenkins.security.SecurityListener;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.reflect.Whitebox;
import org.springframework.security.core.userdetails.UserDetails;

import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;

@RunWith(MockitoJUnitRunner.class)
public class MailResolverWithExtensionTest extends JenkinsRule {
@Rule
public JenkinsRule r = new JenkinsRule();

@Mock
JiraSite site;
@Mock
JiraSession session;
@Mock
JiraRestService service;

@Before
public void createMocks() throws Exception {
Whitebox.setInternalState(site,"jiraSession", session);
Whitebox.setInternalState(session,"service", service);

Map<String, URI> avatars = new HashMap<>();
// pre check condition in Jira User constructor and do not ask me why!!
avatars.put("48x48", new URI("https://foo.com"));
com.atlassian.jira.rest.client.api.domain.User jiraUser =
new com.atlassian.jira.rest.client.api.domain.User(null, "foo", "bar", "foo@beer.com", true, null, avatars, null);

doReturn(session).when(site).getSession(any());
doReturn(jiraUser).when(service).getUser("foo");
}

@Test
public void emailResolverWithSecurityExtension() throws Exception {

HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(true);
realm.createAccount("foo", "pacific_ale");

r.jenkins.setSecurityRealm(realm);

JiraGlobalConfiguration.get().setSites(Collections.singletonList(site));

r.createWebClient().login("foo", "pacific_ale");
}

@TestExtension
public static class DummySecurityListener extends SecurityListener {
@Override
protected void authenticated2(@NonNull UserDetails details) {
check(details.getUsername());
}

@Override
protected void authenticated(@NonNull org.acegisecurity.userdetails.UserDetails details) {
check(details.getUsername());
}

private void check(String userId) {

User user = User.getById(userId, false);

JiraMailAddressResolver jiraMailAddressResolver =
Jenkins.get().getExtensionList(JiraMailAddressResolver.class).get(0);
String email = jiraMailAddressResolver.findMailAddressFor(user);
assertThat(email, is("foo@beer.com"));

}

}
}

0 comments on commit d57cb34

Please sign in to comment.