22 * Licensed under MIT License
33 * Copyright (c) 2017 Bernhard Grünewaldt
44 */
5- package io .codeclou .jenkins .github . webhook . notifier . plugin ;
5+ package io .codeclou .jenkins .githubwebhooknotifierplugin ;
66
77import com .google .gson .Gson ;
88import com .google .gson .JsonSyntaxException ;
9- import hudson .Plugin ;
9+ import hudson .Extension ;
1010import hudson .model .UnprotectedRootAction ;
1111import hudson .util .HttpResponses ;
1212import jenkins .model .Jenkins ;
3333import java .security .cert .CertificateException ;
3434import java .security .cert .X509Certificate ;
3535
36-
37- public class GithubWebhookNotifierPlugin extends Plugin implements UnprotectedRootAction {
36+ @ Extension
37+ public class GithubWebhookNotifyAction implements UnprotectedRootAction {
3838
3939 @ Override
4040 public String getUrlName () {
41- return "github-webhook-notifier-plugin " ;
41+ return "github-webhook-notifier" ;
4242 }
4343
4444 @ Override
@@ -48,11 +48,11 @@ public String getIconFileName() {
4848
4949 @ Override
5050 public String getDisplayName () {
51- return "github-webhook-notifier-plugin " ;
51+ return "github-webhook-notifier" ;
5252 }
5353
5454 /*
55- * http://jenkins.foo/plugin/ github-webhook-notifier-plugin /receive
55+ * http://jenkins.foo/github-webhook-notifier/receive
5656 */
5757 @ RequirePOST
5858 public HttpResponse doReceive (HttpServletRequest request , StaplerRequest staplerRequest ) throws IOException , ServletException {
@@ -66,7 +66,7 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
6666 "git/notifyCommit?url=" +
6767 githubWebhookPayload .getRepository ().getClone_url () +
6868 "&branches=" +
69- githubWebhookPayload .getRef () +
69+ this . normalizeBranchNameForJenkins ( githubWebhookPayload .getRef () ) +
7070 "&sha1=" +
7171 githubWebhookPayload .getAfter ();
7272 SSLContext sslContext = new SSLContextBuilder ()
@@ -84,19 +84,37 @@ public boolean isTrusted(X509Certificate[] certificate, String authType) throws
8484 CloseableHttpResponse response = client .execute (httpGet );
8585 int statusCode = response .getStatusLine ().getStatusCode ();
8686 String gitNotificationResponse = IOUtils .toString (response .getEntity ().getContent (), "UTF-8" );
87- String responseText = "ok triggered: " + gitPluginNotifyUrl + "\n status: " + statusCode + "\n \n " + gitNotificationResponse ;
87+ StringBuilder responseText = new StringBuilder ();
88+ responseText .append ("----------------------------------------------------------------------------------\n " );
89+ responseText .append ("github-webhook-notifier-plugin - parsed webhook payload:\n " );
90+ responseText .append (" ref: " ).append (githubWebhookPayload .getRef ()).append ("\n " );
91+ responseText .append (" before: " ).append (githubWebhookPayload .getBefore ()).append ("\n " );
92+ responseText .append (" after: " ).append (githubWebhookPayload .getAfter ()).append ("\n " );
93+ responseText .append (" clone_url: " ).append (githubWebhookPayload .getRepository ().getClone_url ()).append ("\n " );
94+ responseText .append ("----------------------------------------------------------------------------------\n " );
95+ responseText .append (">> REQUEST\n " ).append (gitPluginNotifyUrl ).append ("\n \n " );
96+ responseText .append ("<< RESPONSE HTTP " ).append (statusCode ).append ("\n " );
97+ responseText .append (gitNotificationResponse );
8898 if (statusCode != 200 ) {
89- return HttpResponses .error (400 , responseText );
99+ return HttpResponses .error (400 , responseText . toString () );
90100 }
91- return HttpResponses .plainText (responseText );
101+ return HttpResponses .plainText (responseText . toString () );
92102 } catch (JsonSyntaxException ex ) {
93- return HttpResponses .error (500 , "json invalid" );
103+ return HttpResponses .error (500 , "github-webhook-notifier-plugin: github webhook json invalid" );
94104 } catch (NoSuchAlgorithmException ex ) {
95- return HttpResponses .error (500 , "NoSuchAlgorithmException" );
105+ return HttpResponses .error (500 , "github-webhook-notifier-plugin: internal error NoSuchAlgorithmException" );
96106 } catch (KeyStoreException ex ) {
97- return HttpResponses .error (500 , "KeyStoreException" );
107+ return HttpResponses .error (500 , "github-webhook-notifier-plugin: internal error KeyStoreException" );
98108 } catch (KeyManagementException ex ) {
99- return HttpResponses .error (500 , "KeyManagementException" );
109+ return HttpResponses .error (500 , "github-webhook-notifier-plugin: internal error KeyManagementException" );
100110 }
101111 }
112+
113+ /*
114+ * converts "refs/heads/develop" to "origin/develop"
115+ */
116+ private String normalizeBranchNameForJenkins (String branchname ) {
117+ return branchname .replace ("refs/heads/" , "origin/" );
118+ }
119+
102120}
0 commit comments