How to synchronize fields between epics and issues in Jira with Scriptrunner? - jira

Let us assume a typical software project in Jira that uses Issues, Subtasks and Epics, with a custom field called "Customer" that can be set on any issue or the Epic. I want to have this field synchronized, so that its value for any tickets get automatically set to the highest parent of the hierarchy, up to the epics. How to achieve that with ScriptRunner for Jira?

This can be done with the following listener:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def customField = customFieldManager.getCustomFieldObjectsByName('Customer').first()
def epicLink = customFieldManager.getCustomFieldObjectsByName('Epic Link').first()
def EPIC_STORY_LINK = "Epic-Story Link"
def issuesToUpdate = [] as ArrayList<Issue>
def valueFromTop = null
if (issue.issueType.name == 'Epic') {
// The issue is an epic. Propagate the custom field value down
valueFromTop = issue.getCustomFieldValue(customField)
issueLinkManager.getOutwardLinks(issue.id).find {
it.issueLinkType.name == EPIC_STORY_LINK
}.each { IssueLink it1 ->
issuesToUpdate.addAll(it1.destinationObject)
it1.destinationObject.subTaskObjects.findAll { it2 ->
issuesToUpdate.addAll(it2)
}
}
}
else {
// Normal issue. Look up to find the reference issue to get the value from.
def issueIterator = issue
while ( true ) {
def value = issueIterator.getCustomFieldValue(customField)
if ( value ) {
valueFromTop = value
}
issuesToUpdate.addAll(issueIterator)
if ( issueIterator.parentObject ) {
issueIterator = issueIterator.parentObject
}
else {
break
}
}
// Check if the parent issue is an Epic.
issueLinkManager.getInwardLinks(issueIterator.id).find {
it.issueLinkType.name == EPIC_STORY_LINK
}.each { IssueLink epicIssueLink ->
// Get the value from the Epic
valueFromTop = epicIssueLink.sourceObject.getCustomFieldValue(customField)
}
// Add child issues to list
issue.subTaskObjects.findAll {
issuesToUpdate.addAll(it)
}
}
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
issuesToUpdate.each {
def mutableTicket = it as MutableIssue
mutableTicket.setCustomFieldValue(customField, valueFromTop)
ComponentAccessor.issueManager.updateIssue(loggedInUser, mutableTicket, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Related

How to change the Git URL in all Jenkins jobs

Does anyone have an updated version of ceilfors answer that works for both AbstractProject and WorkflowJob?
This is the solution I came up with. It was tested on Jenkins 2.355
The test was run from the script console.
For testing purposes, I limited the test to one Freestyle (AbstractProject) and one Pipeline (WorkflowJob) job each. You would need to modify the code below.
I hope others find this useful
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import hudson.plugins.git.*
import jenkins.*
import jenkins.model.*
def modifyGitUrl(url) {
def updatedUrl = url.toString().replace("git#gitlab", "git#github")
// println "updatedUrl = ${updatedUrl}"
return updatedUrl
}
Jenkins.instance.getAllItems(Job.class).each {
project = it.getFullName()
if(project.toString().equals("PL_Quick_Testing") || project.toString().equals("A_Freestyle_Job")) {
try {
if (it instanceof AbstractProject){
def oldScm = it.scm
def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
new UserRemoteConfig(modifyGitUrl(it.url), it.name, it.refspec, it.credentialsId)
}
def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
it.scm = newScm
it.save()
println "Done"
} else if (it instanceof WorkflowJob) {
def oldScm = it.getTypicalSCM()
def definition = it.getDefinition()
String scriptPath = it.getDefinition().getScriptPath()
def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
new UserRemoteConfig(modifyGitUrl(oldScm.userRemoteConfigs.url[0]), it.name, it.refspec, it.credentialsId)
}
def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
def newDefinition = new org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition(newScm, scriptPath)
it.definition = newDefinition
it.save()
println "Done"
} else {
println("${project} has no SCM")
}
} catch (Exception e) {
// e.printStackTrace()
}
}
}

Grails/ Groovy: Formatting Columns using grails.plugin.jxl.builder.ExcelBuilder

