Python script for duplicating Jira Server dashboards via REST API v2

Hey everyone, I’m trying to automate the process of creating a new dashboard with gadgets in Jira Server using Python. I’m using a Jira Python library, but I’m running into some issues.

My code connects to Jira and can view existing dashboards just fine. However, when I try to copy a dashboard, it’s not working as expected. The copy_dashboard function returns None, and if I try to use the REST API directly, I get a 405 error.

Here’s a simplified version of what I’m trying to do:

import jira_connection

jira = jira_connection.connect()

source_dashboard = '12345'
new_dashboard_info = {
    'name': 'Copied Dashboard',
    'description': 'Test automation'
}

result = jira.duplicate_dashboard(source_dashboard, new_dashboard_info)

if result:
    print(f'New dashboard created: {result.id}')
else:
    print('Dashboard duplication failed')

I’m starting to wonder if copying dashboards is even supported in Jira Server. Has anyone successfully done this? Any tips or confirmation would be really helpful. Thanks!

In my experience, duplicating dashboards in Jira Server using the REST API can be challenging because there is no dedicated endpoint for this operation. I found that the solution involves retrieving the source dashboard details, creating a new dashboard with the desired name and description, and then manually copying over each gadget by extracting its data and reassigning it to the new dashboard. A rough example of the approach is as follows:

source = jira.dashboard(source_dashboard)
new_dash = jira.create_dashboard(name=new_dashboard_info['name'], description=new_dashboard_info['description'])

for gadget in source.gadgets():
    gadget_data = gadget.raw
    gadget_data['dashboardId'] = new_dash.id
    jira.add_gadget(new_dash.id, gadget_data)

This method requires multiple API calls and careful handling, especially if there are many gadgets or if some gadgets need special treatment. I hope this insight provides a clearer path forward.

I’ve faced similar challenges when working with Jira Server’s REST API for dashboard operations. Unfortunately, there’s no straightforward method to duplicate dashboards using the API. The approach I’ve found most effective is to break down the process into smaller steps.

First, create a new dashboard using the /rest/api/2/dashboard POST endpoint. Then, fetch the gadgets from the source dashboard using GET /rest/api/2/dashboard/{dashboardId}/gadget. For each gadget, you’ll need to create a new one on the target dashboard using POST /rest/api/2/dashboard/{dashboardId}/gadget.

This process requires multiple API calls and careful error handling. Be prepared to deal with potential issues like gadget compatibility or layout differences between dashboards. It’s not elegant, but it gets the job done when automation is necessary.

yo Alex, i feel ur pain. jira server’s api can be a real pain sometimes. i’ve had luck using the dashboard resource endpoints directly. try somethin like this:

source = jira.get('/rest/api/2/dashboard/' + source_id)
new_dash = jira.post('/rest/api/2/dashboard', json=new_dashboard_info)

for gadget in source['gadgets']:
    jira.post(f'/rest/api/2/dashboard/{new_dash["id"]}/gadget', json=gadget)

it’s not perfect but might get u started. good luck!