After create on transition I need to add only the latest attachement to the newly created issue.

Maud Schlich January 11, 2016

Hello,

from diverse questions and answers found here, I tried to solve my question with the following script. But no attachment gets attached. I guess, the way to address the newly created issue is dead wrong, but what shall I change here? We use 6.4.12 ...

Thanks in advance,

 Maud

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue;
 
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
log.setLevel(org.apache.log4j.Level.DEBUG);
 
// Key of old issue with attachment
Issue issueKey = issue
def id = issueKey.getId()
// Key of new issue (which shall get attachment)
// !!! NOT OK.
def linkedIssueKeyList = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getID())
def linkcounter = linkedIssueKeyList.size()
def lastlinkedIssueKey = linkedIssueKeyList[linkcounter].getId()
 
// User
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().name
 
// Managerservice for attachements
def attachmentManager = ComponentAccessor.getAttachmentManager()
 
// today
def today = new Date()
 
// Last attachement
def attachmentPathManager = ComponentAccessor.getAttachmentPathManager().attachmentPath.toString()
def fileName = issueKey.attachments.last()?.filename.toString()
def fileType = issueKey.attachments.last()?.mimetype.toString()
def fileId = issueKey.attachments.last()?.id
 
// Construct path for attachement
def currentIssue = issue.toString()
def projectKey = currentIssue.replaceAll("[^A-Z]", "").toString();
def path = attachmentPathManager + "/" + projectKey + "/" + currentIssue + "/" + fileId + "/"
File filePath = new File(path.toString())
 
// issue that shall get attachement
Issue newIssue = ComponentAccessor.getIssueManager().getIssueObject(lastlinkedIssueKey)
 
// Any attachment there? Then use it
if (fileName != null) {
    attachmentManager.createAttachmentCopySourceFile(filePath, fileName, fileType, user, newIssue, null, today)
}

 

 

 

 

 

3 answers

0 votes
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 11, 2016

Isn't this a question about the create-on-transition function? If so I don't know much about it.

ScriptRunner has a clone issue function which can accomplish the same thing, there you can provide a closure to control which attachments are copied to the new issue: https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_control_of_cloning_attachments

The closure would be something like:

import com.atlassian.gzipfilter.org.apache.commons.lang.time.DateUtils

checkAttachment = { Attachment attachment ->
    DateUtils.isSameDay(new Date(attachment.created.time), new Date())
}
Maud Schlich January 11, 2016

Well, create-on-transition only either copies all or no attachments, so I have an additional ScriptRunner script added as another post-function. But your sample could be what I want - will check this and come back to you. Thanks for your help.

Maud Schlich January 14, 2016

Hello again,

so I dismissed the create-on-Transition completely and instead do this (right after the indexing):

 

//für Datumsvergleich
Import com.atlassian.gzipfilter.org.apache.commons.lang.time.DateUtils 
//für Pop-up-Nachricht 
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def cf_abw_zsf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Zusammenfassung der Abweichung'} 
issue.summary = sourceIssue.getCustomFieldValue(cf_abw_zsf)
def cf_abw_schritte = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Testschritte der Abweichung'} 
def cf_abw_erwerg = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Erwartetes Ergebnis der Abweichung'}
 def cf_abw_taterg = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Testergebnis der Abweichung'} 
issue.description = 'Durchgeführte Schritte: ' + sourceIssue.getCustomFieldValue(cf_abw_schritte) + ' Erwartetes Ergebnis: ' + sourceIssue.getCustomFieldValue(cf_abw_erwerg)  + ' Tatsächliches Ergebnis: ' + sourceIssue.getCustomFieldValue(cf_abw_taterg)
def cf_abw_anzfehl = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Anzahl Fehlgeschlagen Abweichung'}
//issue.setCustomFieldValue(cf_abw_anzfehl, "0");
//nur Anhänge von heute und vom selben User mitkopieren 
checkAttachment = {attachment -> 
    (DateUtils.isSameDay(new Date(attachment.created.time), new Date()) && attachment.authorKey == issue.reporter) }
//Keine Links mitkopieren 
checkLink = {link -> false}
UserMessageUtil.success('Abweichung erstellt')

 

 

As you see, I have commented line 12 because that is still incorrect (am not yet sure, why), but the closure is now "all Attachements from today from the current user" - right? So still not the last one? Any further hint?

Thanks again - I haven't been coding that much for many years so this is some exciting challenge.

 

I am currently struggling with variants of this like:

 

checkAttachment = {attachment ->  
 (DateUtils.getDuration((new Date() - new Date(attachment.created.time)) <= 7200) && (attachment.authorKey == issue.reporter)) }

 but can't get it working. Would appreciate your help very much here smile

0 votes
Maud Schlich January 11, 2016

I have an issue type named testlog. And a transition from "in test" to "failed" via "report failure". In this transition I not only change the status of test log and some custom fields but also use a create-on-transition (Bob Swift) to create another issue of type "bug". This issue should not inherit all attachements from the testlog but only the latest which typically is some screenshot showing the bug. I also have a transition from "failed" to "failed" via "report another failure" which also does the create-on-transition and should attach the last attachement (so the fourth reported bug only gets the very latest attachment and not the ones from the bugs before). Hope it is clearer now. Thanks for trying to help me.

0 votes
Thanos Batagiannis _Adaptavist_
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 11, 2016

Hi Maud, Could you please give an example of what you want to do ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events