Forums

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

How to change the background color of a HTML customField in Scriptrunner to the "Epic Color"?

C_S_ June 12, 2019

I have used a code I found to create a scripted custom field with scriptrunner that shows me the Epic Link in a issue and also a Sub-Task.

I now want the custom field to have the epic color as the background color, because we use the color to identify different categories of topics (to save a column on the dashboard we use for stand up meetings).

Currently the output of the field is:

Epic Name

I want:

2019-06-12 10_35_26-[ENDO-6637] asdfasfsd - DBG JIRA.jpg

 

The code I used is:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
CustomFieldManager cfManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = cfManager.getCustomFieldObjectByName('Epic Link');
CustomField epicName = cfManager.getCustomFieldObjectByName('Epic Name');
//This is where we store the issue that is linked to the epic.
Issue issue_linked_to_epic
/* determine if this is an epic, if so, return the epic link name */
if (issue.issueTypeObject.name == "Epic"){
return issue.getCustomFieldValue(epicName)
}
/*determine if this is a subTaskIssueTypes(), if so, set the parent to the issue linked to the epic */
Issue issueParent = issue.getParentObject()
if (issueParent == null){
issue_linked_to_epic = issue
} else {
issue_linked_to_epic = issueParent
}
/* get epic link */
Issue epic = (Issue) issue_linked_to_epic.getCustomFieldValue(epicLink)
if (epic == null){
return null
}
String epicNameString = epic.getCustomFieldValue(epicName)
return '<a href=myURL'+ epic +'>'+ epicNameString +'</a>'

 

 

Thank you a lot!

1 answer

1 accepted

2 votes
Answer accepted
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.
June 14, 2019

At first, I was going to say you can't do that ... but with a bit of perseverance ... I think  I found something that might work for you:

Try this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;

CustomFieldManager cfManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = cfManager.getCustomFieldObjectByName('Epic Link');
CustomField epicName = cfManager.getCustomFieldObjectByName('Epic Name');
CustomField epicColor = cfManager.getCustomFieldObjectByName('Epic Colour');
//This is where we store the issue that is linked to the epic.
Issue issue_linked_to_epic
/* determine if this is an epic, if so, return the epic link name */
if (issue.issueType.name == "Epic"){
return issue.getCustomFieldValue(epicName)
}
/*determine if this is a subTaskIssueTypes(), if so, set the parent to the issue linked to the epic */
Issue issueParent = issue.getParentObject()
if (issueParent == null){
issue_linked_to_epic = issue
} else {
issue_linked_to_epic = issueParent
}
/* get epic link */
Issue epic = (Issue) issue_linked_to_epic.getCustomFieldValue(epicLink)
if (epic == null){
return null
}
String epicNameString = epic.getCustomFieldValue(epicName)
String epicColorVal = epic.getCustomFieldValue(epicColor)


return """<a class="aui-label $epicColorVal" href="/browse/$epic">$epicNameString</a>"""

Changed or added lines are bold & underlined 

Note, you don't need to put your full url... just start with "/browse" and that will start at the root of your site. You may need to include /jira/browse if you have multiple apps/instances on the same server. Also using triple quoted lines for HTML makes things much easier to manage and read than to use single quotes and +

C_S_ June 16, 2019

Thanks for the reply!

The code makes the link look like the Epic Link, but it always uses the same color:2019-06-17 08_37_02-Epic Colors - OneNote.png

I will try to figure out how the aui-label gets its color.

Also thanks for the note regarding the shorter url and the advice with the triple quoted lines. I am not a programmer so anything that makes it easier for me helps!

_____________________________________________________

As a quick-and-dirty solution I figured out how to change the background color of the html links, doesn't exactly look like the Epic Link, but doesn't matter.
Now I need to find the hex values for the 15 colors. For Cloud I got them, but we are running Server.

Code:
return """<a href="/browse/$epic" style="color:#FFFFFF; background-color:#42526E">$epicNameString</a>"""

How would I go on and code this once I have all 15 hex codes for the 15 colors?
I could to 15 times 

if (epicColorVal == "ghx-label-1")
return """<a href="/browse/$epic" style="color:#FFFFFF; background-color:#42526E">$epicNameString</a>"""

but I believe there must be a better way.

Thank you again!

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.
June 17, 2019

Have you tried this right within the context of the issue?

I think the ghx-label-1 class has to exist in the css available at any given context. 

Jira loads css and js resources dynamically based on what it determines is needed based on what will be viewed in the current context.

So if you ran that script just in the scripted field "preview" screen, then the class is not available.

But when viewing an issue, then the class should be there should apply a color.

View an issue with an epic and inspect the epic name label in the chrome developer panel. You should be able to view how the color is applied.

C_S_ June 17, 2019

Oh wow, I am sorry.

Works perfectly! You are a legend!

Thank you so much!

Shaun Grose September 20, 2019

Exactly what I was looking for! Thanks!

Suggest an answer

Log in or Sign up to answer