Hallo,
I would like to get the values of two custom fields that both are User Pickers. What do I have to change in the following code to accomplish this?
import com.atlassian.jira.ComponentManager def componentManager = ComponentManager.instance def customFieldManager = componentManager.getCustomFieldManager() def cfX = customFieldManager.getCustomFieldObjectByName("X") def cfY = customFieldManager.getCustomFieldObjectByName("Y") if(cfX == null && cfY == null ){ true } else if(cfX != null && cfY == null ){ if(cfX == currentUser){ true } else { false } } else if(cfX == null && cfY != null ){ if(cfY == currentUser){ true } else { false } } else if(cfX != null && cfY != null ){ false }
Community moderators have prevented the ability to post new answers.
If you want to have the value of the customfield you have to use issue.getCustomFieldValue(cfX).
Henning
Uups... I edited the answer... as Jamie mentioned you can use cfX.getValue(issue). But i prefer the issue.getCustomFieldValue(cfX) way. Don't rember exactly why...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yep, with an issue as the parameter.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There is a difference actually, can't remember what it is off top of my head... I would use issue.getCustomFieldValue(cf) too.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the answers. I want to add this script to some transitions as a condition. The following code only works if I add it as a "Script Condition"-> "Simple scripted condition", but if I try to add it as a file "Script file path:" it doesn't work at all. It think the reason is the "issue". My file path: C:\Program Files\Atlassian\JIRA\groovy\reviewer.groovy
import com.atlassian.jira.ComponentManager def componentManager = ComponentManager.instance def customFieldManager = componentManager.getCustomFieldManager() try{ obTechReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Technical Reviewer'} cfTechReviewer = issue.getCustomFieldValue(obTechReviewer) obFuncReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Functional Reviewer'} cfFuncReviewer = issue.getCustomFieldValue(obFuncReviewer) // no reviewer entered for this issue if(cfTechReviewer == null && cfFuncReviewer == null ){ true // technical reviewer and no functional reviewer entered for this issue } else if(cfTechReviewer != null && cfFuncReviewer == null ){ // check if currentuser is the technical reviewer for this issue if(cfTechReviewer == currentUser){ true } else { false } } else if(cfTechReviewer == null && cfFuncReviewer != null ){ // check if currentuser is the functional reviewer for this issue if(cfFuncReviewer == currentUser){ true } else { false } } else if(cfTechReviewer != null && cfFuncReviewer != null ){ // the reviewer have to be edited for this issue false } } catch (NullPointerException e){ true }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If it couldn't open the file it will be in the logs...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issue for class: Script3
I get the following exception if I run it in the plug-in Script Runner.
There must be a way to get the issue id. But I need it for all the issues in a project not only for one issue. So something like "getIssueObject("XY-123")" won't work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you use the script as a condition, issue exists. If you run it from the console, issue doesn't exist. You can use
issue = componentManager.issueManager.getIssueObject('XY-123')
to test your script in the console. If you want to run the script in the console for all issues of a project look here.
Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tested it for one issue. Console doen'st throw any exceptions with the follwoing code:
import com.atlassian.jira.ComponentManager import com.atlassian.crowd.embedded.api.User 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 def componentManager = ComponentManager.instance def customFieldManager = componentManager.getCustomFieldManager() UserUtil userUtil = ComponentAccessor.getUserUtil() User currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() if (!currentUser) { currentUser = userUtil.getUserObject('jira_bot') } def issue = componentManager.issueManager.getIssueObject('ORD-5') try{ (...code...) } catch (NullPointerException e){ true }
Now my problem is that if I put it as a condition like in the following image, it works:
But if I try to load it from a file like in the following image, it doesn't work:
The file can be found. The console don't throw any exceptions. So I have no ideas why it doesn't work.
PS: What I want with this code is to prevent a issue to be closed if a Technical or Functional Reviewer is entered. And if the currentUser is the reviewer it can be closed. Make it even sense to put this in a file? The only reason I want to put this in a file is to make it easier to do changes if needed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Make sense... can you insert log.error statements into the file to test if the script is called?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Wonder if jira is running as LocalSystem and can't see files under there. In the admin script console run:
new File("c:/program files/whatever/reviewer.groovy").text
and see whether it returns the text of the file when you run it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Henning:
I added this:
(...imports...) import org.apache.log4j.Category def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction") log.setLevel(org.apache.log4j.Level.DEBUG) log.debug "test reviewer" (...code...)
But I have no idea where the log is located to check if "test reviewer" was written in it. I don't use an IDE.
If I run the script in the "Script Runner" it works. There are no problems with running the script from path.
@Jamie:
Where do I find the adminscript console?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It;s in the screenshot above. Did you try my code? Enter only the code in the bottom textarea, not a file path as well. If you enter both the file wins.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The log is located within your jirahome directory under log/atlassian-jira.log
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jamie:
Your code works. It shows me my code as a text.
@Henning:
This is what the log shows me after I changed the Issuestatus to "Start Progress" and then back to "Stop Progress".
2013-03-22 14:56:12,809 http-bio-8080-exec-8 DEBUG tiwaniec 896x711x1 laniyw 0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.PostFunction] test reviewer
2013-03-22 14:56:12,980 http-bio-8080-exec-8 DEBUG tiwaniec 896x712x1 laniyw 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] test reviewer
2013-03-22 14:56:13,869 http-bio-8080-exec-8 DEBUG tiwaniec 896x713x1 laniyw 0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.PostFunction] test reviewer
2013-03-22 14:56:14,025 http-bio-8080-exec-8 DEBUG tiwaniec 896x714x1 laniyw 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] test reviewer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yep I did it. here the code with more log.debug entries:
If I move in workflow the log shows me the following. That is exactly the result I wanted to get, but still I can close the Issue even I'm not the reviewer.
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] started log 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: issue 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: ComponentManager and CustomFieldManager 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: UserUtil 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: check currentUser 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] entered try block 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: getting Technical Reviewer field 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: getting Functional Reviewer field 2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] func reviewer isnt null and currentuser isnt reviewer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, both ways show your code is executed :-) So move on and add more debugging information to see what your code calculates...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you try to save your return value to a variable and return this variable as last step?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm not sure how to do this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Add a
def result = false
at the beginning of your script. Then replace all "true" or "false" with "result = true" or "result = false"
and at the end write
return result
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It doesn't help. If I put it as simple script it works...if I put it as script file path it doesn't work. The log for both variations is equal.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Mmh, weird.
@Jamie, any ideas?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry you don't need to return it as such, just set it. It will be read out of the script binding when your script has finished evaluating. Again, the examples should help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah, you need to set passesCondition to a boolean and return than. Don't define passesCondition, just set to true or false, and return that as the last line.
In the built-in script called "simple scripted condition" you just return true or false, but if you're specifying a file you need it - see https://jamieechlin.atlassian.net/wiki/display/GRV/Conditions
I can see how this might be confusing... and sorry for not pointing this out earlier. I think "simple scripted condition" was my attempt to abstract away some of the detail.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This was it! Thank you both for the help.
Here the final script:
import com.atlassian.jira.ComponentManager import com.atlassian.crowd.embedded.api.User 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 Issue issue = issue def componentManager = ComponentManager.instance def customFieldManager = componentManager.getCustomFieldManager() UserUtil userUtil = ComponentAccessor.getUserUtil() User currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() if (!currentUser) { currentUser = userUtil.getUserObject('jira_bot') } try{ obTechReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Technical Reviewer'} cfTechReviewer = issue.getCustomFieldValue(obTechReviewer) obFuncReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Functional Reviewer'} cfFuncReviewer = issue.getCustomFieldValue(obFuncReviewer) // no reviewer entered for this issue if(cfTechReviewer == null && cfFuncReviewer == null ){ passesCondition = true // technical reviewer and no functional reviewer entered for this issue } else if(cfTechReviewer != null && cfFuncReviewer == null ){ // check if currentuser is the technical reviewer for this issue if(cfTechReviewer == currentUser){ passesCondition = true } else { passesCondition = false } } else if(cfTechReviewer == null && cfFuncReviewer != null ){ // check if currentuser is the functional reviewer for this issue if(cfFuncReviewer == currentUser){ passesCondition = true } else { passesCondition = false } } else if(cfTechReviewer != null && cfFuncReviewer != null ){ // the reviewer have to be edited for this issue passesCondition = false } } catch (NullPointerException e){ passesCondition = true }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good work ;-)
Thanks to Henning for adding in the diagnostics advice.
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.