22 * Licensed under MIT License
33 * Copyright (c) 2017 Bernhard Grünewaldt
44 */
5- package io .codeclou .jenkins .githubwebhooknotifierplugin ;
5+ package io .codeclou .jenkins .githubwebhookbuildtriggerplugin ;
66
77import com .google .gson .Gson ;
88import com .google .gson .JsonSyntaxException ;
99import hudson .Extension ;
1010import hudson .model .*;
11- import hudson .scm .SCMRevisionState ;
12- import hudson .triggers .SCMTrigger ;
13- import hudson .triggers .Trigger ;
1411import hudson .util .HttpResponses ;
15- import io .codeclou .jenkins .githubwebhooknotifierplugin .config .GithubWebhookNotifierPluginBuilder ;
16- import io .codeclou .jenkins .githubwebhooknotifierplugin .webhooksecret .GitHubWebhookUtility ;
12+ import io .codeclou .jenkins .githubwebhookbuildtriggerplugin .config .GithubWebhookBuildTriggerPluginBuilder ;
13+ import io .codeclou .jenkins .githubwebhookbuildtriggerplugin .webhooksecret .GitHubWebhookUtility ;
1714import jenkins .model .Jenkins ;
1815import org .apache .commons .io .IOUtils ;
19- import org .apache .http .client .methods .CloseableHttpResponse ;
20- import org .apache .http .client .methods .HttpGet ;
21- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
22- import org .apache .http .impl .client .CloseableHttpClient ;
23- import org .apache .http .impl .client .HttpClients ;
24- import org .apache .http .ssl .SSLContextBuilder ;
25- import org .apache .http .ssl .TrustStrategy ;
2616import org .kohsuke .stapler .HttpResponse ;
2717import org .kohsuke .stapler .StaplerRequest ;
2818import org .kohsuke .stapler .interceptor .RequirePOST ;
2919
30- import javax .net .ssl .SSLContext ;
3120import javax .servlet .ServletException ;
3221import javax .servlet .http .HttpServletRequest ;
33- import java .io .BufferedReader ;
3422import java .io .IOException ;
3523import java .io .StringWriter ;
36- import java .security .KeyManagementException ;
37- import java .security .KeyStoreException ;
38- import java .security .NoSuchAlgorithmException ;
39- import java .security .cert .CertificateException ;
40- import java .security .cert .X509Certificate ;
4124import java .util .ArrayList ;
4225import java .util .Collection ;
43- import java .util .HashMap ;
4426
4527@ Extension
46- public class GithubWebhookNotifyAction implements UnprotectedRootAction {
28+ public class GithubWebhookBuildTriggerAction implements UnprotectedRootAction {
4729
4830 @ Override
4931 public String getUrlName () {
@@ -68,35 +50,50 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
6850 StringWriter writer = new StringWriter ();
6951 IOUtils .copy (request .getInputStream (), writer , "UTF-8" );
7052 String requestBody = writer .toString ();
71- String githubSignature = request .getHeader ("x-hub-signature" );
7253 Gson gson = new Gson ();
54+ StringBuilder info = new StringBuilder ();
7355 try {
74- String webhookSecretAsConfiguredByUser = GithubWebhookNotifierPluginBuilder .DescriptorImpl .getDescriptor ().getWebhookSecret ();
56+ //
57+ // WEBHOOK SECRET
58+ //
59+ String githubSignature = request .getHeader ("x-hub-signature" );
60+ String webhookSecretAsConfiguredByUser = GithubWebhookBuildTriggerPluginBuilder .DescriptorImpl .getDescriptor ().getWebhookSecret ();
7561 String webhookSecretMessage ="validating webhook payload against wevhook secret." ;
62+ info .append (">> webhook secret validation" ).append ("\n " );
7663 if (webhookSecretAsConfiguredByUser == null ) {
77- webhookSecretMessage = "no webhook secret in global config specified. skipping validation." ;
64+ webhookSecretMessage = " skipping validation since no webhook secret is configured in \n " +
65+ " 'Jenkins' -> 'Configure' tab under 'Github Webhook Build Trigger' section." ;
7866 } else {
7967 Boolean isValid = GitHubWebhookUtility .verifySignature (requestBody , githubSignature , webhookSecretAsConfiguredByUser );
8068 if (!isValid ) {
81- return HttpResponses .error (500 , this .getTextEnvelopedInBanner ("github webhook secret signature check failed. Check your webhook secret." ));
69+ info .append (webhookSecretMessage ).append ("\n " );
70+ return HttpResponses .error (500 , this .getTextEnvelopedInBanner (info .toString () + " ERROR: github webhook secret signature check failed. Check your webhook secret." ));
8271 }
72+ webhookSecretMessage = " ok. Webhook secret validates against " + githubSignature + "\n " ;
8373 }
74+ info .append (webhookSecretMessage ).append ("\n " );
75+ //
76+ // PAYLOAD TO ENVVARS
77+ //
8478 GithubWebhookPayload githubWebhookPayload = gson .fromJson (requestBody , GithubWebhookPayload .class );
85- GithubWebhookEnvironmentContributionAction environmentContributionAction = new GithubWebhookEnvironmentContributionAction (githubWebhookPayload );
79+ EnvironmentContributionAction environmentContributionAction = new EnvironmentContributionAction (githubWebhookPayload );
80+ //
81+ // TRIGGER JOBS
82+ //
8683 String jobNamePrefix = this .normalizeRepoFullName (githubWebhookPayload .getRepository ().getFull_name ());
8784 StringBuilder jobsTriggered = new StringBuilder ();
8885 ArrayList <String > jobsAlreadyTriggered = new ArrayList <>();
8986 StringBuilder causeNote = new StringBuilder ();
90- causeNote .append ("github-webhook-notifier -plugin:\n " );
87+ causeNote .append ("github-webhook-build-trigger -plugin:\n " );
9188 causeNote .append (githubWebhookPayload .getAfter ()).append ("\n " );
9289 causeNote .append (githubWebhookPayload .getRef ()).append ("\n " );
9390 causeNote .append (githubWebhookPayload .getRepository ().getClone_url ());
9491 Cause cause = new Cause .RemoteCause ("github.com" , causeNote .toString ());
9592 Collection <Job > jobs = Jenkins .getInstance ().getAllItems (Job .class );
9693 if (jobs .isEmpty ()) {
97- jobsTriggered .append ("WARNING NO JOBS FOUND!\n " );
98- jobsTriggered .append ("If you are using matrix-based security, please give the following rights to 'Anonymous'.\n " );
99- jobsTriggered .append ("'Job' -> build, discover, read.\n " );
94+ jobsTriggered .append (" WARNING NO JOBS FOUND!\n " );
95+ jobsTriggered .append (" If you are using matrix-based security, please give the following rights to 'Anonymous'.\n " );
96+ jobsTriggered .append (" 'Job' -> build, discover, read.\n " );
10097 }
10198 for (Job job : jobs ) {
10299 if (job .getName ().startsWith (jobNamePrefix ) && ! jobsAlreadyTriggered .contains (job .getName ())) {
@@ -106,16 +103,17 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
106103 projectScheduable .scheduleBuild (0 , cause , environmentContributionAction );
107104 }
108105 }
109- StringBuilder info = new StringBuilder ();
106+ //
107+ // WRITE ADDITONAL INFO
108+ //
110109 info .append (">> webhook content to env vars" ).append ("\n " );
111- info .append ("webhooksecret: " ).append (webhookSecretMessage ).append ("\n " );
112110 info .append (environmentContributionAction .getEnvVarInfo ());
113111 info .append ("\n " );
114112 info .append (">> jobs triggered with name matching '" ).append (jobNamePrefix ).append ("*'" ).append ("\n " );
115113 info .append (jobsTriggered .toString ());
116114 return HttpResponses .plainText (this .getTextEnvelopedInBanner (info .toString ()));
117115 } catch (JsonSyntaxException ex ) {
118- return HttpResponses .error (500 , this .getTextEnvelopedInBanner (" github webhook json invalid" ));
116+ return HttpResponses .error (500 , this .getTextEnvelopedInBanner (info . toString () + " ERROR: github webhook json invalid" ));
119117 }
120118 }
121119
@@ -129,7 +127,7 @@ private String normalizeRepoFullName(String reponame) {
129127 private String getTextEnvelopedInBanner (String text ) {
130128 StringBuilder banner = new StringBuilder ();
131129 banner .append ("----------------------------------------------------------------------------------\n " );
132- banner .append ("github-webhook-notifier -plugin" ).append ("\n " );
130+ banner .append ("github-webhook-build-trigger -plugin" ).append ("\n " );
133131 banner .append ("----------------------------------------------------------------------------------\n " );
134132 banner .append (text );
135133 banner .append ("\n ----------------------------------------------------------------------------------\n " );
0 commit comments