Skip to content

Commit

Permalink
Skip job if commit message starts with '[IGNORE]'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderv32 committed Jan 20, 2018
1 parent 5a47f02 commit 1c1abd5
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,25 @@ 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);
}
if (body.startsWith("payload=")) {
body = body.substring(8);
}

JSONObject jsonObject = JSONObject.fromObject(body);

String jSecret = null;
boolean foundJob = false;

Expand All @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1c1abd5

Please sign in to comment.