Forked from vikrantyadav11/Get JIRA User Data via email as csv attachment .groovy
Created
November 24, 2022 08:21
-
-
Save Rand01ph/070e5180919f0752097102af0b9688e6 to your computer and use it in GitHub Desktop.
Get jira users along with Email Address, status, login data and attach csv file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.atlassian.jira.component.ComponentAccessor | |
import com.atlassian.jira.security.login.LoginManager | |
import com.atlassian.mail.Email | |
import groovy.xml.MarkupBuilder | |
import org.jsoup.Jsoup | |
import javax.mail.internet.MimeBodyPart | |
import javax.mail.internet.MimeMultipart | |
import java.text.SimpleDateFormat | |
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class) | |
def groupManager = ComponentAccessor.groupManager | |
def userUtil = ComponentAccessor.userUtil | |
def adminGroup = groupManager.getUsersInGroup('jira-administrators') | |
def softwareGroup = groupManager.getUsersInGroup('jira-users') | |
//def serviceDeskGroup = groupManager.getUsersInGroup('jira-servicedesk-users') | |
def users = adminGroup + softwareGroup | |
users.unique() | |
final def filePath = "C:/temp" | |
def emailBody = new StringWriter() | |
def html = new MarkupBuilder(emailBody) | |
html.html { | |
head { | |
style(type:'text/css', """ | |
table { | |
border-collapse: collapse; | |
width: 100%; | |
} | |
th, td { | |
text-align: left; | |
padding: 8px; | |
} | |
tr:nth-child(even){background-color: #f2f2f2} | |
th { | |
background-color: #04AA6D; | |
color: white; | |
} | |
""".toString() ) | |
} | |
body { | |
table { | |
thead { | |
tr { | |
th 'User Name' | |
th 'Full Name' | |
th 'Email Address' | |
th 'Last Login' | |
th 'Status' | |
} | |
users.each { | |
def lastLoginTime = loginManager.getLoginInfo(it.username).lastLoginTime | |
def username = it.username | |
def displayName = it.displayName | |
def emailAddress = it.emailAddress | |
def active = it.active | |
if (userUtil.getGroupsForUser(it.name).size() > 0) { | |
tr { | |
if (!active) { | |
td ( username ) | |
td ( displayName ) | |
td ( emailAddress ) | |
td ('Inactive User' ) | |
td ( active ) | |
} else if (!lastLoginTime) { | |
td ( username ) | |
td ( displayName ) | |
td ( emailAddress ) | |
td ('Logon not found' ) | |
td ( active ) | |
} else { | |
def date = new Date(lastLoginTime) | |
def df2 = new SimpleDateFormat("dd/MM/yy hh:mm") | |
def dateText = df2.format(date) | |
td ( username ) | |
td ( displayName ) | |
td ( emailAddress ) | |
td ( dateText ) | |
td ( active ) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
def dest = new File("${filePath}/output.csv") | |
dest.createNewFile() | |
def subject = 'User data' | |
def emailAddr = '[email protected]' | |
def fileWriter = new FileWriter("${filePath}/output.csv") | |
fileWriter.write(generateCSV(emailBody.toString())) | |
fileWriter.close() | |
final static String generateCSV(String tableDetails) { | |
def stringWriter = new StringWriter() | |
def doc = Jsoup.parseBodyFragment(tableDetails) | |
def rows = doc.getElementsByTag('tr') | |
rows.each { | |
def header = it.getElementsByTag('th') | |
def cells = it.getElementsByTag('td') | |
header.each { headerCell -> | |
stringWriter.append(headerCell.text().concat(', ')) | |
} | |
cells.each { cell -> | |
stringWriter.append(cell.text().concat(', ')) | |
} | |
stringWriter.append('\n') | |
} | |
stringWriter.toString() | |
} | |
final static creatMessage(String to, String subject, String content, File file) { | |
def mailServerManager = ComponentAccessor.mailServerManager | |
def mailServer = mailServerManager.defaultSMTPMailServer | |
def multipart = new MimeMultipart() | |
def body = new MimeBodyPart() | |
def mailAttachment = new MimeBodyPart() | |
body.setContent(content, 'text/html; charset=utf-8') | |
mailAttachment.attachFile(file) | |
multipart.addBodyPart(body) | |
multipart.addBodyPart(mailAttachment) | |
def email = new Email(to) | |
email.setSubject(subject) | |
email.setMultipart(multipart) | |
email.setMimeType("text/html") | |
def threadClassLoader = Thread.currentThread().contextClassLoader | |
Thread.currentThread().contextClassLoader = mailServer.class.classLoader | |
mailServer.send(email) | |
Thread.currentThread().contextClassLoader = threadClassLoader | |
} | |
creatMessage(emailAddr, subject, emailBody.toString(), dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment