Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit f116f41

Browse files
author
Bernhard Grünewaldt
committed
working version 1.0.0
1 parent 011ee7c commit f116f41

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

DEVELOPMENT.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ curl -X POST \
88
-d @test-webhook-payload.json \
99
http://localhost:8080/jenkins/plugin/github-webhook-notifier-plugin/receive
1010
```
11+
12+
### Build hpi
13+
14+
```
15+
mvn clean
16+
mvn compile
17+
mvn hpi:hpi
18+
```

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
jenkins-github-webhook-notifier-plugin
33

44

5+
6+
* Plugin does allow self signed certificates and does not check the SSL Cert of `Jenkins.getInstance().getRootUrl();`
7+
58
-----
69

710
 

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212
<groupId>io.codeclou.jenkins.github.webhook.notifier.plugin</groupId>
1313
<artifactId>github-webhook-notifier-plugin</artifactId>
14-
<version>1.0-SNAPSHOT</version>
14+
<version>1.0.0</version>
1515
<packaging>hpi</packaging>
1616

1717
<name>github-webhook-notifier-plugin</name>
@@ -41,5 +41,16 @@
4141
<artifactId>gson</artifactId>
4242
<version>2.8.0</version>
4343
</dependency>
44+
45+
<dependency>
46+
<groupId>org.apache.httpcomponents</groupId>
47+
<artifactId>httpclient</artifactId>
48+
<version>4.5.3</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>commons-io</groupId>
52+
<artifactId>commons-io</artifactId>
53+
<version>2.5</version>
54+
</dependency>
4455
</dependencies>
4556
</project>

src/main/java/io/codeclou/jenkins/github/webhook/notifier/plugin/GithubWebhookNotifierPlugin.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@
99
import hudson.Plugin;
1010
import hudson.util.HttpResponses;
1111
import 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;
1520
import org.kohsuke.stapler.HttpResponse;
1621
import org.kohsuke.stapler.StaplerRequest;
1722
import org.kohsuke.stapler.interceptor.RequirePOST;
1823

24+
import javax.net.ssl.SSLContext;
1925
import javax.servlet.ServletException;
2026
import javax.servlet.http.HttpServletRequest;
2127
import java.io.BufferedReader;
2228
import 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

2536
public 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 + "\nstatus: " + 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 + "\nstatus: " + 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

Comments
 (0)