Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

build script to create dashboard

baptistemuller June 12, 2019

Hi evryone,

I created a script in a RestEndpoints joined to a SciptFragment for create a button which will create a dashboard in your project when you click on it.

But since an update my script don't work and i don't know why because I use the current ScriptRunner doc.

The errors are at the line when I create the dashboard ( at 118  & 143 & 159 & 179 ).

 

The errors message:

Cannot find matching method com.atlassian.jira.portal.PortletConfigurationManager#addDashBoardItem( int, int, com.atlassian.gadgets.dashboard.Color, java.util;LinkedHashmap , com;atlassian;fugue.Option)

 

My Script:

import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.portal.PortalPage
import com.atlassian.jira.project.Project
import com.atlassian.jira.portal.PortletConfigurationManager
import com.atlassian.jira.bc.portal.PortalPageService
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.sharing.SharedEntity
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import org.apache.log4j.Level
import com.atlassian.jira.jql.parser.JqlQueryParser
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import com.atlassian.gadgets.dashboard.Layout
import com.atlassian.gadgets.dashboard.Color
import com.atlassian.fugue.Option
import com.atlassian.query.Query
import com.atlassian.jira.jql.parser.DefaultJqlQueryParser
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.type.ShareType
import javax.ws.rs.core.Response
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.MediaType
import com.atlassian.jira.issue.search.*

@BaseScript CustomEndpointDelegate delegate

