Worklog Filter Script
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. The 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
You need to update “roleToCheck” and “attrIdToCheck” variables for your needs. This script checks whether the user is in a specified project role or not. If the user is not in the role, it removes the worklog attribute from the result.
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.");
}
};