Skip to content

Commit

Permalink
Renamed PR Listener and added handling for other PR events
Browse files Browse the repository at this point in the history
Issue #38
  • Loading branch information
mikesir87 committed Jan 2, 2014
1 parent f5554a0 commit 82d1085
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.nerdwin15.stash.webhook;

import com.atlassian.event.api.EventListener;
import com.atlassian.stash.event.pull.PullRequestEvent;
import com.atlassian.stash.event.pull.PullRequestOpenedEvent;
import com.atlassian.stash.event.pull.PullRequestReopenedEvent;
import com.atlassian.stash.event.pull.PullRequestRescopedEvent;
import com.nerdwin15.stash.webhook.service.SettingsService;
import com.nerdwin15.stash.webhook.service.eligibility.EligibilityFilterChain;
Expand All @@ -12,8 +15,8 @@
* @author Michael Irwin (mikesir87)
* @author Melvyn de Kort (lordmatanza)
*/
public class PullRequestRescopeListener {

public class PullRequestEventListener {
private final EligibilityFilterChain filterChain;
private final Notifier notifier;
private final SettingsService settingsService;
Expand All @@ -24,19 +27,46 @@ public class PullRequestRescopeListener {
* @param notifier The notifier service
* @param settingsService Service to be used to get the Settings
*/
public PullRequestRescopeListener(EligibilityFilterChain filterChain,
public PullRequestEventListener(EligibilityFilterChain filterChain,
Notifier notifier, SettingsService settingsService) {
this.filterChain = filterChain;
this.notifier = notifier;
this.settingsService = settingsService;
}

/**
* Event listener that is notified of pull request open events
* @param event The pull request event
*/
@EventListener
public void onPullRequestOpened(PullRequestOpenedEvent event) {
handleEvent(event);
}

/**
* Event listener that is notified of pull request reopen events
* @param event The pull request event
*/
@EventListener
public void onPullRequestReopened(PullRequestReopenedEvent event) {
handleEvent(event);
}

/**
* Event listener that is notified of pull request rescope events
* @param event The pull request event
*/
@EventListener
public void onPullRequestRescope(PullRequestRescopedEvent event) {
handleEvent(event);
}

/**
* Actually handles the event that was triggered.
* (Made protected to make unit testing easier)
* @param event The event to be handled
*/
protected void handleEvent(PullRequestEvent event) {
if (settingsService.getSettings(event.getPullRequest().getToRef()
.getRepository()) == null) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<!-- Event listeners -->
<component key="repoChangeListener" class="com.nerdwin15.stash.webhook.RepositoryChangeListener"/>
<component key="pullRequestRescopeListener" class="com.nerdwin15.stash.webhook.PullRequestRescopeListener" />
<component key="pullRequestRescopeListener" class="com.nerdwin15.stash.webhook.PullRequestEventListener" />

<component key="settingsRetriever" class="com.nerdwin15.stash.webhook.service.ConcreteSettingsService" />
<component key="jenkinsNotifier" class="com.nerdwin15.stash.webhook.Notifier"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

Expand All @@ -16,7 +15,7 @@
import org.powermock.modules.junit4.PowerMockRunner;

import com.atlassian.stash.event.RepositoryRefsChangedEvent;
import com.atlassian.stash.event.pull.PullRequestRescopedEvent;
import com.atlassian.stash.event.pull.PullRequestEvent;
import com.atlassian.stash.pull.PullRequest;
import com.atlassian.stash.pull.PullRequestRef;
import com.atlassian.stash.repository.Repository;
Expand All @@ -32,14 +31,14 @@
* @author Michael Irwin (mikesir87)
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(PullRequestRescopedEvent.class)
public class PullRequestRescopeListenerTest {
@PrepareForTest(PullRequestEvent.class)
public class PullRequestEventListenerTest {

private Notifier notifier;
private EligibilityFilterChain filterChain;
private PullRequestRescopeListener listener;
private PullRequestEventListener listener;
private SettingsService settingsService;
private PullRequestRescopedEvent event;
private PullRequestEvent event;
private Repository repo;

/**
Expand All @@ -50,15 +49,15 @@ public void setup() throws Exception {
PullRequest request = mock(PullRequest.class);
PullRequestRef toRef = mock(PullRequestRef.class);

event = mock(PullRequestRescopedEvent.class);
event = mock(PullRequestEvent.class);
when(event.getPullRequest()).thenReturn(request);
when(request.getToRef()).thenReturn(toRef);
when(toRef.getRepository()).thenReturn(repo);

notifier = mock(Notifier.class);
filterChain = mock(EligibilityFilterChain.class);
settingsService = mock(SettingsService.class);
listener = new PullRequestRescopeListener(filterChain, notifier,
listener = new PullRequestEventListener(filterChain, notifier,
settingsService);
}

Expand All @@ -80,7 +79,7 @@ public void shouldNotifyWhenChainSaysOk() throws Exception {
when(filterChain.shouldDeliverNotification(contextCaptor.capture()))
.thenReturn(true);

listener.onPullRequestRescope(event);
listener.handleEvent(event);

verify(notifier).notify(repo);
assertEquals(event, contextCaptor.getValue().getEventSource());
Expand All @@ -107,7 +106,7 @@ public void shouldNotifyWhenChainSaysCancel() throws Exception {
when(filterChain.shouldDeliverNotification(contextCaptor.capture()))
.thenReturn(false);

listener.onPullRequestRescope(event);
listener.handleEvent(event);

verify(notifier, never()).notify(repo);
}
Expand All @@ -124,7 +123,7 @@ public void shouldntConsultChainWhenSettingsAreNull() throws Exception {
when(e.getRepository()).thenReturn(repo);
when(settingsService.getSettings(repo)).thenReturn(null);

listener.onPullRequestRescope(event);
listener.handleEvent(event);

verify(notifier, never()).notify(repo);
}
Expand Down

0 comments on commit 82d1085

Please sign in to comment.