I am trying to concatenate some text to a comment entered by the user. How can I do that? Here is my code below. I need to access the comment input by the user and then add new text to it. How can I access this? I am using Jira ScriptRunner custom postfunction to be executed when the user clicks on a transition.
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
def log = Logger.getLogger("atlassian-jira.log")
log.warn("This is the last action ")
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();
JiraWorkflow workflow = workflowManager.getWorkflow(issue);
List <Object> actions = workflow.getLinkedStep(issue.getStatus()).getActions();
def wfd = workflow.getDescriptor()
def actionName = wfd.getAction(transientVars["actionId"] as int).getName();
log.warn("This is the last action "+actionName)
def comment= "+++ added via workflow action "+"\""+actionName+"\"+++"
Here is the code transientVars is the way to go.
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
def log = Logger.getLogger("atlassian-jira.log")
log.warn("This is the last action ")
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();
JiraWorkflow workflow = workflowManager.getWorkflow(issue);
def wfd = workflow.getDescriptor()
def actionName = wfd.getAction(transientVars["actionId"] as int).getName();
log.warn("This is the last action "+actionName)
def comment= "+++ added via workflow action "+"\""+actionName+"\"+++"
String content = transientVars["comment"] +"\n"+comment as String
log.warn("CONTENT"+ content)
transientVars["comment"]= content
Related
I have a Groovy script post-function using Adaptavist ScriptRunner in JIRA. In the script, I look if a checkbox choice is marked on the issue. If so, I want to prefill the custom field Project Manager on the Issue with the value John Smith. The checkbox portion of the code works fine and the script runs to completion without error, but the issue itself does not register the update. I put this together from other accepted examples on this site and the web.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager();
CustomField cf2 = cfm.getCustomFieldObjectByName("Project Manager");
def usermanager = ComponentAccessor.getUserManager()
def webmgr = usermanager.getUserByName("John Smith")
issue.setCustomFieldValue(cf2, webmgr);
cf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf2), webmgr),new DefaultIssueChangeHolder());
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, true);
IssueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,true);
It wanted the username versus the formal name.
def webmgr = usermanager.getUserByName("johnsmit")
Hello we are using Jira and are currently evaluating the Plugin "Scriptrunner for Jira" by Adaptavist.
I'd like to create a custom Listener which simply updates the value of a custom field. The field's type is a default textbox, nothing fancy there.
Regarding to the plugin's documentation and various web-searching, I came up with the following code:
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
def issue = event.issue as Issue
MutableIssue issueToUpdate = (MutableIssue) issue;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'My CustomField'}
issueToUpdate.setCustomFieldValue(cf, "myvalue");
The validator does not complain about anything here and the script seems to be executed without any errors. The problem is that the custom field's value is simply not updated. Maybe some of you guys have the missing piece.
Every line seems to be needed as the validator complains otherwise. Thank you in advance for your help.
I just got an answer from Adaptavist that is finally working. Please find the working code below:
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "My CustomField"}
def changeHolder = new DefaultIssueChangeHolder()
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), "myvalue"),changeHolder)
In my Jira instance, I created a Script Listener for the IssueUpdated event using the ScriptRunner add-on and I'm trying to get a list of the changed fields. For some reason the method getModifiedFields() is coming empty, somebody can help me to fix that?
import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
log.setLevel(org.apache.log4j.Level.DEBUG)
Issue mainIssue = event.issue
MutableIssue mutableIssue = (MutableIssue)mainIssue
def modFields = mutableIssue.getModifiedFields()
log.debug("Modified fields count: "+modFields.count) // null
log.debug("Modified fields: "+modFields.toString()) // [:]
log.debug("Original Ticket: "+mainIssue.key) // EPS-39
After turning google upside down I've found the answer to my question. More background here: my final goal is to update a target Jira ticket (in another project) with whatever was changed in the main ticket. I have a custom field that contains the original ticket key, so I can track it down (ex: ticket "PRJ-1" -> "Original Ticket": "TRG-1"). To do this I'm going to ScriptRunner -> Script listeners -> custom listener and adding an "Issue Updated" listener.
I'm still stuck in the part where I have a custom label field as you can see by the comments in the end of the code, but at least I could manage to get a list of the changed fields in my main ticket.
import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.DefaultIssueService
log.setLevel(org.apache.log4j.Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
Issue mainIssue = event.issue
// Get Custom Field Object "Original Ticket"
def originalTicket = customFieldManager.getCustomFieldObjectByName("Original Ticket")
// Get Value of the Custom Field Object "Original Ticket"
String targetTicketKey = mainIssue.getCustomFieldValue(originalTicket).toString()
// Get Original Ticket Object based on the custom field value
def targetIssue = ComponentAccessor.getIssueManager().getIssueObject(targetTicketKey)
// Get list of modified values in the original ticket to update target ticket
List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()
Object oldValue
Object newValue
def userManager = ComponentAccessor.getUserManager()
def auser = userManager.getUserByKey("Sync User")
def issueManager = ComponentAccessor.getIssueManager()
CustomField custom
// Loop for all the changed fields
while (changeItemListIterator.hasNext()) {
GenericValue changeItem = (GenericValue)changeItemListIterator.next()
String currentFieldName = changeItem.get("field").toString()
log.debug("Current field: "+currentFieldName)
oldValue = changeItem.get("oldstring")
newValue = changeItem.get("newstring")
if (oldValue != null && newValue != null){
log.debug("Field changed from: "+oldValue+" to "+newValue)
switch (currentFieldName){
case "summary":
log.debug("Found switch: Summary")
targetIssue.setSummary(newValue.toString())
break
case "description":
log.debug("Found switch: Description")
targetIssue.setDescription(newValue.toString())
break
case "Affected Version(s)":
log.debug("Found switch: Affected Version(s)")
// This is a label field. I'm stuck here and I don't know how to manipulate the value.
// Labels are a set type, studing more about it.
custom = customFieldManager.getCustomFieldObjectByName("Affected Version(s)")
targetIssue.setCustomFieldValue(custom,newValue)
break
default: log.debug("Not found: "+currentFieldName)
}
}
// Update my target Issue (in another project) that I'm trying to synchronize with the main issue.
issueManager.updateIssue(auser, targetIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false)
}
mutableIssue.modifiedFields used only in scripted Validators.
Iterating through modified fileds in scripted Listener:
List<HashMap<String, Object>> fieldsModified = event.getChangeLog()?.getRelated('ChildChangeItem') as List<HashMap<String, Object>>
for (HashMap<String, Object> field : fieldsModified)
log.debug("Field: ${field["field"]}, old value: ${field["oldstring"]}, new value: ${field["newstring"]}.")
Affected versions is a system field like Summary and Description. Use issue.affectedVersions to update it.
Just need a little direction in finishing my script, I need to gather the values of several radio button fields and perform an if/then condition to determine the default radio button value of one other. This is what I have so far, is this the correct approach? What am I missing? This is a custom field script using Groovy in ScriptRunner.
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
//managers
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager();
//gather the fields needed
def field1 = CustomFieldManager.getCustomFieldObjectByName("Field 1")
def field2 = CustomFieldManager.getCustomFieldObjectByName("Field 2")
//gather the values of the fields
def field1Value = issue.getCustomFieldValue(Field1).getValue()
def field2Value = issue.getCustomFieldValue(Field2).getValue()
if (field1Value == "Agree" && field2Value == "Agree"){
def field3 = customFieldManager.getCustomFieldObjectByName("Field 3")
issue.setCustomFieldValue(field3, "Agree")
} else {
issue.setCustomFieldValue(field3, "Disagree")
}
Figured out what the issue was...Emmanual was correct with the context as I was running this in Scriptrunner that was not implemented with Behaviours nor was it added in the context of a 'Scripted Field' which is required if you're going to call the Issue object within the context of that issue.
I want to get some pointer to write a simple JIRA groovy scripted field – the input is a JQL and the result is the result of the JQL.
For example, if the JQL is "project = RS and fixVersion = 5.0", it will go ahead a list the issues returned from this JQL in the custom field display.
First I created a JIRA field called "Fixed Issues JQL", which supposed I will enter the value of "project = VOL and fixVersion = 6.0" in the JIRA.
Then I create a second JIRA custom field , a groovy scripted field called "Fixed Issues List", which contain the following code:
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Fixed Issues JQL")
def myJQL = issue.getCustomFieldValue(cf) // has a value such as "project = VOL and fixVersion = 6.0"
if (!user) {
user = userUtil.getUserObject('kwhite')
}
List<Issue> issues = null
SearchService.ParseResult parseResult = searchService.parseQuery(user, myJQL)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
// Transform issues from DocumentIssueImpl to the "pure" form IssueImpl (some methods don't work with DocumentIssueImps)
issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
} else {
log.error("Invalid JQL: " + myJQL);
}