Scriptrunner Fragments (Show a web panel) & Jira 9

Andra Marilena SANDULEASA April 1, 2024

Hey guys,

 

When we were still in Jira 7.x.x we managed to link an internal app to Jira. The main goal was for Jira to go search some tickets information on that internal app (not vice versa) and show them on specific issues (if the ticket key from the internal app was added to a label field in Jira on a specific project). In order to do that, we used Fragments in ScripRunner. This worked just fine until we updated Jira from Jira 7 to Jira 8. I guess some classes changed, but I cannot manage to make it run everything I'm changing. 

 

The script is in comments. 

 

Thx for your help,

 

 

1 answer

0 votes
Andra Marilena SANDULEASA April 1, 2024

this is the condition we used:

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;

//return true
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// test
//return false;
def idItCareCF = customFieldManager.getCustomFieldObject((long) 21070) // valeur string de la valeur Ticket It Care (label)
def idItCareV = idItCareCF.getValue(issue)
//return true;
if ((issue.projectObject.key != 'IAI') && (issue.projectObject.key != 'HAB')) return false
if (!idItCareV) return false
if (idItCareV == null) return false
return true;
Andra Marilena SANDULEASA April 1, 2024

and this is the Provider class/Script:

 

import org.apache.log4j.Logger
import org.apache.log4j.Level
import java.util.Date
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.web.bean.PagerFilter
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.RendererManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.RendererManager
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.util.velocity.VelocityRequestContextFactory
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.project.Project
import java.net.URI;
import java.net.URL;

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("itCare");
log.setLevel(Level.DEBUG);

//def writer = new StringWriter()
def issue = context.issue as Issue

def List<Map> ticketStatus =[]
def result = 200

def issueManager = ComponentAccessor.getIssueManager();

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def jiraAuthenticationContext = ComponentAccessor.getComponent(JiraAuthenticationContext)
def idItCareCF = customFieldManager.getCustomFieldObject((long) 21070) // valeur string de la valeur Ticket It Care (label)
ArrayList<String> idItCareV = idItCareCF.getValue(issue)

def remote = new HTTPBuilder("https://xxxxxxxxxxxxx/itcare/")

// on recupere les status des tickets au quel on va retirer "closed"
def statusJson = remote.request(Method.GET) { req ->

    uri.path = 'api/ticket-statuses/'
    headers.'Authorization' = "Basic bXNhbXXXXXXXXXXXXXXXXXXX"
    headers.Accept = 'application/json'
    response.success = { resp,  json ->
        json ?: [:]
    }
    response.failure = { resp ->
        result = resp.status
    }

}
if (result == 200){
    ticketStatus = statusJson
    ticketStatus.removeAll('CLOSED')

} else return;

// on recherche les tickets
result = 200
def grpJson = remote.request(Method.GET) { req ->
    uri.path = 'api/tickets/'
    uri.query = [ticketNumbers : idItCareV]
    headers.'Authorization' = "Basic bXNhXXXXXXXXXXXXXXXXXXXXXXXX"

    headers.Accept = 'application/json'
    response.success = { resp,  json ->
        json ?: [:]
    }
    response.failure = { resp ->
        result = resp.status
    }
}
def HTML = ""
if (result == 200) {

    HTML += '''
    <div id="itCare" style="overflow:auto">
                <table class="aui"  style="margin-top:0">
                <caption style="color:blue">ID It Care</caption>
                    <thead>
                        <tr>
                            <th> Demandeur </th>
                            <th> Ticket </th>
                            <th> Titre  </th>
                            <th> Type de requete </th>
                            <th> Type de demande </th>
                            <th> Statut </th>
                            <th> Creation </th>
                            <th> Mise a jour </th>
                            <th> Fin de traitement estime </th>
                            <th> Degre d'urgence </th>
                        </tr>
                    </thead>
                    <tbody>'''


    List<Map> ticketData = grpJson.content
    ticketData= ticketData.sort { it.updatedAt}.reverse()
    ticketData.each { data ->
        
        def lienItCare = "https://xxxxxxxxxxxxxxxxxxx/support/tickets/"+data.id
        URL url = new URL(lienItCare);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        def urlItcare = uri.toURL();
        def HTMLLinkId = '''<a style="color:blue" href='''+urlItcare+''' target=\"_blank\">'''+data.id+'''</a>'''

        HTML += '''

                        <tr>
                            <td>'''+data.userDisplayName+'''</td>
                            <td>'''+HTMLLinkId+'''</td>
                            <td>'''+data.title+'''</td>
                            <td>'''+data.type+'''</td>
                            <td>'''+data.requestTypeId+'''</td>
                            <td>'''+data.status+'''</td>
                            <td>'''+JsonDateForm(data.createdAt)+'''</td>
                            <td>'''+JsonDateForm(data.updatedAt)+'''</td>
                            <td>'''+JsonDateForm(data.breachTime)+'''</td>
                            <td>'''+data.urgency+'''</td>
                        </tr> '''
    }

    HTML +='''          </tbody>
                </table>
            </div>'''
}


writer.write(HTML)

def JsonDateForm (dateJson){
   
    String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, new Locale("fr", "FR"));
    Date date = simpleDateFormat.parse(dateJson);
    return date.format('dd-MM-YYYY HH:mm')
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.4
TAGS
AUG Leaders

Atlassian Community Events