Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created March 27, 2019 13:27
Show Gist options
  • Save jechlin/11927d14ab4ce344eb2083f4c22432d8 to your computer and use it in GitHub Desktop.
Save jechlin/11927d14ab4ce344eb2083f4c22432d8 to your computer and use it in GitHub Desktop.
package com.onresolve.jira.groovy.jql
import com.atlassian.jira.JiraDataType
import com.atlassian.jira.JiraDataTypes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import java.text.SimpleDateFormat
class CurrentTeams extends AbstractScriptedJqlFunction implements JqlFunction {
@Override
String getDescription() {
"Get current Users Teams"
}
@Override
List<Map> getArguments() {
Collections.EMPTY_LIST
}
List<String> getTeams() {
def pluginAccessor = ComponentAccessor.getPluginAccessor()
def teamsClassLoader = pluginAccessor.getEnabledPlugin('com.tempoplugin.tempo-teams').classLoader
def apiClassLoader = pluginAccessor.getEnabledPlugin('com.tempoplugin.tempo-platform-api').classLoader
def teamService = ComponentAccessor.getOSGiComponentInstanceOfType(teamsClassLoader.loadClass('com.tempoplugin.team.api.TeamService'))
def userRepo = ComponentAccessor.getOSGiComponentInstanceOfType(apiClassLoader.loadClass('com.tempoplugin.platform.api.user.UserRepository'))
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def sdf = new SimpleDateFormat("yyyy-MM-dd")
def today = sdf.format(new Date())
def teams = teamService.getMembershipsForUser(userRepo.getUserByNameEvenWhenUnknown(user.name), today, today)
// todo: check teams.errorCollection
teams.get().collect {it.team.name}
}
@Override
JiraDataType getDataType() {
JiraDataTypes.TEXT
}
@Override
String getFunctionName() {
"currentUserTeams"
}
@Override
List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
getTeams().collect {
new QueryLiteral(operand, it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment