Script runner to pull user details for last field data change?

Tristan Leask May 26, 2017

Hi All,

I have been trying to figure out how I can use Script Runner on a Jira Server 6.3 to show a calculated value which is based on a field that has been edited by a user.

Basically I have one field that a user can edit, and by all accounts, if a user edits that field, the change is record along with the details of the user who edited it (as you can see this in the history of the jira issue).

So, I want to pull that data for that last cange of that field and display the user name in a Groovy Scripted Field.

Is this even possible?  I am ssuming I would need to run some sort of Query in my script which will return a user object type, or even just some text, and then use that result to display in the custom Scripted FIeld.

2 answers

1 accepted

0 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
May 29, 2017

Hi Tristan,

You can create a Scripted Field and it's script will be 

import com.atlassian.jira.component.ComponentAccessor

def changedItem = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)?.last()
"Field ${changedItem?.field} updated from user ${changedItem?.getUserKey()}"

And then in your issue view screen will get something like 

Screen Shot 2017-05-29 at 17.32.36.pngIs this what you were looking for ?

Tristan Leask May 30, 2017

Hi Thanos,

Almost.  That gives the last changed field and the user linked with the change.  I am looking for a specific field.

Ive looked through the API and a couple of examples, and coined something together, but I don't think its the best way to do this, nor do I think it works.  How do you go about adding logging to these scripts?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.changehistory.ChangeHistory
import com.atlassian.jira.issue.history.ChangeItemBean


ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

String fieldName = 'My Custom Field'
String lastUser = 'None'

List<ChangeHistory> changeHistory

changeHistory = changeHistoryManager.getChangeHistories(issue)
changeHistory.sort{it.getTimePerformed()}

for(ChangeHistory change: changeHistory){
    for(ChangeItemBean changeItemBean: change.getChangeItemBeans()) {
        if (changeItemBean.getField().equals(fieldName)){
          lastUser = change.getAuthorDisplayName()
        }
    }    
}

return lastUser

Thanks

Thanos Batagiannis _Adaptavist_
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.
May 30, 2017

Hi Tristan,

Fair enough, then you will need 

import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.*

def changedItem = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)?.findAll {it.field == PRIORITY}?.last()
"Field ${changedItem?.field} updated from user ${changedItem?.getUserKey()} from ${changedItem?.from} to ${changedItem?.to}"

Regarding your question about logging, have a  look at https://www.adaptavist.com/doco/display/SFJ/Set+logging+to+help+debug+your+scripts 

Tristan Leask May 30, 2017

Brilliant!  Thanks, that is a much simpler bit of scripting.

Ash Ray May 31, 2018

@Thanos Batagiannis _Adaptavist_ thanks for the code 

how do we get Display name instead of username?

SCK September 10, 2021

Hi @Thanos Batagiannis _Adaptavist_ Thank you for sharing your knowledge, I have question

I get a user key instead of username

Field Indicator updated from user: JIRAUSER13900 from null to Other

In API it says getUsername() Deprecated. How can I get user full name or username? Any suggestion?

Ramashry Ramashry March 27, 2024

Any suggestions on getting user name instead of getting user key? 

0 votes
Tukaram Bhukya October 27, 2017

Please help me , how to stop logging below message to jira  LOG FILE for above script ?

Script field failed on issue: SSD-1, field: My scripted field
java.util.NoSuchElementException: Cannot access last() element from an empty List
at Script50.run(Script50.groovy:6)

Thanos Batagiannis _Adaptavist_
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.
October 30, 2017

Hey Tukaram,

Can you please make sure that you did not forget to add the safe navigation operator (?) before the last() ?

regards, Thanos

Ramashry Ramashry March 27, 2024

@Thanos Batagiannis _Adaptavist_ 

Hi Thanos, 

Thank you for providing a good script really appreciate your support but I have a different problem that i need the name of user who update the issue last time and it should include the comment section as well like through your script i am geting the name of user who are updating any field in my issue but it is not working when someone comment on the issue could you please help me with this?

 

Suggest an answer

Log in or Sign up to answer