HipChat Integration Plugin REST call

I need to make this REST call , that is provided from the HipChat integration plugin from my JIRA Plugin, specifically from the JAVA code:

 

rest/hipchat/compatibility/1.0/dedicatedroom

 

In order to do that I wrote a Java class like so :

 

public class HCRoomCreate {
    private static final Logger log = Logger.getLogger(HCRoomCreate.class);
    private static String environment = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL);
    public static void createHipChatRoom(String issueKey) {
        String url = environment + "/rest/hipchat/compatibility/1.0/dedicatedroom";
        log.debug("URL configured for the REST Call is " + url);
        HttpClient httpClient = new HttpClient();
        httpClient.setTimeout(6000000);
        String orig = "<username>:<password>";
        //encoding  byte array into  64
        byte[] encoded = Base64.encodeBase64(orig.getBytes());
        String sid  = new String(encoded);
       /* System.out.println("Original String: " + orig );*/
        System.out.println("Base64 Encoded String : " + new String(encoded));
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type", "application/json");
        post.setRequestHeader("Authorization", "Basic " + sid);
        //post.setRequestHeader("Authorization" ,"Bearer " + hipChatToken );
        //String payload = "{ \"name\": "+"\""+issueKey+"\"}";
        String payload = "{\n" +
                "                \"hipChatConfigured\": true,\n" +
                "                    \"projectKey\": \"<projectkey>\",\n" +
                "                    \"issueKey\": \""+issueKey+"\",\n" +
                "                    \"dedicatedRoom\": {\n" +
                "                        \"name\":  \""+issueKey+"\",\n" +
                "                        \"apiUrl\": \"https://<somevalidname>.hipchat.com\",\n" +
                "                        \"privateRoom\": false\n" +
                "                    },\n" +
                "                \"hipChatUserName\": {},\n" +
                "                \"canCreateRoom\": true,\n" +
                "                    \"canAssignRoom\": true,\n" +
                "                    \"connectionStatus\": \"CONNECTED\",\n" +
                "                    \"userCanAccessPrivateRoom\": false,\n" +
                "                    \"hipChatLoginUrl\": \"https://api.hipchat.com/sign_in\",\n" +
                "                    \"privateRoom\": false,\n" +
                "                    \"personalToken\": true,\n" +
                "                    \"jirauser\": true\n" +
                "            }";
        InputStream stream = new ByteArrayInputStream(payload.getBytes());
        post.setRequestBody(stream);
        //post.addParameter("name", issueKey);
        String responseBody = "";
        try {
            httpClient.executeMethod(post);
            responseBody = post.getResponseBodyAsString();
        } catch (IOException e) {
            log.error("Fatal transport error " + e.getMessage());
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
    }

}

But its giving me this error when this method call is invoked on issue creation:

 {"statusCode":403,"message":"You don't have enough permission or aren't logged in to HipChat to assign a dedicated room to this issue."}

 

The <username>/<password> in the above code is valid and I am able to login to JIRA with that, so technically since the HipChat integration plugin is already in the JIRA server, shouldnt the JIRA login credentials be enough?  Please advise.

 

 

 

1 answer

0 vote

Hi Yagnesh,

in order to be able to create a room inside HipChat a user needs HipChat's permission to do it. For example if you first click 'Create a [dedicated] room'  button in JIRA's interface we ask you to authorize against HipChat (using OAuth) and once we know that you are authorized we can create a room on your behalf.

You are getting a 403 response because the JIRA user you are making the call as has not yet been authorized against HC.

@Anatoli Kazatchkov , thanks for the reply. What I am trying to achieve is that when a user creates an jira ticket of a certain issuetype and certain severity, it should create a hipchat room on the fly with the name as the issue key. And also that room should be available under the "Dedicated Room" panel on the issue screen. Thats where I was leveraging the above mentioned dedicatedroom REST call , by calling it from the Java class I mentioned above. Do you know of a better way to acheive this?

Any updates @Anatoli Kazatchkov ? Please advise - I really need such kind of functionality in my plugin.

Yagnesh, it depends if you are writing a Connect add-on or if you are writing an old style add-on that runs within JIRA (i.e. P2). - For Connect add-ons you will need to prompt users to go through the OAuth Dance. You should do it inside your add-on UI, unfortunately there is no way to do it as a part of issue creation as far as I know. Once a user approved the access he/she would not need to do it again the next time. - If you are writing the old style P2 plugin you can access our API and use this methods to create dedicated room: UserService.getUser(String idOrEmail) RoomService.createRoom(String roomName, String ownerUserId, Option<String> topic, Option<Boolean> guestAccess, Option<String> privacy)

@Anatoli Kazatchkov , isnt there any proper way of authentication from the server side (Java) code itself so that I can make this call from the server side directly, using the code I provided?

Probably some additional parameters I can pass in the headers?

By the way, I am trying to make this call from a plugin written for a JIRA server, and not Cloud.

Yagnesh, the user on who's behalf you are making the call needs to log-in into HC. The user input is required if she hasn't authorised JIRA to act on her behalf yet.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Mar 26, 2018 in Hipchat Data Center

Migration of Hipchat server to Data Center - a retrospective

Background While our Hipchat server environment was reliable and performing well, as a significant and growing part of our business, the need to leverage the benefits of Hipchat Data Center&nbs...

458 views 2 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you