Hi guys,
I do not have experience with scripting, and not sure if this can be done by some add-in, so I want to ask if anyone of you has done something similar or can recommend some approach:
This is on Cloud.
Thank you in advance for your response.
Regards,
Klime
Hi,
You could achieve this using ScriptRunner for JIRA Cloud and the REST APIs for JSD.
In your post function on the Create Issue transition, you would need to write a script a bit like this:
def creatorEmailAddress = issue.fields.creator.emailAddress def creatorDomain = creatorEmailAddress.split('@')[0] // We need to retrieve a list of the existing organisations to compare // the email domain // NB - You might need to paginate this request, see the documentation def orgReq = get("/rest/servicedeskapi/organization") .asObject(Map) assert orgReq.status == 200 def existingOrganisations = orgReq.body.values // I've assumed here that the name of the organisation will match the // domain exactly, but you might need different logic here def organization = existingOrganisations.find { it.name == creatorDomain } // Create the organization if it doesn't exist if (organization == null) { def createReq = post("/rest/servicedeskapi/organization") .header('Content-Type', 'application/json') .body([ name: creatorDomain ]) .asObject(Map) assert createReq.status == 201 organization = createReq.body } // Now we add the user def addReq = put("/rest/servicedeskapi/organization/${organization.id}/user") .header('Content-Type', 'application/json') .body([ usernames: [ issue.fields.creator.name ] ]) .asString() assert addReq.status == 204
For your other question, you can definitely do that with ScriptRunner too. On the relevant transition you would need a script a little bit like this:
// Get the field ids def fields = get('/rest/api/2/field') .asObject(List) .body as List<Map> def organizationFieldId = fields.find { it.name == "Organization" }.id def otherCustomFieldId = fields.find { it.name == "Copy of Organization" }.id // This would need changing to meet your specific requirements def targetIssueToUpdate = "EXAMPLE-1" def organization = issue.fields[organizationFieldId] def updateReq = put("/rest/api/2/issue/${targetIssueToUpdate}") .header('Content-Type', 'application/json') .body([ fields: [ (otherCustomFieldId): estimates['Development Sub-Task'] ]) .asString() // check that updating the issue worked assert updateReq.status == 204
I hope that helps,
Jon
EDIT: fixed bugs in code
If only REST calls to the Server version were this easy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jon,
Tried the first script on Create transition, and I found following errors:
CorrelationId: 7b06ad8b-2f6f-424a-9352-6d666191f75a RUN Script identifier: 3946e851-3ec9-4faa-84eb-3b4f0dc9e847 com.adaptavist.sr.cloud.workflow.RunScript ('Create' transition made for issue UTP-9) Took 100ms Logs: 2017-02-03 18:57:49,044 ERROR - Cannot get property 'emailAddress' on null object 2017-02-03 18:57:49,055 ERROR - Class: com.adaptavist.sr.cloud.workflow.RunScript, Config: [className:com.adaptavist.sr.cloud.workflow.RunScript, uuid:3946e851-3ec9-4faa-84eb-3b4f0dc9e847, description:Organization from Email, condition:, executionUser:ADD_ON, additionalCode:def creatorEmailAddress = issue.creator.emailAddress
Not quite sure how to bypass this...
Thanks in advance.
Regards,
Klime
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Try configuring the post function to run after the issue index event:
postfunction-ordering.png
Thanks,
Jon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Jon,
Tried the suggestion - moved the post-function after the indexing, but got again the same errors.
https://drive.google.com/open?id=0B3wqCCkDvaZNX196VWsyTW1UX3M
https://drive.google.com/open?id=0B3wqCCkDvaZNMVpFM3RwTGtJejQ
There are errors in the code window where the variables are defined.
Sorry again for bothering.
Thanks,
Klime
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Apologies Kilme, the first line of the code needs to be:
def creatorEmailAddress = issue.fields.creator.emailAddress
Given you are not using this post function on the Create transition you should be able to move it back to the top of the list of post functions.
Also, some of the errors reported in the code editor are misleading, and we're working on removing them.
Thanks, Jon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks again Jon,
I moved a little bit forward now, got the message as on the screenshot.
https://drive.google.com/open?id=0B3wqCCkDvaZNS2U5dkRyRzl1ZDQ
Tried few combinations in line23 (changed to id.name) and line35 (changed to issue.fields.creator.name), but got other errors, so I returned it as it was - as on the screenshot
Thanks,
Klime
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Line 35 needs changing to:
issue.fields.creator.name
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jon,
I have applied this change, and now if I execute with the Add-on user, I get this error:
2017-02-07 17:56:02,097 WARN - PUT request to https://scriptrunner.connect.adaptavist.com/jira/proxy/rest/servicedeskapi/organization/{organization.id}/user returned an error code: status: 401 - Unauthorized
When I go with the Initiating user, I get this one:
2017-02-07 18:03:59,645 WARN - PUT request to https://scriptrunner.connect.adaptavist.com/jira/proxy/rest/servicedeskapi/organization/{organization.id}/user returned an error code: status: 403 - Forbidden body: {"error": "Add-on 'com.onresolve.jira.groovy.groovyrunner' blocked from impersonating the user because the access token does not have the required scope(s)"} 2017-02-07 18:03:59,647 INFO - PUT /rest/servicedeskapi/organization/{organization.id}/user asString Request Duration: 188ms 2017-02-07 18:03:59,650 ERROR - assert addReq.status == 204 | | | | 403 false status: 403 - Forbidden
Any suggestions on this one?
Thanks,
Klime
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Klime,
Looks like you found another bug in my code!
That PUT request line should look like:
def addReq = put("/rest/servicedeskapi/organization/${organization.id}/user")
with a $ symbol before the open curly brace.
That should fix the problem I think, as long as you run this as the ScriptRunner Add-on user, but you'll need to make sure that that user already has permission to create organisations in JSD.
Thanks, Jon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jon, just to say thank you for all of your effort on trying to help me with this.
I got the same errors with the latest suggestion, and the client unfortunately gave up from this feature.
Thank you again.
Regards,
Klime
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Klime,
Nice automation and a nice answer from Jon. We want to include an organisation example in our documentation for ScriptRunner for server and your first use case is really interesting.
I have a question though. What if the customer's email is a gmail or hotmail ? This will create a Gmail organization and if another customer with a gmail account creates an issue then you will end up having a Gmail organization and it's members being completely strangers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Was a full tutorial created for this by chance?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thanos,
You can use this case freely in your documentation, however, I did not make it work in my environment.
Jon did a great job trying to help - I am sure it is some minor change, but the client gave up from this.
Thank you all for your engagement.
Regards,
Klime
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for your prompt answer Jon.
I will give it a try..
Thanks again
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.