Set different User from Custom Field to Automation Script

Peter Kaufmann April 4, 2019

Hello,

I have the script to implement the Add-on "Log work as another user" with "Automation for Jira" and it works fine. But the User that logged the time is everytime the same. How can I change the script, that is picked the user from a custom field (not the assignee) and write it in this place where is the user for the work log?

Here the lines in my script that I want to change:

String json = "{ \"user\": \"Tester1\", \"timeSpent\": \"2h 22m\", \"startDate\": \"2019-04-02T12:00:00+02:00\", ";
json += "\"adjustEstimate\": \"auto\", \"comment\": \"This is an automation work log\", \"issueId\": \"TEST-123\" }";

 

\"Tester1\" ---> this is the user that i want to change like {{customfield_123456}}

also, I want to change the following fields:

\"2h 22m\" ---> {customfield_456789}

\"2019-04-02T12:00:00+02:00\" ---> {now} or {today}

\"TEST-123\" --->{current_issue}

 

but I think it will be the same like the customfield of the User-picker...or not?

 

Thanks for helping me...

best regards, Peter

 

 

1 answer

1 accepted

0 votes
Answer accepted
Joanna Choules
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.
April 4, 2019

What is the context that your script currently runs in? It's possible that you have access to the current issue in the bindings.

Peter Kaufmann April 4, 2019

Hello Joanna,

the script runs from an admin with access to all issues.

I can enter any singel user in the script ( \"user\": \"Tester1\" or \"user\": \"Tester2\") and it works. A work log as this user will created. But my aim is, that the script read an user from a custom field in the current issue and when the rule is running, the work log should written with the user from that custom field.

Joanna Choules
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.
April 4, 2019

Hi Peter,

Sorry, I meant to ask which functionality the script was implemented within, rather than which user you're running it as. You said you were using Automation for Jira but can you show me where exactly?

Joanna

Peter Kaufmann April 4, 2019

ah okay...

We are using the Add-ons "log work as another user" and "automation for jira". The Automation Add-on runs a rule once a day and checks a few fields in all issues. If the conditions fullfiled than will run the following script: 

 

import java.net.URL;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;

String login(String jiraBaseUrl, String user, String password) throws Exception {
String authUrl = jiraBaseUrl + "rest/auth/1/session" +
"?os_username=" + URLEncoder.encode(user, "UTF-8") +
"&os_password=" + URLEncoder.encode(password, "UTF-8");

HttpURLConnection connection = (HttpURLConnection) new URL(authUrl).openConnection();
connection.setRequestMethod("GET");

String authCookie = "";
for (Entry<String,List<String>> headers : connection.getHeaderFields().entrySet()) {
String key = headers.getKey();
if ((key != null) && (headers.getKey().equalsIgnoreCase("Set-Cookie"))) {
for (String cookieTmp : headers.getValue()) {
if (StringUtils.containsIgnoreCase(cookieTmp, "JSESSION")) {
authCookie = cookieTmp;
break;
}
}
}
}

connection.disconnect();

if (authCookie.isEmpty()) {
throw new Exception("Login error");
}

return authCookie;
}

int logWork(String authCookie, String jiraBaseUrl, String json) {
URL url = new URL(jiraBaseUrl + "rest/logwork-as-another-user/2.0/logwork");

HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setDoOutput(true);
connection.setRequestProperty("Cookie", authCookie);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();

int responseCode = connection.getResponseCode();
connection.disconnect();

return responseCode;
}

// Usage example ***********************************

String jiraBaseUrl = "https://........../jira/";

// First we need to login using rest API
String authCookie = login(jiraBaseUrl, "admin", "password");

String json = "{ \"user\": \"Tester1\", \"timeSpent\": \"2h 22m\", \"startDate\": \"2019-04-02T12:00:00+02:00\", ";
json += "\"adjustEstimate\": \"auto\", \"comment\": \"this is a comment\", \"issueId\": \"TEST-123\" }";

int responseCode = logWork(authCookie, jiraBaseUrl, json);

println("Response code: " + responseCode);

Peter Kaufmann June 18, 2019

Dear Joanna,

do you have any solution for my problem? Many thanks...

 

Peter

Joanna Choules
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.
June 18, 2019

Hi Peter,

If the script is in an 'Execute a ScriptRunner script' action, then the current issue object should be available in the bindings as the variable 'issue'. From there you can get the various data you need:

import com.atlassian.jira.component.ComponentAccessor

def cfm = ComponentAccessor.customFieldManager
def userCf = cfm.getCustomFieldObject(123456L)
def durationCf =
cfm.getCustomFieldObject(456789L)

def user = issue.getCustomFieldValue(userCf)
def duration = issue.getCustomFieldValue(durationCf)
def key = issue.key
def now = new Date()

Joanna

Peter Kaufmann September 4, 2019

Hi Joanna,

thank you for your answer. Yes, it is in an Execute a Scriptrunner script action.

Where do I have to enter the new values: user, duration, key and now in the follow line: 

String json = "{ \"user\": \"Tester1\", \"timeSpent\": \"2h 22m\", \"startDate\": \"2019-04-02T12:00:00+02:00\", ";
json += "\"adjustEstimate\": \"auto\", \"comment\": \"this is a comment\", \"issueId\": \"TEST-123\" }";

 

thx

Peter

Joanna Choules
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.
September 5, 2019

Hi Peter,

You can insert the values into the json string with interpolation:

String json = "{ \"user\": \"${user}\", \"timeSpent\": \"${duration}\", \"startDate\": \"${now}\", "
//and so on

Note that you may need to do some additional formatting on the values before interpolation in order to make them look more like your original example.

Joanna

Peter Kaufmann September 5, 2019

Hi Joanna,

the duration, key and so on works fine...only the user doesn't work.

def userCf = cfm.getCustomFieldObject("customfield_11205")
def user = issue.getCustomFieldValue(userCf)
String json = "{ \"user\": \"${user}\", \"timeSpent\": \"${duration}h\", ";

 

thanks for your patience...

Joanna Choules
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.
September 5, 2019

Hi Peter,

In what way specifically does it not work?

Peter Kaufmann September 5, 2019

That means, that no work will logged, when I use this: \"user\": \"${user}\"

With a real username like \"user\": \"Tester1\" will then be logged.

Joanna Choules
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.
September 5, 2019

This is probably because the default string rendering of an ApplicationUser object is "username(userkey)". In order to interpolate just the name you should use:

${user.username}
Peter Kaufmann September 5, 2019

Yes, it works...but, I see an error (red cross) in the script console: No such property: username for class: java.lang.Object

Joanna Choules
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.
September 5, 2019

This is because the Groovy type checker does not know ahead of time what kind of object user is, so it cannot be sure that it will possess the username property. So long as the script works correctly at run time then the warning can be safely ignored.

Peter Kaufmann September 5, 2019

Yaeh...finally, the script is working! Many thanks for the great job, Joanna. I'm verry happy about this...

 

best regards

Peter

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events