Applinks in Javascript - Get project keys from JIRA

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2013

Hi,

I'm in Confluence. From Javascript, I'd like to get the list of project keys from JIRA. I have an Applink between the two instances, but I don't have administrative permissions on JIRA, so I can't use OAuth. However, the JIRA projects are public.

From Javascript, is it possible to call the REST resources of JIRA?

I'm getting the CORS issue "Origin http://...mymachine... is not allowed by Access-Control-Allow-Origin." Is there any way to get the list of JIRA projects in Confluence?

Thanks

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2013

Found it!

  • When you configure Applinks, it doesn't allow you to perform REST request to the other application,
  • BUT the REST resource of your own application gives you valuable information:
    • The url of the Applink'ed JIRA
    • All the linkable entities in JIRA, a.k.a... all the project keys!

Example:

# Query all applinks:
http://localhost:1990/confluence/rest/applinks/1.0/applicationlink/type/jira

# Answer:

<applicationLinks>
<link rel="self" href="http://localhost:1990/confluence/rest/applinks/1.0/applicationlink/144880e9-a353-312f-9412-ed028e8166fa"/>
<id>144880e9-a353-312f-9412-ed028e8166fa</id>
<typeId>jira</typeId>
<name>Atlassian JIRA</name>
<displayUrl>https://jira.atlassian.com</displayUrl>
<iconUrl>http://localhost:1990/confluence/.../16jira.png</iconUrl>
<rpcUrl>https://jira.atlassian.com</rpcUrl>
<isPrimary>false</isPrimary>
<isSystem>false</isSystem>
</applicationLinks>

# Then take the applink ID and get all applink'ed entities:
http://.../confluence/rest/applinks/1.0/entities/144880e9-a353-312f-9412-ed028e8166fa

# Result:
<entities>
<entity
    iconUrl="http://localhost:1990/confluence/.../16jira.project.png" 
    typeId="jira.project" 
    name="[CLOSED] Clover Maven2Plugin" 
    key="CLMVN"/>

<entity
    iconUrl="http://localhost:1990/confluence/.../16jira.project.png"
    typeId="jira.project" 
    name="A Test Project" 
    key="TST"/>
...

 

 

Source: I just went to the sources of Applinks... https://bitbucket.org/atlassian/application-links/src/applinks-parent-3.10.6/applinks-plugin/src/main/java/com/atlassian/applinks/core/rest/EntityResource.java?at=applinks-parent-3.10.6

Ulrich Kuhnhardt [Comalatech]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 17, 2013

Interesting find. How do you connect to JIRA from within a Confluence onDemand iframe? AP.request() always prepends the on-demand host url. So in this example - even though you can query the confluence applinks REST API, how do you send queries to JIRA from within the Confluence remote app iframe?

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 17, 2013
Hi Ulrich, I'm not actually connecting to JIRA. I'm connecting to Confluence, which in turn connects to JIRA. On OnDemand, if you order Confluence+JIRA, the two apps already have the applink pre-configured. So the Confluence server gets information from JIRA. It seems you're using Atlassian Connect Plugins. I don't have an instance to test this case, but you should be able to use the same solution: - Within a Confluence iframe, make a GET to AP.request(... "/rest/applinks/1.0/applicationlink/type/jira"...) and you will get the list of JIRA applinks - Use the returned id to GET AP.request(..."/applinks/1.0/entities/144880e9-a353-312f-9412-ed028e8166fa"...) and you will get the list of JIRA projects. Hope it clarifies.
1 vote
Jason Nguyen May 22, 2014

Can someone give me advice on passing the cookie or authentication when doing the REST call Adrien mentioned to get the list of JIRA projects?

For some reason one call to a particular JIRA instance works and gives me a list of projects (not sure if it's all the projects or just the ones available to anonymous access), but another call to a different JIRA instance gives me a 401 error. Maybe it's a configuration issue but I'm not too familiar with application linking.

I went here for advice: https://developer.atlassian.com/display/DOCS/REST+and+os_authType

But adding os_authType to the REST URL doesn't seem to help.

function getJiraProjects(applicationId){
        var restUrl = AJS.contextPath() + "/rest/applinks/1.0/entities/"+applicationId+"?os_authType=any";
        $('#select-example')
            .find('option')
            .remove()
            .end();

        console.log(restUrl);
        $.ajax({
            type: 'GET',
            url: restUrl,
            data: {
                key: "value"
            },
            dataType: "xml",
            success: function (xml){
                console.log('Found jira projects successfully for '+applicationId);

                jiraProjectKeys = [];
                $(xml).find("entity").each(function()
                {
                    $('#select-example')
                        .append($('&lt;option&gt;', { value : name })
                            .text($(this).attr("key")));
                    //console.log('appending '+$(this).attr("key")+' to #select-example');
                    jiraProjectKeys.push($(this).attr("key"));
                });
                console.log('jiraKeys =  '+ jiraProjectKeys);


            },
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            error: function(xhr, status, error) {
                console.log('error getting jira projects for application id '+ applicationId + ' '+xhr + ' ' + status);
            }

        });

    }

Jason Nguyen May 23, 2014

Ok I found out why I was getting the 401 error. Me as a user had to set up the authentication within that particular app link b/w JIRA and Confluence which hadn't been done yet. So now everything works for me.

vandyke December 16, 2015

hello @Jason Nguyen and thank you for your code. some parts of the solution are not working the way it should and perhaps you can help me with that. i have linked the confluence and jira test servers successfully with each other. 1) if i am logged as admin i get without any problems the id of jira BUT if i am normal user i get 401 ERROR "this resource requires admin rights" 2) if i have the ID and i am requesting for the JIRA Projects i get 401 ERROR "Authentication Required" BUT if i dont use any ID i get all the projects from confluence but not from jira (without authentication) to solve the second part i have tried some basic authentication methods in ajax but it didnt work. is it possible that the problem lies in my confluence developer version which is connected to a copy of the original jira server? thanks in advance and merry xmas and a happy new year

1 vote
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2013

check this it may help you

https://developer.atlassian.com/static/rest/jira/6.0.1.html#id141644

something you need to try like this

$.ajax({
    url : "http://localhost:8080/jira/rest/api/2/project",
    type : "GET",
    dataType : "json",
    contentType : "application/json",
    success : function(response)
    {
        console.log(response);
    }
});

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2013

That's right, but that's when I get the CORS issue "...is not allowed by Access-Control-Allow-Origin". We can't perform a REST request when the JIRA server is on another domain, unless the JIRA server allows it. It is probably possible to make JIRA allow it, but I don't know how yet.

0 votes
Jason Nguyen May 11, 2014

I've also tried Radu's suggestion of installing apache http in front of JIRA and have yet to figure out how to get aroudn the CORS issue when calling the JIRA REST api from Confluence.

0 votes
Radu Dumitriu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2013


I think you're facing a config issue, rather than a programming one ...
The only way to do it (that I know of) is to install apache httpd in front of JIRA and:

Header add Access-Control-Allow-Origin "http://my.domain1.com" 
Header add Access-Control-Allow-Origin "http://my.domain2.com"


HTH
Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2013

That would work, but I'm using OnDemand.

I'm like "There must be a (programming) way... Somebody must have faced the usecase before!"

Jason Nguyen May 8, 2014

Hey Adrien, did you ever find a workaround?

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2014

No, I just used the answer above where the Applink returns the list of JIRA projects. I can't call other REST apis of JIRA.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events