Snippets

Adaptavist Jira Cloud - Change Filter or Dashboard Owner Script console

Created by Kristian Walker last modified
/*
* This example script console script shows how to change the owner of a filter or dashboard . 
* Note this script should be run as the ScriptRunner add on user
* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/

// Note this script should be run as the ScriptRunner add on user

// Enter the Id of the filter to update here
def filterId = ''; 

// Enter the Id of the dashboard to update here
def dashboardId = '';

// Enter the accountId of the new owner here
def newOwnerAccountId = '';

if (filterId.size() == 0 && newOwnerAccountId.size() == 0 || dashboardId.size() == 0 && newOwnerAccountId.size() == 0) {
        return "You must specify a filter id or dashboard I=id and the accountId for the new user to be able to change a filter or dashboards ownership";
} 

if (filterId.size() > 0) {

    def updateFilterOwner = put("/rest/api/3/filter/${filterId}/owner")
            .header('Content-Type', 'application/json')
            .body([
                    accountId: newOwnerAccountId
            ])
            .asString()

    assert updateFilterOwner.status >= 200 && updateFilterOwner.status < 300

    logger.info("The filter with the Id: ${filterId} was updated to be owned by the user with the accountId of ${newOwnerAccountId}.")
}


if (dashboardId.size() > 0) {

    def updateDashboardOwner = put('/rest/api/3/dashboard/bulk/edit')
            .header('Content-Type', 'application/json')
            .body([
                    action            : 'changeOwner',
                    changeOwnerDetails: [
                            autofixName: false,
                            newOwner   : newOwnerAccountId
                    ],
                    entityIds         : [dashboardId.toInteger()],
                    permissionDetails : [
                            sharePermissions: [
                                    [
                                            user: [
                                                    accountId: newOwnerAccountId,
                                            ],
                                            id  : dashboardId.toInteger(),
                                            type: "user"
                                    ]
                            ]
                    ]
            ])
            .asString()

    assert updateDashboardOwner.status >= 200 && updateDashboardOwner.status < 300

    logger.info("The dashboard with the Id: ${dashboardId} was updated to be owned by the user with the accountId of ${newOwnerAccountId}.")
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.