Forums

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

Can Scriptrunner be used to add a list of URLs to a project's shortcuts?

Jeremy Jedlicka
Contributor
October 27, 2022

We have a set list of links that gets added to each project's shortcuts. Instead of adding these one by one each time a new project space is created, I'd like to automate this.

Is this possible via scriptrunner?  I haven't found anything in the Jira backend to set default shortcuts.  If it is possible, can someone point me in the right direction to the groovy documentation explaining this?

Thanks.

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 5, 2022

Hi @Jeremy Jedlicka

After going through your description, if you intend to auto-insert the shortcuts in the Project Shortcuts section, as shown in the image below, this is not directly doable using ScriptRunner because it is controlled by the "AO_550953_SHORTCUT" table.

The only way the shortcuts can be added is by inserting the values into that table, which is not recommended.

section1.png

On the other hand, if you are okay with adding the shortcuts outside this section, as shown in the image below:-

section2.png

this can be done using ScriptRunner's fragments.

If you would like to try the 1st approach, you could try the sample code below on the ScriptRunner console:-

import groovy.sql.Sql
import java.sql.Driver

final def projectKey = 10000

def props = new Properties()
props.setProperty('user', 'adaptavist')
props.setProperty('password', 'qwerty')

def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver
def conn = driver.connect('jdbc:postgresql://localhost:5432/Jira8', props)
def sql = new Sql(conn)

def insertions = [
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('Adaptavist', ${projectKey}, 'https://www.adaptavist.com', '');",
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('Adaptavist Library', ${projectKey}, 'https://library.adaptavist.com', '');",
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('ScriptRunner Docs', ${projectKey}, 'https://docs.adaptavist.com/sr4js/latest', '');",
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('Groovy Lang', ${projectKey}, 'https://groovy-lang.org', '');",
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('Groovy Tutorial', ${projectKey}, 'https://www.guru99.com/groovy-tutorial.html', '');",
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('Jira Api Docs', ${projectKey}, 'https://docs.atlassian.com/software/jira/docs/api/8.22.2/', '');",
"insert into \"AO_550953_SHORTCUT\" (\"NAME\", \"PROJECT_ID\", \"SHORTCUT_URL\", \"ICON\") values ('Atlassian Community', ${projectKey}, 'https://community.atlassian.com', '');"
] as ArrayList<String>

try {
insertions.each {
sql.execute(it)
}
} finally {
sql.close()
conn.close()
}

Please note that the sample code provided above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Before running this code, please ensure that you back up your Jira database so you can revert to it if anything goes wrong.

Also, before triggering this code in the ScriptConsole, please manually add one link in the Project Shortcuts section.

Once you have added the link, you can trigger the code in ScriptConsole.

If you intend to remove the links, you must manually do it using the Jira UI

I hope this helps to answer your question.

Thank you and Kind regards,

Ram

Jeremy Jedlicka
Contributor
November 7, 2022

@Ram Kumar Aravindakshan _Adaptavist_ 

Thanks for pointing me in the right direction.  The Fragments approach will work just fine for us.

I've got the links to display correctly.  Now, I'm curious that since I'm adding links this way, can I hide the Project shortcuts via Fragments also?  I've tried these modules but none hide that section.

Modules.png

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 7, 2022

Hi @Jeremy Jedlicka

If you want to use the Fragment approach, you will need to use the Show Web Panel fragment as shown in the image below:-

fragment_config.png

Once you have selected that fragment, you will need to set the Location to jira.project.software.sidebar, add a key and a Menu text as shown in the image below:-

image1.png

 

Below is the sample code that I have tested with:-

def links = """
<div style="margin:10px">
<a href="https://www.google.com" target="_blank">Google</a><br/>
<div style="height:10px;"></div>
<a href="https://www.yahoo.com" target="_blank">Yahoo</a></br>
<div style="height:10px;"></div>
<a href="https://www.wikipedia.com" target="_blank">Wikipedia</a>
</div>
"""
writer.write(links.toString())

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

If you notice, the code is basic HTML, and the value is passed to the writer as a String.

Below is a screenshot of the output produced:-

image2.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

 

Jeremy Jedlicka
Contributor
November 8, 2022

@Ram Kumar Aravindakshan _Adaptavist_ 

I have the links working and displaying properly.  What I'm trying to do now, since I have the links to the panel, is to hide the default Project Shortcuts section.

For that I'm using the Hide System or Plugin UI Element, but none of the obvious options pertain to that section.  These are what I've tried so far:

Modules.png

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 9, 2022

Hi @Jeremy Jedlicka

If you want to hide the Project Shortcuts section, you will need to include the condition, in the fragment, for example:-

jiraHelper.project.key = 'MOCK'

The condition above specifies for which Project should the Project Shortcuts be hidden.

Below is a screenshot of the configuration:-

image1.png

Below is a screenshot of the hidden Project Shortcuts section:-

image2.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 11, 2022

Hi @Jeremy Jedlicka

Does the suggested solution work for you?

If yes, please accept the answer.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer