Hello,
I am looking for automation which would clone newly created ticket based on the custom field value. I am using Listeners and Clone pre-defined template with the code:
---
package com.workflow.listener
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
// LOGGING INFO
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//def issueKey = issue.getKey()
log.info("Checking Code Coverage Flag for issue ${issue.getKey()}")
// TRIGGER
//issue.issueType.name == 'Bug'
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Test Coverage'}
cfValues['Test Coverage'] == '?'
--
But when a new issue is created with this custom field and value, a Clone issue is not created :( If I am using just type of issue, it is working ...
In both cases, no issues in the log - showing the last create issue that was checked.
2019-08-20 11:44:19,221 INFO [runner.ScriptRunnerImpl]: Checking Code Coverage Flag for issue RHV-6488
Any hint, suggestion, please?
Thanks, Peter-Dave, but still no success :(
I am trying to create a Custom listener, but again, no success yet.
Any idea? Thx Frank
--- My code ---
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger
// LOGGING INFO
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//def issueKey = issue.getKey()
log.info("Checking Code Coverage Flag for issue ${issue.getKey()}")
public class myClass {
// Your local CustomFieldManager
private final CustomFieldManager myCustomFieldManager;
// Class constructor requesting the injected CustomFieldManager
public myClass(CustomFieldManager injectedCustomFieldManager) {
this.myCustomFieldManager = injectedCustomFieldManager;
}
public void myMethod() {
CustomField customField = myCustomFieldManager.getCustomFieldObjectByName("Test Coverage");
Object customFieldValue = customField.getValue(issue)
// TRIGGER
if (customFieldValue.value.contains("?"))
create.newissue {
issue.summary = 'Cloned issue'
}
}
}
Good luck with that one... I can't help you there.
Creating issues from scratch is more complicated and there are other better examples that what I could provide in the community. It will involve the issueFactory service.
I'd recommend you stick with the built-in "Cones and issue, and links" listener rather than a custom one.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, Peter-Dave, I would love to stay with "Clone listener" but it is not working for some reason.
If I am adding this code to the Condition Field:
def originalIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issue.getKey())
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)
cfValue == '?'
---
Nothing is cloned :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Send some messages to the log and find out why it isn't working...
import com.atlassian.jira.component.ComponentAccessor
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)
log.info "Test Coverage Value: $cfValue"
log.ingo "Test Coverage equals '?': ${cfValue == '?'}
cfValue == '?'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks. Receiving the error on line 8
Script410.groovy: 8: expecting anything but ''\n''; got it anyway @ line 8, column 54. equals '?': ${cfValue == '?'} ^ 1 error </pre>.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just removed the line #8 from the script and run it, but there is no information in the log.
Time (on server): Tue Aug 27 2019 15:55:25 GMT+0200 (Central European Summer Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
No logs were found for this execution.
Just from Payload:
"Test Coverage": "? (com.atlassian.jira.issue.customfields.option.LazyLoadedOption)",
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I type this code directly in the message box... you have you do a bit of your own syntax checking. Looks like I forgot to close the double quotes.
But your last message gives use the real reason ... you never mentioned that the Test Coverage field is a single select. Single select don't store string values, they store "Options". So try this
import com.atlassian.jira.component.ComponentAccessor
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)?.value
log.info "Test Coverage Value: $cfValue"
log.ingo "Test Coverage equals '?': ${cfValue == '?'}"
cfValue == '?'
Also, look in your atlassian-jira.log, sometimes logs don't appear in scriptrunner execution history.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Apologize me Peter-Dave, I am new to scripting and groovy too. I appreciate your help a lot! Thank you for patience with me.
I tried to repair after I posted it ... learning by doing. :)
With this update, it is WORKING! Great, many thanks.
Just if the Events is set to "Issue Created" - logically the listener started to generate new issue in the row.
So is there any limit, how to set that only when issue type "bug" is created as I am duplicating bug to the task type?
Like:
def issueManager = ComponentAccessor.issueManager
issueType = issue.getIssueType();
IssueType = issue
if ((issue.getIssueType() = "bug"))
// Checking if cf Test Coverage has value "?"
import com.atlassian.jira.component.ComponentAccessor
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)?.value
log.info "Test Coverage Value: $cfValue"
log.ingo "Test Coverage equals '?': ${cfValue == '?'}"
cfValue == '?'
// not
} else
return false;
{
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
issue.issueType is an issueType object ... so you'll need to compare the name.
import com.atlassian.jira.component.ComponentAccessor
if( issue.issueType.name.toLowerCase() == 'bug' ){
// Checking if cf Test Coverage has value "?"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)?.value
cfValue == '?'
} else {
false
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I did some updates, but still a lot of errors there :(
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
UserManager userManager = ComponentAccessor.getUserManager();
MutableIssue issue = issue
// Condition that only Bug with CF and "?" will be duplicated
if(issue.issueType.name == "Bug" ){
log.info "Start of clone issue listener on issue: $issueType.name"
// Checking if cf Test Coverage has value "?"
log.info "Start of clone issue listener on issue: $event.issue"
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
log.info "Custom Field object found: $cf.name"
def cfValue = event.issue.getCustomFieldValue(cf)?.value
log.info "Test Coverage Value: $cfValue"
log.info "Test Coverage equals '?': ${cfValue == '?'}"
cfValue == '?'
// not a Bug - do nothing
}else {
return false;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Peter-Dave - you made my day!!! Fabolous, it is working as I need exactly.
Many, many thanks. I played with the code previously and didn't realize that you already corrected it. Once again, big thanks and have a great day too!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad I could help.
Please mark the question as answered so others can find it too.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Peter-Dave, if I will add additional condition if Component is not there, what's wrong on this code, please?
// Checking if cf Test Coverage has value "?" & Component is not Documentation
if( issue.issueType.name.toLowerCase() == 'bug' && ! issue.components*.name.contains('Documentation'){
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)?.value
cfValue == '?'
} else {
false
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I see two errors in this:
1) your if block is missing a closing parens
2) your component check will fail where there are zero component because *.name will be null and therefore cannot evaluate the contains condition.
This should work:
// Checking if cf Test Coverage has value "?" & Component is not Documentation
if( issue.issueType.name.toLowerCase() == 'bug' && ! issue.components.any{it.name.contains('Documentation')}){
def customFieldName = "Test Coverage"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = event.issue.getCustomFieldValue(cf)?.value
cfValue == '?'
} else {
false
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Many thanks, Peter-Dave, for those corrections. It is very good learning for me when you are making points like that to understand groovy scripting better.
Many thanx again!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Are you using this code in the "Condition" field of the Clones and Issue and links built-in script listener?
Or somewhere else?
Can you share more of your configuration?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Peter-Dave,
yes I used Clones and Issue and links built-in script and with those "Condition" in the field :
--
package com.workflow.listener
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
// LOGGING INFO
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
//def issueKey = issue.getKey()
log.info("Checking Code Coverage Flag for issue ${issue.getKey()}")
// TRIGGER
//issue.issueType.name == 'Bug'
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Test Coverage'}
cfValues['Test Coverage'] == '?'
---
BECAUSE when I used only "Condition" per Example "Has string custom field value equal to" - where is the code
"cfValues['Some Custom Field'] == 'Some Value'"
and my case is
cfValues['Test Coverage'] == '?'
It results in result:
2019-08-21 11:02:57,233 WARN [utils.LazyCustomFieldsMap]: Did not find requested custom field Some Custom Field on issue RHV-15021
So it means, that I need to add into conditions
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
And this help to fix the issue with the error in log.
But Copy of the issue was not created neither.
Maybe issue with value "?" for custom field?
Thanks, Frank
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't think the cfValues variable is available in listeners. This is something that's exposed only on workflows post function
So you'll need to fetch custom field and custom field values manually.
In the listener, if you've selected to fire on any issue events, you'll have access to the issue with event.issue.
To get the value of the custom field from that issue, call
def cfValue = event.issue.getCustomFieldValue(cf)
then, you can check for
cfValue == '?' to trigger your listener action
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.