91
91
import org .apache .commons .codec .digest .DigestUtils ;
92
92
import org .apache .commons .io .IOUtils ;
93
93
import org .apache .commons .lang3 .StringUtils ;
94
+ import org .apache .hc .client5 .http .classic .methods .HttpPut ;
94
95
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
95
96
import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
97
+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
98
+ import org .apache .hc .core5 .http .ContentType ;
96
99
import org .apache .hc .core5 .http .HttpHost ;
97
100
import org .apache .hc .core5 .http .HttpStatus ;
101
+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
102
+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
98
103
import org .apache .hc .core5 .http .message .BasicNameValuePair ;
99
104
100
105
import static java .util .Objects .requireNonNull ;
@@ -675,17 +680,41 @@ public void registerCommitWebHook(BitbucketWebHook hook) throws IOException {
675
680
676
681
@ Override
677
682
public void updateCommitWebHook (BitbucketWebHook hook ) throws IOException {
683
+ String payload = JsonParser .toString (hook );
678
684
switch (webhookImplementation ) {
679
685
case PLUGIN :
680
686
// API documentation at https://help.moveworkforward.com/BPW/how-to-manage-configurations-using-post-webhooks-f#HowtomanageconfigurationsusingPostWebhooksforBitbucketAPIs?-UpdateapostwebhookbyID
681
- putRequest (
682
- UriTemplate
683
- .fromTemplate (this .baseURL + WEBHOOK_REPOSITORY_CONFIG_PATH )
684
- .set ("owner" , getUserCentricOwner ())
685
- .set ("repo" , repositoryName )
686
- .set ("id" , hook .getUuid ())
687
- .expand (), JsonParser .toString (hook )
688
- );
687
+ String path = UriTemplate
688
+ .fromTemplate (this .baseURL + WEBHOOK_REPOSITORY_CONFIG_PATH )
689
+ .set ("owner" , getUserCentricOwner ())
690
+ .set ("repo" , repositoryName )
691
+ .set ("id" , hook .getUuid ())
692
+ .expand ();
693
+ logger .warning ("Updating PLUGIN web hook sending " + payload );
694
+ HttpPut request = new HttpPut (path );
695
+ request .setAbsoluteRequestUri (true );
696
+ request .setEntity (new StringEntity (payload , ContentType .create ("application/json" , "UTF-8" )));
697
+ try (ClassicHttpResponse response = executeMethod (request )) {
698
+ int statusCode = response .getCode ();
699
+ if (statusCode == HttpStatus .SC_NOT_FOUND ) {
700
+ String errorMessage = getResponseContent (response );
701
+ logger .warning ("Error updating PLUGIN web hook" );
702
+ throw new FileNotFoundException ("Resource " + request .getRequestUri () + " not found: " + errorMessage );
703
+ }
704
+ if (statusCode == HttpStatus .SC_NO_CONTENT ) {
705
+ EntityUtils .consumeQuietly (response .getEntity ());
706
+ logger .warning ("No response returned from PLUGIN rest call -> 204" );
707
+ }
708
+ String content = getResponseContent (response );
709
+ if (statusCode != HttpStatus .SC_OK && statusCode != HttpStatus .SC_CREATED ) {
710
+ throw buildResponseException (response , content );
711
+ }
712
+ logger .warning ("Response from PLUGIN rest call " + content );
713
+ } catch (FileNotFoundException | BitbucketRequestException e ) {
714
+ throw e ;
715
+ } catch (IOException e ) {
716
+ throw new IOException ("Communication error, requested URL: " + request , e );
717
+ }
689
718
break ;
690
719
691
720
case NATIVE :
@@ -695,7 +724,7 @@ public void updateCommitWebHook(BitbucketWebHook hook) throws IOException {
695
724
.set ("owner" , getUserCentricOwner ())
696
725
.set ("repo" , repositoryName )
697
726
.set ("id" , hook .getUuid ())
698
- .expand (), JsonParser . toString ( hook )
727
+ .expand (), payload
699
728
);
700
729
break ;
701
730
0 commit comments