1
1
package se .bjurr .prnfb .listener ;
2
2
3
+ import static com .atlassian .bitbucket .permission .Permission .ADMIN ;
3
4
import static com .google .common .base .Optional .absent ;
4
5
import static com .google .common .base .Optional .of ;
5
6
import static java .lang .Boolean .FALSE ;
16
17
17
18
import org .slf4j .Logger ;
18
19
20
+ import se .bjurr .prnfb .http .ClientKeyStore ;
21
+ import se .bjurr .prnfb .http .HttpResponse ;
22
+ import se .bjurr .prnfb .http .Invoker ;
23
+ import se .bjurr .prnfb .http .NotificationResponse ;
24
+ import se .bjurr .prnfb .http .UrlInvoker ;
25
+ import se .bjurr .prnfb .service .PrnfbRenderer ;
26
+ import se .bjurr .prnfb .service .PrnfbRenderer .ENCODE_FOR ;
27
+ import se .bjurr .prnfb .service .PrnfbRendererFactory ;
28
+ import se .bjurr .prnfb .service .SettingsService ;
29
+ import se .bjurr .prnfb .service .VariablesContext ;
30
+ import se .bjurr .prnfb .service .VariablesContext .VariablesContextBuilder ;
31
+ import se .bjurr .prnfb .settings .PrnfbHeader ;
32
+ import se .bjurr .prnfb .settings .PrnfbNotification ;
33
+ import se .bjurr .prnfb .settings .PrnfbSettingsData ;
34
+ import se .bjurr .prnfb .settings .TRIGGER_IF_MERGE ;
35
+
19
36
import com .atlassian .bitbucket .event .pull .PullRequestCommentAddedEvent ;
20
37
import com .atlassian .bitbucket .event .pull .PullRequestCommentDeletedEvent ;
21
38
import com .atlassian .bitbucket .event .pull .PullRequestCommentEditedEvent ;
31
48
import com .atlassian .bitbucket .event .pull .PullRequestUpdatedEvent ;
32
49
import com .atlassian .bitbucket .pull .PullRequest ;
33
50
import com .atlassian .bitbucket .pull .PullRequestService ;
51
+ import com .atlassian .bitbucket .user .SecurityService ;
52
+ import com .atlassian .bitbucket .util .Operation ;
34
53
import com .atlassian .event .api .EventListener ;
35
54
import com .google .common .annotations .VisibleForTesting ;
36
55
import com .google .common .base .Optional ;
37
56
38
- import se .bjurr .prnfb .http .ClientKeyStore ;
39
- import se .bjurr .prnfb .http .HttpResponse ;
40
- import se .bjurr .prnfb .http .Invoker ;
41
- import se .bjurr .prnfb .http .NotificationResponse ;
42
- import se .bjurr .prnfb .http .UrlInvoker ;
43
- import se .bjurr .prnfb .service .PrnfbRenderer ;
44
- import se .bjurr .prnfb .service .PrnfbRenderer .ENCODE_FOR ;
45
- import se .bjurr .prnfb .service .PrnfbRendererFactory ;
46
- import se .bjurr .prnfb .service .SettingsService ;
47
- import se .bjurr .prnfb .service .VariablesContext ;
48
- import se .bjurr .prnfb .service .VariablesContext .VariablesContextBuilder ;
49
- import se .bjurr .prnfb .settings .PrnfbHeader ;
50
- import se .bjurr .prnfb .settings .PrnfbNotification ;
51
- import se .bjurr .prnfb .settings .PrnfbSettingsData ;
52
- import se .bjurr .prnfb .settings .TRIGGER_IF_MERGE ;
53
-
54
57
public class PrnfbPullRequestEventListener {
55
58
56
59
private static final Logger LOG = getLogger (PrnfbPullRequestEventListener .class );
@@ -64,18 +67,21 @@ public static void setInvoker(Invoker invoker) {
64
67
private final ExecutorService executorService ;
65
68
private final PrnfbRendererFactory prnfbRendererFactory ;
66
69
private final PullRequestService pullRequestService ;
70
+ private final SecurityService securityService ;
67
71
68
72
private final SettingsService settingsService ;
69
73
70
74
public PrnfbPullRequestEventListener (
71
75
PrnfbRendererFactory prnfbRendererFactory ,
72
76
PullRequestService pullRequestService ,
73
77
ExecutorService executorService ,
74
- SettingsService settingsService ) {
78
+ SettingsService settingsService ,
79
+ SecurityService securityService ) {
75
80
this .prnfbRendererFactory = prnfbRendererFactory ;
76
81
this .pullRequestService = pullRequestService ;
77
82
this .executorService = executorService ;
78
83
this .settingsService = settingsService ;
84
+ this .securityService = securityService ;
79
85
}
80
86
81
87
private Invoker createInvoker () {
@@ -187,9 +193,19 @@ public boolean isNotificationTriggeredByAction(
187
193
if (notification .getTriggerIfCanMerge () != ALWAYS && pullRequest .isOpen ()) {
188
194
// Cannot perform canMerge unless PR is open
189
195
boolean isConflicted =
190
- pullRequestService
191
- .canMerge (pullRequest .getToRef ().getRepository ().getId (), pullRequest .getId ())
192
- .isConflicted ();
196
+ securityService //
197
+ .withPermission (ADMIN , "Can merge" ) //
198
+ .call (
199
+ new Operation <Boolean , RuntimeException >() {
200
+ @ Override
201
+ public Boolean perform () throws RuntimeException {
202
+ return pullRequestService //
203
+ .canMerge (
204
+ pullRequest .getToRef ().getRepository ().getId (),
205
+ pullRequest .getId ()) //
206
+ .isConflicted ();
207
+ }
208
+ });
193
209
if (ignoreBecauseOfConflicting (notification .getTriggerIfCanMerge (), isConflicted )) {
194
210
return FALSE ;
195
211
}
0 commit comments