Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Move worklogs from one issue to another

Patrice Champet
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 10, 2022

Hello,

As Jira Admin, we are, time to times, asked to move worklogs from one issue to another.

In order to answer this need, we started to create a script, manually executed by admin for now.

We have an issue with this script that is working well with only one worklog.
But with a loop on several worklogs, both source and target issue are not up to date.

It looks like some indexing operation is not properly triggered when moving the worklogs.

Do you have any idea or advice to fix this script ?

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.worklog.WorklogManager
import org.apache.log4j.Level

log.setLevel(Level.DEBUG);
log.debug("start")
 
def sourcekey = "ISSUE-8";
def targetkey ="PROJECT-6848";
String dateStart = '20201231';
def usersVisa = ["user1","user2"]
def issueManager = ComponentAccessor.getIssueManager();
 
MutableIssue issue = issueManager.getIssueObject(sourcekey);
MutableIssue newIssue = issueManager.getIssueObject(targetkey);

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
Date date = Date.parse( 'yyyyMMdd', dateStart );

WorklogManager worklogManager = ComponentAccessor.getWorklogManager()
 
List<Worklog> workLogs = worklogManager.getByIssue(issue)

log.debug(workLogs.size())
def count = 0
for(Worklog worklog in workLogs)
{
    worklogManager = ComponentAccessor.getWorklogManager()
    if(
      (worklog.getStartDate() >= date)
        &&
        (usersVisa.size()==0
         ||
        usersVisa.contains(worklog.authorKey.toString().toUpperCase())
        )
    )
    {
       log.debug(worklog.authorKey)
       log.debug(worklog.getStartDate())
       log.debug(worklog.getTimeSpent())
       
        WorklogImpl newWorklog = new WorklogImpl(
                worklogManager,
                newIssue,
                null,
                worklog.authorKey,
                worklog.getComment()+ " ... Moved from " + sourcekey,
                worklog.getStartDate(),
                worklog.getGroupLevel(),
                worklog.getRoleLevelId(),
                worklog.getTimeSpent()
        )
 
        worklogManager.create(worklog.getAuthorObject(),newWorklog,0L,true);
        worklogManager.delete(currentUser, worklog, null, true);

        count++;
        //working only if run several times
          break;
    }
}
log.debug(count)

 

0 answers

Suggest an answer

Log in or Sign up to answer