-
Notifications
You must be signed in to change notification settings - Fork 7
PLUGINS-2225: Plugin steps updated to use Slack's OAuth. #11
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
Open
Jyoti-Prakash01
wants to merge
2
commits into
master
Choose a base branch
from
PLUGINS-2225
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 -> | ||
| 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 | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.