Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to list of all the Issue types Schemes and issue types for whole Jira instance

Anuradha Yadav August 3, 2022

Hi Team,

Is there any script to fetch  list of all the Issue types Schemes and issue types for whole Jira instance?

 

Regards,

Anuradha

1 answer

1 accepted

0 votes
Answer accepted
Aditya Sastry
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 3, 2022

Hi Anuradha,

Below is the script to get the output. I've defined other schemes as well and you can use whatever you need. 

Please mark the answer as accepted if it solves your request.

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category


def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)

def issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager()
def wrkflwScheme = ComponentAccessor.getWorkflowSchemeManager()
def issueTypeScreenSchemeManager=ComponentAccessor.getIssueTypeScreenSchemeManager()
def issueTypeScreenScheme = ComponentAccessor.getIssueTypeScreenSchemeManager()
def fieldScheme = ComponentAccessor.getFieldLayoutManager()
def wrkflw = ComponentAccessor.getWorkflowManager()
def issueType = ComponentAccessor.getIssueTypeSchemeManager()

log.info(issueTypeScreenSchemeManager.getIssueTypeScreenSchemes()*.name)
log.info(issueType.getAllSchemes()*.name)

 Thanks,
Aditya

Anuradha Yadav August 3, 2022

Thanks it works! But I wanted a excel sheet where the list has How many issues have particular issue type, project key (name), project lead.? 

Something like below?

  th("issuetype")
        th("Issue Count")
        th("Issuetype scheme")
        th("Project Count")
        th("Project Key")
        th("Project Name")
        th("Project Lead")
        th("Project Lead Emails")

 

I have script from my team but as I am new with groovy / scrip runner and I am having difficulties in writing/modifying a script

Script below works, but want to modify to fetch data about issue types and issue type schemes:

 

import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.status.Status
import com.atlassian.jira.mail.Email
import com.atlassian.jira.mail.settings.MailSettings
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.scheme.SchemeManager
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.workflow.AssignableWorkflowScheme
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import com.atlassian.jira.workflow.WorkflowSchemeManager
import com.atlassian.mail.MailException
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil
import com.onresolve.scriptrunner.canned.CannedScript
import com.onresolve.scriptrunner.canned.jira.utils.CannedScriptUtils
import com.onresolve.scriptrunner.canned.util.BuiltinScriptErrors
import com.onresolve.scriptrunner.canned.util.SimpleBuiltinScriptErrors
import groovy.xml.MarkupBuilder
import org.apache.log4j.Logger
import com.atlassian.jira.config.StatusManager

import javax.activation.DataHandler
import javax.activation.FileDataSource
import javax.mail.MessagingException
import javax.mail.Multipart
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMultipart
import java.sql.Timestamp
import java.text.SimpleDateFormat

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
ProjectManager projectManager = ComponentAccessor.getProjectManager()
WorkflowSchemeManager workflowSchemeManager = ComponentAccessor.getWorkflowSchemeManager()
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager()
StatusManager statusManager = ComponentAccessor.getComponentOfType(StatusManager.class)
IssueManager issueManager = ComponentAccessor.getIssueManager()

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm")

//All projects
List<Project> projects = new ArrayList<>()
projects.addAll(projectManager.getProjects())

//All issues
List<String> issueStatuses = new ArrayList<>()
for (Project project : projects) {
    Collection<Long> issueIds = issueManager.getIssueIdsForProject(project.id)
    for (Long issueId : issueIds) {
        Issue issue = issueManager.getIssueObject(issueId)
        issueStatuses.add(issue.status.name)
    }
}

//All workflows
Collection<JiraWorkflow> allWorkflows = new ArrayList<>()
allWorkflows = workflowManager.getWorkflows()
List<JiraWorkflow> activeWorkflows = new ArrayList<>()
List<JiraWorkflow> inactiveWorkflows = new ArrayList<>()
for (JiraWorkflow workflow : allWorkflows) {
    if(workflow.isActive() == true){
        activeWorkflows.add(workflow)
    } else {
        inactiveWorkflows.add(workflow)
    }
}

