44import static javax .ws .rs .core .MediaType .APPLICATION_JSON ;
55import static javax .ws .rs .core .Response .ok ;
66import static javax .ws .rs .core .Response .status ;
7+ import static javax .ws .rs .core .Response .Status .NOT_FOUND ;
78import static javax .ws .rs .core .Response .Status .OK ;
89import static javax .ws .rs .core .Response .Status .UNAUTHORIZED ;
910import static se .bjurr .prnfb .transformer .ButtonTransformer .toButtonDto ;
2728import javax .ws .rs .core .Response ;
2829
2930import com .atlassian .annotations .security .XsrfProtectionExcluded ;
31+ import com .google .common .base .Optional ;
32+ import com .google .common .collect .Iterables ;
3033
3134import se .bjurr .prnfb .http .NotificationResponse ;
3235import se .bjurr .prnfb .presentation .dto .ButtonDTO ;
@@ -60,9 +63,7 @@ public ButtonServlet(
6063 @ Consumes (APPLICATION_JSON )
6164 @ Produces (APPLICATION_JSON )
6265 public Response create (ButtonDTO buttonDto ) {
63- if (!userCheckService .isAdminAllowed ( //
64- buttonDto .getProjectKey ().orNull (), //
65- buttonDto .getRepositorySlug ().orNull ())) {
66+ if (!userCheckService .isAdminAllowed (buttonDto )) {
6667 return status (UNAUTHORIZED ) //
6768 .build ();
6869 }
@@ -82,10 +83,7 @@ public Response create(ButtonDTO buttonDto) {
8283 @ Produces (APPLICATION_JSON )
8384 public Response delete (@ PathParam ("uuid" ) UUID prnfbButtonUuid ) {
8485 PrnfbButton prnfbButton = settingsService .getButton (prnfbButtonUuid );
85- if (!userCheckService .isAdminAllowed ( //
86- prnfbButton .getProjectKey ().orNull () //
87- ,
88- prnfbButton .getRepositorySlug ().orNull ())) {
86+ if (!userCheckService .isAdminAllowed (prnfbButton )) {
8987 return status (UNAUTHORIZED ) //
9088 .build ();
9189 }
@@ -97,40 +95,18 @@ public Response delete(@PathParam("uuid") UUID prnfbButtonUuid) {
9795 @ Produces (APPLICATION_JSON )
9896 public Response get () {
9997 List <PrnfbButton > buttons = settingsService .getButtons ();
100- Iterable <PrnfbButton > allowedButtons = userCheckService .filterAllowed (buttons );
98+ Iterable <PrnfbButton > allowedButtons = userCheckService .filterAdminAllowed (buttons );
10199 List <ButtonDTO > dtos = toButtonDtoList (allowedButtons );
102100 Collections .sort (dtos );
103101 return ok (dtos , APPLICATION_JSON ).build ();
104102 }
105103
106- @ GET
107- @ Path ("/repository/{repositoryId}/pullrequest/{pullRequestId}" )
108- @ Produces (APPLICATION_JSON )
109- public Response get (
110- @ PathParam ("repositoryId" ) Integer repositoryId ,
111- @ PathParam ("pullRequestId" ) Long pullRequestId ) {
112- if (!userCheckService .isViewAllowed ()) {
113- return status (UNAUTHORIZED ).build ();
114- }
115- List <PrnfbButton > buttons = buttonsService .getButtons (repositoryId , pullRequestId );
116- Iterable <PrnfbButton > allowedButtons = userCheckService .filterAllowed (buttons );
117- List <ButtonDTO > dtos = toButtonDtoList (allowedButtons );
118- Collections .sort (dtos );
119-
120- populateButtonFormDtoList (repositoryId , pullRequestId , dtos );
121-
122- return ok (dtos , APPLICATION_JSON ).build ();
123- }
124-
125104 @ GET
126105 @ Path ("/projectKey/{projectKey}" )
127106 @ Produces (APPLICATION_JSON )
128107 public Response get (@ PathParam ("projectKey" ) String projectKey ) {
129- if (!userCheckService .isViewAllowed ()) {
130- return status (UNAUTHORIZED ).build ();
131- }
132108 List <PrnfbButton > buttons = settingsService .getButtons (projectKey );
133- Iterable <PrnfbButton > allowedButtons = userCheckService .filterAllowed (buttons );
109+ Iterable <PrnfbButton > allowedButtons = userCheckService .filterAdminAllowed (buttons );
134110 List <ButtonDTO > dtos = toButtonDtoList (allowedButtons );
135111 Collections .sort (dtos );
136112 return ok (dtos , APPLICATION_JSON ).build ();
@@ -142,11 +118,8 @@ public Response get(@PathParam("projectKey") String projectKey) {
142118 public Response get (
143119 @ PathParam ("projectKey" ) String projectKey ,
144120 @ PathParam ("repositorySlug" ) String repositorySlug ) {
145- if (!userCheckService .isViewAllowed ()) {
146- return status (UNAUTHORIZED ).build ();
147- }
148121 List <PrnfbButton > buttons = settingsService .getButtons (projectKey , repositorySlug );
149- Iterable <PrnfbButton > allowedButtons = userCheckService .filterAllowed (buttons );
122+ Iterable <PrnfbButton > allowedButtons = userCheckService .filterAdminAllowed (buttons );
150123 List <ButtonDTO > dtos = toButtonDtoList (allowedButtons );
151124 Collections .sort (dtos );
152125 return ok (dtos , APPLICATION_JSON ).build ();
@@ -157,30 +130,26 @@ public Response get(
157130 @ Produces (APPLICATION_JSON )
158131 public Response get (@ PathParam ("uuid" ) UUID uuid ) {
159132 PrnfbButton button = settingsService .getButton (uuid );
160- if (!userCheckService .isAllowedUseButton (button )) {
133+ if (!userCheckService .isAdminAllowed (button )) {
161134 return status (UNAUTHORIZED ).build ();
162135 }
163136 ButtonDTO dto = toButtonDto (button );
164137 return ok (dto , APPLICATION_JSON ).build ();
165138 }
166139
167- private void populateButtonFormDtoList (
168- Integer repositoryId , Long pullRequestId , List <ButtonDTO > dtos ) {
169- for (ButtonDTO dto : dtos ) {
170- PrnfbRendererWrapper renderer =
171- buttonsService .getRenderer (repositoryId , pullRequestId , dto .getUuid ());
172- List <ButtonFormElementDTO > buttonFormDtoList = dto .getButtonFormList ();
173- if (buttonFormDtoList != null ) {
174- for (ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList ) {
175- String defaultValue = buttonFormElementDto .getDefaultValue ();
176- if (!isNullOrEmpty (defaultValue )) {
177- String defaultValueRendered = renderer .render (defaultValue , ENCODE_FOR .NONE );
178- buttonFormElementDto .setDefaultValue (defaultValueRendered );
179- }
180- }
181- dto .setButtonFormList (buttonFormDtoList );
182- }
183- }
140+ @ GET
141+ @ Path ("/repository/{repositoryId}/pullrequest/{pullRequestId}" )
142+ @ Produces (APPLICATION_JSON )
143+ public Response get (
144+ @ PathParam ("repositoryId" ) Integer repositoryId ,
145+ @ PathParam ("pullRequestId" ) Long pullRequestId ) {
146+ List <PrnfbButton > buttons = buttonsService .getButtons (repositoryId , pullRequestId );
147+ List <ButtonDTO > dtos = toButtonDtoList (buttons );
148+ Collections .sort (dtos );
149+
150+ populateButtonFormDtoList (repositoryId , pullRequestId , dtos );
151+
152+ return ok (dtos , APPLICATION_JSON ).build ();
184153 }
185154
186155 @ POST
@@ -192,15 +161,36 @@ public Response press(
192161 @ PathParam ("repositoryId" ) Integer repositoryId ,
193162 @ PathParam ("pullRequestId" ) Long pullRequestId ,
194163 @ PathParam ("uuid" ) final UUID buttionUuid ) {
195- PrnfbButton button = settingsService .getButton (buttionUuid );
196- if (!userCheckService .isAllowedUseButton (button )) {
197- return status (UNAUTHORIZED ).build ();
164+ List <PrnfbButton > buttons = buttonsService .getButtons (repositoryId , pullRequestId );
165+ Optional <PrnfbButton > button =
166+ Iterables .tryFind (buttons , (b ) -> b .getUuid ().equals (buttionUuid ));
167+ if (!button .isPresent ()) {
168+ return status (NOT_FOUND ).build ();
198169 }
199170 String formData = request .getParameter ("form" );
200171 List <NotificationResponse > results =
201172 buttonsService .handlePressed (repositoryId , pullRequestId , buttionUuid , formData );
202173
203- ButtonPressDTO dto = toTriggerResultDto (button , results );
174+ ButtonPressDTO dto = toTriggerResultDto (button . get () , results );
204175 return ok (dto , APPLICATION_JSON ).build ();
205176 }
177+
178+ private void populateButtonFormDtoList (
179+ Integer repositoryId , Long pullRequestId , List <ButtonDTO > dtos ) {
180+ for (ButtonDTO dto : dtos ) {
181+ PrnfbRendererWrapper renderer =
182+ buttonsService .getRenderer (repositoryId , pullRequestId , dto .getUuid ());
183+ List <ButtonFormElementDTO > buttonFormDtoList = dto .getButtonFormList ();
184+ if (buttonFormDtoList != null ) {
185+ for (ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList ) {
186+ String defaultValue = buttonFormElementDto .getDefaultValue ();
187+ if (!isNullOrEmpty (defaultValue )) {
188+ String defaultValueRendered = renderer .render (defaultValue , ENCODE_FOR .NONE );
189+ buttonFormElementDto .setDefaultValue (defaultValueRendered );
190+ }
191+ }
192+ dto .setButtonFormList (buttonFormDtoList );
193+ }
194+ }
195+ }
206196}
0 commit comments