[转]Perform a JQL Search in ScriptRunner for Jira

2022/2/12 23:46:37

本文主要是介绍[转]Perform a JQL Search in ScriptRunner for Jira,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文转自:https://library.adaptavist.com/entity/perform-a-jql-search-in-scriptrunner-for-jira

Overview

Use this snippet to look for issues based on a JQL search. This code can be used as part of a larger bulk-administration or workflow automation task, in the Script Console and other features.

Example

The available issue resolutions in a project have been updated, leaving several issues with incorrect resolution values. I want to locate all issues with the incorrect value, so I can perform a bulk action to update them all. To save me time manually searching, I can use this script to run a JQL search, locating all affected issues.

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level

// Set log level to INFO
log.setLevel(Level.INFO)

// The JQL query you want to search with
final jqlSearch = "Some JQL query"

// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
    log.error('Invalid query')
    return null
}

try {
    // Perform the query to get the issues
    def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
    def issues = results.results
    issues.each {
        log.info(it.key)
    }

    issues*.key
} catch (SearchException e) {
    e.printStackTrace()
    null
}

 

Cloud

def jqlSearch = "project = \"Some Project\" and issueType = Epic"

post('/rest/api/2/search')
    .header('Content-Type', 'application/json')
    .body([
        jql: jqlSearch,
    ])
    .asObject(Map).body.issues.each { Map issue ->
        //Here you can do something with each issue
        logger.warn "Issue key: ${issue.key}"
        def fields = issue.fields
        logger.warn "Issue summary: ${(fields as Map).summary}"
    }

 

 

 

https://library.adaptavist.com/entity/bulk-update-the-value-of-a-system-field-on-jira-issues

Overview

Use this script in the Script Console to update the value of a system field for all issues returned by a JQL query.

Example

As a project manager, I want to modify the description of a set of similar issues in a project. With this script, I can easily bulk change all of these issue descriptions automatically, saving me time and reducing the risk of human error.

 

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 com.atlassian.jira.issue.search.SearchQuery

// The issues returned from that JQL will get altered
final searchQuery = "project = TEST"

// Get some components
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueService = ComponentAccessor.issueService

// Perform the search
def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, loggedInUser), PagerFilter.unlimitedFilter)

// Iterate all the results to update each issue
searchResults.results.each { documentIssue ->
    // Define the new params (a new description)
    def issueInputParameters = issueService.newIssueInputParameters()
    issueInputParameters.setDescription("A new description")

    // Update the issue
    def issueId = documentIssue.document.fields.find { it.name() == "issue_id" }.stringValue().toLong()
    def updateValidationResult = issueService.validateUpdate(loggedInUser, issueId, issueInputParameters)
    assert updateValidationResult.valid : updateValidationResult.errorCollection

    // Validate the update
    def issueResult = issueService.update(loggedInUser, updateValidationResult)
    assert issueResult.valid : issueResult.errorCollection
}

 

 

 

 



这篇关于[转]Perform a JQL Search in ScriptRunner for Jira的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程