From 1c1abd578fd90f7b4f5815e8968d20be6f9bb844 Mon Sep 17 00:00:00 2001 From: Alexander Verhaar Date: Sat, 20 Jan 2018 19:32:34 +0100 Subject: [PATCH] Skip job if commit message starts with '[IGNORE]' --- .../jenkinsci/plugins/gogs/GogsWebHook.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java b/src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java index fd22df9..3ed2012 100644 --- a/src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java +++ b/src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java @@ -147,7 +147,18 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException // Get the POST stream String body = IOUtils.toString(req.getInputStream(), DEFAULT_CHARSET); if (!body.isEmpty() && req.getRequestURI().contains("/" + URLNAME + "/")) { - String contentType = req.getContentType(); + JSONObject jsonObject = JSONObject.fromObject(body); + JSONObject commits = (JSONObject) jsonObject.getJSONArray("commits").get(0); + String message = (String) commits.get("message"); + + if (message.startsWith("[IGNORE]")) { + // Ignore commits starting with message "[IGNORE]" + result.setStatus(200, "Ignoring push"); + exitWebHook(result, rsp); + return; + } + + String contentType = req.getContentType(); if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded")) { body = URLDecoder.decode(body, DEFAULT_CHARSET); } @@ -155,8 +166,6 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException body = body.substring(8); } - JSONObject jsonObject = JSONObject.fromObject(body); - String jSecret = null; boolean foundJob = false; @@ -171,7 +180,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException if (job != null) { foundJob = true; - /* secret is stored in the properties of Job */ + /* secret is stored in the properties of Job */ final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class); if (property != null) { /* only if Gogs secret is defined on the job */ jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */ @@ -185,7 +194,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException if (job != null) { foundJob = true; - /* secret is stored in the properties of Job */ + /* secret is stored in the properties of Job */ final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class); if (property != null) { /* only if Gogs secret is defined on the job */ jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */ @@ -216,13 +225,13 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException result.setStatus(404, msg); LOGGER.warning(msg); } else if (isNullOrEmpty(jSecret) && isNullOrEmpty(gSecret)) { - /* No password is set in Jenkins and Gogs, run without secrets */ + /* No password is set in Jenkins and Gogs, run without secrets */ result = payloadProcessor.triggerJobs(jobName, gogsDelivery); } else if (!isNullOrEmpty(jSecret) && jSecret.equals(gSecret)) { - /* Password is set in Jenkins and Gogs, and is correct */ + /* Password is set in Jenkins and Gogs, and is correct */ result = payloadProcessor.triggerJobs(jobName, gogsDelivery); } else { - /* Gogs and Jenkins secrets differs */ + /* Gogs and Jenkins secrets differs */ result.setStatus(403, "Incorrect secret"); } } else {