Skip to content

Commit

Permalink
Block notifications triggered by non bitbucket materials
Browse files Browse the repository at this point in the history
  • Loading branch information
psaikkonen committed Aug 25, 2020
1 parent aa0df99 commit 6c60952
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
apply plugin: 'java'

group = 'com.eficode.bitbucketnotifier'
version = '1.1.0-SNAPSHOT'
version = '1.0.1'

// these values that go into plugin.xml
project.ext.pluginDesc = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.LinkedTreeMap;
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;

Expand All @@ -35,6 +36,7 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class StageStatusRequestExecutor implements RequestExecutor {
private static final Gson GSON = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
Expand Down Expand Up @@ -62,33 +64,42 @@ public GoPluginApiResponse execute() throws Exception {

protected void sendNotification() throws Exception {
PluginSettings pluginSettings = pluginRequest.getPluginSettings();
Map material = this.request.pipeline.buildCause.get(0).material;

String revision = this.request.pipeline.buildCause.get(0).modifications.get(0).revision;
URL url = new URL(pluginSettings.getApiUrl() + "/rest/build-status/1.0/commits/" + revision);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", getAuthHeader(pluginSettings));
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);

OutputStream os = connection.getOutputStream();
String buildUrl = pluginSettings.getGoServerUrl() + "/go/pipelines/value_stream_map/" + request.pipeline.name + "/" + request.pipeline.counter;
byte[] input = ("{\"state\": \"" + this.parseBuildState(request.pipeline.stage.state) + "\", " +
"\"key\": \"" + revision + "\", " +
"\"name\": \"" + request.pipeline.counter + "\", " +
"\"url\": \"" + buildUrl + "\"}").getBytes("utf-8");
os.write(input, 0, input.length);

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
if (material.get("type").equals("git")) {
LinkedTreeMap gitConfig = (LinkedTreeMap) material.get("git-configuration");
String gitUrl = (String) gitConfig.get("url");

if (gitUrl.contains(pluginSettings.getApiUrl())) {
String revision = this.request.pipeline.buildCause.get(0).modifications.get(0).revision;
URL url = new URL(pluginSettings.getApiUrl() + "/rest/build-status/1.0/commits/" + revision);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", getAuthHeader(pluginSettings));
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);

OutputStream os = connection.getOutputStream();
String buildUrl = pluginSettings.getGoServerUrl() + "/go/pipelines/value_stream_map/" + request.pipeline.name + "/" + request.pipeline.counter;
byte[] input = ("{\"state\": \"" + this.parseBuildState(request.pipeline.stage.state) + "\", " +
"\"key\": \"" + revision + "\", " +
"\"name\": \"" + request.pipeline.counter + "\", " +
"\"url\": \"" + buildUrl + "\"}").getBytes("utf-8");
os.write(input, 0, input.length);

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
StringBuilder response = new StringBuilder();
String responseLine;

while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
connection.getInputStream().close();
}
}
System.out.println(response.toString());
connection.getInputStream().close();
}

private String parseBuildState(String stageState) {
Expand Down

0 comments on commit 6c60952

Please sign in to comment.