Skip to content
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
70 changes: 70 additions & 0 deletions src/main/groovy/SlackRestHelper.groovy
Original file line number Diff line number Diff line change
@@ -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 ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method used anywhere?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would replace much of the logic here?
https://github.com/UrbanCode/Slack-UCD/blob/master/src/main/zip/postToSlack.groovy

Options:
A) I'm okay with including this code, but you should likely comment it out or label its purpose so we know.
B) Update the code where you expect it to be used. (But likely should be done in a separate PR).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New step that we have added, we are using only executePostMethod from SlackRestHelper.
Which i picked from slack ucd plugin (hcl github). We can remove it. Right now we are not using that method.

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
}

}
4 changes: 4 additions & 0 deletions src/main/zip/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,9 @@
Fix z/OS IBM-1047 encoding error.
Removed unecessary jars.
</release-note>
<release-note plugin-version="6">
Added step - Post Notification to Slack using OAuth Token.
</release-note>

</release-notes>
</pluginInfo>
37 changes: 36 additions & 1 deletion src/main/zip/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<plugin xmlns="http://www.urbancode.com/PluginXMLSchema_v1" xmlns:server="http://www.urbancode.com/PluginServerXMLSchema_v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<identifier id="com.urbancode.air.plugin.slack" name="Slack" version="5"/>
<identifier id="com.urbancode.air.plugin.slack" name="Slack" version="6"/>
<description>
Plugin for sending notifications to Slack.
</description>
Expand Down Expand Up @@ -57,6 +57,41 @@
<arg file="${PLUGIN_INPUT_PROPS}"/>
<arg file="${PLUGIN_OUTPUT_PROPS}"/>
</command>
</step-type>
<step-type name="Post Notification to Slack using OAuth Token">
<description>Send a notification to a Slack channel using OAuth Token.</description>
<properties>
<property name="token" required="true">
<property-ui default-value="${p:resource/slack/authToken}" description="Auth token for user (secure)" label="Auth Token" type="secureBox"/>
</property>
<property name="channel" required="true">
<property-ui description="Channel to send message to. Can be an encoded ID, or a name." label="Channel" type="textBox"/>
</property>
<property name="text" required="true">
<property-ui default-value="${p:server.url}/#applicationProcessRequest/${p?:parentRequest.id}" description="Text of the message to send." label="Message" type="textAreaBox"/>
</property>
<property name="username">
<property-ui description="Name of bot. If you select Post as User, this field is ignored." label="Bot Name" type="textBox"/>
</property>
<property name="as_user">
<property-ui description="Select to post the message as the authenticated user instead of as a bot." label="Post as User" type="checkBox"/>
</property>
</properties>
<post-processing><![CDATA[
if (properties.get("exitCode") != 0) {
properties.put(new java.lang.String("Status"), new java.lang.String("Failure"));
}
else {
properties.put("Status", "Success");
}
]]></post-processing>
<command program="${GROOVY_HOME}/bin/groovy">
<arg value="-cp"/>
<arg path="classes:lib/gson.jar:lib/commons-codec.jar:lib/commons-httpclient-3.1.jar:lib/commons-codec-1.2.jar"/>
<arg file="postMessageWithOauth.groovy"/>
<arg file="${PLUGIN_INPUT_PROPS}"/>
<arg file="${PLUGIN_OUTPUT_PROPS}"/>
</command>
</step-type>
<step-type name="Post Attachment Notification to Slack">
<description>
Expand Down
31 changes: 31 additions & 0 deletions src/main/zip/postMessageWithOauth.groovy
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions src/main/zip/upgrade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
</migrate>
<migrate to-version="5">
</migrate>
<migrate to-version="6">
<migrate-command name="Post Notification to Slack using OAuth Token">
</migrate-command>
</migrate>
</plugin-upgrade>