1919
2020import javax .servlet .ServletException ;
2121import javax .servlet .http .HttpServletRequest ;
22+ import javax .swing .plaf .basic .BasicToolTipUI ;
2223import java .io .IOException ;
2324import java .io .StringWriter ;
2425import java .util .ArrayList ;
@@ -51,7 +52,11 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
5152 IOUtils .copy (request .getInputStream (), writer , "UTF-8" );
5253 String requestBody = writer .toString ();
5354 Gson gson = new Gson ();
55+ GithubWebhookPayload githubWebhookPayload = gson .fromJson (requestBody , GithubWebhookPayload .class );
5456 StringBuilder info = new StringBuilder ();
57+ if (githubWebhookPayload == null ) {
58+ return HttpResponses .error (500 , this .getTextEnvelopedInBanner (" ERROR: payload json is empty at least requestBody is empty!" ));
59+ }
5560 try {
5661 //
5762 // WEBHOOK SECRET
@@ -71,11 +76,11 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
7176 }
7277 webhookSecretMessage = " ok. Webhook secret validates against " + githubSignature + "\n " ;
7378 }
74- info .append (webhookSecretMessage ).append ("\n " );
79+ info .append (webhookSecretMessage ).append ("\n \n " );
7580 //
7681 // PAYLOAD TO ENVVARS
7782 //
78- GithubWebhookPayload githubWebhookPayload = gson . fromJson ( requestBody , GithubWebhookPayload . class );
83+
7984 EnvironmentContributionAction environmentContributionAction = new EnvironmentContributionAction (githubWebhookPayload );
8085 //
8186 // TRIGGER JOBS
@@ -92,7 +97,8 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
9297 Collection <Job > jobs = Jenkins .getInstance ().getAllItems (Job .class );
9398 if (jobs .isEmpty ()) {
9499 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 " );
100+ jobsTriggered .append (" You either have no jobs or if you are using matrix-based security,\n " );
101+ jobsTriggered .append (" please give the following rights to 'Anonymous':\n " );
96102 jobsTriggered .append (" 'Job' -> build, discover, read.\n " );
97103 }
98104 for (Job job : jobs ) {
@@ -126,8 +132,8 @@ private String normalizeRepoFullName(String reponame) {
126132
127133 private String getTextEnvelopedInBanner (String text ) {
128134 StringBuilder banner = new StringBuilder ();
129- banner .append ("----------------------------------------------------------------------------------\n " );
130- banner .append ("github-webhook-build-trigger-plugin" ).append ("\n " );
135+ banner .append ("\n ----------------------------------------------------------------------------------\n " );
136+ banner .append (" github-webhook-build-trigger-plugin" ).append ("\n " );
131137 banner .append ("----------------------------------------------------------------------------------\n " );
132138 banner .append (text );
133139 banner .append ("\n ----------------------------------------------------------------------------------\n " );
0 commit comments