It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Set different User from Custom Field to Automation Script Edited

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

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.

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

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);

Dear Joanna,

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

 

Peter

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Marketplace Apps & Integrations

Join our Webinar: 5 use cases to supercharge Jira with apps

G’day Atlassian Community! We’re excited to announce that we’ll be doing a new webinar on 5 use cases to supercharge Jira Cloud. The Atlassian Marketplace offers customers thousands of apps and int...

130 views 1 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you