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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,321
Community Members
 
Community Events
184
Community Groups

Need help to Set custom field for issues with the same labels

Edited
  1. Hello

My team did some import for X-RAY and we need to associate tests with preconditions based on the labels. 

The only common field is the labels. 

The prob is that the precondition field is a custom one that need issue key. we have to specify the issue key to do the association.

I'm not that good in scriptrunner nor groovy.

If someone could help us to script this.

Big thanks

1 answer

0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Dec 06, 2021

Hi @Oussama Brik ,

I am not sure to understand fully your problem, but here is a groovy script that will return issues with the label "test" : 

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter

def jqlSearch = "labels = \"test\""

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = userManager.getUserByKey('berry_a')
List<Issue> issues = null
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.results.collect {issueManager.getIssueObject(it.id)}
} else {
log.error("Invalid JQL: " + jqlSearch);
}
return issues

Hope that helps

thanks @Antoine Berry  I found a way to solve my prob:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def GetIssueManager = ComponentAccessor.getIssueManager()
def log = Logger.getLogger("com.acme.workflows")
log.setLevel(Level.DEBUG)

final customFieldName = "customfield_12345"
def IssueCF = customFieldManager.getCustomFieldObject(customFieldName)


def query1 = jqlQueryParser.parseQuery('project = MyProject AND (type = bug) AND (labels is not Empty) ')
def results1 = searchProvider.search(query1, user, PagerFilter.getUnlimitedFilter())
def List1 = results1.issues.collect{issueManager.getIssueByCurrentKey(it.key)}

def query2 = jqlQueryParser.parseQuery('project = MyProject AND (type = task) AND (labels is not Empty)')
def Results2 = searchProvider.search(query2, user, PagerFilter.getUnlimitedFilter())
def List2 = Results2.issues.collect{issueManager.getIssueByCurrentKey(it.key)}


Results2.getIssues().each { documentIssue ->
def Issue2Key = documentIssue.key
def Label2 = documentIssue.labels
String Label2_String = Label2.first()

results1.getIssues().each { documentIssue2 ->
def Issue1Key = documentIssue2.key
def Issue1Object = issueManager.getIssueObject(Issue1Key)
def Issue1Label = documentIssue2.labels
String Label1_String = Issue1Label.first()
def Issue1CF = documentIssue2.getCustomFieldValue(IssueCF)
if(Label1_String.equals(Label2_String) && !Issue1CF){
Issue1Object.setCustomFieldValue(IssueCF, [Issue2Key])
GetIssueManager.updateIssue(user, Issue1Object, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

}
Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events