Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
Enhance notificatioin message with pipeline instance details (via pip…
Browse files Browse the repository at this point in the history
…eline history api)
  • Loading branch information
matt-richardson committed Feb 25, 2015
1 parent 5553fa8 commit 9690e47
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# gocd-websocket-notifier
WebSocket based GoCD build notifier

*Note*: this plugin is still under development. It should work for basic use cases.
There is a very high chance that the json that it publishes will change (to include more pipeline details).
Listens to notifications from the GoCD notification api, and publishes them over websockets.
This initial use case is for low impact, fast build notifiers, such as
[gocd-windows-tray-build-notifier](https://github.com/matt-richardson/gocd-windows-tray-build-notifier).

The only supported notification (the only one available as of Feb 2015) is the stage-status.
This has been enhanced to provide further pipeline instance information (via the pipeline
history api) and include them in the message as `x-pipeline-instance-details`.

If you run into any issues please [raise it](https://github.com/matt-richardson/gocd-websocket-notifier/issues),
or better yet, send a PR.
or better yet, send a PR. Please note, I am not a Java developer, so any (constructive) feedback gratefully
appreciated.

## Setup
Download jar from releases & place it in /plugins/external & restart Go Server.
Expand All @@ -24,8 +31,6 @@ However, I hope that this will be useful to others.
## Planned Enhancements
* Send [a reply](http://www.go.cd/documentation/developer/writing_go_plugins/notification/version_1_0/stage_status_notification.html#response---from-the-plugin)
if cannot notify listeners.
* Query to get more [pipeline details](https://github.com/gocd/documentation/blob/master/user/api/pipeline_api.md#pipeline-history)
and include them in the published message. Unfortunately will require credentials configured in the config.

## License
http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.matt-richardson</groupId>
<artifactId>gocd-websocket-notifier</artifactId>
<version>0.2</version>
<version>0.3</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.matt_richardson.gocd.websocket_notifier;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class PipelineDetailsPopulator {
String mergeInPipelineInstanceDetails(JsonElement notification, JsonElement pipelineInstance)
{
JsonObject json = notification.getAsJsonObject();
json.add("x-pipeline-instance-details", pipelineInstance);
return json.toString();
}

JsonElement downloadPipelineInstanceDetails(String pipelineName) throws IOException {
String sURL = "http://localhost:8153/go/api/pipelines/" + pipelineName + "/history";

URL url = new URL(sURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();

JsonParser parser = new JsonParser();
JsonElement rootElement = parser.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject json = rootElement.getAsJsonObject();
JsonArray pipelines = json.get("pipelines").getAsJsonArray();
return pipelines.get(0);
}

public String extendMessageToIncludePipelineDetails(String requestBody) {
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(requestBody).getAsJsonObject();

String result = requestBody;
try {
JsonElement extraDetails = downloadPipelineInstanceDetails(json.get("pipeline-name").getAsString());
result = mergeInPipelineInstanceDetails(json, extraDetails);
} catch (IOException e) {
//TODO: log
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;

public class WebSocketPipelineListener {
private final PipelineDetailsPopulator populator;
private PipelineWebSocketServer webSocketServer;
private Logger LOGGER = Logger.getLoggerFor(WebSocketPipelineListener.class);

public WebSocketPipelineListener(PipelineWebSocketServer webSocketServer) {
this.webSocketServer = webSocketServer;
this.populator = new PipelineDetailsPopulator();
}

public void notify(GoPluginApiRequest message)
throws Exception {
LOGGER.info("notify called with request name '" + message.requestName() + "' and requestBody '" + message.requestBody() + "'");
this.webSocketServer.sendToAll(message.requestBody());
String expandedMessage = populator.extendMessageToIncludePipelineDetails(message.requestBody());
this.webSocketServer.sendToAll(expandedMessage);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<go-plugin id="gocd.websocket.notifier" version="1">
<about>
<name>WebSocket Notification Plugin</name>
<version>0.2</version>
<version>0.3</version>
<target-go-version>15.1.0</target-go-version>
<description>Plugin to send build notifications via websockets to any listener</description>
<vendor>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.matt_richardson.gocd.websocket_notifier;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import junit.framework.TestCase;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class PipelineDetailsPopulatorTest extends TestCase {
public void testMergeInPipelineInstanceDetails() throws Exception {
String originalJson = "{\"pipeline-name\":\"test\",\"pipeline-counter\":13,\"stage-name\":\"defaultStage\",\"stage-counter\":\"1\",\"stage-state\":\"Passed\",\"stage-result\":\"Passed\",\"create-time\":\"2015-02-19T07:41:21.162Z\",\"last-transition-time\":\"2015-02-19T07:41:45.576Z\"}";
String additionalJson = "{\"build_cause\":{\"approver\":\"\",\"material_revisions\":[{\"modifications\":[{\"email_address\":null,\"id\":10,\"modified_time\":1424331650000,\"user_name\":\"Matt Richardson \u003C[email protected]\u003E\",\"comment\":\"Update readme with details about development status\",\"revision\":\"a50098faa112d8324d9acf9ade727f837d7930dd\"}],\"material\":{\"description\":\"URL: /home/vagrant/gocd-websocket-notifier2, Branch: master\",\"fingerprint\":\"f77449ab015e3fd36c3a2088ea6338f4d4e0922f04bdcc4c5104bbacd1a72dbe\",\"type\":\"Git\",\"id\":1},\"changed\":true}],\"trigger_forced\":false,\"trigger_message\":\"modified by Matt Richardson \u003C[email protected]\u003E\"},\"name\":\"test\",\"natural_order\":13.0,\"can_run\":true,\"comment\":null,\"stages\":[{\"name\":\"defaultStage\",\"approved_by\":\"changes\",\"jobs\":[{\"name\":\"defaultJob\",\"result\":\"Passed\",\"state\":\"Completed\",\"id\":13,\"scheduled_date\":1424331681162}],\"can_run\":true,\"result\":\"Passed\",\"approval_type\":\"success\",\"counter\":\"1\",\"id\":13,\"operate_permission\":true,\"rerun_of_counter\":null,\"scheduled\":true}],\"counter\":13,\"id\":13,\"preparing_to_schedule\":false,\"label\":\"13\"}";

JsonParser parser = new JsonParser();
JsonElement pipelineInstance = parser.parse(additionalJson);
JsonElement notification = parser.parse(originalJson);

PipelineDetailsPopulator populator = new PipelineDetailsPopulator();
String result = populator.mergeInPipelineInstanceDetails(notification, pipelineInstance);
assertThat(result, is("{\"pipeline-name\":\"test\",\"pipeline-counter\":13,\"stage-name\":\"defaultStage\",\"stage-counter\":\"1\",\"stage-state\":\"Passed\",\"stage-result\":\"Passed\",\"create-time\":\"2015-02-19T07:41:21.162Z\",\"last-transition-time\":\"2015-02-19T07:41:45.576Z\",\"x-pipeline-instance-details\":{\"build_cause\":{\"approver\":\"\",\"material_revisions\":[{\"modifications\":[{\"email_address\":null,\"id\":10,\"modified_time\":1424331650000,\"user_name\":\"Matt Richardson <[email protected]>\",\"comment\":\"Update readme with details about development status\",\"revision\":\"a50098faa112d8324d9acf9ade727f837d7930dd\"}],\"material\":{\"description\":\"URL: /home/vagrant/gocd-websocket-notifier2, Branch: master\",\"fingerprint\":\"f77449ab015e3fd36c3a2088ea6338f4d4e0922f04bdcc4c5104bbacd1a72dbe\",\"type\":\"Git\",\"id\":1},\"changed\":true}],\"trigger_forced\":false,\"trigger_message\":\"modified by Matt Richardson <[email protected]>\"},\"name\":\"test\",\"natural_order\":13.0,\"can_run\":true,\"comment\":null,\"stages\":[{\"name\":\"defaultStage\",\"approved_by\":\"changes\",\"jobs\":[{\"name\":\"defaultJob\",\"result\":\"Passed\",\"state\":\"Completed\",\"id\":13,\"scheduled_date\":1424331681162}],\"can_run\":true,\"result\":\"Passed\",\"approval_type\":\"success\",\"counter\":\"1\",\"id\":13,\"operate_permission\":true,\"rerun_of_counter\":null,\"scheduled\":true}],\"counter\":13,\"id\":13,\"preparing_to_schedule\":false,\"label\":\"13\"}}"));
}

//TODO: fix, as not a great test - external dependency on a running go instance
public void testDownloadPipelineInstanceDetails() throws Exception {
PipelineDetailsPopulator populator = new PipelineDetailsPopulator();
JsonElement element = populator.downloadPipelineInstanceDetails("test");
assertThat(element, notNullValue());
}
}

0 comments on commit 9690e47

Please sign in to comment.