99import hudson .Plugin ;
1010import hudson .util .HttpResponses ;
1111import jenkins .model .Jenkins ;
12- import org .apache .commons .httpclient .HttpClient ;
13- import org .apache .commons .httpclient .HttpMethod ;
14- import org .apache .commons .httpclient .methods .GetMethod ;
12+ import org .apache .commons .io .IOUtils ;
13+ import org .apache .http .client .methods .CloseableHttpResponse ;
14+ import org .apache .http .client .methods .HttpGet ;
15+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
16+ import org .apache .http .impl .client .CloseableHttpClient ;
17+ import org .apache .http .impl .client .HttpClients ;
18+ import org .apache .http .ssl .SSLContextBuilder ;
19+ import org .apache .http .ssl .TrustStrategy ;
1520import org .kohsuke .stapler .HttpResponse ;
1621import org .kohsuke .stapler .StaplerRequest ;
1722import org .kohsuke .stapler .interceptor .RequirePOST ;
1823
24+ import javax .net .ssl .SSLContext ;
1925import javax .servlet .ServletException ;
2026import javax .servlet .http .HttpServletRequest ;
2127import java .io .BufferedReader ;
2228import java .io .IOException ;
29+ import java .security .KeyManagementException ;
30+ import java .security .KeyStoreException ;
31+ import java .security .NoSuchAlgorithmException ;
32+ import java .security .cert .CertificateException ;
33+ import java .security .cert .X509Certificate ;
2334
2435
2536public class GithubWebhookNotifierPlugin extends Plugin {
@@ -36,22 +47,40 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
3647 GithubWebhookPayload githubWebhookPayload = gson .fromJson (reader , GithubWebhookPayload .class );
3748 // Trigger Git-Plugins notify push SCM Polling Endpoint
3849 String gitPluginNotifyUrl = jenkinsRootUrl +
39- "git/notifyCommit?" +
50+ "git/notifyCommit?url= " +
4051 githubWebhookPayload .getRepository ().getClone_url () +
4152 "&branches=" +
4253 githubWebhookPayload .getRef () +
4354 "&sha1=" +
4455 githubWebhookPayload .getAfter ();
45- HttpClient client = new HttpClient ();
46- HttpMethod method = new GetMethod (gitPluginNotifyUrl );
47- int statusCode = client .executeMethod (method );
48- String responseText = "triggered: " + gitPluginNotifyUrl + "\n status: " + statusCode ;
56+ SSLContext sslContext = new SSLContextBuilder ()
57+ .loadTrustMaterial (null , new TrustStrategy () {
58+ @ Override
59+ public boolean isTrusted (X509Certificate [] certificate , String authType ) throws CertificateException {
60+ return true ;
61+ }
62+ }).build ();
63+ CloseableHttpClient client = HttpClients .custom ()
64+ .setSSLContext (sslContext )
65+ .setSSLHostnameVerifier (new NoopHostnameVerifier ())
66+ .build ();
67+ HttpGet httpGet = new HttpGet (gitPluginNotifyUrl );
68+ CloseableHttpResponse response = client .execute (httpGet );
69+ int statusCode = response .getStatusLine ().getStatusCode ();
70+ String gitNotificationResponse = IOUtils .toString (response .getEntity ().getContent (), "UTF-8" );
71+ String responseText = "ok triggered: " + gitPluginNotifyUrl + "\n status: " + statusCode + "\n \n " + gitNotificationResponse ;
4972 if (statusCode != 200 ) {
5073 return HttpResponses .error (400 , responseText );
5174 }
5275 return HttpResponses .plainText (responseText );
5376 } catch (JsonSyntaxException ex ) {
5477 return HttpResponses .error (500 , "json invalid" );
78+ } catch (NoSuchAlgorithmException ex ) {
79+ return HttpResponses .error (500 , "NoSuchAlgorithmException" );
80+ } catch (KeyStoreException ex ) {
81+ return HttpResponses .error (500 , "KeyStoreException" );
82+ } catch (KeyManagementException ex ) {
83+ return HttpResponses .error (500 , "KeyManagementException" );
5584 }
5685 }
5786}
0 commit comments