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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to pass variables to query builder

Hi,

I have a requirement:

In Validators in worflow transition I have to check this:

1.  In the transition screen, I ask for the Fix Versions and if the fix version is entered do the following:

Read the fixversions and see if the same fix version is assigned to any of the tickets in the same project.  To do this I am doing this script validator Groovy:

Problem here is I am retrieving the count but there are tickets which matches the condition.  I suspect problem is with fixVersion().in(fvname) in the below code.  Please help me on how to pass the fix versions to the query ?

Collection<Version> fixVersionList = issue.getFixVersions();
String fvname = "";
int i = 0;
if (fixVersionList.size() > 0)
{
for (Version v : fixVersionList)
{
if( i == 0)
{
fvname = "\""+v.getName()+"\"";
}
else
{
fvname = fvname+"\",\""+"\""+v.getName()+"\"";
}
i = i + 1;
}
builder = JqlQueryBuilder.newBuilder();
builder.where().project().eq("XXX").and().status().in("Open","In Progress").and().fixVersion().in(fvname);
Query query = builder.buildQuery();
SearchService searchService = ComponentManager.getInstance().getSearchService();
SearchResults results = null;
try
{
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
results = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
}
catch (Exception e){}
nCount = results.getTotal();

 

4 answers

We can help you write that, but can you just add a few words on why you want this? What happens if the fix version is used on another issue? What if it's not?

Hi Jamie, it is our process here that one fix version can not be assigned to multiple tickets.  So want to ensure that the no two tickets has the same fixversion.

Also I am looking at your example code passing JQL here https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/running-a-jql-query.html

Also can you please let us know how can I pass my variables in the groovy script to JQL?

I want to go which ever is the best approach.

Answering my own question:

 

Collection<Version> fixVersionList = issue.getFixVersions();
def int fvsize = fixVersionList.size();
if (fvsize >= 1){
def String CSVfvs = "";
Issue curissue = issue
curissuekey = curissue.getKey();

# Get all the associated fix version with in QUotes and comma separated
for (Version v : fixVersionList) {
fvname = v.getName();
CSVfvs = CSVfvs + "\"" + fvname + "\",";
}

# Remove trailing comma
if (CSVfvs != null && CSVfvs.length() > 0 && CSVfvs.charAt(CSVfvs.length() - 1) == ',') {
CSVfvs = CSVfvs.substring(0, CSVfvs.length() - 1);
}

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def jql = "project = JRN AND key != " + curissuekey + " and fixVersion in (" + CSVfvs + ")"
def query = jqlQueryParser.parseQuery(""+ jql + "")
try {
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
def int noOfTickets = results.getTotal()
if(noOfTickets > 0) {
message = "In Try JQL = "+ jql + "result count = " + results.getTotal() + "Current Key" + curissuekey + "Fvnames = "+ CSVfvs
invalidInputException = new InvalidInputException(message);
}
}
catch (Exception e) {
invalidInputException = new InvalidInputException(e.getMessage());
}
}

Answering my own question:

 

Collection<Version> fixVersionList = issue.getFixVersions();
def int fvsize = fixVersionList.size();
if (fvsize >= 1){
def String CSVfvs = "";
Issue curissue = issue
curissuekey = curissue.getKey();

# Get all the associated fix version with in QUotes and comma separated
for (Version v : fixVersionList) {
fvname = v.getName();
CSVfvs = CSVfvs + "\"" + fvname + "\",";
}

# Remove trailing comma
if (CSVfvs != null && CSVfvs.length() > 0 && CSVfvs.charAt(CSVfvs.length() - 1) == ',') {
CSVfvs = CSVfvs.substring(0, CSVfvs.length() - 1);
}

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def jql = "project = JRN AND key != " + curissuekey + " and fixVersion in (" + CSVfvs + ")"
def query = jqlQueryParser.parseQuery(""+ jql + "")
try {
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
def int noOfTickets = results.getTotal()
if(noOfTickets > 0) {
message = "In Try JQL = "+ jql + "result count = " + results.getTotal() + "Current Key" + curissuekey + "Fvnames = "+ CSVfvs
invalidInputException = new InvalidInputException(message);
}
}
catch (Exception e) {
invalidInputException = new InvalidInputException(e.getMessage());
}
}

Answering my own question:

Collection<Version> fixVersionList = issue.getFixVersions();
def int fvsize = fixVersionList.size();
if (fvsize >= 1){
def String CSVfvs = "";
Issue curissue = issue
curissuekey = curissue.getKey();

# Get all the associated fix version with in QUotes and comma separated
for (Version v : fixVersionList) {
fvname = v.getName();
CSVfvs = CSVfvs + "\"" + fvname + "\",";
}

# Remove trailing comma
if (CSVfvs != null && CSVfvs.length() > 0 && CSVfvs.charAt(CSVfvs.length() - 1) == ',') {
CSVfvs = CSVfvs.substring(0, CSVfvs.length() - 1);
}

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def jql = "project = JRN AND key != " + curissuekey + " and fixVersion in (" + CSVfvs + ")"
def query = jqlQueryParser.parseQuery(""+ jql + "")
try {
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
def int noOfTickets = results.getTotal()
if(noOfTickets > 0) {
message = "In Try JQL = "+ jql + "result count = " + results.getTotal() + "Current Key" + curissuekey + "Fvnames = "+ CSVfvs
invalidInputException = new InvalidInputException(message);
}
}
catch (Exception e) {
invalidInputException = new InvalidInputException(e.getMessage());
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Learn how to request and manage app requests in the Marketplace

G’day everyone! Super exciting news coming from the Marketplace. We have now fully rolled out the ability for end-users to submit app requests to admins directly from within the product! No longer ...

43 views 1 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you