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 13 Next »

These scripts are run just before “Log Work Dialog” or “Log Work Custom Field” is displayed and allows you to define preset values or modify worklog attributes.

Hide Worklog Attribute Depending on Issue Custom Field Value

Following script checks value of a “Single Select” custom field and if value of this field is “Development” it removes “Requirement Analysis” value from “Type of Work” worklog attribute.

import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;

//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);

def issueManager = ComponentAccessor.getComponent(IssueManager.class);
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class);
def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey);

log.debug("issue:{}", issue);

def selectCustomField = customFieldManager.getCustomFieldObject(10901L); 
def selectedOption = issue.getCustomFieldValue(selectCustomField); //instance of com.atlassian.jira.issue.customfields.option.Option
log.debug("Selected Option:{}", selectedOption);

if (selectedOption.optionId == 10404) { //if you want you can also check selectedOption.value == "Development"
  log.debug("Removing 'Requirement Analysis' from 'Type of Work' options");
  def workTypeAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Type of Work"};
  if (workTypeAttr != null) {
    def reqAnalysisWorkAttr = workTypeAttr.attributeValues.find {it.name == "Requirement Analysis"};
    if (reqAnalysisWorkAttr != null) {
      workTypeAttr.attributeValues.remove(reqAnalysisWorkAttr)
    } 
  }
}
return worklogPreEntryParameters;

Hide Remaining Estimate

Following script injects a JavaScript to Log Work Dialog and this JavaScript code sets remaining estimate strategy to “Auto” and hides “remaining estimate” section of worklog dialog.

worklogPreEntryParameters.jsScript = '''
  AJS.$("#log-work-adjust-estimate-auto").prop("checked", true);
  AJS.$("#wp-fg-estimates").hide();
''';
return worklogPreEntryParameters;

Set Default Value of Worklog Attribute Depending on User

Following script set default value of “Invoice Number” attribute to “12345” if current user is “admin”.

import com.atlassian.jira.component.*;
import com.atlassian.jira.security.*;

def authContext = ComponentAccessor.getJiraAuthenticationContext();
def loggedInUser = authContext.getLoggedInUser();

def invoiceNumberAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Invoice Number"};
def defaultInvoice = "";
if (loggedInUser.key == "admin") {
  defaultInvoice = "12345"
}

if (invoiceNumberAttr != null) {
  script = '''
    var $invoiceNumber = AJS.$("#wa_%s");
    if ($invoiceNumber.val() === "") {
      $invoiceNumber.val("%s");
    }
  '''; 
  worklogPreEntryParameters.jsScript = String.format(script, invoiceNumberAttr.id, defaultInvoice);
}

return worklogPreEntryParameters;

Depending on User’s Group Sets Default Value of a Worklog Attribute

Assume that we have an attribute “Work Location” that can have one of the two values: On-site or Off-shore. The default is On-site. The following script checks if a user is in the group “all-Onsite” and if not, it sets the attribute value as Off-shore on the dialogue.

import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;
import com.deniz.jira.worklog.*;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.groups.GroupManager;
 
//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);
 
def issueManager = ComponentAccessor.getComponent(IssueManager.class);
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class);
def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey);
 
def workTypeAttrUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName();
 
def groupManager = ComponentAccessor.getComponent(GroupManager);

if(!groupManager.getGroupNamesForUser(workTypeAttrUser).contains('all-Onsite')) { // assumption is that all-Onsite only contains list of associates who are at onsite
  log.debug("Offshore team member");
   def workTypeAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Work Location"};
    if (workTypeAttr != null) {
      def offSiteAttr = workTypeAttr.attributeValues.find {it.name == "Off-site"};
      if (offSiteAttr != null) {
         offSiteAttr.setDefaultValue(true);
      }
    }
}
 
return worklogPreEntryParameters;

Hide a Worklog Attribute Depending on User’s Group

Following script checks “user group” of current user and show “Invoice Number” worklog attribute only if user is member of “finance-users” user group.

import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;
import com.deniz.jira.worklog.*;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.groups.GroupManager;
 
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName();
def groupManager = ComponentAccessor.getComponent(GroupManager);
def invoiceNumberAttr = worklogPreEntryParameters.attrTypes.find {it.name == "Invoice Number"};

if (invoiceNumberAttr != null) {
  if(!groupManager.getGroupNamesForUser(currentUser).contains('finance-users')) { 
    def script = '''
      var $invoiceNumber = AJS.$("#wa_%s");
      $invoiceNumber.hide();
    '''
    worklogPreEntryParameters.jsScript = String.format(script, invoiceNumberAttr.id);
  }
} 
return worklogPreEntryParameters;

Making WorklogPRO’s “Log Work” Dialog Very Similar To Jira’s Own “Log Work” Dialog

In this script we are removing every WP specific items such as “issue picker”, “user selector”, “period selector”, “navigator” and “copy comment to issue” feature. Note that, you don’t need to remove “Account” and “Worklog Attributes” because they are only visible if you create them. So, if you don’t want them, don’t create them.

worklogPreEntryParameters.jsScript = '''
  var $dialog = AJS.$("#add-worklog-dialog-timesheet");
  $dialog.find("nav").remove();
  $dialog.find("#wp-fg-issue").remove();
  $dialog.find("#wp-fg-author").remove();
  $dialog.find("#wp-fg-period").remove();
  $dialog.find("#wp-fg-copy-to-comments").remove();
''';
return worklogPreEntryParameters;

Change Default Value of Account if Issue Has Specific Key

