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

Bamboo 6 and latest Rocket.Chat (0.58) support #5

Merged
merged 9 commits into from
Sep 5, 2017
Merged
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: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Just a reminder of the License, this is provided "WITHOUT WARRANTIES OR CONDITIO

# Configure
1. Go to your **RocketChat Server** tab in Bamboo administration
2. Fill all the fields, be careful the Server url should end with **/api**
2. Fill all the fields, be careful the Server url should end with **/api/v1**

# Use it
You can now use it from the **Notifications** tab in your plan configuration.
Expand All @@ -18,9 +18,14 @@ You can now use it from the **Notifications** tab in your plan configuration.
At this time, you can't use LDAP accounts and must use RocketChat local accounts.

# Working Versions
- 5.7.1 (It has only been tested with that version because it's the one we use in production
- Bamboo v6.0.0
- It might work with other versions and I'll really appreciate that you tell me if it works with your version

# Development
1. Download and install the Atlassian Plugin SDK.
2. Run **atlas-package** from the project root.
3. Grab your jar file that has just been generated in the newly created **target** directory.

# Thanks
I would like to specially thanks guys from [Sofico](http://www.sofico.be) for sharing their code with the world. Original code can be found here [BitBucket](https://bitbucket.org/sofico/bamboo-sametime-plugin).

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.ca.bamboo.plugins</groupId>
<artifactId>bamboo-rocketchat-plugin</artifactId>
<version>1.0</version>
<version>2.0</version>

<organization>
<name>Clement Agarini</name>
Expand All @@ -19,8 +19,8 @@
<packaging>atlassian-plugin</packaging>

<properties>
<bamboo.version>5.7.1</bamboo.version>
<bamboo.data.version>5.7.1</bamboo.data.version>
<bamboo.version>6.0.0</bamboo.version>
<bamboo.data.version>6.0.0</bamboo.data.version>
<amps.version>5.0.4</amps.version>
<plugin.testrunner.version>1.2.0</plugin.testrunner.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RocketChatGlobalConfiguration extends BambooActionSupport {
private BandanaManager bandanaManager = null;

@Override
public String doInput() throws Exception {
public String input() throws Exception {

return INPUT;
}
Expand All @@ -36,7 +36,7 @@ public String doInput() throws Exception {
* Set values in bandana
*/
@Override
public String doExecute() throws Exception {
public String execute() throws Exception {

bandanaManager.setValue(PlanAwareBandanaContext.GLOBAL_CONTEXT, PROP_RC_SERVER, this.rcServer);
bandanaManager.setValue(PlanAwareBandanaContext.GLOBAL_CONTEXT, PROP_RC_USER, this.rcUser);
Expand All @@ -47,19 +47,18 @@ public String doExecute() throws Exception {

/**
* Validate values
* (doesnt seem to get called?)
*/
@Override
public void validate() {

if(StringUtils.isEmpty(this.rcPassword)) {
addFieldError(rcPassword, "Please (re)enter the password.");
addFieldError("rcPassword", "Please (re)enter the password.");
}
if(StringUtils.isEmpty(this.rcUser)) {
addFieldError(rcUser, "Please enter the RocketChat user.");
addFieldError("rcUser", "Please enter the RocketChat user.");
}
if(StringUtils.isEmpty(this.rcServer)) {
addFieldError(rcServer, "Please enter the RocketChat server.");
addFieldError("rcServer", "Please enter the RocketChat server.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ public String getRecipientConfig() {
public String getEditHtml() {

// Get our template
String editTemplateLocation = ((NotificationRecipientModuleDescriptor)getModuleDescriptor()).getEditTemplate();
// Inject settings into the template context
Map<String, Object> context = new HashMap<String, Object>();
String editTemplateLocation = ((NotificationRecipientModuleDescriptor)getModuleDescriptor()).getEditTemplate();
// Inject settings into the template context
Map<String, Object> context = new HashMap<String, Object>();

// User
if (this.rcChannel != null)
context.put(VAL_RC_CHANNEL, this.rcChannel);
// Render html
return templateRenderer.render(editTemplateLocation, context);
// User
if (this.rcChannel != null)
context.put(VAL_RC_CHANNEL, this.rcChannel);
// Render html
return templateRenderer.render(editTemplateLocation, context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,13 @@ public void sendNotification(Notification notification) {

// IM Messages to be sent
String plainTextMessage = null;
String richTextMessage = null;

HtmlImContentProvidingNotification imContentProvider = (HtmlImContentProvidingNotification) notification;
plainTextMessage = imContentProvider.getIMContent();

// Make sure we have a message
if (StringUtils.isEmpty(richTextMessage)) {
richTextMessage = plainTextMessage;
}
if (StringUtils.isEmpty(plainTextMessage)) {
plainTextMessage = richTextMessage;
}

// Send when we have users to send to and a message to send
if (targetChannel != null && richTextMessage != null && plainTextMessage != null) {


// Send when we have users to send to and a message to send
if (targetChannel != null && plainTextMessage != null) {

RocketChatConnection rcConnection = null;

try {
Expand All @@ -73,8 +64,9 @@ public void sendNotification(Notification notification) {
rcConnection.logout();
}
}
} else {
log.log(Level.INFO, "Not sending since no targets and/or text is available to be sent.");
}

} else {
log.log(Level.INFO, "Not sending since no targets and/or text is available to be sent.");
}
}
}
42 changes: 7 additions & 35 deletions src/main/java/fr/ca/bamboo/plugins/rc/RocketChatConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
public class RocketChatConnection {

private static final Logger log = Logger.getLogger(RocketChatConnection.class.getName());
private static Map<String, String> roomsById = new HashMap<String, String>();

private String server;
private String user;
Expand All @@ -43,11 +42,9 @@ public RocketChatConnection(String server, String user, String pass) {
this.password = pass;

configure();
// Login
login();
}


/**
* Configures jersey client
*/
Expand All @@ -72,54 +69,29 @@ private void login() {
this.userId = data.getString("userId");
this.authToken = data.getString("authToken");
}
else{
throw new RuntimeException("Failed to fetch public rooms with error: " + responseObject.getString("message"));
}
fetchPublicRooms();
}

private void fetchPublicRooms() {
WebResource resource = restapi.path("publicRooms");

ClientResponse response = addTokens(resource).get(ClientResponse.class);

JSONObject responseObject = JSONObject.fromObject(response.getEntity(String.class));

if (responseObject.getString("status").equals("success")) {
JSONArray rooms = responseObject.getJSONArray("rooms");

for (int i = 0; i < rooms.size(); i++) {
{
JSONObject room = (JSONObject) rooms.get(i);
roomsById.put(room.getString("name"), room.getString("_id"));
}
}
}
else{
else {
throw new RuntimeException("Failed to fetch public rooms with error: " + responseObject.getString("message"));
}
}

public void sendMessage(String roomName, String message) {
// joinRoom(roomName);
WebResource resource = restapi.path("rooms").path(roomsById.get(roomName)).path("send");
WebResource resource = restapi.path("chat.postMessage");

JSONObject object = new JSONObject();
object.put("msg", message);
object.put("channel", roomName);
object.put("text", message);

ClientResponse response = addTokens(resource).header("Content-Type", "application/json").post(ClientResponse.class, object.toString());
JSONObject responseObject = JSONObject.fromObject(response.getEntity(String.class));

if (!responseObject.getString("status").equals("success")) {
if (!responseObject.getString("success").equals(true)) {
throw new RuntimeException("Failed to send the message with error: " + responseObject.getString("message"));
}
}

public void logout() {
WebResource resource = restapi.path("logout");

ClientResponse response = addTokens(resource).get(ClientResponse.class);

JSONObject responseObject = JSONObject.fromObject(response.getEntity(String.class));

if (!responseObject.getString("status").equals("success")) {
Expand All @@ -128,10 +100,10 @@ public void logout() {
}

private WebResource.Builder addTokens(WebResource original) {
return original.header("X-Auth-Token", authToken).header("X-User-Id", userId);
return original.header("X-Auth-Token", this.authToken).header("X-User-Id", this.userId);
}

public boolean isLoggedIn() {
return authToken != null && userId != null;
return this.authToken != null && this.userId != null;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/configureRocketChatServer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
titleKey='RocketChat Server'
description='Send IM notifications using RocketChat.']

[@ww.textfield name='rcServer' labelKey="rc.server.global" required='true' value =rcServer descriptionKey='rc.server.global.description' /]
[@ww.textfield name='rcUser' labelKey="rc.user.global" required='true' value =rcUser descriptionKey='rc.user.description' /]
[@ww.textfield name='rcServer' labelKey="rc.server.global" required='true' value=rcServer descriptionKey='rc.server.global.description' /]
[@ww.textfield name='rcUser' labelKey="rc.user.global" required='true' value=rcUser descriptionKey='rc.user.description' /]
[@ww.password name='rcPassword' labelKey="rc.password.global" required='true' value=rcPassword showPassword="true" descriptionKey='rc.password.description' /]

[@ui.clear /]
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/english.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
rc.server.global=Server
rc.server.global.description=The Rocket Chat server url, this should be the api url ending with /api
rc.server.global.description=The Rocket Chat server url, this should be the api url ending with /api/v1
rc.user.global=User
rc.user.description=The user to send the notifications from
rc.password.global=Password
rc.password.description=The password of the configured user

rc.channel=Send to Channel
rc.channel.description=Send the notification to the specified channel, if no channel is set not notification will be sent.
rc.channel.description=Send the notification to the specified channel, if no channel is set not notification will be sent. Include the hash before the channel name.