├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── hbakkum │ │ └── rundeck │ │ └── plugins │ │ └── hipchat │ │ ├── HipChatApiAuthTokenManager.java │ │ ├── HipChatNotificationMessageGenerator.java │ │ ├── HipChatNotificationPlugin.java │ │ ├── HipChatNotificationPluginException.java │ │ ├── HipChatNotificationPluginUtils.java │ │ ├── http │ │ ├── HttpRequestExecutor.java │ │ ├── HttpResponse.java │ │ └── RestyHttpRequestExecutor.java │ │ └── roomnotifier │ │ ├── HipChatApiVersion1RoomNotifier.java │ │ ├── HipChatApiVersion2RoomNotifier.java │ │ ├── HipChatRoomNotifier.java │ │ └── HipChatRoomNotifierFactory.java └── resources │ └── templates │ └── hipchat-message.ftl └── test ├── java └── com │ └── hbakkum │ └── rundeck │ └── plugins │ └── hipchat │ ├── HipChatApiAuthTokenManagerUnitTest.java │ ├── HipChatNotificationMessageGeneratorUnitTest.java │ └── roomnotifier │ ├── HipChatApiVersion1RoomNotifierUnitTest.java │ └── HipChatApiVersion2RoomNotifierUnitTest.java └── resources └── templates └── hipchat-message-override-test.ftl /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | target 4 | .classpath 5 | .project 6 | .settings 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rundeck-hipchat-plugin 2 | ====================== 3 | 4 | Sends rundeck notification messages to a HipChat room. 5 | 6 | Latest version is 1.7.0, full release notes can be found [here](https://github.com/hbakkum/rundeck-hipchat-plugin/wiki/Release-Notes). 7 | 8 | Installation Instructions 9 | ------------------------- 10 | 11 | 1. Either download the latest release from Maven Central 12 | ([link](http://search.maven.org/#search%7Cga%7C1%7Crundeck-hipchat-plugin)) or build a snapshot from source. 13 | 2. Copy the plugin jar (rundeck-hipchat-plugin-\.jar) into your $RDECK_BASE/libext - no restart of rundeck required. 14 | 15 | See the [rundeck documentation](http://rundeck.org/docs/manual/plugins.html#installing-plugins) for more 16 | information on installing rundeck plugins. 17 | 18 | 19 | User Guide 20 | ------------------------- 21 | 22 | Can be found [here](https://github.com/hbakkum/rundeck-hipchat-plugin/wiki/User-Guide). 23 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.hbakkum.rundeck.plugins 5 | rundeck-hipchat-plugin 6 | 1.7.1-SNAPSHOT 7 | jar 8 | 9 | rundeck-hipchat-plugin 10 | Sends rundeck job notification messages to a HipChat room 11 | https://github.com/hbakkum/rundeck-hipchat-plugin 12 | 13 | 14 | 15 | hbakkum 16 | Hayden Bakkum 17 | hayden.bakkum@gmail.com 18 | 19 | 20 | 21 | 22 | 23 | The Apache Software License, Version 2.0 24 | http://www.apache.org/licenses/LICENSE-2.0.txt 25 | 26 | 27 | 28 | 29 | scm:git:git://github.com/hbakkum/rundeck-hipchat-plugin.git 30 | scm:git:git@github.com:hbakkum/rundeck-hipchat-plugin.git 31 | https://github.com/hbakkum/rundeck-hipchat-plugin 32 | HEAD 33 | 34 | 35 | 36 | UTF-8 37 | 38 | 39 | 40 | 41 | ossrh 42 | https://oss.sonatype.org/content/repositories/snapshots 43 | 44 | 45 | ossrh 46 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | maven-jar-plugin 55 | 2.4 56 | 57 | 58 | 59 | com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPlugin 60 | 1.1 61 | true 62 | ${project.version} 63 | lib/freemarker-2.3.19.jar lib/jackson-core-asl-1.9.12.jar lib/jackson-mapper-asl-1.9.12.jar lib/resty-0.3.2.jar 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-dependency-plugin 72 | 2.8 73 | 74 | 75 | copy 76 | compile 77 | 78 | copy 79 | 80 | 81 | 82 | 83 | org.freemarker 84 | freemarker 85 | 2.3.19 86 | 87 | 88 | org.codehaus.jackson 89 | jackson-core-asl 90 | 1.9.12 91 | 92 | 93 | org.codehaus.jackson 94 | jackson-mapper-asl 95 | 1.9.12 96 | 97 | 98 | us.monoid.web 99 | resty 100 | 0.3.2 101 | 102 | 103 | ${project.build.directory}/classes/lib 104 | 105 | 106 | 107 | 108 | 109 | 110 | org.apache.maven.plugins 111 | maven-release-plugin 112 | 2.5 113 | 114 | true 115 | false 116 | release 117 | deploy 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | release 127 | 128 | 129 | performRelease 130 | true 131 | 132 | 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-source-plugin 138 | 2.2.1 139 | 140 | 141 | attach-sources 142 | 143 | jar-no-fork 144 | 145 | 146 | 147 | 148 | 149 | org.apache.maven.plugins 150 | maven-javadoc-plugin 151 | 2.9.1 152 | 153 | 154 | attach-javadocs 155 | 156 | jar 157 | 158 | 159 | 160 | 161 | 162 | 163 | org.sonatype.plugins 164 | nexus-staging-maven-plugin 165 | 1.6.3 166 | true 167 | 168 | ossrh 169 | https://oss.sonatype.org/ 170 | true 171 | 172 | 173 | 174 | 175 | org.apache.maven.plugins 176 | maven-gpg-plugin 177 | 1.5 178 | 179 | 180 | sign-artifacts 181 | verify 182 | 183 | sign 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | org.rundeck 196 | rundeck-core 197 | 2.1.1 198 | 199 | 200 | 201 | org.freemarker 202 | freemarker 203 | 2.3.19 204 | 205 | 206 | 207 | org.codehaus.jackson 208 | jackson-mapper-asl 209 | 1.9.12 210 | 211 | 212 | 213 | us.monoid.web 214 | resty 215 | 0.3.2 216 | 217 | 218 | 219 | org.slf4j 220 | slf4j-api 221 | 1.7.5 222 | provided 223 | 224 | 225 | 226 | org.testng 227 | testng 228 | 6.8.8 229 | test 230 | 231 | 232 | 233 | org.mockito 234 | mockito-all 235 | 1.9.5 236 | test 237 | 238 | 239 | 240 | 241 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/HipChatApiAuthTokenManager.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Loads and manages API Auth tokens for HipChat rooms. Loads auth token for a room from a string representation: 8 | * 9 | * ${room_name_or_id}:${api_auth_token} 10 | * 11 | * e.g. 12345:TT0Xj1dPMP4rOKbza4hqP2GNEgbtv9BZWisDavy0 12 | * 13 | * Comma separate to specify tokens for multiple rooms: 14 | * 15 | * 12345:TT0Xj1dPMP4rOKbza4hqP2GNEgbtv9BZWisDavy0, 2468:P2GNEgbtv9BZWisDavy0TT0Xj1dPMP4rOKbza4hq 16 | * 17 | * If the ${room_name_or_id} component is dropped, then the token will be used as the default in the case where no token is found for a room: 18 | * 19 | * 12345:TT0Xj1dPMP4rOKbza4hqP2GNEgbtv9BZWisDavy0, 2468:P2GNEgbtv9BZWisDavy0TT0Xj1dPMP4rOKbza4hq, WisDavy0TT0Xj1dPdPMP4rOKbza4hqP2GNEgbt 20 | * ^ 21 | * default token 22 | * 23 | * For HipChat API v1 use, a single notification level token will work for every room and thus only a single default token needs to be specified. 24 | * For HipChat API v2 use, a 'room notification' token may need to be generated for each target room 25 | * 26 | * @author hbakkum 27 | */ 28 | public class HipChatApiAuthTokenManager { 29 | 30 | private final Map roomApiAuthTokenAssociations = new HashMap(); 31 | 32 | private String defaultApiAuthToken; 33 | 34 | public HipChatApiAuthTokenManager(final String apiAuthTokenData) { 35 | load(apiAuthTokenData); 36 | } 37 | 38 | public String getApiAuthTokenForRoom(final String room) { 39 | if (roomApiAuthTokenAssociations.containsKey(room)) { 40 | return roomApiAuthTokenAssociations.get(room); 41 | } else { 42 | return defaultApiAuthToken; 43 | } 44 | } 45 | 46 | private void load(final String apiAuthTokenData) { 47 | final String[] apiAuthTokens = apiAuthTokenData.trim().split("\\s*,\\s*"); 48 | for (final String apiAuthToken : apiAuthTokens) { 49 | final String[] apiAuthTokenParts = apiAuthToken.split(":"); 50 | if (apiAuthTokenParts.length == 2) { 51 | roomApiAuthTokenAssociations.put(apiAuthTokenParts[0], apiAuthTokenParts[1]); 52 | } else { 53 | defaultApiAuthToken = apiAuthTokenParts[0]; 54 | } 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/HipChatNotificationMessageGenerator.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat; 2 | 3 | import freemarker.template.Configuration; 4 | import freemarker.template.Template; 5 | import freemarker.template.TemplateException; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.io.StringWriter; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | /** 14 | * @author Hayden Bakkum 15 | */ 16 | public class HipChatNotificationMessageGenerator { 17 | 18 | public String generateMessage( 19 | final String messageTemplateLocation, 20 | final String defaultMessageTemplateName, 21 | final String trigger, 22 | final Map executionData, 23 | final Map config) { 24 | final Configuration freeMarkerCfg = new Configuration(); 25 | 26 | final String templateName = determineTemplateName(messageTemplateLocation, defaultMessageTemplateName, freeMarkerCfg); 27 | 28 | final Map model = new HashMap(); 29 | model.put("trigger", trigger); 30 | model.put("execution", executionData); 31 | model.put("config", config); 32 | 33 | final StringWriter sw = new StringWriter(); 34 | try { 35 | final Template template = freeMarkerCfg.getTemplate(templateName); 36 | template.process(model,sw); 37 | 38 | } catch (IOException ioEx) { 39 | throw new HipChatNotificationPluginException("Error loading HipChat notification message template: [" + ioEx.getMessage() + "].", ioEx); 40 | } catch (TemplateException templateEx) { 41 | throw new HipChatNotificationPluginException("Error merging HipChat notification message template: [" + templateEx.getMessage() + "].", templateEx); 42 | } 43 | 44 | return sw.toString(); 45 | } 46 | 47 | private String determineTemplateName(final String messageTemplateLocation, 48 | final String defaultMessageTemplateName, 49 | final Configuration freeMarkerCfg) { 50 | String templateName; 51 | 52 | if (messageTemplateLocation != null && messageTemplateLocation.length() > 0) { 53 | final File messageTemplateFile = new File(messageTemplateLocation); 54 | try { 55 | freeMarkerCfg.setDirectoryForTemplateLoading(messageTemplateFile.getParentFile()); 56 | } catch (IOException ioEx) { 57 | throw new HipChatNotificationPluginException("Error setting FreeMarker template loading directory: [" + ioEx.getMessage() + "].", ioEx); 58 | } 59 | templateName = messageTemplateFile.getName(); 60 | 61 | } else { 62 | freeMarkerCfg.setClassForTemplateLoading(HipChatNotificationPlugin.class, "/templates"); 63 | templateName = defaultMessageTemplateName; 64 | } 65 | 66 | return templateName; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/HipChatNotificationPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Hayden Bakkum 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.hbakkum.rundeck.plugins.hipchat; 18 | 19 | import com.dtolabs.rundeck.core.plugins.Plugin; 20 | import com.dtolabs.rundeck.core.plugins.configuration.PropertyScope; 21 | import com.dtolabs.rundeck.plugins.descriptions.PluginDescription; 22 | import com.dtolabs.rundeck.plugins.descriptions.PluginProperty; 23 | import com.dtolabs.rundeck.plugins.notification.NotificationPlugin; 24 | import com.hbakkum.rundeck.plugins.hipchat.roomnotifier.HipChatRoomNotifier; 25 | import com.hbakkum.rundeck.plugins.hipchat.roomnotifier.HipChatRoomNotifierFactory; 26 | import org.apache.commons.lang.StringUtils; 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | 30 | import java.util.HashMap; 31 | import java.util.Map; 32 | 33 | import static org.apache.commons.lang.StringUtils.isBlank; 34 | 35 | /** 36 | * Sends Rundeck job notification messages to a HipChat room. 37 | * 38 | * @author Hayden Bakkum 39 | */ 40 | @Plugin(service= "Notification", name="HipChatNotification") 41 | @PluginDescription(title="HipChat") 42 | public class HipChatNotificationPlugin implements NotificationPlugin { 43 | 44 | private static final Logger LOG = LoggerFactory.getLogger(HipChatNotificationPlugin.class); 45 | 46 | private static final String HIPCHAT_API_DEFAULT_BASE_URL = "https://api.hipchat.com"; 47 | private static final String HIPCHAT_API_DEFAULT_VERSION = "v1"; 48 | 49 | private static final String HIPCHAT_MESSAGE_COLOR_GREEN = "green"; 50 | private static final String HIPCHAT_MESSAGE_COLOR_YELLOW = "yellow"; 51 | private static final String HIPCHAT_MESSAGE_COLOR_RED = "red"; 52 | 53 | private static final String HIPCHAT_MESSAGE_DEFAULT_TEMPLATE = "hipchat-message.ftl"; 54 | 55 | private static final String TRIGGER_START = "start"; 56 | private static final String TRIGGER_SUCCESS = "success"; 57 | private static final String TRIGGER_FAILURE = "failure"; 58 | 59 | private static final Map TRIGGER_MESSAGE_COLORS = new HashMap(); 60 | static { 61 | TRIGGER_MESSAGE_COLORS.put(TRIGGER_START, HIPCHAT_MESSAGE_COLOR_YELLOW); 62 | TRIGGER_MESSAGE_COLORS.put(TRIGGER_SUCCESS, HIPCHAT_MESSAGE_COLOR_GREEN); 63 | TRIGGER_MESSAGE_COLORS.put(TRIGGER_FAILURE, HIPCHAT_MESSAGE_COLOR_RED); 64 | } 65 | 66 | @PluginProperty( 67 | title = "Room(s)", 68 | description = "HipChat room name or ID (ID is recommended) to send notification message to. To specify multiple rooms, separate with a comma", 69 | required = true) 70 | private String room; 71 | 72 | @PluginProperty( 73 | title = "HipChat Server Base URL", 74 | description = "Base URL of HipChat Server", 75 | required = false, 76 | defaultValue = HIPCHAT_API_DEFAULT_BASE_URL, 77 | scope = PropertyScope.Project) 78 | private String hipchatServerBaseUrl; 79 | 80 | @PluginProperty( 81 | title = "HipChat API Version", 82 | description = "HipChat API version to use ", 83 | required = false, 84 | defaultValue = HIPCHAT_API_DEFAULT_VERSION, 85 | scope = PropertyScope.Project) 86 | private String apiVersion; 87 | 88 | @PluginProperty( 89 | title = "API Auth Token(s)", 90 | description = "HipChat API authentication token(s). For HipChat API v1, a single notification level token will do. For HipChat API v2, a token per room may be required - see user guide for more information", 91 | required = true, 92 | scope = PropertyScope.Project) 93 | private String apiAuthToken; 94 | 95 | @PluginProperty( 96 | title = "Notification Message Template", 97 | description = 98 | "Absolute path to a FreeMarker template that will be used to generate the notification message. " + 99 | "If unspecified a default message template will be used.", 100 | required = false, 101 | scope = PropertyScope.Project) 102 | private String messageTemplateLocation; 103 | 104 | @PluginProperty( 105 | title = "Proxy Host", 106 | description = "Proxy host to use when communicating to the HipChat API.", 107 | required = false, 108 | defaultValue = "", 109 | scope = PropertyScope.Project) 110 | private String proxyHost; 111 | 112 | @PluginProperty( 113 | title = "Proxy Port", 114 | description = "Proxy port to use when communicating to the HipChat API.", 115 | required = false, 116 | defaultValue = "", 117 | scope = PropertyScope.Project) 118 | private String proxyPort; 119 | 120 | @PluginProperty( 121 | title = "Send User Notification", 122 | description = "Whether this message should trigger a user notification (change the tab color, play a sound, notify mobile phones, etc). Each recipient's notification preferences are taken into account.", 123 | required = false, 124 | defaultValue = "") 125 | private boolean sendUserNotification; 126 | 127 | @PluginProperty( 128 | title = "API Auth Token Override", 129 | description = "Overrides any HipChat API authentication tokens used in your framework or project property files. " + 130 | "For HipChat API v1, a single notification level token will do. For HipChat API v2, a token per room may be required - see user guide for more information. " + 131 | "__NOTE__: Configuring tokens at this level will likely result in token duplication and will also mean that your token will be available in clear text within the rundeck UI. " + 132 | "Only manage your API tokens here if you are comfortable with these issues and it is impractical to manage tokens in your framework or project properties files (which is the recommended approach).", 133 | required = false, 134 | scope = PropertyScope.InstanceOnly) 135 | private String apiAuthTokenOverride; 136 | 137 | /** 138 | * Sends a message to a HipChat room when a job notification event is raised by Rundeck. 139 | * 140 | * @param trigger name of job notification event causing notification 141 | * @param executionData job execution data 142 | * @param config plugin configuration 143 | * @throws HipChatNotificationPluginException when any error occurs sending the HipChat message 144 | * @return true, if all HipChat notifications were successfully sent to each room 145 | */ 146 | @Override 147 | public boolean postNotification(final String trigger, final Map executionData, final Map config) { 148 | if (!TRIGGER_MESSAGE_COLORS.containsKey(trigger)) { 149 | throw new IllegalArgumentException("Unknown trigger type: [" + trigger + "]."); 150 | } 151 | 152 | final HipChatRoomNotifier hipChatRoomNotifier = HipChatRoomNotifierFactory.get(apiVersion, proxyHost, proxyPort); 153 | final HipChatApiAuthTokenManager hipChatApiAuthTokenManager = new HipChatApiAuthTokenManager(isBlank(apiAuthTokenOverride) ? apiAuthToken : apiAuthTokenOverride); 154 | final HipChatNotificationMessageGenerator hipChatNotificationMessageGenerator = new HipChatNotificationMessageGenerator(); 155 | 156 | final String color = TRIGGER_MESSAGE_COLORS.get(trigger); 157 | final String message = hipChatNotificationMessageGenerator.generateMessage(messageTemplateLocation, HIPCHAT_MESSAGE_DEFAULT_TEMPLATE, trigger, executionData, config); 158 | 159 | return sendRoomNotifications(hipChatRoomNotifier, hipChatApiAuthTokenManager, message, color); 160 | } 161 | 162 | private boolean sendRoomNotifications( 163 | final HipChatRoomNotifier hipChatRoomNotifier, 164 | final HipChatApiAuthTokenManager hipChatApiAuthTokenManager, 165 | final String message, 166 | final String color) { 167 | boolean didAllNotificationsSendSuccessfully = true; 168 | 169 | final String[] rooms = this.room.trim().split("\\s*,\\s*"); 170 | for (final String room : rooms) { 171 | final String apiAuthTokenForRoom = hipChatApiAuthTokenManager.getApiAuthTokenForRoom(room); 172 | if (apiAuthTokenForRoom == null || apiAuthTokenForRoom.isEmpty()) { 173 | LOG.error("Cannot send notification to room [{}] as no API Auth Token found for this room.", room); 174 | continue; 175 | } 176 | 177 | try { 178 | hipChatRoomNotifier.sendRoomNotification(hipchatServerBaseUrl, room, message, color, apiAuthTokenForRoom, sendUserNotification); 179 | 180 | } catch (Exception ex) { 181 | LOG.error("Error sending HipChat notification to room: [{}]", room, ex); 182 | didAllNotificationsSendSuccessfully = false; 183 | } 184 | } 185 | 186 | return didAllNotificationsSendSuccessfully; 187 | } 188 | 189 | } 190 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/HipChatNotificationPluginException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Hayden Bakkum 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.hbakkum.rundeck.plugins.hipchat; 18 | 19 | /** 20 | * @author Hayden Bakkum 21 | */ 22 | public class HipChatNotificationPluginException extends RuntimeException { 23 | 24 | /** 25 | * Constructor. 26 | * 27 | * @param message error message 28 | */ 29 | public HipChatNotificationPluginException(String message) { 30 | super(message); 31 | } 32 | 33 | /** 34 | * Constructor. 35 | * 36 | * @param message error message 37 | * @param cause exception cause 38 | */ 39 | public HipChatNotificationPluginException(String message, Throwable cause) { 40 | super(message, cause); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/HipChatNotificationPluginUtils.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat; 2 | 3 | import java.io.IOException; 4 | import java.io.UnsupportedEncodingException; 5 | import java.net.HttpURLConnection; 6 | import java.net.URLEncoder; 7 | 8 | /** 9 | * @author Hayden Bakkum 10 | */ 11 | public final class HipChatNotificationPluginUtils { 12 | 13 | public static String urlEncode(String s) { 14 | try { 15 | return URLEncoder.encode(s, "UTF-8").replace("+", "%20"); 16 | } catch (UnsupportedEncodingException unsupportedEncodingException) { 17 | throw new HipChatNotificationPluginException("URL encoding error: [" + unsupportedEncodingException.getMessage() + "].", unsupportedEncodingException); 18 | } 19 | } 20 | 21 | public static int getResponseCode(final HttpURLConnection connection) { 22 | try { 23 | return connection.getResponseCode(); 24 | } catch (IOException ioEx) { 25 | throw new HipChatNotificationPluginException("Failed to obtain HTTP response from HipChat server: [" + ioEx.getMessage() + "].", ioEx); 26 | } 27 | } 28 | 29 | public static boolean isNotEmpty(final String value) { 30 | return value != null && !"".equals(value); 31 | } 32 | 33 | private HipChatNotificationPluginUtils() {} 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/http/HttpRequestExecutor.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.http; 2 | 3 | /** 4 | * @author Hayden Bakkum 5 | */ 6 | public interface HttpRequestExecutor { 7 | 8 | void setProxy(final String proxyHost, final int proxyPort); 9 | 10 | HttpResponse execute(final String url); 11 | 12 | HttpResponse execute(final String url, final String jsonRequestBody); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/http/HttpResponse.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.http; 2 | 3 | /** 4 | * @author Hayden Bakkum 5 | */ 6 | public class HttpResponse { 7 | 8 | public static final int STATUS__NO_CONTENT = 204; 9 | 10 | public static final String CONTENT_TYPE__JSON = "application/json"; 11 | 12 | private final int responseCode; 13 | 14 | private final String contentType; 15 | 16 | private final String responseBody; 17 | 18 | public HttpResponse(final int responseCode, final String contentType, final String responseBody) { 19 | this.responseCode = responseCode; 20 | this.contentType = contentType; 21 | this.responseBody = responseBody; 22 | } 23 | 24 | public int getResponseCode() { 25 | return responseCode; 26 | } 27 | 28 | public String getContentType() { 29 | return contentType; 30 | } 31 | 32 | public String getResponseBody() { 33 | return responseBody; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return 39 | "HttpResponse ["+ 40 | "responseCode = "+responseCode+","+ 41 | "contentType = "+contentType+","+ 42 | "responseBody = "+responseBody+ 43 | "]"; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/http/RestyHttpRequestExecutor.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.http; 2 | 3 | import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException; 4 | import us.monoid.json.JSONException; 5 | import us.monoid.json.JSONObject; 6 | import us.monoid.web.Resty; 7 | import us.monoid.web.TextResource; 8 | 9 | import java.io.IOException; 10 | import java.net.HttpURLConnection; 11 | 12 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.getResponseCode; 13 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.isNotEmpty; 14 | import static us.monoid.web.Resty.content; 15 | 16 | /** 17 | * @author Hayden Bakkum 18 | */ 19 | public class RestyHttpRequestExecutor implements HttpRequestExecutor { 20 | 21 | private String proxyHost; 22 | private int proxyPort = -1; 23 | 24 | @Override 25 | public void setProxy(final String proxyHost, final int proxyPort) { 26 | this.proxyHost = proxyHost; 27 | this.proxyPort = proxyPort; 28 | } 29 | 30 | @Override 31 | public HttpResponse execute(final String url) { 32 | HttpURLConnection httpConnection = null; 33 | try { 34 | final Resty resty = new Resty(); 35 | if (isProxySet()) { 36 | resty.setProxy(proxyHost, proxyPort); 37 | } 38 | final TextResource textResource = resty.text(url); 39 | httpConnection = textResource.http(); 40 | 41 | return toHttpResponse(textResource, httpConnection); 42 | 43 | } catch (IOException ioEx) { 44 | throw new HipChatNotificationPluginException("Error opening connection to HipChat URL: [" + ioEx.getMessage() + "].", ioEx); 45 | 46 | } finally { 47 | if (httpConnection != null) { 48 | httpConnection.disconnect(); 49 | } 50 | } 51 | } 52 | 53 | @Override 54 | public HttpResponse execute(final String url, final String jsonRequestBody) { 55 | HttpURLConnection httpConnection = null; 56 | try { 57 | final Resty resty = new Resty(); 58 | if (isProxySet()) { 59 | resty.setProxy(proxyHost, proxyPort); 60 | } 61 | final TextResource textResource = resty.text(url, content(new JSONObject(jsonRequestBody))); 62 | httpConnection = textResource.http(); 63 | 64 | return toHttpResponse(textResource, httpConnection); 65 | 66 | } catch (IOException ioEx) { 67 | throw new HipChatNotificationPluginException("Error opening connection to HipChat URL: [" + ioEx.getMessage() + "].", ioEx); 68 | 69 | } catch (JSONException jsonEx) { 70 | throw new HipChatNotificationPluginException("Malformed JSON request body: [" + jsonEx.getMessage() + "].", jsonEx); 71 | 72 | } finally { 73 | if (httpConnection != null) { 74 | httpConnection.disconnect(); 75 | } 76 | } 77 | } 78 | 79 | private boolean isProxySet() { 80 | return isNotEmpty(proxyHost) && proxyPort > -1; 81 | } 82 | 83 | private HttpResponse toHttpResponse(final TextResource textResource, final HttpURLConnection httpConnection) { 84 | final int responseCode = getResponseCode(httpConnection); 85 | final String contentType = httpConnection.getHeaderField("content-type"); 86 | final String responseBody = textResource.toString(); 87 | 88 | return new HttpResponse(responseCode, contentType, responseBody); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/roomnotifier/HipChatApiVersion1RoomNotifier.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.roomnotifier; 2 | 3 | import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException; 4 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor; 5 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpResponse; 6 | import org.codehaus.jackson.annotate.JsonIgnoreProperties; 7 | import org.codehaus.jackson.annotate.JsonProperty; 8 | import org.codehaus.jackson.map.ObjectMapper; 9 | 10 | import java.io.IOException; 11 | import java.util.Map; 12 | 13 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.urlEncode; 14 | 15 | /** 16 | * @author Hayden Bakkum 17 | */ 18 | public class HipChatApiVersion1RoomNotifier implements HipChatRoomNotifier { 19 | 20 | private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH = "rooms/message"; 21 | private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY = "?auth_token=%s&format=json&message_format=html&room_id=%s&from=%s&message=%s&color=%s¬ify=%s"; 22 | private static final String HIPCHAT_API_VERSION = "v1"; 23 | 24 | private static final String HIPCHAT_MESSAGE_FROM_NAME = "Rundeck"; 25 | 26 | private final HttpRequestExecutor httpRequestExecutor; 27 | 28 | public HipChatApiVersion1RoomNotifier(final HttpRequestExecutor httpRequestExecutor) { 29 | this.httpRequestExecutor = httpRequestExecutor; 30 | } 31 | 32 | @Override 33 | public void sendRoomNotification( 34 | final String baseURL, 35 | final String room, 36 | final String message, 37 | final String color, 38 | final String authToken, 39 | final boolean sendUserNotification) { 40 | 41 | final String urlQueryString = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY, 42 | urlEncode(authToken), 43 | urlEncode(room), 44 | urlEncode(HIPCHAT_MESSAGE_FROM_NAME), 45 | urlEncode(message), 46 | urlEncode(color), 47 | sendUserNotification ? 1 : 0); 48 | 49 | final HipChatAPIResponse hipChatResponse = invokeHipChatAPI(baseURL, HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH, urlQueryString); 50 | 51 | if (hipChatResponse.hasError()) { 52 | throw new HipChatNotificationPluginException("Error returned from HipChat API: [" + hipChatResponse.getErrorMessage() + "]."); 53 | } 54 | 55 | if (!"sent".equals(hipChatResponse.status)) { 56 | throw new HipChatNotificationPluginException("Unknown status returned from HipChat API: [" + hipChatResponse.status + "]."); 57 | } 58 | } 59 | 60 | @Override 61 | public String getSupportedApiVersion() { 62 | return HIPCHAT_API_VERSION; 63 | } 64 | 65 | private HipChatAPIResponse invokeHipChatAPI(final String hipchatServerBaseUrl, final String urlPath, final String urlQueryString) { 66 | final HttpResponse httpResponse = httpRequestExecutor.execute(hipchatServerBaseUrl + "/" + HIPCHAT_API_VERSION + "/" + urlPath + urlQueryString); 67 | 68 | // naively check that a HipChat API response was obtained. 69 | if (HttpResponse.CONTENT_TYPE__JSON.equals(httpResponse.getContentType())) { 70 | return toHipChatResponse(httpResponse.getResponseBody()); 71 | } else { 72 | throw new HipChatNotificationPluginException("Request did not reach HipChat API. Response code was [" + httpResponse.getResponseCode() + "]. Are your proxy settings correct?"); 73 | } 74 | } 75 | 76 | private HipChatAPIResponse toHipChatResponse(final String responseBody) { 77 | try { 78 | return new ObjectMapper().readValue(responseBody, HipChatAPIResponse.class); 79 | } catch (IOException ioEx) { 80 | throw new HipChatNotificationPluginException("Error reading HipChat API JSON response: [" + ioEx.getMessage() + "].", ioEx); 81 | } 82 | } 83 | 84 | @JsonIgnoreProperties(ignoreUnknown = true) 85 | private static class HipChatAPIResponse { 86 | 87 | @JsonProperty 88 | private String status; 89 | 90 | @JsonProperty 91 | private Map error; 92 | 93 | private boolean hasError() { 94 | return error != null; 95 | } 96 | 97 | private String getErrorMessage() { 98 | return (String) error.get("message"); 99 | } 100 | 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/roomnotifier/HipChatApiVersion2RoomNotifier.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.roomnotifier; 2 | 3 | import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException; 4 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor; 5 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpResponse; 6 | import org.codehaus.jackson.map.ObjectMapper; 7 | import org.codehaus.jackson.node.JsonNodeFactory; 8 | import org.codehaus.jackson.node.ObjectNode; 9 | 10 | import java.io.IOException; 11 | 12 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.urlEncode; 13 | 14 | /** 15 | * @author Hayden Bakkum 16 | */ 17 | public class HipChatApiVersion2RoomNotifier implements HipChatRoomNotifier { 18 | 19 | private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH = "room/%s/notification"; 20 | private static final String HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY = "?auth_token=%s"; 21 | private static final String HIPCHAT_API_VERSION = "v2"; 22 | 23 | private final HttpRequestExecutor httpRequestExecutor; 24 | 25 | public HipChatApiVersion2RoomNotifier(final HttpRequestExecutor httpRequestExecutor) { 26 | this.httpRequestExecutor = httpRequestExecutor; 27 | } 28 | 29 | @Override 30 | public void sendRoomNotification( 31 | final String baseURL, 32 | final String room, 33 | final String message, 34 | final String color, 35 | final String authToken, 36 | final boolean sendUserNotification) { 37 | 38 | final ObjectNode requestBody = JsonNodeFactory.instance.objectNode(); 39 | requestBody.put("message", message); 40 | requestBody.put("color", color); 41 | requestBody.put("message_format", "html"); 42 | requestBody.put("notify", sendUserNotification); 43 | 44 | 45 | final String urlPath = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_PATH, urlEncode(room)); 46 | final String urlQueryString = String.format(HIPCHAT_API_ROOM_NOTIFICATION_URL_QUERY, urlEncode(authToken)); 47 | 48 | final HttpResponse httpResponse = httpRequestExecutor.execute(baseURL + "/" + HIPCHAT_API_VERSION + "/" + urlPath + urlQueryString, requestBody.toString()); 49 | 50 | if (httpResponse.getResponseCode() != HttpResponse.STATUS__NO_CONTENT) { 51 | throw toHipChatNotificationPluginException(httpResponse); 52 | } 53 | } 54 | 55 | @Override 56 | public String getSupportedApiVersion() { 57 | return HIPCHAT_API_VERSION; 58 | } 59 | 60 | private HipChatNotificationPluginException toHipChatNotificationPluginException(final HttpResponse httpResponse) { 61 | if (HttpResponse.CONTENT_TYPE__JSON.equals(httpResponse.getContentType())) { 62 | final String errorMessage = getErrorMessage(httpResponse.getResponseBody()); 63 | if (errorMessage != null && !errorMessage.isEmpty()) { 64 | return new HipChatNotificationPluginException("HipChat API returned an error: ["+errorMessage+"]"); 65 | } 66 | } 67 | 68 | return new HipChatNotificationPluginException("Unexpected response received from HipChat API: ["+httpResponse+"]"); 69 | } 70 | 71 | private String getErrorMessage(final String responseBody) { 72 | try { 73 | return new ObjectMapper().readTree(responseBody).path("error").path("message").asText(); 74 | } catch (IOException ioEx) { 75 | throw new HipChatNotificationPluginException("Error reading HipChat API JSON response: [" + ioEx.getMessage() + "].", ioEx); 76 | } 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/roomnotifier/HipChatRoomNotifier.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.roomnotifier; 2 | 3 | /** 4 | * @author Hayden Bakkum 5 | */ 6 | public interface HipChatRoomNotifier { 7 | 8 | void sendRoomNotification(String baseURL, String room, String message, String color, String authToken, boolean sendUserNotification); 9 | 10 | String getSupportedApiVersion(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/hbakkum/rundeck/plugins/hipchat/roomnotifier/HipChatRoomNotifierFactory.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.roomnotifier; 2 | 3 | import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException; 4 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor; 5 | import com.hbakkum.rundeck.plugins.hipchat.http.RestyHttpRequestExecutor; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.isNotEmpty; 11 | 12 | /** 13 | * @author Hayden Bakkum 14 | */ 15 | public class HipChatRoomNotifierFactory { 16 | 17 | private static final Map HIPCHAT_ROOM_NOTIFIERS = new HashMap(); 18 | private static final HttpRequestExecutor httpRequestExecutor = new RestyHttpRequestExecutor(); 19 | 20 | static { 21 | final HipChatRoomNotifier[] hipChatRoomNotifiers = { 22 | new HipChatApiVersion1RoomNotifier(httpRequestExecutor), 23 | new HipChatApiVersion2RoomNotifier(httpRequestExecutor) 24 | }; 25 | 26 | for (final HipChatRoomNotifier hipChatRoomNotifier : hipChatRoomNotifiers) { 27 | HIPCHAT_ROOM_NOTIFIERS.put(hipChatRoomNotifier.getSupportedApiVersion(), hipChatRoomNotifier); 28 | } 29 | } 30 | 31 | public static HipChatRoomNotifier get(final String apiVersion, final String proxyHost, final String proxyPort) { 32 | final HipChatRoomNotifier hipChatRoomNotifier = HIPCHAT_ROOM_NOTIFIERS.get(apiVersion); 33 | 34 | if (hipChatRoomNotifier == null) { 35 | throw new HipChatNotificationPluginException("Unknown or unsupported HipChat API version: ["+apiVersion+"]"); 36 | } 37 | 38 | if (isNotEmpty(proxyHost) && isNotEmpty(proxyPort)) { 39 | httpRequestExecutor.setProxy(proxyHost, Integer.valueOf(proxyPort)); 40 | } 41 | 42 | return hipChatRoomNotifier; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/resources/templates/hipchat-message.ftl: -------------------------------------------------------------------------------- 1 | Execution of job 2 | 3 | <#if execution.job.group?has_content>${execution.job.group}/${execution.job.name} 4 | <#if trigger == "start"> 5 | started 6 | <#elseif trigger == "failure"> 7 | failed 8 | <#elseif trigger == "success"> 9 | succeeded 10 | 11 |
    12 |
  • User: ${execution.context.job.username}
  • 13 |
  • ExecId: ${execution.context.job.execid}
  • 14 |
15 | View Output -------------------------------------------------------------------------------- /src/test/java/com/hbakkum/rundeck/plugins/hipchat/HipChatApiAuthTokenManagerUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat; 2 | 3 | import org.testng.annotations.Test; 4 | 5 | import static org.testng.Assert.assertEquals; 6 | 7 | public class HipChatApiAuthTokenManagerUnitTest { 8 | 9 | private static final String MULTIPLE_ROOM_AUTH_TOKENS_AND_DEFAULT = "1111:atoken, 2222:anothertoken, defaulttoken"; 10 | 11 | private HipChatApiAuthTokenManager hipChatApiAuthTokenManager; 12 | 13 | @Test 14 | public void testRoomTokenIsReturnedWhenOneExists() { 15 | hipChatApiAuthTokenManager = new HipChatApiAuthTokenManager(MULTIPLE_ROOM_AUTH_TOKENS_AND_DEFAULT); 16 | 17 | assertEquals(hipChatApiAuthTokenManager.getApiAuthTokenForRoom("2222"), "anothertoken"); 18 | } 19 | 20 | @Test 21 | public void testDefaultTokenIsReturnedWhenNoTokenExistsforRoom() { 22 | hipChatApiAuthTokenManager = new HipChatApiAuthTokenManager(MULTIPLE_ROOM_AUTH_TOKENS_AND_DEFAULT); 23 | 24 | assertEquals(hipChatApiAuthTokenManager.getApiAuthTokenForRoom("3333"), "defaulttoken"); 25 | } 26 | 27 | @Test 28 | public void testDefaultTokenIsReturnedWhenOnlyDefaultTokenIsSpecified() { 29 | hipChatApiAuthTokenManager = new HipChatApiAuthTokenManager("defaulttoken"); 30 | 31 | assertEquals(hipChatApiAuthTokenManager.getApiAuthTokenForRoom("1111"), "defaulttoken"); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/hbakkum/rundeck/plugins/hipchat/HipChatNotificationMessageGeneratorUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat; 2 | 3 | import org.testng.annotations.BeforeMethod; 4 | import org.testng.annotations.Test; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import static org.testng.Assert.assertEquals; 10 | 11 | /** 12 | * @author Hayden Bakkum 13 | */ 14 | public class HipChatNotificationMessageGeneratorUnitTest { 15 | 16 | private static final Map EXECUTION_DATA = new HashMap(); 17 | static { 18 | final Map job = new HashMap(); 19 | job.put("href", "http://rundeck/jobs/my_job"); 20 | job.put("group", "job_group"); 21 | job.put("name", "job_name"); 22 | job.put("username", "hbakkum"); 23 | job.put("execid", "1"); 24 | 25 | final Map context = new HashMap(); 26 | context.put("job", job); 27 | 28 | EXECUTION_DATA.put("job", job); 29 | EXECUTION_DATA.put("context", context); 30 | EXECUTION_DATA.put("href", "http://rundeck/jobs/my_job/output"); 31 | } 32 | 33 | private HipChatNotificationMessageGenerator messageGenerator; 34 | 35 | @BeforeMethod 36 | public void setUp() { 37 | this.messageGenerator = new HipChatNotificationMessageGenerator(); 38 | } 39 | 40 | @Test 41 | public void testDefaultTemplateGetsUsedWhenNoTemplateLocationIsSpecified() { 42 | final String expectedMessage = "Execution of job\n" + 43 | "\n" + 44 | "job_group/job_name\n" + 45 | " started\n" + 46 | "
    \n" + 47 | "
  • User: hbakkum
  • \n" + 48 | "
  • ExecId: 1
  • \n" + 49 | "
\n" + 50 | "View Output"; 51 | 52 | final String actualMessage = messageGenerator.generateMessage(null, "hipchat-message.ftl", "start", EXECUTION_DATA, null); 53 | 54 | assertEquals(actualMessage, expectedMessage); 55 | } 56 | 57 | @Test 58 | public void testDefaultTemplateIsOverriddenWhenTemplateLocationIsSpecified() { 59 | final String expectedMessage = "job_name started"; 60 | final String templateLocation = this.getClass().getResource("/templates/hipchat-message-override-test.ftl").getFile(); 61 | 62 | final String actualMessage = messageGenerator.generateMessage(templateLocation, "hipchat-message.ftl", "start", EXECUTION_DATA, null); 63 | 64 | assertEquals(actualMessage, expectedMessage); 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/com/hbakkum/rundeck/plugins/hipchat/roomnotifier/HipChatApiVersion1RoomNotifierUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.roomnotifier; 2 | 3 | import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException; 4 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor; 5 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpResponse; 6 | import org.mockito.ArgumentCaptor; 7 | import org.testng.annotations.BeforeMethod; 8 | import org.testng.annotations.Test; 9 | 10 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.urlEncode; 11 | import static org.mockito.Matchers.anyString; 12 | import static org.mockito.Mockito.*; 13 | import static org.testng.Assert.assertEquals; 14 | import static org.testng.Assert.assertTrue; 15 | 16 | /** 17 | * @author Hayden Bakkum 18 | */ 19 | public class HipChatApiVersion1RoomNotifierUnitTest { 20 | 21 | private static final String HIPCHAT_BASE_URL = ""; 22 | private static final String HIPCHAT_ROOM_NAME = "Test Room"; 23 | private static final String HIPCHAT_MESSAGE = "Hello World"; 24 | private static final String HIPCHAT_COLOR = "red"; 25 | private static final String HIPCHAT_AUTH_TOKEN = "abcdef"; 26 | 27 | private HttpRequestExecutor requestExecutor; 28 | 29 | private HipChatApiVersion1RoomNotifier roomNotifier; 30 | 31 | @BeforeMethod 32 | public void setUp() { 33 | requestExecutor = mock(HttpRequestExecutor.class); 34 | 35 | roomNotifier = new HipChatApiVersion1RoomNotifier(requestExecutor); 36 | } 37 | 38 | @Test 39 | public void testSendRoomNotificationUrlHasExpectedBaseUrl() { 40 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 41 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 42 | 43 | roomNotifier.sendRoomNotification( 44 | HIPCHAT_BASE_URL, 45 | HIPCHAT_ROOM_NAME, 46 | HIPCHAT_MESSAGE, 47 | HIPCHAT_COLOR, 48 | HIPCHAT_AUTH_TOKEN, 49 | true 50 | ); 51 | 52 | assertTrue(captureHipChatUrl().startsWith(HIPCHAT_BASE_URL)); 53 | } 54 | 55 | @Test 56 | public void testSendRoomNotificationUrlHasExpectedVersion() { 57 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 58 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 59 | 60 | roomNotifier.sendRoomNotification( 61 | HIPCHAT_BASE_URL, 62 | HIPCHAT_ROOM_NAME, 63 | HIPCHAT_MESSAGE, 64 | HIPCHAT_COLOR, 65 | HIPCHAT_AUTH_TOKEN, 66 | true 67 | ); 68 | 69 | assertTrue(captureHipChatUrl().contains("v1")); 70 | } 71 | 72 | @Test 73 | public void testSendRoomNotificationUrlHasExpectedRoomName() { 74 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 75 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 76 | 77 | roomNotifier.sendRoomNotification( 78 | HIPCHAT_BASE_URL, 79 | HIPCHAT_ROOM_NAME, 80 | HIPCHAT_MESSAGE, 81 | HIPCHAT_COLOR, 82 | HIPCHAT_AUTH_TOKEN, 83 | true 84 | ); 85 | 86 | assertTrue(captureHipChatUrl().contains("room_id="+urlEncode(HIPCHAT_ROOM_NAME))); 87 | } 88 | 89 | @Test 90 | public void testSendRoomNotificationUrlHasExpectedMessage() { 91 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 92 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 93 | 94 | roomNotifier.sendRoomNotification( 95 | HIPCHAT_BASE_URL, 96 | HIPCHAT_ROOM_NAME, 97 | HIPCHAT_MESSAGE, 98 | HIPCHAT_COLOR, 99 | HIPCHAT_AUTH_TOKEN, 100 | true 101 | ); 102 | 103 | assertTrue(captureHipChatUrl().contains("message="+urlEncode(HIPCHAT_MESSAGE))); 104 | } 105 | 106 | @Test 107 | public void testSendRoomNotificationUrlHasExpectedColor() { 108 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 109 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 110 | 111 | roomNotifier.sendRoomNotification( 112 | HIPCHAT_BASE_URL, 113 | HIPCHAT_ROOM_NAME, 114 | HIPCHAT_MESSAGE, 115 | HIPCHAT_COLOR, 116 | HIPCHAT_AUTH_TOKEN, 117 | true 118 | ); 119 | 120 | assertTrue(captureHipChatUrl().contains("color="+urlEncode(HIPCHAT_COLOR))); 121 | } 122 | 123 | @Test 124 | public void testSendRoomNotificationUrlHasExpectedAuthToken() { 125 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 126 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 127 | 128 | roomNotifier.sendRoomNotification( 129 | HIPCHAT_BASE_URL, 130 | HIPCHAT_ROOM_NAME, 131 | HIPCHAT_MESSAGE, 132 | HIPCHAT_COLOR, 133 | HIPCHAT_AUTH_TOKEN, 134 | true 135 | ); 136 | 137 | assertTrue(captureHipChatUrl().contains("auth_token="+urlEncode(HIPCHAT_AUTH_TOKEN))); 138 | } 139 | 140 | @Test 141 | public void testSendRoomNotificationUrlHasExpectedFullUrl() { 142 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 143 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 144 | 145 | roomNotifier.sendRoomNotification( 146 | HIPCHAT_BASE_URL, 147 | HIPCHAT_ROOM_NAME, 148 | HIPCHAT_MESSAGE, 149 | HIPCHAT_COLOR, 150 | HIPCHAT_AUTH_TOKEN, 151 | true 152 | ); 153 | 154 | assertEquals(captureHipChatUrl(), HIPCHAT_BASE_URL+"/v1/rooms/message?auth_token="+urlEncode(HIPCHAT_AUTH_TOKEN)+"&format=json&message_format=html&room_id="+urlEncode(HIPCHAT_ROOM_NAME)+"&from=Rundeck&message="+urlEncode(HIPCHAT_MESSAGE)+"&color="+urlEncode(HIPCHAT_COLOR)+"¬ify=1"); 155 | } 156 | 157 | @Test(expectedExceptions = HipChatNotificationPluginException.class) 158 | public void testHipChatExceptionThrownWhenHipChatAPIReturnsError() { 159 | final HttpResponse httpResponse = createHipChatHttpResponse(false); 160 | 161 | when(requestExecutor.execute(anyString())).thenReturn(httpResponse); 162 | 163 | roomNotifier.sendRoomNotification( 164 | HIPCHAT_BASE_URL, 165 | HIPCHAT_ROOM_NAME, 166 | HIPCHAT_MESSAGE, 167 | HIPCHAT_COLOR, 168 | HIPCHAT_AUTH_TOKEN, 169 | true 170 | ); 171 | } 172 | 173 | private String captureHipChatUrl() { 174 | final ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); 175 | verify(requestExecutor).execute(argument.capture()); 176 | 177 | return argument.getValue(); 178 | } 179 | 180 | private HttpResponse createHipChatHttpResponse(final boolean didSend) { 181 | final String status = didSend ? "sent" : "error"; 182 | 183 | final HttpResponse httpResponse = mock(HttpResponse.class); 184 | when(httpResponse.getContentType()).thenReturn(HttpResponse.CONTENT_TYPE__JSON); 185 | when(httpResponse.getResponseBody()).thenReturn("{ \"status\": \""+status+"\" }"); 186 | 187 | return httpResponse; 188 | } 189 | 190 | } 191 | -------------------------------------------------------------------------------- /src/test/java/com/hbakkum/rundeck/plugins/hipchat/roomnotifier/HipChatApiVersion2RoomNotifierUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hbakkum.rundeck.plugins.hipchat.roomnotifier; 2 | 3 | import com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginException; 4 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpRequestExecutor; 5 | import com.hbakkum.rundeck.plugins.hipchat.http.HttpResponse; 6 | import org.mockito.ArgumentCaptor; 7 | import org.testng.annotations.BeforeMethod; 8 | import org.testng.annotations.Test; 9 | 10 | import java.util.List; 11 | 12 | import static com.hbakkum.rundeck.plugins.hipchat.HipChatNotificationPluginUtils.urlEncode; 13 | import static org.mockito.Matchers.anyString; 14 | import static org.mockito.Mockito.*; 15 | import static org.testng.Assert.assertEquals; 16 | import static org.testng.Assert.assertTrue; 17 | 18 | /** 19 | * @author Hayden Bakkum 20 | */ 21 | public class HipChatApiVersion2RoomNotifierUnitTest { 22 | 23 | private static final String HIPCHAT_BASE_URL = ""; 24 | private static final String HIPCHAT_ROOM_NAME = "Test Room"; 25 | private static final String HIPCHAT_MESSAGE = "Hello World"; 26 | private static final String HIPCHAT_COLOR = "red"; 27 | private static final String HIPCHAT_AUTH_TOKEN = "abcdef"; 28 | 29 | private HttpRequestExecutor requestExecutor; 30 | 31 | private HipChatApiVersion2RoomNotifier roomNotifier; 32 | 33 | @BeforeMethod 34 | public void setUp() { 35 | requestExecutor = mock(HttpRequestExecutor.class); 36 | 37 | roomNotifier = new HipChatApiVersion2RoomNotifier(requestExecutor); 38 | } 39 | 40 | @Test 41 | public void testSendRoomNotificationUrlHasExpectedBaseUrl() { 42 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 43 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 44 | 45 | roomNotifier.sendRoomNotification( 46 | HIPCHAT_BASE_URL, 47 | HIPCHAT_ROOM_NAME, 48 | HIPCHAT_MESSAGE, 49 | HIPCHAT_COLOR, 50 | HIPCHAT_AUTH_TOKEN, 51 | true 52 | ); 53 | 54 | assertTrue(captureHipChatUrl().startsWith(HIPCHAT_BASE_URL)); 55 | } 56 | 57 | @Test 58 | public void testSendRoomNotificationUrlHasExpectedVersion() { 59 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 60 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 61 | 62 | roomNotifier.sendRoomNotification( 63 | HIPCHAT_BASE_URL, 64 | HIPCHAT_ROOM_NAME, 65 | HIPCHAT_MESSAGE, 66 | HIPCHAT_COLOR, 67 | HIPCHAT_AUTH_TOKEN, 68 | true 69 | ); 70 | 71 | assertTrue(captureHipChatUrl().contains("v2")); 72 | } 73 | 74 | @Test 75 | public void testSendRoomNotificationUrlHasExpectedRoomName() { 76 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 77 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 78 | 79 | roomNotifier.sendRoomNotification( 80 | HIPCHAT_BASE_URL, 81 | HIPCHAT_ROOM_NAME, 82 | HIPCHAT_MESSAGE, 83 | HIPCHAT_COLOR, 84 | HIPCHAT_AUTH_TOKEN, 85 | true 86 | ); 87 | 88 | assertTrue(captureHipChatUrl().contains(urlEncode(HIPCHAT_ROOM_NAME))); 89 | } 90 | 91 | @Test 92 | public void testSendRoomNotificationUrlHasExpectedMessage() { 93 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 94 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 95 | 96 | roomNotifier.sendRoomNotification( 97 | HIPCHAT_BASE_URL, 98 | HIPCHAT_ROOM_NAME, 99 | HIPCHAT_MESSAGE, 100 | HIPCHAT_COLOR, 101 | HIPCHAT_AUTH_TOKEN, 102 | true 103 | ); 104 | 105 | assertTrue(captureHipChatRequestBody().contains("\"message\":\""+HIPCHAT_MESSAGE+"\"")); 106 | } 107 | 108 | @Test 109 | public void testSendRoomNotificationUrlHasExpectedColor() { 110 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 111 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 112 | 113 | roomNotifier.sendRoomNotification( 114 | HIPCHAT_BASE_URL, 115 | HIPCHAT_ROOM_NAME, 116 | HIPCHAT_MESSAGE, 117 | HIPCHAT_COLOR, 118 | HIPCHAT_AUTH_TOKEN, 119 | true 120 | ); 121 | 122 | assertTrue(captureHipChatRequestBody().contains("\"color\":\""+HIPCHAT_COLOR+"\"")); 123 | } 124 | 125 | @Test 126 | public void testSendRoomNotificationUrlHasExpectedAuthToken() { 127 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 128 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 129 | 130 | roomNotifier.sendRoomNotification( 131 | HIPCHAT_BASE_URL, 132 | HIPCHAT_ROOM_NAME, 133 | HIPCHAT_MESSAGE, 134 | HIPCHAT_COLOR, 135 | HIPCHAT_AUTH_TOKEN, 136 | true 137 | ); 138 | 139 | assertTrue(captureHipChatUrl().contains("auth_token="+urlEncode(HIPCHAT_AUTH_TOKEN))); 140 | } 141 | 142 | @Test 143 | public void testSendRoomNotificationUrlHasExpectedFullUrl() { 144 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 145 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 146 | 147 | roomNotifier.sendRoomNotification( 148 | HIPCHAT_BASE_URL, 149 | HIPCHAT_ROOM_NAME, 150 | HIPCHAT_MESSAGE, 151 | HIPCHAT_COLOR, 152 | HIPCHAT_AUTH_TOKEN, 153 | true 154 | ); 155 | 156 | assertEquals(captureHipChatUrl(), HIPCHAT_BASE_URL+"/v2/room/"+urlEncode(HIPCHAT_ROOM_NAME)+"/notification?auth_token="+urlEncode(HIPCHAT_AUTH_TOKEN)); 157 | } 158 | 159 | @Test 160 | public void testSendRoomNotificationUrlHasExpectedFullRequestBody() { 161 | final HttpResponse httpResponse = createHipChatHttpResponse(true); 162 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 163 | 164 | roomNotifier.sendRoomNotification( 165 | HIPCHAT_BASE_URL, 166 | HIPCHAT_ROOM_NAME, 167 | HIPCHAT_MESSAGE, 168 | HIPCHAT_COLOR, 169 | HIPCHAT_AUTH_TOKEN, 170 | true 171 | ); 172 | 173 | assertEquals(captureHipChatRequestBody(), "{\"message\":\""+HIPCHAT_MESSAGE+"\",\"color\":\""+HIPCHAT_COLOR+"\",\"message_format\":\"html\",\"notify\":true}"); 174 | } 175 | 176 | @Test(expectedExceptions = HipChatNotificationPluginException.class) 177 | public void testHipChatExceptionThrownWhenHipChatAPIReturnsError() { 178 | final HttpResponse httpResponse = createHipChatHttpResponse(false); 179 | when(requestExecutor.execute(anyString(), anyString())).thenReturn(httpResponse); 180 | 181 | roomNotifier.sendRoomNotification( 182 | HIPCHAT_BASE_URL, 183 | HIPCHAT_ROOM_NAME, 184 | HIPCHAT_MESSAGE, 185 | HIPCHAT_COLOR, 186 | HIPCHAT_AUTH_TOKEN, 187 | true 188 | ); 189 | } 190 | 191 | private String captureHipChatUrl() { 192 | return captureRequestExecutorArgs().get(0); 193 | } 194 | 195 | private String captureHipChatRequestBody() { 196 | return captureRequestExecutorArgs().get(1); 197 | } 198 | 199 | private List captureRequestExecutorArgs() { 200 | final ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); 201 | verify(requestExecutor).execute(argument.capture(), argument.capture()); 202 | 203 | return argument.getAllValues(); 204 | } 205 | 206 | private HttpResponse createHipChatHttpResponse(final boolean didSend) { 207 | final int responseCode = didSend ? HttpResponse.STATUS__NO_CONTENT : 400; 208 | 209 | final HttpResponse httpResponse = mock(HttpResponse.class); 210 | when(httpResponse.getResponseCode()).thenReturn(responseCode); 211 | 212 | return httpResponse; 213 | } 214 | 215 | } 216 | -------------------------------------------------------------------------------- /src/test/resources/templates/hipchat-message-override-test.ftl: -------------------------------------------------------------------------------- 1 | ${execution.job.name} started --------------------------------------------------------------------------------