1. Stefan Saasen
  2. Hipchat for Confluence

Commits

Rich Manalang  committed 6a6d9e4

yay... it works

  • Participants
  • Parent commits 88198da
  • Branches master

Comments (0)

Files changed (8)

File src/main/java/com/atlassian/labs/hipchat/actions/SaveSpaceConfigurationAction.java

View file
 package com.atlassian.labs.hipchat.actions;
 
 import com.atlassian.confluence.core.ConfluenceActionSupport;
-import com.atlassian.confluence.security.Permission;
 import com.atlassian.confluence.security.PermissionManager;
 import com.atlassian.labs.hipchat.components.ConfigurationManager;
 import com.opensymphony.xwork.Action;
     private final PermissionManager permissionManager;
 
     private String hipChatAuthToken;
+    private String key;
+    private String roomId;
 
     public SaveSpaceConfigurationAction(ConfigurationManager configurationManager, PermissionManager permissionManager)
     {
     }
 
     @Override
-    public boolean isPermitted()
+    public String execute() throws Exception
     {
-        return permissionManager.hasPermission(getRemoteUser(), Permission.ADMINISTER, PermissionManager.TARGET_SYSTEM);
+        // TODO: Validate inputs.
+        configurationManager.setNotifyRooms(key, roomId);
+        return Action.SUCCESS;
     }
 
-    public void setHipChatAuthToken(String value)
-    {
-        this.hipChatAuthToken = value;
+    public String getKey() {
+        return key;
     }
 
-    @Override
-    public String execute() throws Exception
-    {
-        // TODO: Validate inputs.
-        configurationManager.updateConfiguration(hipChatAuthToken);
-        return Action.SUCCESS;
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getRoomId() {
+        return roomId;
+    }
+
+    public void setRoomId(String roomId) {
+        this.roomId = roomId;
     }
 }

File src/main/java/com/atlassian/labs/hipchat/actions/ViewSpaceConfigurationAction.java

View file
 {
     private final ConfigurationManager configurationManager;
 
+    private String roomId;
+
     public ViewSpaceConfigurationAction(ConfigurationManager configurationManager)
     {
         this.configurationManager = configurationManager;
     }
 
-    public String getHipChatAuthToken()
-    {
-        return configurationManager.getHipChatAuthToken();
-    }
-
     @Override
     public String execute() throws Exception
     {
+        setRoomId(configurationManager.getHipChatRooms(key));
         return Action.SUCCESS;
     }
 
+    public void setRoomId(String roomId) {
+        this.roomId = roomId;
+    }
+
+    public String getRoomId() {
+        return roomId;
+    }
 }

File src/main/java/com/atlassian/labs/hipchat/components/AnnotatedListener.java

View file
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.DisposableBean;
 
+import java.util.StringTokenizer;
+
 public class AnnotatedListener implements DisposableBean {
     private static final Logger log = LoggerFactory.getLogger(AnnotatedListener.class);
 
     protected EventPublisher eventPublisher;
     private ApplicationProperties applicationProperties;
     private HipChatProxyClient hipChatProxyClient;
+    private ConfigurationManager configurationManager;
 
 
     public AnnotatedListener(EventPublisher eventPublisher, ApplicationProperties applicationProperties,
                              ConfigurationManager configurationManager) {
         this.eventPublisher = eventPublisher;
         this.applicationProperties = applicationProperties;
+        this.configurationManager = configurationManager;
         eventPublisher.register(this);
         this.hipChatProxyClient = new HipChatProxyClient(configurationManager);
     }
         String url = applicationProperties.getBaseUrl() + "/x/" + new TinyUrl(event.getBlogPost()).getIdentifier();
         String creator = event.getBlogPost().getCreatorName();
         String postLink = "<a href=\"" + url + "\"><b>" + title + "</b></a>";
-        hipChatProxyClient.notifyRoom(postLink, creator);
+        String roomsToNotify = configurationManager.getHipChatRooms(event.getBlogPost().getSpaceKey());
+        StringTokenizer rooms = new StringTokenizer(roomsToNotify, ",");
+        while (rooms.hasMoreTokens()) {
+            hipChatProxyClient.notifyRoom(rooms.nextToken(), postLink, creator);
+        }
     }
 
     // Unregister the listener if the plugin is uninstalled or disabled.

File src/main/java/com/atlassian/labs/hipchat/components/ConfigurationManager.java

View file
     public String getHipChatAuthToken() {
         return getValue(HIPCHAT_AUTH_TOKEN_KEY);
     }
+    
+    public String getHipChatRooms(String spaceKey){
+        return getValue(spaceKey);
+    }
 
     private String getValue(String storageKey) {
         PluginSettings settings = pluginSettingsFactory.createSettingsForKey(PLUGIN_STORAGE_KEY);
         settings.put(HIPCHAT_AUTH_TOKEN_KEY, authToken);
     }
 
+    public void setNotifyRooms(String spaceKey, String rooms) {
+        PluginSettings settings = pluginSettingsFactory.createSettingsForKey(PLUGIN_STORAGE_KEY);
+        settings.put(spaceKey, rooms);
+    }
+
 
 }

