Hi ,
I have 2 projects, where project 1 has initiative issuetype and project 2 has story issuetype. Users log their time using Jira worklog which is then auto-synced to tempo timesheet in stories. Now, I want to write a listener script which can actually copy any new worklog added to stories in project 2 to the respective initiatives in project 1. Please note that stories are linked to Epics in Project 2 and the Epics are child issues of Initiatives in Project 1.
I started with the below script where I initially hardcoded the story issuekey for testing and then found its Epic and then the Epic’s parent initiative. Now I’m stuck at finding the “worklogid” of the worklogs added to the stories. If I get it, I can then retrieve the worklogs of the story and then push the data to tempo timesheet’s Rest API.
import com.atlassian.jira.issue.MutableIssue
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovyx.net.http.ContentType
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.adaptavist.hapi.jira.issues.Issues
import com.adaptavist.hapi.jira.users.Users
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.common.TempoDateTimeFormatter
import com.tempoplugin.core.datetime.api.TempoDate
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.worklog.v4.rest.InputWorklogsFactory
import com.tempoplugin.worklog.v4.rest.TimesheetWorklogBean
import com.tempoplugin.worklog.v4.rest.WorkAttributeValueInputBean
import com.tempoplugin.worklog.v4.services.WorklogService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.worklog.Worklog
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.Worklog
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.core.workattribute.api.WorkAttributeValueService
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
@WithPlugin('is.origo.jira.tempo-plugin')
@PluginModule
WorkAttributeService workAttributeService
@PluginModule
WorklogService worklogService
@PluginModule
InputWorklogsFactory inputWorklogsFactory
//List all user-defined variables
def issueId = 436784 //JRA-4851 story's IssueID
MutableIssue issue = issueMgr.getIssueObject("JRA-4851")
//log.warn "story key" + issue.Getkey()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def timeSpentForEachStory
def creds = "user.name:OTI5NTUyMjQ3MzEwOrhyTimCCDJP/XT7sK+L62GDWrMm"
issueLinkManager.getInwardLinks(issueId).each { issueLink ->
if (issueLink.issueLinkType.name == "Epic-Story Link") {
def linkedIssueAtEpic = issueLink.destinationObject
log.warn "Story's Epic: " + linkedIssueAtEpic.getEpic()
//log.warn linkedIssueAtEpic.getEpic().getId()
def parentLink = customFieldManager.getCustomFieldObjectByName('Parent Link');
def epic = linkedIssueAtEpic.getCustomFieldValue(epicLink)
if(epic) { // here we check if the story is connected to an epic
epic = (Issue)epic
if(epic.getCustomFieldValue(parentLink)) { // here we check if epic is connected to an parent (via portfolio)
def parentLinkReference = epic.getCustomFieldValue(parentLink);
log.warn "Epic's Initiative: " + parentLinkReference
def worklogManager = ComponentAccessor.getWorklogManager()
workAttributeValueService.getWorkAttributeValueByWorklogAndWorkAttribute(worklog.id).returnedValue // this is not returning the worklogid
}
//Get the Temp details of the given issue id
// def http = new HTTPBuilder( 'https://yourjirasite.com/rest/tempo-timesheets/3/worklogs/'+linkedIssueAtEpic.id )
// http.request(Method.GET, ContentType.JSON){ req ->
// headers.'Content-Type' = 'application/json'
// headers.'Authorization' = 'Basic ' + creds.bytes.encodeBase64().toString()
// response.success = { resp, json ->
// log.warn(json)
// Add all fields needed to create a new worklog
// def timesheetWorklogBean = new TimesheetWorklogBean()
// timesheetWorklogBean.issueIdOrKey = json.id
// timesheetWorklogBean.comment = json.comment
// timesheetWorklogBean.startDate = json.dateStarted
// timesheetWorklogBean.workerKey = json.author.key
// timesheetWorklogBean.timeSpentSeconds = json.timeSpentSeconds
// timesheetWorklogBean.remainingEstimateSeconds = json.issue.remainingEstimateSeconds
// def inputWorklogs = inputWorklogsFactory.buildForCreate(timesheetWorklogBean)
// worklogService.createTempoWorklogs(inputWorklogs)
// }
}
}
}
}
Can somebody guide if I am taking correct approach ? or If someone has solved this already using a better approach, please share the same and help me.
Thanks
Vhee