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

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

Convert HTML tags in description field (when copying from Insight Textarea attribute)

Edited
Dirk Ronsmans Community Leader Sep 15, 2021

I have a couple of textarea fields defined in Insight.

When copying these over to a text field in an issue (e.g. Description) the content is copied over but with the HTML tags as formatting (and not rendered).

I'm guessing this is due to the fact that the Description field isn't HTML but more a richtext type of field?

Anybody know of a way/method/work around to not lose the formatting and but also not show the html tags?

Right now doing a simple copy of the content will give you a single line of text with the HTML around it..

 

Meaning:

Do the following:

- 1

- 2

will turn in to

<p>Do the following:</p><p>- 1</p><p>- 2</p>

2 answers

0 votes

Hi @Dirk Ronsmans ,

Insight stores the text value as (X)HTML:

import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade;
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean;
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeValueBean;

@WithPlugin("com.riadalabs.jira.plugins.insight")

import org.apache.log4j.Level;
log.setLevel(Level.DEBUG);

ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade);
List<ObjectAttributeBean> objectAttributeBeanList = objectFacade.findObjectAttributeBeans(15);
objectAttributeBeanList.each {
ObjectAttributeValueBean objectAttributeValueBean = it.getObjectAttributeValueBeans().find();
log.debug(objectAttributeValueBean.getTextValue())
}

In the example above, in the output we would find:

<p><strong>Population</strong></p><ul><li>2021 estimate:&nbsp;<strong>45,605,826</strong></li></ul><p><strong>Area</strong></p><ul><li>Total:&nbsp;<strong>2,780,400&nbsp;km2</strong></li></ul><p>&nbsp;</p>

Since Jira expects a different format (Jira WikiMarkup), then the above could be converted by using com.atlassian.renderer.wysiwyg.converter.DefaultWysiwygConverter

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.renderer.wysiwyg.converter.DefaultWysiwygConverter;

String fromInsight = "<p><strong>Population</strong></p><ul><li>2021 estimate:&nbsp;<strong>45,605,826</strong></li></ul><p><strong>Area</strong></p><ul><li>Total:&nbsp;<strong>2,780,400&nbsp;km2</strong></li></ul><p>&nbsp;</p>"
DefaultWysiwygConverter defaultWysiwygConverter = new DefaultWysiwygConverter()
String wikiMarkup = defaultWysiwygConverter.convertXHtmlToWikiMarkup(fromInsight);

IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue mutableIssue = (MutableIssue) issueManager.getIssueObject("FOO-1");
mutableIssue.setDescription(wikiMarkup)

ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
issueManager.updateIssue(applicationUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

Resulting on:

Screenshot 2021-09-21 at 23.10.41.png

For the example you have described, the above should do the trick. However, there will be conversion that will not work, requiring fine tuning.

Kind regards,
Rafael

0 votes
Dirk Ronsmans Community Leader Sep 16, 2021

I did a bit more research myself and seems I would need to find a way to convert HTML to jira wiki markup?

Anybody has a clue if such a method/class exists that can be used in a scriptrunner script?

Dirk Ronsmans Community Leader Sep 20, 2021

Thanks @Matt Doar__ LinkedIn .

I wasn't really able to find a quick solution there but I noticed my main issue was the addition of <p></p> arround the text itself.

So a simple 

replace("<p>","\\\\")

and

replace("</p>","")

did the trick, I'll have to see what else comes out when we start using more formatting on the Insight objects but for now this will do :)

Like Matt Doar__ LinkedIn likes this

Suggest an answer

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

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

319 views 3 14
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