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

Groovy Script Email LDAP Group

JamesR January 22, 2013

JIRA v5.2.2

SUMMARY: I have a Groovy script that -- currently -- successfully sends emails to individuals (not necessarilly JIRA users) within our organization. I'm having trouble using LDAP-defined groups.

QUESTION: Can you take a look at my script and let me know if there's a way to adjust so that I'll be able to send emails to LDAP-defined groups; not just individuals?

SCRIPT:

def componentManager = ComponentManager.getInstance();
def mailServiceManager = componentManager.getMailServerManager():
def mailServer = mailServiceManager.getDefaultSMTPMailServer();
Email jemail = new Email("LDAPGROUPNAME");
jemail.setFrom("me@me.com");
jemail.setFromName("Me");
jemail.setMimeType("text/html");
jemail.setSubtject("Testies");
jemail.setBody("Testing This");
mailServer.send(jemail);

ERRORS (ONLY OCCUR WHEN I USE AN LDAP-DEFINED GROUP):

  • javax.script.ScriptException: com.atlassian.mail.MailException: javax.mail.SendFailedException: Invalid Addresses
  • com.sun.mail.smtp.SMTPAddressFailedException: 501 5.1.3 Invalid address

Thanks for your consideration.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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 22, 2013

Are you talking about LDAP distribution lists? Or groups defined in LDAP that you see in jira through synchronisation?

If the latter you need to iterate the group and mail the users individually, eg with com.atlassian.jira.security.groups.GroupManager#getUsersInGroup

If the former then the SMTP address for the group is not available in jira, you need to query ldap for it.

You can get an ldap connection for any of the configured user directories, can't find example code right now. Otherwise you can create your ldap connection and get the DL address yourself, using eg groovy-ldap (easy) or gldapo (harder).

JamesR January 22, 2013

Jamie --- this is awesome, thank you for covering all the bases in making up for my lack of detailed question. Your guidance on the LDAP distribution list is perfect --- I'll research the query ldap approach.

Thanks again, Jamie, good luck again West Ham today.

JamesR January 22, 2013

I've given up on Real Madrid this season, too. :(

JamieA
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 22, 2013

I've given up on this season ;-)

Jozef Kotlár
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 22, 2013

Just a note about LDAP in groovyrunner - I was trying to do so using groovy-ldap (as a groovy service), but given up the the fight with classloaders so all the code I have rewritten to pure old java API.

JamieA
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 22, 2013

Yeah that can be a bitter fight. Should work though if you drop groovy-ldap and groovy-all into web-inf/lib... works for me from the admin panel anyway. But better to get your connection from embedded crowd, otherwise you have to configure everything twice.

Jozef Kotlár
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 23, 2013

You are right with getting connection attributes and it is as easy as

def cds = com.atlassian.jira.component.ComponentAccessor.getComponent(com.atlassian.crowd.embedded.api.CrowdDirectoryService)
def attrs = cds.findAllDirectories().find{it.isActive() && it.attributes.keySet().contains('ldap.url')}?.attributes

Deleted user October 28, 2013

I’d like to try using Groovy-Ldap in conjunction with Script Runner Scripted Fields. We dropped the groovy-ldap jar file and restarted JIRA. My sys admin says this file is from 2007 – so this could be my problem.

Here is my requirement:

Display a searchable select list on the Create screen that “queries” Active Directory users. These users are NOT JIRA-users.

I’ve added the following code into the Script section:

import org.apache.directory.groovyldap.LDAP

LDAP = LDAP.newInstance('ldap://SERVER:389','cn=User, ou=IT, dc=com',
<Password>)

// search op
results = ldap.search('(objectClass=*)', 'dc=com', SearchScope.ONE )

def ResultList = null
        if (results) {
            ResultList = entry.cn
}

I have a few issues . . . .

First, How do I test that ScriptRunner is utilizing the Groovy-Ldap API? I receive an error when reindexing: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: groovy/lang/Closure

Second, since I need this field to display on the screen, I’ve added the following to the description field of Field A, so, I can see Field B (Scripted Field). I’ve updated the config information in the javascript code from: https://gist.github.com/jechlin/5380119. I can’t seem to get this to work. I’ve taken out the code to look for an action.

Jamie – you mentioned above – groovy-all – is this what I’m missing?

Using JIRA 6.0.8.

Thank You.

JamieA
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.
November 2, 2013

The library is old, but still works fine, more or less.

You should put groovy-ldap into web-inf/lib. You should NOT put groovy-all.jar into web-inf/lib, or anywhere else, it is already bundled in the plugin.

First of all do some simple tests in admin -> script console, don't go straight to scripted fields and blow up the indexing...

0 votes
Aleksey Shirokih November 2, 2013

@Jamie Echlin got the same error

java.lang.NoClassDefFoundError: groovy/lang/Closure

library already in /opt/jira/atlassian-jira/WEB-INF/lib

just after jira restart.

code is

import org.apache.directory.groovyldap.LDAP
lda_server = 'ldap://192.168.0.1:389'
ldap_user = 'ldap_user'
ldap_password = 'XXXXX'
ldap = LDAP.newInstance(lda_server, ldap_user, ldap_password )

got exception on last line

Özerk Dolanbay December 16, 2013

Hi Aleksey,

I got the same error as you. Could you find the reason or any solutions?

Aleksey Shirokih December 16, 2013
Dmitri Barski April 14, 2014

It kind of works if you put groovy-ldap.jar into the scriptrunner's jar, into the META-INF/lib directory. I was still not able to really retrieve information from LDAP (JNDI to "normal object" conversion did not work, or I was not smart enough to figure it out), but I was perfectly able to validate whether a particular entry exists, which was my intention in the first place.

TAGS
AUG Leaders

Atlassian Community Events