I have a working report that results in an Emailed spreadsheet.
I want columns to be sized correctly (or to be close, based on known conditions) and for formats (currency for example) to match requirements. The searches I have done haven't gotten me anywhere, but I am a newbie.
My Controller (simplified):
def myReport = {
def destEmail = 'whositz#domain.com'
def reportDate
if(params?.reportDate){
reportDate = params.reportDate
} else {
reportDate = new Date()
}
myReportService.execute(reportEmail, reportDate)
}
The Service (also simplified):
import grails.plugin.jxl.builder.ExcelBuilder
import groovy.sql.Sql
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
#Mixin(ExcelBuilder)
class myReportService implements ApplicationContextAware {
ApplicationContext applicationContext
def mailService
def dataSource
def execute(String sendToEmail, Date reportDate){
if(!mailService) {
mailService = applicationContext.getBean('mailService')
}
String path = "/tmp/someReport.xls"
String reportTitle = reportDate.format('yy.MM')+ ".thisReport"
def sql = new Sql(dataSource)
def sqlText = """
SELECT ys.acct_num AS ACCTNUM, ys.st_name as ACCTNAME, SUM(i.amt_due) AS AMTDUE
FROM invoice i, yax_sax ys
WHERE ys.ss_it = true
AND i.acct_id=ys.id
AND i.is_on=true
GROUP BY ys.acct_num, ys.sort_name
ORDER BY ys.acct_num
"""
try {
workbook(path){
sheet(reportTitle){
def rowIdx = 0
cell(0,rowIdx,"Acct #")
cell(1,rowIdx,"Acct Name")
cell(2,rowIdx,"Amt Due")
cell(3,rowIdx,"Trans ID") //Trans Id is a blank field
rowIdx++
sql.eachRow(sqlText,[]){ row ->
cell(0,rowIdx, row.ACCTNUM)
cell(1,rowIdx, row.ACCTNAME)
cell(2,rowIdx, row.AMTDUE)
cell(3,rowIdx, "")
rowIdx++
}
}
}
} catch (Throwable e) {
e.printStackTrace()
throw (e)
} finally {
sql.close()
}
mailService.sendMail {
multipart true
to sendToEmail
subject "An Report for $reportDate"
body '''
Report Attached.
'''
attachBytes "someReport.xls",'application/pdf', new File(path).readBytes()
}
}
I would appreciate responses on my question as well as ways I can better format questions in order to get responses.
When using the plugin DSL (workbook, sheet, cell) you are basically calling methods from grails.plugin.jxl.builder.ExcelBuilder.
The ExcelBuilder#cell() method return a grails.plugin.jxl.Cell object which you can use for further manipulation of the cell format and features. The plugins offers some built-in formatting methods which are document here. However you can apply further formatting by manipulating the underlying JXL object.
For example, you can do the following for having a cell with currency format:
Controller code
class ReportController {
def reportService
def index() {
response.setContentType('application/vnd.ms-excel')
response.setHeader('Content-Disposition', 'Attachment;Filename="example.xls"')
reportService.execute(response.outputStream)
}
}
Service code
import grails.plugin.jxl.Cell
import grails.plugin.jxl.builder.ExcelBuilder
import jxl.write.NumberFormats
import jxl.write.WritableCellFormat
#Mixin(ExcelBuilder)
class ReportService {
def execute(OutputStream outputStream){
String reportTitle = new Date().format('yy.MM')+ ".thisReport"
try {
workbook(outputStream){
sheet(reportTitle){
cell(0, 0, "Currency Example")
Cell myCell = cell(0,1, 1000)
WritableCellFormat currencyFormat = new WritableCellFormat(NumberFormats.ACCOUNTING_FLOAT);
myCell.format = currencyFormat
}
}
} catch (Throwable e) {
e.printStackTrace()
throw (e)
}
}
}
this works for me, using the mixin, and grails 2.3.5.
def setColWidth(def sheet, def col, def width){
CellView cv = sheet.getColumnView(col);
cv.setSize(width * 256 + 100); /* Every character is 256 units wide, so scale it. */
sheet.setColumnView(col, cv);
}
def setColWidths(def sheet){
setColWidth(sheet, 0, 16)
setColWidth(sheet, 1, 14)
setColWidth(sheet, 2, 14)
setColWidth(sheet, 5, 14)
setColWidth(sheet, 6, 14)
setColWidth(sheet, 7, 14)
setColWidth(sheet, 10, 20)
}

displaying errors in grails without refreshing the page

I have a page with dynamic list boxes(selecting value from the first list populates the values in the second list box).
The validation errors for the list boxes are working fine, but while displaying the error messages the page is getting refreshed and the selected values are been set to initial status(need to select the values again in the list boxes)
The page is designed to add any number of list boxes using ajax calls, so adding and selecting the values again is going to be a rework.
Could you help me in displaying the validation errors and keeping the selected values as they are(previously I faced a similar situation which was resolved by replacing local variables of preprocess and postprocess with a global variable, this time no luck with that approach)
Any hints/help would be great
static constraints = {
deviceMapping(
validator: {val, obj ->
Properties dm = (Properties) val;
def deviceCheck = [:];
if (obj.customErrorMessage == null) {
for (def device : dm) {
if (device.key == null || "null".equalsIgnoreCase(device.key)) {
return ["notSelected"];
}
deviceCheck.put(device.key, "");
}
if (deviceCheck.size() != obj.properties["numberOfDevices"]) {
return ["multipleDevicesError"];
}
}
}
)
customErrorMessage (
validator: {
if ("sameDeviceMultipleTimes".equals(it)) {
return ['sameDeviceMultipleTimes']
}
}
)
}
public LinkedHashMap<String, Object> preProcess(sessionObject, params, request) {
Submission submission = (Submission) sessionObject;
def selectedFileName = sessionObject.fileName;
logger.debug("submission.deviceMapping :"+submission.deviceMapping)
try {
Customer customer = Customer.get(submission.customerId);
OperatingSystem operatingSystem = OperatingSystem.get(submission.operatingSystemId)
def ftpClientService = new FtpClientService();
def files = ftpClientService.listFilesInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToPackageDirectory, selectedFileName, operatingSystem, customer.ftpCustomerTempDirectory);
def terminalService = new TerminalService();
OperatingSystem os = OperatingSystem.get(submission.getOperatingSystemId());
def manufacturers = terminalService.getAllDeviceManufacturersForType(os.getType());
logger.debug("manufacturers after os type :"+manufacturers)
logger.debug("files in preprocess :"+files)
def devicesForFiles = [:]
files.each { file ->
def devicesForThisFile = [];
submission.deviceMapping.each { device ->
if (device.value == file.fileName) {
String manufacturer = terminalService.getManufacturerFromDevice("${device.key}");
def devicesForManufacturer = terminalService.getDevicesForManufacturerAndType(manufacturer, os.getType());
devicesForThisFile.push([device:device.key, manufacturer: manufacturer, devicesForManufacturer: devicesForManufacturer]);
}
}
devicesForFiles.put(file.fileName,devicesForThisFile);
}
logger.debug("devicesForFiles :"+devicesForFiles)
return [command: this, devicesForFiles: devicesForFiles, files: files, manufacturers: manufacturers];
} catch (Exception e) {
logger.warn("FTP threw exception");
logger.error("Exception", e);
this.errors.reject("mapGameToDeviceCommand.ftp.connectionTimeOut","A temporary FTP error occurred");
return [command: this];
}
}
public LinkedHashMap<String, Object> postProcess(sessionObject, params, request) {
Submission submission = (Submission) sessionObject;
Properties devices = params.devices;
Properties files = params.files;
mapping = devices.inject( [:] ) { map, dev ->
// Get the first part of the version (up to the first dot)
def v = dev.key.split( /\./ )[ 0 ]
map << [ (dev.value): files[ v ] ]
}
deviceMapping = new Properties();
params.files.eachWithIndex { file, i ->
def device = devices["${file.key}"];
if (deviceMapping.containsKey("${device}")) {
this.errors.reject("You cannot use the same device more than once");
return [];
//customErrorMessage = "sameDeviceMultipleTimes";
}
deviceMapping.put("${device}", "${file.value}");
}
if (params.devices != null) {
this.numberOfDevices = params.devices.size(); //Used for the custom validator later on
} else {
this.numberOfDevices = 0;
}
//logger.debug("device mapping :"+deviceMapping);
submission.deviceMapping = mapping;
return [command: this, deviceMapping: mapping, devicesForFiles: devicesForFiles ];
}
}
The problem is in your gsp page. Be sure that all field are initialised with a value
<g:text value="${objectInstance.fieldname}" ... />
Also the way it is selecting values is through id, so be sure to set it as well:
<g:text value="${objectInstance.fieldname}" id=${device.manufacturer.id} ... />

