Add Tempo Team Members to Advanced Roadmaps

Advanced Roadmaps creates new virtual team members. That means, if you have added absences to the same user in another team, those wouldn’t be applied to the new team member. The benefit: Team members can plan absences for a team individually.

One of the next upcoming blog posts will present a solution for adding vacation days to all existing team members.

Implementation

Finish these two blog posts first:

Sync Tempo with Advanced Roadmaps

Call REST Endpoints from PostgreSQL Trigger

You need myGroovy, a great, and free, open source Jira add-on.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import com.atlassian.sal.api.net.Request.MethodType
import com.atlassian.sal.api.net.TrustedRequestFactory
import javax.ws.rs.core.HttpHeaders
import javax.ws.rs.core.MultivaluedMap

class JiraUser {
    def jiraUserId
}

class PersonDTO {
    JiraUser jiraUser
    def abilities = []
    def absences = []

    PersonDTO(String jiraUserKey) {
        jiraUser = new JiraUser(jiraUserId: jiraUserKey)
    }
}

class NewResource {
    def teamId
    def person
}

def doPost(remoteUrl, body) {
    def baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
    def trustedFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)
    def request = trustedFactory.createTrustedRequest(MethodType.POST, "${baseUrl}${remoteUrl}")
    request.addTrustedTokenAuthentication(new URL(baseUrl).getHost())
    request.addHeader(HttpHeaders.ACCEPT, "application/json")
    request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json")
    def requestString = new JsonBuilder(body).toString()
    request.setRequestBody(requestString)
    def response = null
    try {
        response = request.execute()
    } catch(Exception ex) { /* do nothing */ }
    def jsonSlurper = new JsonSlurper()
    def result
    try {
        result = jsonSlurper.parseText(response)
    } catch (ex) { /* We do nothing */ }
    result
}

def doGet(remoteUrl) {
    def baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
    def trustedFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)
    def request = trustedFactory.createTrustedRequest(MethodType.GET, "${baseUrl}${remoteUrl}")
    request.addTrustedTokenAuthentication(new URL(baseUrl).getHost())
    request.addHeader(HttpHeaders.ACCEPT, "application/json")
    def response = null
    try {
        response = request.execute()
    } catch(Exception ex) { /* do nothing */ }
    def jsonSlurper = new JsonSlurper()
    def result
    try {
        result = jsonSlurper.parseText(response)
    } catch (ex) { /* We do nothing */ }
    result
}

def getTeamIdToTempoTeamId(tempoTeamId) {
    def allTeams = doGet("/rest/teams-api/1.0/team")
    def matcher
    def arTeamId = allTeams.find {
        matcher = it.title =~ /^.*?\(TEMPOID(?<tempoid>\d+)\)$/
        matcher.matches()
        tempoTeamId == matcher.group("tempoid")
    }
    arTeamId.id
}

def getAllPersonsToJiraUserKey(jiraUserKey) {
    def allPersons = doGet("/rest/teams-api/1.0/person")
    allPersons.findAll { it.jiraUser.jiraUserId == jiraUserKey }
}

def getJiraUserKeyToPersonId(personId) {
    doGet("/rest/teams-api/1.0/person/" + personId).jiraUser.jiraUserId
}

def getResources() {
    doGet("/rest/teams-api/1.0/resource")
}

def addNewResource(newResource) {
    doPost("/rest/teams-api/1.0/resource", newResource)
}

// Main

MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(true)
def tempoTeamId = queryParams.getFirst("teamid")
def jiraUserKey = queryParams.getFirst("userkey")

def allResources = getResources()

// Add missing person to the team
def teamId = getTeamIdToTempoTeamId(tempoTeamId)
def resourcesOfTheTeam = allResources.findAll { it.teamId == teamId }
resourcesOfTheTeam.any { it.put("jiraUserKey", getJiraUserKeyToPersonId(it.person.id)) }
if (!resourcesOfTheTeam.find { it.jiraUserKey == jiraUserKey }) {
    def personsToJiraUserKey = getAllPersonsToJiraUserKey(jiraUserKey)
    def firstPersonId = personsToJiraUserKey.collect { it.id }.min()
    addNewResource(new NewResource(
            teamId: teamId,
            person: firstPersonId ? personsToJiraUserKey.find { it.id == firstPersonId } : new PersonDTO(jiraUserKey)
    ))
}

Reach out to me for professional support!

Contact