Forum Discussion

Mike282's avatar
Mike282
Helper III
6 years ago
Solved

Connecting Jira to Power BI using the Web API

Hi all,

 

I've recently been using Jira to manage my workload, utilising user stories, epics and sprints. I've been looking for a method to extract my user story and task data from a specific project in Jira and was looking at using Jira's web API to do the connection. I read another thread where someone has posted a M script code which utilises Jira's web API and I'm having some issues connecting with it (link to thread below).

https://community.powerbi.com/t5/Desktop/Jira-and-Power-BI/td-p/393785/page/2

 

The above code required authenticating, after reading Jira's Authentication document I created another table that encoded the API token from Jira in Base 64.

 

Figure 1. Created a table which held the Base 64 encoded username and API token

 

I then copied the below M script into a new table. Note I added the authorization in the header. Note that in the header I've referenced the encoded Credentials table.

 

 

let 
    BaseUrl = Web.Contents("https://companyname.atlassian.net/rest/api/3/search?jql=project in ('TEST')", [Headers=[Authorization="Basic " & Credentials]]),

    JiraIDPerPage = 1000,
 
    GetJson = (Url) =>
        let 
            RawData = Web.Contents(Url),
            Json    = Json.Document(RawData)
        in  Json,
 
    GetJiraIDCount = () =>
        let Url   = BaseUrl & "&maxResults=0",
            Json  = GetJson(Url),
            Count = Json[#"total"]
        in  Count,
 
    GetPage = (Index) =>
        let Skip  = "&startAt=" & Text.From(Index * JiraIDPerPage),
            Top   = "&maxResults=" & Text.From(JiraIDPerPage),
            Url   = BaseUrl & Skip & Top,
            Json  = GetJson(Url),
            Value = Json[#"issues"]
        in  Value,
 
    JiraIDCount = List.Max({ JiraIDPerPage, GetJiraIDCount() }),
    PageCount   = Number.RoundUp(JiraIDCount / JiraIDPerPage),
    PageIndices = { 0 .. PageCount - 1 },
    Pages       = List.Transform(PageIndices, each GetPage(_)),
    JiraID    = List.Union(Pages),
    Table       = Table.FromList(JiraID, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(Table, "Column1", {"id", "key", "fields"}, {"Column1.id", "Column1.key", "Column1.fields"})
in
    #"Expanded Column1"

 

 

When I loaded the query I'm getting this error:

 

Figure 2. Error that I'm getting

 

Not sure what I'm doing wrong here. I quite like this script as it seems to account for the fact that the API only seems to retrieve a max of 50-100 issues and relies on pagination to retrieve additional records.

 

Now I've then tried using the web data source and adding in the credentials in the header like the below script:

 

 

let
    Source = Json.Document(Web.Contents("https://companyname.atlassian.net/rest/api/3/search?jql=project in ('TEST')", [Headers=[Authorization="Basic " & Credentials]])),
    issues = Source[issues],
    #"Converted to Table" = Table.FromList(issues, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"expand", "id", "self", "key", "fields"}, {"Column1.expand", "Column1.id", "Column1.self", 
in
    #"Expanded Column1"

 

 

This seems to work but the returend value in a nested JSON format seems only allow a Maxresult of 50 records.

 

Figure 3. Returned a max of 50. I've got 26 records so I'm getting close

 

I prefer to use the first code provided in the other thread but not sure how to get it to work. Any help would be greatly appreciated.

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Mike282 ,

    It seems like you are trying to stored web connector result in BASEURL parameters. (obviously, it require to stored your rest API link instead of web.contents result)

     

    let 
        BaseUrl = "https://companyname.atlassian.net/rest/api/3/search?jql=project in ('TEST')",
        JiraIDPerPage = 1000,
     
        GetJson = (Url) =>
            let 
                RawData = Web.Contents(Url,[Headers=[Authorization="Basic " & Credentials]]),
                Json    = Json.Document(RawData)
            in  Json,
     
        GetJiraIDCount = () =>
            let Url   = BaseUrl & "&maxResults=0",
                Json  = GetJson(Url),
                Count = Json[#"total"]
            in  Count,
     
        GetPage = (Index) =>
            let Skip  = "&startAt=" & Text.From(Index * JiraIDPerPage),
                Top   = "&maxResults=" & Text.From(JiraIDPerPage),
                Url   = BaseUrl & Skip & Top,
                Json  = GetJson(Url),
                Value = Json[#"issues"]
            in  Value,
     
        JiraIDCount = List.Max({ JiraIDPerPage, GetJiraIDCount() }),
        PageCount   = Number.RoundUp(JiraIDCount / JiraIDPerPage),
        PageIndices = { 0 .. PageCount - 1 },
        Pages       = List.Transform(PageIndices, each GetPage(_)),
        JiraID    = List.Union(Pages),
        Table       = Table.FromList(JiraID, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(Table, "Column1", {"id", "key", "fields"}, {"Column1.id", "Column1.key", "Column1.fields"})
    in
        #"Expanded Column1"

     

    BTW, you can take a look at following link about getting more than 50 results from Jira rest API:

    PowerBI content pack only pulling 50 rows from API 

    Regards,
    Xiaoxin Sheng

11 Replies

  • Any help on this would be greatly appreciated. I'm unfamiliar with how I'd handle paging with the returned JSON. At the moment I only have 26 issues but I'd definitely go over the 50 issue mark and at that point I need to find a way to get results in the next page.

      • Jeanxyz's avatar
        Jeanxyz
        Power Participant

        I tried the paid connector, but the issue is the connector is based on user license, we are a company of 100 users, but for some reason, the Jira cloud license is for 3000 users. As a result, we need to pay for a connector of 3000 users. That becomes very expensive. Or did I miss something about your pricing policy?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Mike282 ,

    It seems like you are trying to stored web connector result in BASEURL parameters. (obviously, it require to stored your rest API link instead of web.contents result)

     

    let 
        BaseUrl = "https://companyname.atlassian.net/rest/api/3/search?jql=project in ('TEST')",
        JiraIDPerPage = 1000,
     
        GetJson = (Url) =>
            let 
                RawData = Web.Contents(Url,[Headers=[Authorization="Basic " & Credentials]]),
                Json    = Json.Document(RawData)
            in  Json,
     
        GetJiraIDCount = () =>
            let Url   = BaseUrl & "&maxResults=0",
                Json  = GetJson(Url),
                Count = Json[#"total"]
            in  Count,
     
        GetPage = (Index) =>
            let Skip  = "&startAt=" & Text.From(Index * JiraIDPerPage),
                Top   = "&maxResults=" & Text.From(JiraIDPerPage),
                Url   = BaseUrl & Skip & Top,
                Json  = GetJson(Url),
                Value = Json[#"issues"]
            in  Value,
     
        JiraIDCount = List.Max({ JiraIDPerPage, GetJiraIDCount() }),
        PageCount   = Number.RoundUp(JiraIDCount / JiraIDPerPage),
        PageIndices = { 0 .. PageCount - 1 },
        Pages       = List.Transform(PageIndices, each GetPage(_)),
        JiraID    = List.Union(Pages),
        Table       = Table.FromList(JiraID, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(Table, "Column1", {"id", "key", "fields"}, {"Column1.id", "Column1.key", "Column1.fields"})
    in
        #"Expanded Column1"

     

    BTW, you can take a look at following link about getting more than 50 results from Jira rest API:

    PowerBI content pack only pulling 50 rows from API 

    Regards,
    Xiaoxin Sheng

  • Jeanxyz's avatar
    Jeanxyz
    Power Participant

    I want to use the script to import Jira data into Power BI. How can I input my credentials in the script? I assume I need to update the following code? Which password should I use, my windows password or API token?

     

  • marineded's avatar
    marineded
    Regular Visitor

    I used the script that you gave us.
    I created the table for the credentials but I have this error... don't find why !

    Could someone knows how to do it?

     

    Thank you