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

Commit 440ec96

Browse files
committed
Removing RESCOPED event, will trigger _FROM and _TO instead #37
* Also adding logging that shows event name together with from and to hashes.
1 parent 76a9217 commit 440ec96

File tree

10 files changed

+297
-197
lines changed

10 files changed

+297
-197
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Changelog of Pull Request Notifier for Stash.
44

5+
## 1.15
6+
* Removing RESCOPED event, its confusing when to use it together with _FROM and _TO. RESCOPED was triggered when both _FROM and _TO changed at the exact same time. Now, just check _FROM if you only want to trigger when source branch changes, _TO if only target and both if you want to trigger for both.
7+
* Adding logging to make it easier to debug what events are triggered.
8+
59
## 1.14
610
* New variables with information about the user who issued the event
711
* ${PULL_REQUEST_USER_DISPLAY_NAME} Example: Some User

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The Pull Request Notifier for Stash can:
2626
* Send custom HTTP headers
2727
* Can optionally use proxy to connect
2828

29-
If you only want to trigger on RESCOPED_FROM, or RESCOPED_TO, you will also need to trigger on RESCOPED. Stash will fire an event, RESCOPED, if target and/or source branch is changed. The plugin has its own implementation to create the RESCOPED_FROM and RESCOPED_TO events. RESCOPED is transformed to RESCOPED_FROM if only source branch changed, RESCOPED_TO if only target branch changed and kept as RESCOPED if both changed.
29+
The plugin has its own implementation to create the RESCOPED_FROM and RESCOPED_TO events. RESCOPED is transformed to RESCOPED_TO if target branch changed, RESCOPED_FROM if source branch, or both, changed.
3030

3131
The filter text as well as the URL support variables. These are:
3232

src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestAction.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
import static com.atlassian.stash.pull.PullRequestAction.UNAPPROVED;
1111
import static com.atlassian.stash.pull.PullRequestAction.UPDATED;
1212
import static com.google.common.collect.Lists.newArrayList;
13+
import static java.lang.Boolean.FALSE;
1314

1415
import java.util.List;
1516
import java.util.Map;
1617

18+
import se.bjurr.prnfs.settings.PrnfsNotification;
19+
1720
import com.atlassian.stash.event.pull.PullRequestEvent;
1821
import com.atlassian.stash.event.pull.PullRequestRescopedEvent;
1922
import com.google.common.collect.ImmutableMap;
@@ -30,7 +33,7 @@ public class PrnfsPullRequestAction {
3033
.put(MERGED.name(), new PrnfsPullRequestAction(MERGED.name())) //
3134
.put(OPENED.name(), new PrnfsPullRequestAction(OPENED.name())) //
3235
.put(REOPENED.name(), new PrnfsPullRequestAction(REOPENED.name())) //
33-
.put(RESCOPED.name(), new PrnfsPullRequestAction(RESCOPED.name())) //
36+
.put(RESCOPED.name(), new PrnfsPullRequestAction(RESCOPED_FROM)) //
3437
.put(RESCOPED_FROM, new PrnfsPullRequestAction(RESCOPED_FROM)) //
3538
.put(RESCOPED_TO, new PrnfsPullRequestAction(RESCOPED_TO)) //
3639
.put(UNAPPROVED.name(), new PrnfsPullRequestAction(UNAPPROVED.name())) //
@@ -62,8 +65,7 @@ public static List<PrnfsPullRequestAction> values() {
6265
return newArrayList(values.values());
6366
}
6467

65-
@SuppressWarnings("deprecation")
66-
public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event) {
68+
public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event, PrnfsNotification notification) {
6769
if (event instanceof PullRequestRescopedEvent) {
6870
PullRequestRescopedEvent rescopedEvent = (PullRequestRescopedEvent) event;
6971
boolean toChanged = !rescopedEvent.getPreviousToHash().equals(
@@ -74,8 +76,22 @@ public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event
7476
return PrnfsPullRequestAction.valueOf(RESCOPED_FROM);
7577
} else if (toChanged && !fromChanged) {
7678
return PrnfsPullRequestAction.valueOf(RESCOPED_TO);
79+
} else {
80+
if (notification.getTriggers().contains(values.get(RESCOPED_FROM))) {
81+
return PrnfsPullRequestAction.valueOf(RESCOPED_FROM);
82+
} else if (notification.getTriggers().contains(values.get(RESCOPED_TO))) {
83+
return PrnfsPullRequestAction.valueOf(RESCOPED_TO);
84+
}
7785
}
7886
}
7987
return PrnfsPullRequestAction.valueOf(event.getAction().name());
8088
}
89+
90+
@Override
91+
public boolean equals(Object obj) {
92+
if (obj instanceof PrnfsPullRequestAction) {
93+
return getName().equals(((PrnfsPullRequestAction) obj).getName());
94+
}
95+
return FALSE;
96+
}
8197
}

