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

Use incoming-webhooks integration URL #3

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
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ See the [rundeck documentation](http://rundeck.org/docs/manual/plugins.html#inst
information on installing rundeck plugins.

## Configuration
This plugin uses Slack webhooks. Create a new webhook and copy the provided token.
This plugin uses Slack incoming webhooks integration. [Create a new webhook](https://demarque.slack.com/services/new) and copy the provided URL.

![configuration](config.png)

The only required configuration settings are:

- `API Auth Token`: webhook token
- `Team Domain`: the `mycompany` part of `https://mycompany.slack.com`
The only required configuration setting is the webhook URL.

Additionally you can set:

Expand Down Expand Up @@ -78,4 +75,4 @@ You have three possibilties to enable slack icons for incoming web-hooks:
* Original author: Hayden Bakkum @hbakkum
* @totallyunknown
* @notandy
* @lusis
* @lusis
Binary file modified config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@
@PluginDescription(title="Slack", description="Sends Rundeck Notifications to Slack")
public class SlackNotificationPlugin implements NotificationPlugin {

private static final String SLACK_API_BASE = ".slack.com/";
private static final String SLACK_API_URL_SCHEMA = "https://";
private static final String SLACK_API_WEHOOK_PATH = "services/hooks/incoming-webhook";
private static final String SLACK_API_TOKEN = "?token=%s";

private static final String SLACK_MESSAGE_COLOR_GREEN = "good";
private static final String SLACK_MESSAGE_COLOR_YELLOW = "warning";
private static final String SLACK_MESSAGE_COLOR_RED = "danger";
Expand All @@ -68,25 +63,19 @@ public class SlackNotificationPlugin implements NotificationPlugin {

private static final Configuration FREEMARKER_CFG = new Configuration();



@PluginProperty(
title = "API Auth Token",
description = "Slack API authentication token.",
required = true)
private String apiAuthToken;

@PluginProperty(
title = "Team Domain",
description = "Slack team domain.",
required = true)
private String teamDomain;
title = "Incoming webhook URL",
description = "Slack Incoming webhook URL",
required = true
)
private String wh_url;

@PluginProperty(
title = "Channel",
description = "Override default Slack channel to send notification message to.",
required = false,
defaultValue = "#general")
defaultValue = "#general"
)
private String room;

@PluginProperty(
Expand Down Expand Up @@ -157,19 +146,13 @@ public boolean postNotification(String trigger, Map executionData, Map config) {
throw new IllegalArgumentException("Unknown trigger type: [" + trigger + "].");
}

if (teamDomain.isEmpty()) {
throw new SlackNotificationPluginException(
"Slack teamDomain 'plugin.Notification.SlackNotification.teamDomain' missing in framework or project properties");
}

if (apiAuthToken.isEmpty()) {
if (wh_url.isEmpty()) {
throw new SlackNotificationPluginException(
"Slack apiAuthToken 'plugin.Notification.SlackNotification.apiAuthToken' missing in framework or project properties");
"Slack wh_url 'plugin.Notification.SlackNotification.wh_url' missing in framework or project properties");
}

String message = generateMessage(trigger, executionData, config, room);
String token = String.format(SLACK_API_TOKEN, urlEncode(apiAuthToken));
String slackResponse = invokeSlackAPIMethod(teamDomain, token, message);
String slackResponse = invokeSlackAPIMethod(wh_url, message);

if ("ok".equals(slackResponse)) {
return true;
Expand Down Expand Up @@ -218,8 +201,8 @@ private String urlEncode(String s) {
}
}

private String invokeSlackAPIMethod(String teamDomain, String token, String message) {
URL requestUrl = toURL(SLACK_API_URL_SCHEMA + teamDomain + SLACK_API_BASE + SLACK_API_WEHOOK_PATH + token);
private String invokeSlackAPIMethod(String whurl, String message) {
URL requestUrl = toURL(whurl);

HttpURLConnection connection = null;
InputStream responseStream = null;
Expand All @@ -241,7 +224,7 @@ private URL toURL(String url) {
try {
return new URL(url);
} catch (MalformedURLException malformedURLEx) {
throw new SlackNotificationPluginException("Slack API URL is malformed: [" + malformedURLEx.getMessage() + "].", malformedURLEx);
throw new SlackNotificationPluginException("Slack webhook URL is malformed: [" + malformedURLEx.getMessage() + "].", malformedURLEx);
}
}

Expand Down Expand Up @@ -292,7 +275,7 @@ private String getSlackResponse(InputStream responseStream) {
try {
return new Scanner(responseStream,"UTF-8").useDelimiter("\\A").next();
} catch (Exception ioEx) {
throw new SlackNotificationPluginException("Error reading Slack API JSON response: [" + ioEx.getMessage() + "].", ioEx);
throw new SlackNotificationPluginException("Error reading webhook JSON response: [" + ioEx.getMessage() + "].", ioEx);
}
}

Expand Down