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

Web fragment for rendering a pdf with the result of a jql

Amanda Pinto
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 22, 2020

The following is the code I am trying to implement in the Rest Endpoint for creating a Web fragment for rendering a pdf with the result of a jql

This code does not work and it is complaining about the Permissions library and some of the static methods (which usually only complains but allows you to create the script, not in this case)

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript

import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.Permissions
import com.atlassian.jira.user.UserProjectHistoryManager
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.util.velocity.VelocityRequestContextFactory
import com.atlassian.jira.web.bean.*
import com.atlassian.plugin.web.api.model.WebFragmentBuilder

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.springframework.util.FileCopyUtils

@BaseScript CustomEndpointDelegate delegate

//identify the last project accessed by the User 
def lastProjectAccessed = ComponentAccessor.getComponent(UserProjectHistoryManager.class)
def projectAcessed = lastProjectAccessed.getCurrentProject(Permissions.BROWSE, ComponentAccessor.jiraAuthenticationContext.loggedInUser)
def projectName = projectAcessed.name

// PDF configuration
def userName = "admin" // this user runs the search and generates the PDF
def jql = "XXXX"

def templateName = "issue-navigator-fo.vm"
def title = "XXXX"

// run the search
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def query = jqlQueryParser.parseQuery(jql)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.userUtil.getUser(userName)
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

// render the PDF
def issues = results.issues
if (issues) {
def pdfApiClass = this.class.forName('com.midori.jira.plugin.pdfview.api.PdfApi', true, ComponentAccessor.pluginAccessor.classLoader)
def pdfApi = ComponentAccessor.getOSGiComponentInstanceOfType(pdfApiClass)
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(user)
def pdfResult = pdfApi.getPdf(templateName, title.toString(), issues, [:])

// write the pdf to a file with a timestamped filename like "example-20170526-1304.pdf")
FileCopyUtils.copy(pdfResult.bytes, new File("c:\\tmp\\example-${new Date().format('yyyyMMdd-HHmm')}.pdf"))
}

def searchService = ComponentAccessor.getComponent(SearchService)
def jiraAuthenticationContext = ComponentAccessor.getComponent(JiraAuthenticationContext)

def velocityRequestContextFactory = ComponentAccessor.getComponent(VelocityRequestContextFactory)
def requestContext = velocityRequestContextFactory.getJiraVelocityRequestContext()
def baseUrl = requestContext.getBaseUrl()

results.issues.collect { issue ->
def issueLabel = "${issue.key} ${issue.summary}"

String iconUrl = issue.issueTypeObject.iconUrl
if (!iconUrl.startsWith("http://") && !iconUrl.startsWith("https://")) {
iconUrl = baseUrl + iconUrl;
}
new WebFragmentBuilder(50).
id("sample-web-item-${issue.key.toLowerCase()}").
label(issueLabel).
title(issueLabel).
styleClass("").
addParam("class", "issue-link").
addParam("data-issue-key", issue.getKey()).
addParam("iconUrl", iconUrl).
webItem("").
url(baseUrl + "/browse/" + issue.getKey()).
build()
}

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events