EP_MTN(httpMethod: "GET") { MultivaluedMap queryParams, String body ->

// initialisation de la boite de dialogue
def dialog =
"""
<section role="dialog" id="sr-dialog" class="aui-layer aui-dialog2 aui-dialog2-medium" aria-hidden="true"
data-aui-remove-on-hide="true">
<header class="aui-dialog2-header">
<h2 class="aui-dialog2-header-amain">Passage de votre projet en projet de type Maintenance</h2>
<a class="aui-dialog2-header-close">
<span class="aui-icon aui-icon-small aui-iconfont-close-dialog">Close</span>
</a>
</header>
<div class="aui-dialog2-content">
<p>
Vous venez de rajouter un dashboard à votre projet,
<strong>
ne pas ré-appuyer sur le bouton si vous ne voulez pas créer plusieurs dashboards dans votre projet.</strong><br />
Si vous avez cliqué par erreur sur ce bouton et que vous voulez <strong>effacer </strong>le dashboard créé,<br />
il vous suffit d'aller dans l'onglet <strong>Dashboards>Manage Dashboards>My</strong>,
et de le supprimer à l'aide du bouton d'option correspondant à votre Dashboard.
</p>
</div>
<footer class="aui-dialog2-footer">
<div class="aui-dialog2-footer-actions">
<button id="dialog-close-button" class="aui-button aui-button-link">Fermer</button>
</div>
<div class="aui-dialog2-footer-hint">Contacter <strong>Baptiste Muller</strong> en cas de <strong>problème</strong>
ou <strong>question.</strong></div>
</footer>
</section>
"""
// get the key of the current project
String pKey= queryParams.getFirst("projectKey")
log.setLevel(Level.DEBUG)
log.debug "Project Id is ${pKey}"

// get the current project with his key
Project prj = ComponentAccessor.getProjectManager().getProjectObjByKey(pKey)

// initialisation of variables for creating the dashboard
def portletConfigurationManager = ComponentAccessor.getComponent(PortletConfigurationManager.class)
def portalPageService = ComponentAccessor.getComponent(PortalPageService.class)
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
JiraServiceContext sourceUserServiceCtx = new JiraServiceContextImpl(user)

// get the name of the current project
def pName = prj.getName()
SearchRequestManager srm = ComponentAccessor.getComponentOfType(SearchRequestManager.class)
String filter3Id = ""
List filters = srm.findByNameIgnoreCase(pName + " - Project filter")

// get the filter Id for the Two Dimensional Filter Statistics.1
if(filters != null && filters.size()>0 ) {
filter3Id = filters.get(0).getId()
}

// configuration of the dashboard(name & description)
def dashboardTemplate = new PortalPage.Builder()
.name(prj.getKey()+" - Dashboard")
.owner(user)
.layout(Layout.AA)
.permissions(SharedEntity.SharePermissions.GLOBAL)
.build()
def targetDash = portalPageService.createPortalPage(sourceUserServiceCtx, dashboardTemplate)

// initialisation of query for the filters
Query query1=
new DefaultJqlQueryParser().parseQuery("project =\""+prj.getName()+
"\" AND filter = \"COMMON - Current Year Maintenance Filter\" AND filter = \"COMMON - Estimated with no logs\"")
Query query2=
new DefaultJqlQueryParser().parseQuery("project =\""+prj.getName()+
"\" AND (filter = \"CPT - PA-Maintenance Filter\" OR issueFunction in subtasksOf(\"filter = 'CPT - PA-Maintenance Filter'\"))")

// initialisation of two filters for the dashboard
SearchRequest filter1=buildFilterWithName(prj, "Maintenance Montrol", query1)
SearchRequest filter2=buildFilterWithName(prj, "PA-Maintenance board filter", query2)

// get the filters Id
String filter1Id = filter1.getId()
String filter2Id = filter2.getId()

// création du dashboard en prenant comme modèle le dashboard de reference(CPT-Dashboard)

// first gadget
portletConfigurationManager.addDashBoardItem(targetDash.id, 0, 0,
Option.some(URI.create('rest/gadgets/1.0/g/jira-timesheet-plugin:timechart-gadget/timechart-gadget.xml')),
Color.color1,
[
"projectOrFilter": "",
"isConfigured": "true",
"gadgetTitle": "Maintenance (X Jrs (X*8 hrs) par FREQUENCE)",
"refresh": "false",
"sumSubTasks": "false",
"collapseFieldGroups": "true",
"filterid": "10707",
"groupByField": "issuetype",
"commentFirstWord": "",
"projectRole": "",
"num_weeks": "3",
"reporting_day": "",
"excludeGroup": "",
"user": "",
"projectid": prj.getId().toString(),
"group": "4sh-users"
],
Option.none()
)

// Filter Results
portletConfigurationManager.addDashBoardItem(targetDash.id, 0, 1,
Option.some(URI.create(
'rest/gadgets/1.0/g/com.atlassian.jira.gadgets:filter-results-gadget/gadgets/filter-results-gadget.xml')),
Color.color1,
[
"filterId": filter1Id,
"isConfigured": "true",
"columnNames": "issuetype|issuekey|summary|status|timespent|customfield_10208",
"isPopup": "false",
"num": "10",
"refresh": "false"
],
Option.none()
)

// Two Dimensional Filter Statistics.1
portletConfigurationManager.addDashBoardItem(targetDash.id, 1, 0,
Option.some(URI.create(
'rest/gadgets/1.0/g/com.atlassian.jira.gadgets:two-dimensional-stats-gadget/gadgets/two-dimensional-stats-gadget.xml')),
Color.color1,
[
"filterId": filter3Id,
"isConfigured": "true",
"sortDirection": "asc",
"isPopup": "false",
"ystattype": "statuses",
"more": "false",
"refresh": "false",
"sortBy": "natural",
"xstattype": "issuetype",
"numberToShow": "5"
],
Option.none()
)

// Two Dimensional Filter Statistics.1
portletConfigurationManager.addDashBoardItem(targetDash.id, 1, 1,
Option.some(URI.create(
'rest/gadgets/1.0/g/com.atlassian.jira.gadgets:two-dimensional-stats-gadget/gadgets/two-dimensional-stats-gadget.xml')),
Color.color1,
[
"filterId": filter2Id,
"isConfigured": "true",
"sortDirection": "asc",
"isPopup": "false",
"ystattype": "statuses",
"more": "false",
"refresh": "false",
"sortBy": "natural",
"xstattype": "issuetype",
"numberToShow": "5"
],
Option.none()
)
// création of the dialog box
Response.ok().type(MediaType.TEXT_HTML).entity(dialog.toString()).build()}

// method to create a filter
SearchRequest buildFilterWithName(Project prj1, String name, Query query) {
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
JiraServiceContextImpl ctx = new JiraServiceContextImpl(user)
SearchRequestService searchService = ComponentAccessor.getComponent(SearchRequestService)

SearchRequest filter = new SearchRequest()
filter.setQuery(query)
filter.setName(prj1.getName()+" - "+name)
filter.setOwner(user)
log.debug(filter)

// permission to share
def sharePerm = new SharePermissionImpl(null, ShareType.Name.PROJECT, prj1.getId().toString(), null)
filter.setPermissions(new SharedEntity.SharePermissions([sharePerm] as Set))

SearchRequest newfilter=searchService.createFilter(ctx,filter)
log.debug(ctx.getErrorCollection())
log.debug(filter)

return newfilter;
}

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events