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
What is the context that your script currently runs in? It's possible that you have access to the current issue in the bindings.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Joanna,
do you have any solution for my problem? Many thanks...
Peter
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Peter,
In what way specifically does it not work?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That means, that no work will logged, when I use this: \"user\": \"${user}\"
With a real username like \"user\": \"Tester1\" will then be logged.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, it works...but, I see an error (red cross) in the script console: No such property: username for class: java.lang.Object
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yaeh...finally, the script is working! Many thanks for the great job, Joanna. I'm verry happy about this...
best regards
Peter
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.