Forums

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

JETI: How do i use OSGi component with scriptrunner

Matthew Van Kuyk
Contributor
May 28, 2018

Hi All,

 

I am trying to call JETI from scriptrunner. therefore I want to use the OSGi component described here: https://metainf.atlassian.net/wiki/spaces/PLUG/pages/66093062/API+-+Integration+with+other+services

Unfortunately I am not getting it to work with my code:

import com.atlassian.jira.ComponentManager;
import com.atlassian.plugin.PluginAccessor;

ComponentManager componentManager = ComponentManager.getInstance();
PluginAccessor pluginAccessor = componentManager.getPluginAccessor();


Class emailServiceClass = pluginAccessor.getClassLoader().findClass("com.metainf.jira.plugin.emailissue.api.EmailService");
def emailService = componentManager.getOSGiComponentInstanceOfType(emailServiceClass);
Class emailDefinitionApiClass = pluginAccessor.getClassLoader().findClass("com.metainf.jira.plugin.emailissue.api.EmailDefinitionApi");
def emailDefinitionApi = componentManager.getOSGiComponentInstanceOfType(emailDefinitionApiClass);

emailDefinitionApi.issue = "XXX-123"
emailDefinitionApi.to = ["x@mycomp.local"]
try{
    emailService.sendEmail(emailDefinitionApi);
    
}catch(Exception Ex){log.error(Ex.toString());}

 

versions:

JIRA 7.7

JSD 3.10.0

JETI 7.1.0.16

Scriptrunner 5.4.1

2 answers

0 votes
Paolo Bolla August 12, 2018

Hi,

use this code

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.metainf.jira.plugin.emailissue.api.EmailService
import com.metainf.jira.plugin.emailissue.api.EmailDefinitionApi

@WithPlugin("com.metainf.jira.plugin.emailissue")

@PluginModule
EmailService emailService

emailService =
ComponentAccessor.getOSGiComponentInstanceOfType(EmailService.class);
EmailDefinitionApi em = new EmailDefinitionApi()
em.issue = "your issuekey"
em.to = ["your@mail"]

try{
emailService.sendEmail(em)
}catch(Exception Ex){
log.error(Ex.toString());
}
Stefan Bärthlein April 1, 2019

I'm getting this error - my be because I'm using a valuation license?

regards, s.

java.lang.NoClassDefFoundError: com/metainf/jira/plugin/emailissue/api/EmailService
java.lang.NoClassDefFoundError: com/metainf/jira/plugin/emailissue/api/EmailService
at Script371.run(Script371.groovy:7) [?:?]
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:176) [?:?]
at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$5.callCurrent(Unknown Source) [?:?]
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:165) [?:?]
at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript$6.call(Unknown Source) [?:?]
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:70) [?:?]
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript$3.call(Unknown Source) [?:?]
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.doScript(CustomScriptFunction.groovy:46) [?:?]
Caused by: java.lang.ClassNotFoundException: com.metainf.jira.plugin.emailissue.api.EmailService
... 8 more
Stefan Bärthlein April 2, 2019

OK - Decompiling the JETI-JAR made it clear how to use the API. I Succeeded using following code:

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.metainf.jira.plugin.emailissue.api.EmailService
import com.metainf.jira.plugin.emailissue.api.EmailDefinitionApi
import com.metainf.jira.plugin.emailissue.action.EmailOptions

// https://scriptrunner.adaptavist.com/5.3.28/jira/scripting-other-plugins.html
@WithPlugin("com.metainf.jira.plugin.emailissue") // Addon App Key

@PluginModule
EmailService emailService

emailService = ComponentAccessor.getOSGiComponentInstanceOfType(EmailService.class);
EmailDefinitionApi em = new EmailDefinitionApi()
em.issue = "BD-145" // hard codiert zu Testzwecken
em.to = ["cf:customfield_11401"]
em.cc = ["tester04.sbg.ac.at@outlook.de"]
em.emailSubject = "Das ist ein Email-Test über die JETI API gesendet"
em.emailBody = "Das ist der Inhalt der Email... 2ter Test"
em.addAttachments = "ALL"
em.emailTemplate = "1" // Alle Kommentare
em.emailOptions = new EmailOptions()
em.emailOptions.setAddToWatchers(false)
em.emailOptions.setReplyToMe(false)
em.emailOptions.setMailAsMe(false)
em.emailOptions.setAddComments(false)
em.emailOptions.setSuppressCommentEvent(true)
em.emailOptions.setEmailFormat("html")
em.emailOptions.setCommentVisibility("g:jira-administrators")
em.emailOptions.setRichHtml(true)

try{
emailService.sendEmail(em)
}catch(Exception Ex){
log.error(Ex.toString());
}

regards, s.
Vytautas Chamutovskij May 9, 2019

Hi Stefan.

     What does it mean?

em.emailTemplate = "1"

 I've tried to set for this option the Template id from table AO_544E33_TEMPLATE, but I always get emails based on default template. I've tried to set a template name - result is the same.

 

Best regards!

Stefan Bärthlein May 9, 2019

Hi @Vytautas Chamutovskij , so did I :o)

I've opened an request at META-INF and think they are fixing the issue - for now the Request has not been closed and is still in the status "under devlopement".

jfyi

regards,

stefan.

Like Vytautas Chamutovskij likes this
Vytautas Chamutovskij May 9, 2019

Thanks Stefan! Say please the number of your request - I'll watch it.

0 votes
Alexey Matveev
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.
May 29, 2018

You can try to use the WithPlugin and PluginModule annotations as it is written here:

https://scriptrunner.adaptavist.com/5.3.28/jira/scripting-other-plugins.html

Make sure that the service, which you try to inject is exported as public service. Also log the value of the service. You must make sure, that the service was successfully imported.

Matthew Van Kuyk
Contributor
June 14, 2018

Dear Alexey,

thank you for your answer. I wans't able to get it to work and I reverted to do a REST call :-S. Which is a workaround and not a solution for me.

Kind regards,

Matt

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events