Quartz Schedular in Grails

I have OperationLog class and I create 1000 records with the information which supplied by another class called Validator.
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[operationLogInstanceList: OperationLog.list(params), operationLogInstanceTotal: OperationLog.count()]
}
def create = {
def operationLogInstance = new OperationLog()
operationLogInstance.properties = params
operationLogInstance.validator = Validator.get(params.validatorId)
operationLogInstance.operation = Operation.get(params.operationId)
return [operationLogInstance: operationLogInstance]
}
def save = {
int i = 0;
1000.times {
def operationLogInstance = new OperationLog(params)
operationLogInstance.validator = Validator.get((i));
operationLogInstance.save(flush: true)
i ++;
}
redirect(action: "list")
}
}
My question is this. How can I create these records one by one with the help of quartz scheduler and each should be saved in 5 minutes.
Note: I created a job (MyJob.groovy) already. I have my execute and triggers method all empty.
As far as I understand you, you get data from the user? And you want to save this data 1000 times, every 5 minutes one?
So you want to call a service to do this (the data as a parameter)?
So this could be done via Threads (anywhere, should also work in controllers...
Thread.start {
1000.times {
def operationLogInstance = new OperationLog(params)
println(params.validator)
operationLogInstance.validator = Validator.get(params.validator.id);
operationLogInstance.save(flush: true)
}
wait(300000)
}
May be there is a OperationLog.withSession { ... } necessary around it.
Alternatively you could feed a quatz job (using a service that save the logs you want to save...)looking like this:
class OperationLogJob {
static triggers = {
simple name:'Operation Save', startDelay:0, repeatInterval:300000
}
def sessionRequired = true
def concurrent = false
def operationsLogService
def execute() {
def operationLogInstance = operationsLogService.getLogsToSave()
if(operationLogInstance) {
operationLogInstance.validator = Validator.get(params.validator.id);
operationLogInstance.save(flush: true)
}
}
}
}
The operationsLogService.getLogsToSave() method returns (and deletes) a value from a stack that you can fill in the controller method (eg. 1000.times {operationsLogService.addLog(log) })

