This repository has been archived by the owner on Jun 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance notificatioin message with pipeline instance details (via pip…
…eline history api)
- Loading branch information
1 parent
5553fa8
commit 9690e47
Showing
6 changed files
with
96 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/matt_richardson/gocd/websocket_notifier/PipelineDetailsPopulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/test/java/com/matt_richardson/gocd/websocket_notifier/PipelineDetailsPopulatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |