From 377709478b723015d47131dc913bf451f1fbc51a Mon Sep 17 00:00:00 2001 From: Jyoti Parkash Date: Tue, 26 Feb 2019 19:59:46 +0530 Subject: [PATCH 1/2] PLUGINS-2225: Plugin steps updated to use Slack's OAuth. --- src/main/zip/info.xml | 4 ++++ src/main/zip/plugin.xml | 12 ++++++------ src/main/zip/postToSlack.groovy | 27 +++++++++++++++++++-------- src/main/zip/upgrade.xml | 12 ++++++++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/main/zip/info.xml b/src/main/zip/info.xml index 0a91f76..7c5c0a9 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. + + Plugin steps updated to use Slack's OAuth. + + diff --git a/src/main/zip/plugin.xml b/src/main/zip/plugin.xml index 56b94c5..b856351 100644 --- a/src/main/zip/plugin.xml +++ b/src/main/zip/plugin.xml @@ -8,7 +8,7 @@
- + Plugin for sending notifications to Slack. @@ -19,8 +19,8 @@ Send a notification to a Slack channel. - - + + @@ -63,9 +63,9 @@ Send a notification to a Slack channel. - - - + + + diff --git a/src/main/zip/postToSlack.groovy b/src/main/zip/postToSlack.groovy index a645f75..76fec3d 100644 --- a/src/main/zip/postToSlack.groovy +++ b/src/main/zip/postToSlack.groovy @@ -5,6 +5,7 @@ * U.S. Government Users Restricted Rights: Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * @author cooperc */ +import groovy.json.JsonSlurper; import com.urbancode.air.AirPluginTool; import com.urbancode.air.CommandHelper; @@ -26,7 +27,7 @@ catch (IOException e) { } // properties -final def webhook = props['webhook']; +final def oauthAccessToken = props['oauthAccessToken']; final def slackChannel = props['channel']; final def colour = props['colour']; final def emoji = props['emoji']; @@ -86,17 +87,27 @@ try{ "application/json", "UTF-8" ); + + def postUrl = "https://slack.com/api/chat.postMessage?token="+oauthAccessToken+"&channel="+slackChannel+"&text="+component + def http = new HttpClient(); - def post = new PostMethod(webhook); - post.setRequestEntity(requestEntity); - - def status = http.executeMethod(post); - - if (status == 200){ + def post = new PostMethod(postUrl); + post.setRequestEntity(requestEntity); + + def status = http.executeMethod(post); + + def slurper = new JsonSlurper() + def jsonSlurper = slurper.parseText(post.getResponseBodyAsString()) + + if (status == 200 && jsonSlurper.ok==true){ println "Success: ${status}"; - System.exit(0);; + System.exit(0); } else { println "Failure: ${status}" + + //TODO - will create resource bundle to handle all errors with api. + println "Error: ${jsonSlurper.error}" + println "" System.exit(3); } } catch (Exception e) { diff --git a/src/main/zip/upgrade.xml b/src/main/zip/upgrade.xml index a93dd02..1ffbebf 100644 --- a/src/main/zip/upgrade.xml +++ b/src/main/zip/upgrade.xml @@ -21,4 +21,16 @@ + + + + + + + + + + + + From 67c5aa37dd8a0ff9733388929ead65b27ce164a5 Mon Sep 17 00:00:00 2001 From: Jyoti Parkash Date: Wed, 6 Mar 2019 23:33:29 +0530 Subject: [PATCH 2/2] PLUGINS-2225 : Added step - Post Notification to Slack using OAuth Token. --- src/main/groovy/SlackRestHelper.groovy | 70 ++++++++++++++++++++++++ src/main/zip/info.xml | 2 +- src/main/zip/plugin.xml | 45 +++++++++++++-- src/main/zip/postMessageWithOauth.groovy | 31 +++++++++++ src/main/zip/postToSlack.groovy | 27 +++------ src/main/zip/upgrade.xml | 10 +--- 6 files changed, 151 insertions(+), 34 deletions(-) create mode 100644 src/main/groovy/SlackRestHelper.groovy create mode 100644 src/main/zip/postMessageWithOauth.groovy 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 7c5c0a9..d270a0b 100644 --- a/src/main/zip/info.xml +++ b/src/main/zip/info.xml @@ -152,7 +152,7 @@ Removed unecessary jars. - Plugin steps updated to use Slack's OAuth. + Added step - Post Notification to Slack using OAuth Token. diff --git a/src/main/zip/plugin.xml b/src/main/zip/plugin.xml index b856351..c6daea9 100644 --- a/src/main/zip/plugin.xml +++ b/src/main/zip/plugin.xml @@ -19,8 +19,8 @@ Send a notification to a Slack channel. - - + + @@ -57,15 +57,50 @@ + + + Send a notification to a Slack channel using OAuth Token. + + + + + + + + + + + + + + + + + + + + + + + + + Send a notification to a Slack channel. - - - + + + 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/postToSlack.groovy b/src/main/zip/postToSlack.groovy index 76fec3d..a645f75 100644 --- a/src/main/zip/postToSlack.groovy +++ b/src/main/zip/postToSlack.groovy @@ -5,7 +5,6 @@ * U.S. Government Users Restricted Rights: Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * @author cooperc */ -import groovy.json.JsonSlurper; import com.urbancode.air.AirPluginTool; import com.urbancode.air.CommandHelper; @@ -27,7 +26,7 @@ catch (IOException e) { } // properties -final def oauthAccessToken = props['oauthAccessToken']; +final def webhook = props['webhook']; final def slackChannel = props['channel']; final def colour = props['colour']; final def emoji = props['emoji']; @@ -87,27 +86,17 @@ try{ "application/json", "UTF-8" ); - - def postUrl = "https://slack.com/api/chat.postMessage?token="+oauthAccessToken+"&channel="+slackChannel+"&text="+component - def http = new HttpClient(); - def post = new PostMethod(postUrl); - post.setRequestEntity(requestEntity); - - def status = http.executeMethod(post); - - def slurper = new JsonSlurper() - def jsonSlurper = slurper.parseText(post.getResponseBodyAsString()) - - if (status == 200 && jsonSlurper.ok==true){ + def post = new PostMethod(webhook); + post.setRequestEntity(requestEntity); + + def status = http.executeMethod(post); + + if (status == 200){ println "Success: ${status}"; - System.exit(0); + System.exit(0);; } else { println "Failure: ${status}" - - //TODO - will create resource bundle to handle all errors with api. - println "Error: ${jsonSlurper.error}" - println "" System.exit(3); } } catch (Exception e) { diff --git a/src/main/zip/upgrade.xml b/src/main/zip/upgrade.xml index 1ffbebf..cbcc820 100644 --- a/src/main/zip/upgrade.xml +++ b/src/main/zip/upgrade.xml @@ -22,15 +22,7 @@ - - - - - - - - - +