removeFrom* not working and with no errors

I have what I think is a simple problem but have been unable to solve...
For some reason I have a controller that uses removeFrom*.save() which throws no errors but does not do anything.
Running
Grails 1.2
Linux/Ubuntu
The following application is stripped down to reproduce the problem...
I have two domain objects via create-domain-class
- Job (which has many notes)
- Note (which belongs to Job)
I have 3 controllers via create-controller
- JobController (running scaffold)
- NoteController (running scaffold)
- JSONNoteController
JSONNoteController has one primary method deleteItem which aims to remove/delete a note.
It does the following
some request validation
removes the note from the job - jobInstance.removeFromNotes(noteInstance).save()
deletes the note - noteInstance.delete()
return a status and remaining data set as a json response.
When I run this request - I get no errors but it appears that jobInstance.removeFromNotes(noteInstance).save() does nothing and does not throw any exception etc.
How can I track down why??
I've attached a sample application that adds some data via BootStrap.groovy.
Just run it - you can view the data via the default scaffold views.
If you run linux, from a command line you can run the following
GET "http://localhost:8080/gespm/JSONNote/deleteItem?job.id=1&note.id=2"
You can run it over and over again and nothing different happens. You could also paste the URL into your webbrowser if you're running windows.
Please help - I'm stuck!!!
Code is here link text
Note Domain
package beachit
class Note
{
Date dateCreated
Date lastUpdated
String note
static belongsTo = Job
static constraints =
{
}
String toString()
{
return note
}
}
Job Domain
package beachit
class Job
{
Date dateCreated
Date lastUpdated
Date createDate
Date startDate
Date completionDate
List notes
static hasMany = [notes : Note]
static constraints =
{
}
String toString()
{
return createDate.toString() + " " + startDate.toString();
}
}
JSONNoteController
package beachit
import grails.converters.*
import java.text.*
class JSONNoteController
{
def test = { render "foobar test" }
def index = { redirect(action:listAll,params:params) }
// the delete, save and update actions only accept POST requests
//static allowedMethods = [delete:'POST', save:'POST', update:'POST']
def getListService =
{
def message
def status
def all = Note.list()
return all
}
def getListByJobService(jobId)
{
def message
def status
def jobInstance = Job.get(jobId)
def all
if(jobInstance)
{
all = jobInstance.notes
}
else
{
log.debug("getListByJobService job not found for jobId " + jobId)
}
return all
}
def listAll =
{
def message
def status
def listView
listView = getListService()
message = "Done"
status = 0
def response = ['message': message, 'status':status, 'list': listView]
render response as JSON
}
def deleteItem =
{
def jobInstance
def noteInstance
def message
def status
def jobId = 0
def noteId = 0
def instance
def listView
def response
try
{
jobId = Integer.parseInt(params.job?.id)
}
catch (NumberFormatException ex)
{
log.debug("deleteItem error in jobId " + params.job?.id)
log.debug(ex.getMessage())
}
if (jobId && jobId > 0 )
{
jobInstance = Job.get(jobId)
if(jobInstance)
{
if (jobInstance.notes)
{
try
{
noteId = Integer.parseInt(params.note?.id)
}
catch (NumberFormatException ex)
{
log.debug("deleteItem error in noteId " + params.note?.id)
log.debug(ex.getMessage())
}
log.debug("note id =" + params.note.id)
if (noteId && noteId > 0 )
{
noteInstance = Note.get(noteId)
if (noteInstance)
{
try
{
jobInstance.removeFromNotes(noteInstance).save()
noteInstance.delete()
message = "note ${noteId} deleted"
status = 0
}
catch(org.springframework.dao.DataIntegrityViolationException e)
{
message = "Note ${noteId} could not be deleted - references to it exist"
status = 1
}
/*
catch(Exception e)
{
message = "Some New Error!!!"
status = 10
}
*/
}
else
{
message = "Note not found with id ${noteId}"
status = 2
}
}
else
{
message = "Couldn't recognise Note id : ${params.note?.id}"
status = 3
}
}
else
{
message = "No Notes found for Job : ${jobId}"
status = 4
}
}
else
{
message = "Job not found with id ${jobId}"
status = 5
}
listView = getListByJobService(jobId)
} // if (jobId)
else
{
message = "Couldn't recognise Job id : ${params.job?.id}"
status = 6
}
response = ['message': message, 'status':status, 'list' : listView]
render response as JSON
} // deleteNote
}
I got it working... though I cannot explain why.
I replaced the following line in deleteItem
noteInstance = Note.get(noteId)
with the following
noteInstance = jobInstance.notes.find { it.id == noteId }
For some reason the jobInstance.removeFromNotes works with the object returned by that method instead of .get
What makes it stranger is that all other gorm functions (not sure about the dynamic ones actually) work against the noteInstance.get(noteId) method.
At least it's working though!!
See this thread: http://grails.1312388.n4.nabble.com/GORM-doesn-t-inject-hashCode-and-equals-td1370512.html
I would recommend using a base class for your domain objects like this:
abstract class BaseDomain {
#Override
boolean equals(o) {
if(this.is(o)) return true
if(o == null) return false
// hibernate creates dynamic subclasses, so
// checking o.class == class would fail most of the time
if(!o.getClass().isAssignableFrom(getClass()) &&
!getClass().isAssignableFrom(o.getClass())) return false
if(ident() != null) {
ident() == o.ident()
} else {
false
}
}
#Override
int hashCode() {
ident()?.hashCode() ?: 0
}
}
That way, any two objects with the same non-null database id will be considered equal.
I just had this same issue come up. The removeFrom function succeeded, the save succeeded but the physical record in the database wasn't deleted. Here's what worked for me:
class BasicProfile {
static hasMany = [
post:Post
]
}
class Post {
static belongsTo = [basicProfile:BasicProfile]
}
class BasicProfileController {
...
def someFunction
...
BasicProfile profile = BasicProfile.findByUser(user)
Post post = profile.post?.find{it.postType == command.postType && it.postStatus == command.postStatus}
if (post) {
profile.removeFromPost(post)
post.delete()
}
profile.save()
}
So it was the combination of the removeFrom, followed by a delete on the associated domain, and then a save on the domain object.

Resources