File src/main/java/com/atlassian/labs/hipchat/utils/HipChatProxyClient.java

View file
     private static String API_BASE_URI = "https://api.hipchat.com";
 
     //TODO remove hardcoded room_id and add config for linking to space
-    private static String ROOM_ID = "47513";
     private static String MSG = " - a new blog post by ";
     private HttpClient httpClient;
     private String authToken;
         return "";
     }
 
-    public void notifyRoom(String postLink, String creator)
+    public void notifyRoom(String room, String postLink, String creator)
     {
         PostMethod post = new PostMethod(API_BASE_URI + "/v1/rooms/message?auth_token=" + authToken);
         post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
-        post.addParameter("room_id", ROOM_ID);
+        post.addParameter("room_id", room);
         post.addParameter("from", "Confluence");
         post.addParameter("message", postLink + MSG + "<b>" + creator + "</b>");
         post.addParameter("color", "purple");

File src/main/resources/atlassian-plugin.xml

View file
                 <result name="success" type="velocity">/templates/admin/space-config.vm</result>
             </action>
             <action name="doconfigure-hipchat" class="com.atlassian.labs.hipchat.actions.SaveSpaceConfigurationAction">
-                <result name="success" type="redirect">/spaces/hipchat.action?key=$action.key</result>
+                <result name="success" type="redirect">/spaces/hipchat.action?key=${key}</result>
             </action>
 
         </package>

File src/main/resources/js/space.js

View file
             rooms = new Rooms;
             rooms.fetch().done(function(d){
                 var activeRooms = _.sortBy(_.filter(d.rooms,function(room){ return !room.is_archived;}),function(room){
-                    return room.name;
+                    return room.name.toLowerCase();
+                });
+                var checkedRooms = hcRoomIds.split(", ");
+                _.map(activeRooms, function(room){
+                    if(_.indexOf(checkedRooms, String(room.room_id))>-1){
+                        room.checked = 'checked="checked"';
+                    } else {
+                        room.checked = "";
+                    }
+                    return room;
                 });
                 _view.$el.html(_view.template({rooms: activeRooms}));
             })

File src/main/resources/templates/admin/space-config.vm

View file
 <script id="rooms-tmpl" type="text/tmpl">
     <% _.each(rooms, function(room){ %>
     <div class="checkbox">
-        <input class="checkbox" type="checkbox" name="<%= room.room_id %>">
+        <input class="checkbox" type="checkbox" <%= room.checked %>  name="roomId" value="<%= room.room_id %>">
         <label for="<%= room.room_id %>"><%= room.name %></label>
     </div>
     <% }) %>
 </script>
+<script type="text/javascript">
+    var hcRoomIds = "$action.roomId";
+</script>
 <form id="hipchat-form" action="doconfigure-hipchat.action" method="post"
       class="aui edit-space-details">
     <h2>HipChat Configuration</h2>