//All statuses
List<Status> statuses = new ArrayList<>()
statuses.addAll(statusManager.getStatuses())

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.table(){
    tr{
        th("Status")
        th("Issue Count")
        th("Workflow Count")
        th("Active Workflows")
        th("Inactive Workflows")
        th("Project Count")
        th("Project Key")
        th("Project Name")
        th("Project Lead")
        th("Project Lead Emails")
    }
    for (Status status : statuses) {
        tr{
            td(status.name) // Status Name
            td(issueStatuses.count(status.name)) // Issue Count
            def activeLinkedWorkflows = []
            def inactiveLinkedWorkflows = []
            for (JiraWorkflow workflow : activeWorkflows) {
                if (workflow.getLinkedStatusObjects()*.id.contains(status.getId())) {
                    activeLinkedWorkflows.add(workflow.name)
                }
            }
            for (JiraWorkflow workflow : inactiveWorkflows) {
                if (workflow.getLinkedStatusObjects()*.id.contains(status.getId())) {
                    inactiveLinkedWorkflows.add(workflow.name)
                }
            }
            td(activeLinkedWorkflows.size() + inactiveLinkedWorkflows.size()) // Count
            td(activeLinkedWorkflows.toString())// Active
            td(inactiveLinkedWorkflows.toString()) // Inactive
            def linkedProjects = []
            def linkedProjectLeads = []
            def linkedProjectLeadEmails = []
            projects.each { project ->
                def scheme = workflowSchemeManager.getWorkflowScheme(project)
                def wfs = workflowManager.getWorkflowsFromScheme(scheme)*.name
                wfs.each{wf ->
                    if(activeLinkedWorkflows.contains(wf)){
                        linkedProjects.add(project)
                    }
                }

            }
            linkedProjects.unique()
            linkedProjects.each { project ->
                linkedProjectLeads.add(project.lead.getDisplayName())
                linkedProjectLeadEmails.add(project.lead.getEmailAddress())
            }
            td(linkedProjects.size()) // Project Count
            td(linkedProjects.key.toString())// Project Key
            td(linkedProjects.name.toString())// Project Name
            td(linkedProjectLeads.toString())// Project Leads
            td(linkedProjectLeadEmails.toString())// Project Lead Emails
        }
    }
}


writer.toString()

private static Boolean sendEmail(String emailAddr, String subject, String body, StringWriter attachmentData) {
    MailSettings mailSettings = ComponentAccessor.getComponent(MailSettings)
    if (mailSettings?.send()?.disabled) {
        return false
    }
    SMTPMailServer mailServer = ComponentAccessor.mailServerManager.defaultSMTPMailServer
    if (!mailServer) {
        return false
    }

    Email email = new Email(emailAddr)
    email.setMimeType('text/html')
    email.setSubject(subject)
    email.setBody(body)

    String fileName = "/tmp/${System.currentTimeMillis().toString()}_status_information.html"
    File myFile = new File(fileName)
    myFile << attachmentData.toString()
    Multipart multipart = new MimeMultipart("mixed")
    MimeBodyPart attachPart = new MimeBodyPart()
    File csvFile = new File(fileName)
    FileDataSource source = new FileDataSource(csvFile)
    attachPart.setDataHandler(new DataHandler(source))
    attachPart.setFileName(csvFile.getName())
    multipart.addBodyPart(attachPart)
    email.setMultipart(multipart)

    try {
        ContextClassLoaderSwitchingUtil.runInContext(SMTPMailServer.classLoader) {
            mailServer.send(email)
        }
        return true
    } catch (MailException e) {
        return false
    }
}

sendEmail(authenticationContext.getLoggedInUser().getEmailAddress(), "Status Overview", "Please find the results attached.", writer)

Like Sergiienko Volodymyr likes this
Anuradha Yadav August 3, 2022

Can you please modify script to fetch below details in excel:

 

Issue typeIssue type IDIssue CountIssue type schemeProject CountProject KeyProject NameProject LeadProject Lead Emails
Anuradha Yadav August 4, 2022

Hi, @Aditya Sastry ,

Could you please update?

 

Regards,

Anuradha

Suggest an answer

Log in or Sign up to answer