Forums

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

Groovy Script to validate data in text fields

Venkata Nagamothu July 20, 2022

Hi All,

I have text field in Jira called 'Project'.. Likewise , I had a table in Confluence with one of columns as name 'Project' .

So, when users enter data in the field 'Project' it should validate the data in column from Confluence page .. If data in field 'Project' is not available in Confluence table column 'Project' ticket should not be created..

Any scripts or references are much appreciated.. Please reach back to me in case of unclear requirement so I can eloborate in detail..

 

Thanks,

Venkat

2 answers

0 votes
Venkata Nagamothu July 21, 2022

Hi Peter,

I was able to get Third Columns which is description in Confluence Table and not able to read the First Column 'Project'

ERROR.JPG

Also, I was not able to insert these values into field in Jira

 

Your response is much appreciated

 

Thanks,

Venkata

PD Sheehan
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.
July 21, 2022

Can you share a sample of what your page/table looks like in its storage format?

Venkata Nagamothu July 21, 2022

Thanks Peter for the response!Keys.JPG

 

As per above Screenshot of Confluence Table.. We are looking to have Project Key(Test123, TEST456 etc..) into a Jira Field called "Project Key".. Either Scripted Field which populates data from Confluence or Workflow or Field Validator so when user enter data it should be text same as "Project Key" Column in Confluence Table else request/ticket should not be created.

 

Let me know if need any further information from my end

 

Thanks,

0 votes
PD Sheehan
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.
July 20, 2022

Here is a sample script that should get you started in the correct direction:

import groovy.json.JsonSlurper
import com.atlassian.jira.applinks.JiraApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.net.Request
import org.jsoup.Jsoup

def confluenceLink = ComponentAccessor.getComponent(JiraApplicationLinkService).getPrimaryApplicationLink(ConfluenceApplicationType)
def conflUrl = confluenceLink?.displayUrl

def pageId = 325059187 //put your own
def request = confluenceLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, "$conflUrl/rest/api/content/$pageId?expand=body.storage")

def pageJson = request.execute() as String
def pageBody = new JsonSlurper().parseText(pageJson)['body']['storage']['value'] as String

def doc = Jsoup.parse(pageBody)
def table = doc.select('table')[0]
def headers = table.select('th').collect{it.text()}
def rows = table.select('tr').drop(1).collect{row->
def rowMap =[:]
row.select('td').eachWithIndex{col, i->
rowMap[headers[i]] = col.text()
}
rowMap
}

def testProject = 'AAABC'
def projectColHeader = 'Project'
assert testProject in rows.collect{it[projectColHeader]}

This will

  1. Get the application link for confluence
  2. Fetch a page (given it's ID) using the application link
  3. Parse the json string into an object
  4. Extract the storage representation of the page body from the object
  5. Parse the content using Jsoup
  6. Find the first table (index 0)
  7. Gather the headers from the table
  8. Gather all the other rows from the table and gather them as maps with keys matching the corresponding header. This will probably fail if you have merged cells
  9. Test a sample project key against the list

From there, you should be able to find other references for how to get your project field and use that to compare against the extracted table data.

PD Sheehan
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.
July 21, 2022

Seeing your table screenshot, I replicated your table.

Viewing the storage format of that page would still be helpful in cases there is other xml that might interfere with the jsoup parsing.

Here is what happen on my instance after I re-created a page that looks like yours:

2022-07-21 09_37_30-table page - Peter-Dave Sheehan - Confluence Test.png

Here is a successful result

2022-07-21 09_36_59-Script Console.png

And here is a failure result

2022-07-21 09_36_34-Script Console.png

Venkata Nagamothu July 21, 2022

Peter,

Is it possible to provide your email address? Also, let me which one are you using Workflow validator for the field or Scripted Field.? How to clear the error that "Assertion Failed"

Err1.JPG

Let me know if I am missing anything 

 

Thanks

PD Sheehan
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.
July 21, 2022

I'm only using the console as a means of doing a proof of concept. 

You asked for sample scripts and references. I gave you a sample script that can read a table from a confluence page.

You will have to do some work on your side to understand what the script is doing and modify it according to your business case. and integrate it with your workflow.

If you are not able to make those modifications you should hire someone that can. Implementing a script supplied from a forum without understanding what it does and being able to manipulate it is a dangerous proposition.

I share my groovy knowledge voluntarily. It's not part of my job. So no, I will not be sharing my email address.

As for helping you further, I've requested a sample of the storage format of your confluence page twice. Knowing exactly what the text in the page looks like is critical to being able to parse the content correctly.

The fact that your assert statement is returning a bunch of null values tells me that either 1) the column header is not exactly "Project Key" (perhaps there are some extra space or other non-printing characters) or 2) the Project Key column is not actually aligned directly above the project key values (perhaps because of some merged cells).

Suggest an answer

Log in or Sign up to answer