Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GE Flowdock Plugin changes #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
106 changes: 106 additions & 0 deletions src/main/java/com/xebialabs/xlrelease/flowdock/plugin/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
* FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
*/
package com.xebialabs.xlrelease.flowdock.plugin;

import com.xebialabs.deployit.plugin.api.reflect.Type;
import com.xebialabs.xlrelease.domain.Phase;
import com.xebialabs.xlrelease.domain.PlanItem;
import com.xebialabs.xlrelease.domain.Release;
import com.xebialabs.xlrelease.domain.Task;
import com.xebialabs.xlrelease.domain.status.TaskStatus;

import java.io.UnsupportedEncodingException;

/**
* Created by ankurtrivedi on 02/05/16.
*/
public class ChatMessage extends FlowdockMessage{

public static final String XLRELEASE_RELEASE_MAIL = "[email protected]";

protected String externalUserName;
protected String subject;
protected String fromAddress;
protected String source;
protected String event;

public ChatMessage() {
this.externalUserName = "XLRelease";
this.subject = "Message from XL Release";
this.fromAddress = XLRELEASE_RELEASE_MAIL;
this.source = "";
}

public void setExternalUserName(String externalUserName) {
this.externalUserName = externalUserName;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}

public void setSource(String source) {
this.source = source;
}

public void setEvent(String event) {
this.event = event;
}

@Override
public String asPostData() throws UnsupportedEncodingException {
StringBuffer postData = new StringBuffer();
postData.append("subject=").append(urlEncode(subject));
postData.append("&content=").append(urlEncode(content));
postData.append("&from_address=").append(urlEncode(fromAddress));
postData.append("&source=").append(urlEncode(source));
postData.append("&external_user_name=").append(urlEncode(externalUserName));
postData.append("&tags=").append(urlEncode(tags));
postData.append("&event=").append(urlEncode(event));
return postData.toString();
}

public static ChatMessage fromAuditableDeployitEvent(PlanItem pi) {
ChatMessage msg = new ChatMessage();
String content = "";

if(pi instanceof Release){
content = "@team Release " + pi.getProperty("title") +
" assigned to " + pi.getProperty("owner") + " has status " + ((Release) pi).getStatus().value();
}
else if (pi instanceof Phase){
content = "@team Phase " + pi.getProperty("title") + " has status " + ((Phase) pi).getStatus().value();

}
else if(pi instanceof Task) {

Task task = (Task) pi;
if (task.getStatus().equals(TaskStatus.IN_PROGRESS) && task.getTaskType().equals(Type.valueOf("xlrelease.Task"))) {
content = "@"+pi.getProperty("owner")+" Approval Pending for " + pi.getProperty("title");


}
else {

content = "@team Task " + pi.getProperty("title") +
" assigned to " + pi.getProperty("owner") + " has status " + ((Task) pi).getStatus().value();
}
}

msg.setContent(content);
msg.setSubject("XL Release event");
msg.setFromAddress(XLRELEASE_RELEASE_MAIL);
msg.setSource("XL Release");
msg.setTags("XL Release");
msg.setEvent("message");

return msg;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
* FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
*/
package com.xebialabs.xlrelease.flowdock.plugin;


Expand All @@ -12,11 +17,19 @@
import java.io.IOException;
import java.net.ProtocolException;
import java.net.MalformedURLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.Proxy;
import java.net.InetSocketAddress;
import org.apache.jackrabbit.util.Base64;

/**
* Created by jdewinne on 2/5/15.
*/
public class FlowdockApi {

Logger logger = LoggerFactory.getLogger(FlowdockApi.class);

private FlowdockConfiguration flowdockConfiguration;

public FlowdockApi(FlowdockConfiguration flowdockConfiguration) {
Expand All @@ -25,34 +38,66 @@ public FlowdockApi(FlowdockConfiguration flowdockConfiguration) {

public void pushTeamInboxMessage(TeamInboxMessage msg) throws FlowdockException {
try {
doPost("/messages/team_inbox/", msg.asPostData());
logger.debug("FlowdockApi: Ready to send Team Inbox Notification");
doPost(flowdockConfiguration.getApiUrl()+"/messages/team_inbox/"+flowdockConfiguration.getFlowToken(), msg.asPostData());
logger.debug("FlowdockApi: Team Inbox Notification posted successfully");
} catch(UnsupportedEncodingException ex) {
throw new FlowdockException("Cannot encode request data: " + ex.getMessage());
}
}

public void pushChatMessage(ChatMessage msg) throws FlowdockException {
try {
logger.debug("FlowdockApi: Ready to send chat Notification");
doPost(flowdockConfiguration.getApiUrl()+"/flows/"+flowdockConfiguration.getOrgParamName()+"/messages", msg.asPostData());
logger.debug("FlowdockApi: Chat Notification posted successfully");
} catch(UnsupportedEncodingException ex) {
throw new FlowdockException("Cannot encode request data: " + ex.getMessage());
}
}

private void doPost(String path, String data) throws FlowdockException {
private void doPost(String flowdockUrl, String data) throws FlowdockException {
URL url;
HttpURLConnection connection = null;
String flowdockUrl = flowdockConfiguration.getApiUrl() + path + flowdockConfiguration.getFlowToken();

try {
// create connection
url = new URL(flowdockUrl);
connection = (HttpURLConnection) url.openConnection();
logger.debug("FlowdockApi: Creating connection");


//connection with Proxy

if (!flowdockConfiguration.getProxyHost().isEmpty() && flowdockConfiguration.getProxyHost().trim().length() != 0
&& flowdockConfiguration.getProxyPort() != 0) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(flowdockConfiguration.getProxyHost(), flowdockConfiguration.getProxyPort()));

// connection = (HttpURLConnection) url.openConnection();
System.setProperty("https.proxyHost", flowdockConfiguration.getProxyHost());
System.setProperty("https.proxyPort", String.valueOf(flowdockConfiguration.getProxyPort()));
connection = (HttpURLConnection) new URL(flowdockUrl).openConnection(proxy);
} else {
url = new URL(flowdockUrl);
connection = (HttpURLConnection) url.openConnection();
}

String encoded = Base64.encode(flowdockConfiguration.getApiKey());
connection.setRequestProperty("Authorization", "Basic "+encoded);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

// send the request

DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();

if(connection.getResponseCode() != 200) {
if(connection.getResponseCode() != 200 && connection.getResponseCode() != 201 ) {
StringBuffer responseContent = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
Expand All @@ -61,8 +106,9 @@ private void doPost(String path, String data) throws FlowdockException {
responseContent.append(responseLine);
}
in.close();

} catch(Exception ex) {
// nothing we can do about this
logger.debug("FlowdockApi: Response is " + responseContent.toString());
} finally {
throw new FlowdockException("Flowdock returned an error response with status " +
connection.getResponseCode() + " " + connection.getResponseMessage() + ", " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
* FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
*/
package com.xebialabs.xlrelease.flowdock.plugin;

/**
Expand All @@ -6,24 +11,60 @@
public class FlowdockConfiguration {

private String apiUrl = "";
private String apiKey = "";
private String proxyHost = "";
private int proxyPort = 0;
private String templateName = "";
private String flowToken = "";
private String orgParamName="";
private Boolean enabled = Boolean.FALSE;

public FlowdockConfiguration(String apiUrl, String flowToken, Boolean enabled) {
// Add the API token as the global config

//organization/flowparameterized name for the flow


public FlowdockConfiguration(String apiUrl, String apiKey, Boolean enabled, String proxyHost, int proxyPort, String templateName, String flowToken, String orgParamName) {
this.apiUrl = apiUrl;
this.flowToken = flowToken.replaceAll("\\s", "");
this.apiKey = apiKey;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.enabled = enabled;
this.templateName = templateName;
this.flowToken = flowToken;
this.orgParamName = orgParamName;
}

public String getApiUrl() {
return apiUrl;
}

public String getFlowToken() {
return flowToken;
public String getApiKey() {
return apiKey;
}

public String getProxyHost() {
return proxyHost;
}

public int getProxyPort() {
return proxyPort;
}

public Boolean isEnabled() {
return enabled;
}

public String getTemplateName() {
return templateName;
}

public String getFlowToken() {
return flowToken;
}

public String getOrgParamName() {
return orgParamName;
}

}
Loading