Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

This script is run before returning worklogs from REST API of the app. You can use it to remove/modify certain worklogs or worklog attributes. Script is passed an instance of worklogWrapper object. This object is a complex data structure and can be modified within the script. But modifications should update every part of the structure consistently, otherwise display anomalies may occur. Please use this script as a last resort.

Remove a Worklog Attribute If User is not in a Certain Project Role

import com.atlassian.jira.component.*;
import com.atlassian.jira.security.roles.*;
import com.atlassian.jira.project.*;
import org.slf4j.*;

Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);
log.debug("<=================>");

def roleToCheck = "Consultant";
def attrIdToCheck = 1;

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
if (loggedInUser == null) {
  return;
}

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class) as ProjectRoleManager;
def developerRole = projectRoleManager.getProjectRole(roleToCheck) as ProjectRole;

def projectManager = ComponentAccessor.getComponent(ProjectManager.class) as ProjectManager;


if (developerRole == null) {
  log.warn("Project role " + roleToCheck + "does not exist");
  return;
}


worklogWrapper.projects.each { projectLog -> 
  def project = projectManager.getProjectObj(projectLog.id);
  log.debug("Checking project" + project);
  if (!projectRoleManager.isUserInProjectRole(loggedInUser, developerRole, project)) {
    log.debug("User is NOT a member of " + roleToCheck + ". Removing worklog attribute.");
    projectLog.issues.each { issue -> 
      issue.workLogs.each { worklog -> 
        worklog.workLogAttributes.removeIf {it -> it.getAttrTypeId() == attrIdToCheck};
      };
    };
  } else {
    log.debug("User is a member of " + roleToCheck + ". Not removing worklog attribute.");
  }
};
  • No labels