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

How to create sub-task with a field and assignee information coming from a predfined confluence page

Bhupesh Nagda January 5, 2022

Hi,

 

I have a requirement where subtasks are supposed to be auto-created when a new parent is created in jira but the catch is that a single line text field(name) and assignee on the sub-task must be populated from a confluence page table with key value mappings. So if there are 10 rows then 10 sub-tasks should be created.

Name Assignee

IRR bngda

ABC dlynch

I have scriptrunner for Jira but not for confluence so finding it difficult to approach a solution for this.

I have created sub-task using script post function create sub-task and additional issue actions for small automations but this i am not able to think as number of sub-tasks cannot be added in advance and will depend on confluence page table length.

We don;t want user to select Name and Assignee manually on a jira ticket so maintaining list in confluence and not via custom field in jira. But if there is an easy way to do this completely in jira also then it would be great. Also maintaining list in jira makes it easy for non admin users to manage it.

 

Any help guidance would be highly appreciated!

BR,

Bhupesh

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave 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.
January 5, 2022

While I can see Ravi's point, I'm not sure I completely agree.

I think having a confluence page as an easy place for users to maintain a list of values is a good approach in my opinion.

There are some limitations and possible complications to be aware of, but it's definitely better than hardcoding the key-value mapping and then requiring the jira admin to update frequently.

Here is a short script to get you started to get values from a table in a confluence page using application link authenticated request:

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

def pageId=35241928 //put your own page ID here

def appLink = ComponentAccessor.getComponent(JiraApplicationLinkService).getPrimaryApplicationLink(ConfluenceApplicationType)
def conflUrl = appLink?.displayUrl
def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()

def pageUrl = "$conflUrl/rest/api/content/$pageId?expand=body.storage"

def request = applicationLinkRequestFactory.createRequest(Request.MethodType.GET, pageUrl )
def response = request.execute()
def soup = Jsoup.parse(response)
def map = soup.select('table')[0].select('tr').tail().collectEntries{row->
[(row.childNodes[0]):(row.childNodes[1])]
}

Some assumptions:

  1. Confluence and Jira are linked via application links (I think OAuth with impersonation is required)
  2.  The page only has a single table. If you have more than one, you can change the index (currently retrieving table index 0). Beware of inserting new tables in the future and changing the index.
  3. The table contains a header row. We use "tail()" to get everything except the first row.
  4. The table only has 2 columns and the key is in column 0 and the value is in column 1
  5. Keys are unique. if you have more than one row with a specific key, you might want to look at using a different structure for your data.
Ravi Sagar _Sparxsys_
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.
January 5, 2022

That's a great script. My only concern was that it relies on Confluence page to exist with a specific table, which is can be tricky sometimes to control because people can edit the page easily.

Thanks for sharing this, Jsoup is a great for parsing.

Ravi

Bhupesh Nagda January 6, 2022

Hi @Peter-Dave Sheehan 

Thank you for the great help!

So all my key value pairs are in this map and then i need to create a script listener which will run the sub-task creation code inside a loop till the length of this map field? Or maybe a custom script post function inside parent create transition with sub-task creation code inside a loop. How would you suggest me to create the sub-tasks ?

I also need to figure out the code for sub-task creation as out of box create sub-task script runner post function can't be utilized for this requirement, or am i wrong in saying this?

Thank you again for the guidance :) you are helping me convert a requirement which i initially thought may not be possible to implement.

 

BR,

Bhupesh

Peter-Dave 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.
January 6, 2022

I think if you can use a post function, that's probably more specific than a listener.

But you are correct, the built-in create sub-task feature can't be used as-is.

You need to script the sub-task creation from scratch

map.each{key, value ->
//code to create subtask
}

You can borrow code for creating subtasks from the library: https://library.adaptavist.com/entity/create-a-sub-task-and-link-to-parent-issue-in-jira

Bhupesh Nagda January 10, 2022

hi @Peter-Dave Sheehan 

It worked like a charm, but there is one small issue and it's with jsoup parsing , the key value pairs are having the table descriptor tags associated with them like this <td>IRR</td> <td>bngda</td>

i tried to do a bit research but couldn't find a way to get rid of the tags, do you know how can i get rid of them?

Thanka alot again :)

BR,

Bhupesh

Bhupesh Nagda January 10, 2022

this is how the page is store on confluence

<table class="wrapped">
<colgroup>
<col/>
<col/>
</colgroup>
<tbody>
<tr>
<th>Application Name</th>
<th>Application Owner</th>
</tr>
<tr>
<td>IRR</td>
<td>bngda</td>
</tr>
<tr>
<td>RISK</td>
<td>dlynch</td>
</tr>
<tr>
<td>FRR</td>
<td>drdovan</td>
</tr>
</tbody>
</table>

Peter-Dave 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.
January 10, 2022

Here is a jsoup implementation that's more explicit and will get the text elements of each column:

def map = soup.select('table')[0].select('tr').tail().collectEntries{row->
   def cols = row.select('td')
   [(cols[0].text()):cols[1].text()]
}
0 votes
Ravi Sagar _Sparxsys_
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.
January 5, 2022

Hi @Bhupesh Nagda 

Well using Confluence page to store these key-value mapping is also not the best approach IMO.

Since you have ScriptRunner, you can always access your Confluence page (using application link or REST API) and parse that table where you have that key-value mapping. Technically it is possible but relying on a table stored in Confluence to update an Issue in Jira is probably adding too much complexity to your automation.

Doing it entirely from Confluence might be easier though using a macro to call Jira REST API.

I hope it helps.

Ravi

TAGS
AUG Leaders

Atlassian Community Events