This script changes account of an issue if issue key has specific value. You can modify it to check user, user-group, project, roles etc. It only searches accounts at first level for simplicity. If you want to set a child account, you need to make a recursive search for the account. If you now account ID, you can directly use it instead of searching it.

import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;

//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);

def issueManager = ComponentAccessor.getComponent(IssueManager.class);
def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey);

log.debug("issue:{}", issue);

if (issue.key == "UT-7") {
  def account = worklogPreEntryParameters.accounts.find {it.name == "IT Spendings"}
  if (account != null) {
    log.debug("found account = " + account);
    def script = '''
      var $accountSelect = AJS.$("#wp-account-select");
      $accountSelect.val(%s);
      $accountSelect.trigger("change")
    '''
    worklogPreEntryParameters.jsScript = String.format(script, account.id);
  }
}
return worklogPreEntryParameters;

Setting Default Values for Account & Attributes Depending on Project

import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;
 
//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);
 
def script = '''
  var projectKeys = ["ERP"];
  function getProjectKey() {
    var issueKey=AJS.$("#log-work-issue-picker").val()[0];
    if (issueKey) {
      return issueKey.split("-")[0].trim();
    }
    return "NO Project Key";
  }
  function setDefaults() {
    var projectKey = getProjectKey();
    if (projectKeys.indexOf(projectKey) == -1) {
      AJS.log("Non-special project:" + projectKey + "!");
      return;  
    }
    AJS.log("handling project:"+projectKey);
    
    //setting default account
    var $accountSelect = AJS.$("#wp-account-select");
    var primaSolutionStandardAccountId = $accountSelect.find("option:contains(Prima Solutions Standard)").val();
    $accountSelect.val(primaSolutionStandardAccountId);
    $accountSelect.trigger("change")
    
    //seting Chargeability
    var $chargeabilitySelect = AJS.$("#wa_8");
    var noChargeId = $chargeabilitySelect.find("option:contains(Non-Charge)").val();
    $chargeabilitySelect.val(noChargeId);
    $chargeabilitySelect.trigger("change")
  }
  
  AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) {
    setTimeout(setDefaults, 10);
  });

'''

worklogPreEntryParameters.jsScript = script;

return worklogPreEntryParameters;

Make Copy to Issue Comments Checked Depending on Project

import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;
 
//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);
 
def script = '''
  var projectKeys = ["ERP", "ITSD"]; //Change this line and add your project keys here. 
  function getProjectKey() {
    var issueKey=AJS.$("#log-work-issue-picker").val()[0];
    if (issueKey) {
      return issueKey.split("-")[0].trim();
    }
    return "NO Project Key";
  }
  function setDefaults() {
    var projectKey = getProjectKey();
    if (projectKeys.indexOf(projectKey) == -1) {
      AJS.log("Non-special project:" + projectKey + "!");
      return;  
    }
    AJS.log("handling project:"+projectKey);
    
    AJS.$("#wp-copy-to-issue-comments").click();
  }
  
  AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) {
    setTimeout(setDefaults, 10);
  });

'''

worklogPreEntryParameters.jsScript = script;

return worklogPreEntryParameters;

Set Time Spent Field to a Default Value

worklogPreEntryParameters.timeSpent = "10m"

Get Issue Details Every Time When Issue Picker Changes

def script = '''
  function issueChanged() {
    var issueKey=AJS.$("#log-work-issue-picker").val()[0];
    console.log("Issue key:" + issueKey);
    
    fetch("/rest/api/2/issue/ERP-75").then(function(response) {
      if (response.status === 200) {
        response.json().then(function(data) {
          console.log(data);
        });
      }
    });
  }
  AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) {
    setTimeout(issueChanged, 10);
  });

'''

worklogPreEntryParameters.jsScript = script;

return worklogPreEntryParameters;

Assigning The Selected Option of Select List(single choice) Custom Field to Single Select Type Worklog Attribute Value as a Default

This script gets the select list(single choice) type custom field value defined for the issue then assign to the single select attribute value as a default value in WorklogPro dialog. Custom field id and attribute name in the corresponding lines of script are needed to be specified. This script runs in 'Before Worklog Dialog Display' script type.

import com.atlassian.jira.component.*;
import com.deniz.jira.worklog.data.attr.AttrType;
import com.deniz.jira.worklog.data.attr.AttrValue;
import com.deniz.jira.worklog.services.attr.AttrTypeService;
import com.deniz.jira.worklog.data.attr.AttrValueImp;
import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters;
import com.deniz.jira.worklog.services.*;
import com.atlassian.jira.issue.*;
import java.util.*;
import org.slf4j.*;
 
def issueManager = ComponentAccessor.getComponent(IssueManager.class);
def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey);
 
def attrTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(AttrTypeService.class);
def attrTypes = ComponentAccessor.getOSGiComponentInstanceOfType(AttrType.class);
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class);
 
def selectCustomField = customFieldManager.getCustomFieldObject(10400); //Getting custom field with custom field id
def selectedOption = issue.getCustomFieldValue(selectCustomField);
 
def singleSelectAttrId = attrTypeService.getAttrTypeWithName("Single Select Attribute").get().ID; //Getting attribute with attribute name
def singleSelectAttr = worklogPreEntryParameters.attrTypes.find {element -> element.id == singleSelectAttrId};
def singleSelectAttrValues=singleSelectAttr.attributeValues;
 
def attrNames=singleSelectAttrValues.collect {it -> it.name};
for(option in attrNames){
    def AttrOption=option.toString();
    if(AttrOption == selectedOption.toString()){
      def attrImp=singleSelectAttrValues.find {it -> it.name==AttrOption};
      attrImp.setDefaultValue(true);
    }
  }

  • No labels