src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.atlassian.stash.event.pull.PullRequestRescopedEvent;
3030
import com.atlassian.stash.event.pull.PullRequestUnapprovedEvent;
3131
import com.atlassian.stash.event.pull.PullRequestUpdatedEvent;
32+
import com.atlassian.stash.pull.PullRequest;
3233
import com.atlassian.stash.repository.RepositoryService;
3334
import com.google.common.annotations.VisibleForTesting;
3435
import com.google.common.base.Optional;
@@ -62,55 +63,57 @@ public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory
6263

6364
@EventListener
6465
public void onEvent(PullRequestApprovedEvent e) {
65-
handleEvent(e, fromPullRequestEvent(e));
66+
handleEvent(e);
6667
}
6768

6869
@EventListener
6970
public void onEvent(PullRequestCommentAddedEvent e) {
70-
handleEvent(e, fromPullRequestEvent(e));
71+
handleEvent(e);
7172
}
7273

7374
@EventListener
7475
public void onEvent(PullRequestDeclinedEvent e) {
75-
handleEvent(e, fromPullRequestEvent(e));
76+
handleEvent(e);
7677
}
7778

7879
@EventListener
7980
public void onEvent(PullRequestMergedEvent e) {
80-
handleEvent(e, fromPullRequestEvent(e));
81+
handleEvent(e);
8182
}
8283

8384
@EventListener
8485
public void onEvent(PullRequestOpenedEvent e) {
85-
handleEvent(e, fromPullRequestEvent(e));
86+
handleEvent(e);
8687
}
8788

8889
@EventListener
8990
public void onEvent(PullRequestReopenedEvent e) {
90-
handleEvent(e, fromPullRequestEvent(e));
91+
handleEvent(e);
9192
}
9293

9394
@EventListener
9495
public void onEvent(final PullRequestRescopedEvent e) {
95-
handleEvent(e, fromPullRequestEvent(e));
96+
handleEvent(e);
9697
}
9798

9899
@EventListener
99100
public void onEvent(PullRequestUnapprovedEvent e) {
100-
handleEvent(e, fromPullRequestEvent(e));
101+
handleEvent(e);
101102
}
102103

103104
@EventListener
104105
public void onEvent(PullRequestUpdatedEvent e) {
105-
handleEvent(e, fromPullRequestEvent(e));
106+
handleEvent(e);
106107
}
107108

108109
@VisibleForTesting
109-
public void handleEvent(PullRequestEvent pullRequestEvent, PrnfsPullRequestAction action) {
110-
final PrnfsRenderer renderer = new PrnfsRenderer(pullRequestEvent, repositoryService);
110+
public void handleEvent(PullRequestEvent pullRequestEvent) {
111111
try {
112112
final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings());
113113
for (final PrnfsNotification notification : settings.getNotifications()) {
114+
final PrnfsRenderer renderer = new PrnfsRenderer(pullRequestEvent, repositoryService, notification);
115+
PrnfsPullRequestAction action = fromPullRequestEvent(pullRequestEvent, notification);
116+
PullRequest pr = pullRequestEvent.getPullRequest();
114117
if (notification.getFilterRegexp().isPresent()
115118
&& notification.getFilterString().isPresent()
116119
&& !compile(notification.getFilterRegexp().get()).matcher(renderer.render(notification.getFilterString().get()))
@@ -122,8 +125,13 @@ public void handleEvent(PullRequestEvent pullRequestEvent, PrnfsPullRequestActio
122125
if (notification.getPostContent().isPresent()) {
123126
postContent = Optional.of(renderer.render(notification.getPostContent().get()));
124127
}
125-
UrlInvoker urlInvoker = urlInvoker().withUrlParam(renderer.render(notification.getUrl()))
126-
.withMethod(notification.getMethod()).withPostContent(postContent);
128+
String renderedUrl = renderer.render(notification.getUrl());
129+
logger.info(action.getName() + " "//
130+
+ pr.getFromRef().getId() + "(" + pr.getFromRef().getLatestChangeset() + ") -> " //
131+
+ pr.getToRef().getId() + "(" + pr.getToRef().getLatestChangeset() + ")" + " " //
132+
+ renderedUrl);
133+
UrlInvoker urlInvoker = urlInvoker().withUrlParam(renderedUrl).withMethod(notification.getMethod())
134+
.withPostContent(postContent);
127135
if (notification.getUser().isPresent() && notification.getPassword().isPresent()) {
128136
final String userpass = notification.getUser().get() + ":" + notification.getPassword().get();
129137
final String basicAuth = "Basic " + new String(printBase64Binary(userpass.getBytes(UTF_8)));

0 commit comments

Comments
 (0)