diff --git a/src/main/groovy/SlackRestHelper.groovy b/src/main/groovy/SlackRestHelper.groovy new file mode 100644 index 0000000..330c817 --- /dev/null +++ b/src/main/groovy/SlackRestHelper.groovy @@ -0,0 +1,70 @@ +import org.apache.commons.httpclient.* +import org.apache.commons.httpclient.methods.* +import groovy.json.JsonSlurper + +public class SlackRestHelper { + + def private sessionId + def private JsonSlurper jsonSlurper + def private HttpClient client + def private restUrl + def private baseUrl + def private cookie + + def public SlackRestHelper() { + + jsonSlurper = new JsonSlurper() + client = new HttpClient() + } + + def private executePostMethod = { token, channel, message, username, as_user -> + HttpMethod method = new PostMethod("https://slack.com/api/chat.postMessage") + method.addRequestHeader("ContentType", "application/x-www-form-urlencoded") + method.addParameter("token", token) + method.addParameter("channel", channel) + method.addParameter("text", message) + method.addParameter("username", username) + method.addParameter("as_user", as_user) + + def methodResult = client.executeMethod(method) + def response = method.getResponseBodyAsString() + def responseJson = null + + if (methodResult == 200) { + responseJson = jsonSlurper.parseText(response) + } + else { + throw new Exception ("Received response ${methodResult}: ${response}") + } + + if (responseJson.ok == false) { + throw new Exception ("Slack command error: " + responseJson.error) + } + + println("Posted message successfully.") + return responseJson + } + + def private executeIncomingWebhookPostMethod = {webhookURL, channel, message -> + HttpMethod method = new PostMethod(webhookURL) + method.addRequestHeader("ContentType", "application/x-www-form-urlencoded") + + def payload = "{\"channel\":\"$channel\", \"text\":\"$message\"}" + method.setRequestBody(payload) + + def methodResult = client.executeMethod(method) + def response = method.getResponseBodyAsString() + + if (methodResult != 200) { + throw new Exception ("Received response ${methodResult}: ${response}") + } + + if (response != "ok") { + throw new Exception ("Slack command error: " + responseJson.error) + } + + println("Posted Incoming Webhook message successfully.") + return response + } + +} \ No newline at end of file diff --git a/src/main/zip/info.xml b/src/main/zip/info.xml index 0a91f76..d270a0b 100644 --- a/src/main/zip/info.xml +++ b/src/main/zip/info.xml @@ -151,5 +151,9 @@ Fix z/OS IBM-1047 encoding error. Removed unecessary jars. + + Added step - Post Notification to Slack using OAuth Token. + + diff --git a/src/main/zip/plugin.xml b/src/main/zip/plugin.xml index 56b94c5..c6daea9 100644 --- a/src/main/zip/plugin.xml +++ b/src/main/zip/plugin.xml @@ -8,7 +8,7 @@
- + Plugin for sending notifications to Slack. @@ -57,6 +57,41 @@ + + + Send a notification to a Slack channel using OAuth Token. + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/zip/postMessageWithOauth.groovy b/src/main/zip/postMessageWithOauth.groovy new file mode 100644 index 0000000..51e32bc --- /dev/null +++ b/src/main/zip/postMessageWithOauth.groovy @@ -0,0 +1,31 @@ +/* + * Licensed Materials - Property of IBM Corp. + * IBM UrbanCode Deploy + * (c) Copyright IBM Corporation 2011, 2014. All Rights Reserved. + * + * U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by + * GSA ADP Schedule Contract with IBM Corp. + */ + /* This is an example step groovy to show the proper use of APTool + * In order to use import these utilities, you have to use the "pluginutilscripts" jar + * that comes bundled with this plugin example. + */ + import com.urbancode.air.AirPluginTool + + def apTool = new AirPluginTool(this.args[0], this.args[1]) + def props = apTool.getStepProperties() + + def token = props['token'] + def channel = props['channel'] + def text = props['text'] + def username = props['username'] + def as_user = props['as_user'] + def helper = new SlackRestHelper() + + try { + println("Executing post message...") + helper.executePostMethod(token, channel, text, username, as_user) + } + catch (Exception ee) { + throw new Exception("Command failed with message: " + ee.message) + } \ No newline at end of file diff --git a/src/main/zip/upgrade.xml b/src/main/zip/upgrade.xml index a93dd02..cbcc820 100644 --- a/src/main/zip/upgrade.xml +++ b/src/main/zip/upgrade.xml @@ -21,4 +21,8 @@ + + + +