Skip to main content
Book your demo
Example scripts
To homepage
Jira
Data centre icon
Data Center

Create a Tempo Plan when an Issue is Assigned to a User

Features
Listeners
Created 2 year(s) ago, Updated 6 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.19)
compatibility bullet
ScriptRunner For Jira (7.10.0)
Language |
groovy
import com.atlassian.oauth.Request
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

import groovy.json.JsonOutput

import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.charset.StandardCharsets
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@WithPlugin('com.tempoplugin.tempo-plan-core')

@PluginModule
ApplicationProperties applicationProperties

// Default start time
final startTime = '09:00'

// Weekends and holidays are not included by default. If a plan needs to be created on weekends and holidays, set this to "true"
final includeNonWorkingDays = false

final today = LocalDate.now()

def issue = event.issue
def endDate = issue.dueDate?.toLocalDateTime()?.toLocalDate()

// Do nothing if the issue has been unassigned
if (!issue.assignee) {
    return
}

if (!(issue.estimate && endDate)) {
    log.error('Issue requires both an estimate and a due date. Plan not created')
    return
}

def url = applicationProperties.getBaseUrl(UrlMode.CANONICAL) + '/rest/tempo-planning/1/plan'

def requestBody = JsonOutput.toJson([
    assigneeKey          : 'JIRAUSER10000',
    assigneeType         : 'USER',
    day                  : DateTimeFormatter.ISO_LOCAL_DATE.format(today),
    end                  : DateTimeFormatter.ISO_LOCAL_DATE.format(endDate),
    includeNonWorkingDays: includeNonWorkingDays,
    planItemId           : issue.id,
    planItemType         : 'ISSUE',
    secondsPerDay        : issue.estimate,
    start                : DateTimeFormatter.ISO_LOCAL_DATE.format(today),
    startTime            : startTime
])

HttpRequest request = HttpRequest.newBuilder()
    .uri(OAuthRequestSigner.createOAuthUri(url))
    .header("Content-Type", "application/json")
    .header("Authorization", OAuthRequestSigner.createAuthorizationHeader(url, Request.HttpMethod.GET))
    .POST(HttpRequest.BodyPublishers.ofString(requestBody, StandardCharsets.UTF_8))
    .build()

HttpResponse<String> response = HttpClient.newHttpClient()
    .send(request, HttpResponse.BodyHandlers.ofString())

if (response.statusCode() >= 400) {
    throw new Exception("Status code: ${response.statusCode()}. Response body: ${response.body()}")
}

response.body()
Having an issue with this script?
Report it here