Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Finding current user correctly #107
Browse files Browse the repository at this point in the history
 * Was looking up currently logged in user with the users username. That caused NullPointerException  if username not same as user slug.
  • Loading branch information
tomasbjerre committed Apr 2, 2016
1 parent 97dc36d commit 296f894
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Changelog of Pull Request Notifier for Bitbucket.
### No issue
Using git changelog plugin 1.20

[2a8a6b934a6a6b2](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/2a8a6b934a6a6b2) Tomas Bjerre *2016-03-15 21:10:32*
[97dc36d24773408](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/97dc36d24773408) Tomas Bjerre *2016-03-15 21:15:55*

## 2.16
### GitHub [#103](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/103) Support Bitbucket Server 4.4
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/se/bjurr/prnfb/ManualResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@
import se.bjurr.prnfb.settings.PrnfbSettings;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestService;
import com.atlassian.bitbucket.repository.RepositoryService;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.SecurityService;
import com.atlassian.bitbucket.user.UserService;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.sal.api.user.UserKey;
import com.atlassian.sal.api.user.UserManager;
Expand All @@ -59,13 +58,13 @@
public class ManualResource {
private static Gson gson = new Gson();
private final UserManager userManager;
private final UserService userService;
private final PullRequestService pullRequestService;
private final PrnfbPullRequestEventListener prnfbPullRequestEventListener;
private final SecurityService securityService;
private final PluginSettingsFactory pluginSettingsFactory;
private final ApplicationPropertiesService propertiesService;
private final RepositoryService repositoryService;
private final AuthenticationContext authenticationContext;
private static final List<AdminFormValues.BUTTON_VISIBILITY> adminOk = newArrayList();
private static final List<AdminFormValues.BUTTON_VISIBILITY> systemAdminOk = newArrayList();
static {
Expand All @@ -74,11 +73,12 @@ public class ManualResource {
systemAdminOk.add(SYSTEM_ADMIN);
}

public ManualResource(UserManager userManager, UserService userService, PluginSettingsFactory pluginSettingsFactory,
PullRequestService pullRequestService, PrnfbPullRequestEventListener prnfbPullRequestEventListener,
RepositoryService repositoryService, ApplicationPropertiesService propertiesService, SecurityService securityService) {
public ManualResource(AuthenticationContext authenticationContext, UserManager userManager,
PluginSettingsFactory pluginSettingsFactory, PullRequestService pullRequestService,
PrnfbPullRequestEventListener prnfbPullRequestEventListener, RepositoryService repositoryService,
ApplicationPropertiesService propertiesService, SecurityService securityService) {
this.authenticationContext = authenticationContext;
this.userManager = userManager;
this.userService = userService;
this.pullRequestService = pullRequestService;
this.prnfbPullRequestEventListener = prnfbPullRequestEventListener;
this.securityService = securityService;
Expand Down Expand Up @@ -159,9 +159,8 @@ private Map<PrnfbVariable, Supplier<String>> getVariables(final PrnfbSettings se

private PrnfbRenderer getRenderer(final PullRequest pullRequest, PrnfbNotification prnfbNotification,
PrnfbPullRequestAction pullRequestAction, Map<PrnfbVariable, Supplier<String>> variables, HttpServletRequest request) {
ApplicationUser bitbucketUser = userService.getUserBySlug(userManager.getRemoteUser(request).getUsername());
return new PrnfbRenderer(pullRequest, pullRequestAction, bitbucketUser, repositoryService, propertiesService,
prnfbNotification, variables);
return new PrnfbRenderer(pullRequest, pullRequestAction, authenticationContext.getCurrentUser(), repositoryService,
propertiesService, prnfbNotification, variables);
}

static boolean allowedUseButton(PrnfbButton candidate, boolean isAdmin, boolean isSystemAdmin) {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/se/bjurr/prnb/admin/utils/PrnfbTestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import se.bjurr.prnfb.settings.PrnfbSettings;
import se.bjurr.prnfb.settings.SettingsStorage;

import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.bitbucket.event.pull.PullRequestEvent;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.pull.PullRequest;
Expand All @@ -63,7 +64,6 @@
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.user.EscalatedSecurityContext;
import com.atlassian.bitbucket.user.SecurityService;
import com.atlassian.bitbucket.user.UserService;
import com.atlassian.bitbucket.util.Operation;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
Expand Down Expand Up @@ -134,6 +134,8 @@ public static PrnfbTestBuilder prnfbTestBuilder() {

private PullRequest pullRequest;

private AuthenticationContext authenticationContext;

private PrnfbTestBuilder() {
fakeRandomCounter = 0L;
fakeRandom(new FakeRandom());
Expand All @@ -159,10 +161,10 @@ public <T> T execute(TransactionCallback<T> action) {
pullRequestService = mock(PullRequestService.class);
listener = new PrnfbPullRequestEventListener(pluginSettingsFactory, repositoryService, propertiesService,
pullRequestService, new SyncExecutorService());
UserService userService = mock(UserService.class);
withPullRequest(pullRequestEventBuilder().build().getPullRequest());
manualResouce = new ManualResource(userManager, userService, pluginSettingsFactory, pullRequestService, listener,
repositoryService, propertiesService, securityService);
authenticationContext = mock(AuthenticationContext.class);
manualResouce = new ManualResource(authenticationContext, userManager, pluginSettingsFactory, pullRequestService,
listener, repositoryService, propertiesService, securityService);
}

public PrnfbTestBuilder delete(String id) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1603,19 +1603,23 @@ public void testThatEventTriggeredByButtonCanResultInSeveralNotifications() thro
.withFieldValue(FORM_TYPE, TRIGGER_CONFIG_FORM.name()) //
.build() //
) //
.store().withNotification( //
.store()//
.withNotification( //
notificationBuilder() //
.withFieldValue(url, "http://bjurr.se/?456=${" + BUTTON_TRIGGER_TITLE + "}") //
.withFieldValue(events, BUTTON_TRIGGER) //
.withFieldValue(filter_string, "${" + BUTTON_TRIGGER_TITLE + "}") //
.withFieldValue(filter_regexp, "button") //
.withFieldValue(FORM_TYPE, TRIGGER_CONFIG_FORM.name()).build()) //
.store().withNotification( //
.withFieldValue(FORM_TYPE, TRIGGER_CONFIG_FORM.name())//
.build()) //
.store()//
.withNotification( //
notificationBuilder() //
.withFieldValue(FORM_IDENTIFIER, "Button Form") //
.withFieldValue(FORM_TYPE, BUTTON_CONFIG_FORM.name()) //
.withFieldValue(button_title, "button text 123") //
.withFieldValue(button_visibility, EVERYONE.name()).build()) //
.withFieldValue(button_visibility, EVERYONE.name())//
.build()) //
.store() //
.triggerButton("Button Form") //
.invokedUrl(0, "http://bjurr.se/?123=button+text+123") //
Expand Down

0 comments on commit 296f894

Please sign in to comment.