Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/main/java/org/jenkins/plugins/JobListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Extension
public class JobListener extends RunListener<AbstractBuild> {

private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8");
private OkHttpClient client;

private static final Logger log = LoggerFactory.getLogger(JobListener.class);

public JobListener() {
super(AbstractBuild.class);
Expand All @@ -38,7 +33,7 @@ public void onStarted(AbstractBuild build, TaskListener listener) {
String buildName = build.getDisplayName();
String buildVars = build.getBuildVariables().toString();
NotificationEvent event = new NotificationEvent(projectName, buildName, buildUrl, buildVars, "start");
httpPost(webHookUrl, event);
httpPost(webHookUrl, event, listener);
}

@Override
Expand All @@ -59,15 +54,15 @@ public void onCompleted(AbstractBuild build, @Nonnull TaskListener listener) {
NotificationEvent event = new NotificationEvent(projectName, buildName, buildUrl, buildVars, "");
if (publisher.onSuccess && result.equals(Result.SUCCESS)) {
event.event = "success";
httpPost(webHookUrl, event);
httpPost(webHookUrl, event, listener);
}
if (publisher.onFailure && result.equals(Result.FAILURE)) {
event.event = "failure";
httpPost(webHookUrl, event);
httpPost(webHookUrl, event, listener);
}
if (publisher.onUnstable && result.equals(Result.UNSTABLE)) {
event.event = "unstable";
httpPost(webHookUrl, event);
httpPost(webHookUrl, event, listener);
}
}

Expand All @@ -80,15 +75,20 @@ private WebHookPublisher GetWebHookPublisher(AbstractBuild build) {
return null;
}

private void httpPost(String url, Object object) {
private void httpPost(String url, Object object, @Nonnull TaskListener listener) {
String jsonString = JSON.toJSONString(object);
RequestBody body = RequestBody.create(JSON_MEDIA_TYPE, jsonString);
Request request = new Request.Builder().url(url).post(body).build();
try {
Response response = client.newCall(request).execute();
log.debug("Invocation of webhook {} successful", url);
if (response.isSuccessful()) {
listener.getLogger().println("Invocation of webhook " + url + " successful");
} else {
listener.getLogger().println("Invocation of webhook " + url + " failed: HTTP " + response.code() + " " + response.message());
}
response.close();
} catch (Exception e) {
log.info("Invocation of webhook {} failed", url, e);
listener.getLogger().println("Invocation of webhook " + url + " failed: " + e);
}
}
}