Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
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);
    }
  }

The script above is assigning the value of the custom field of the issue to the relevant attribute values as a default before the worklog dialog was opened. However, if the issue is changed in the issue picker section while the worklog dialog was open, these values are not updated according to the selected issue. To update the attribute values according to the selected issue when the user changes the issue while the dialog is open ,the script below is needed to be used. The script below assigns the custom fields values specified with the IDs customfield_10500 and customfield_10501 to the attributes as a default by assigning the corresponding select type html elements which have IDs 'wa_10' and 'wa_11'. As with the script above, the important part here is that both the custom field and the attribute have the same values.

Code Block
import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters;
import java.util.*;

def script = '''
  function issueChanged() {
    var issueKey=AJS.$("#log-work-issue-picker").val()[0];
    
    fetch(`/rest/api/2/issue/${issueKey}`).then(function(response) {
      if (response.status === 200) {
        response.json().then(function(data) {
          console.log("DATA:", data);
          
          if(data.fields.customfield_10500 !== null){
           var selectedOptionDevelopmentBucket=data.fields.customfield_10500.value.toString();
           console.log("selected option development bucket:", selectedOptionDevelopmentBucket);
           var devBucId =AJS.$("option:contains('" + selectedOptionDevelopmentBucket + "')").addClass("on").val();
           console.log("devBucId:", devBucId);
             AJS.$("#wa_10").val(devBucId).prop('selected', true);
             AJS.$("#wa_10").trigger("change");
          }else{
             AJS.$("#wa_10").val('').prop('selected', true);
             AJS.$("#wa_10").trigger("change");
          }
            
          if(data.fields.customfield_10501 !== null){
            var selectedOptionProductBucket=data.fields.customfield_10501.value.toString();
            console.log("selected option product bucket:", selectedOptionProductBucket);
            var prodBucId =AJS.$("option:contains('" + selectedOptionProductBucket + "')").addClass("on").val();
            console.log("prodBucId:", prodBucId);
              AJS.$("#wa_11").val(prodBucId).prop('selected', true)
              AJS.$("#wa_11").trigger("change");
          }else{
            AJS.$("#wa_11").val('').prop('selected', true)
            AJS.$("#wa_11").trigger("change");
          }
        });
      }
    });
  }
  AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) {
    setTimeout(issueChanged, 10);
  });

'''
worklogPreEntryParameters.jsScript = script;
return worklogPreEntryParameters;

Removing 'Remaining Estimate' field of Log Work Custom Field

This script works in ‘Before Worklog Dailog Display' script type, it provides 'Remaining Estimate’ field to be removed from log work custom field.

Code Block
import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters;
def script = '''
  var $remainingEstimate=AJS.$("#wp-fg-estimates").remove();
''';
worklogPreEntryParameters.jsScript = script;
return worklogPreEntryParameters;

Resize of Worklog Dialog

Code Block
import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters;
worklogPreEntryParameters.jsScript = '''
  setTimeout(function () {
      AJS.$("#add-worklog-dialog-timesheet").width("700px");
  }, 10);
'''
return worklogPreEntryParameters;

Resize of Worklog Dialog

Set an error message below the issue, if value of a field is not provided. In the below example, we are checking the value of “STAR Reference” field and if it is not provided, we are showing an error message just below the issue.

Code Block
languagegroovy
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.deniz.jira.worklog.services.attr.AttrTypeService;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import org.slf4j.*;
import com.deniz.jira.worklog.*;
import com.atlassian.jira.security.JiraAuthenticationContext;

//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 referenceFieldObject = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("STAR Reference");
def error = null;
def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey);

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

if(referenceFieldObject!=null){
  def referenceFieldValue = (Option) issue.getCustomFieldValue(referenceFieldObject);
  log.debug("referenceFieldValue:{}.", referenceFieldValue );
  // Check if reference is absent and not in exception list
    if (referenceFieldValue == null || referenceFieldValue == ""){
     error = "No logging possible: No STAR Reference selected in this issue.";
    }
  // Check if reference is disabled
   if ((referenceFieldValue != null) && (referenceFieldValue.getDisabled()==true)){
    error = "No logging possible: The STAR Reference selected in this issue has been disabled";
   }
}

if (error != null) {
  log.debug("setting error:{}", error);
  worklogPreEntryParameters.jsScript = """
  setTimeout(function() {
    AJS.\$("<p id='star-error' class='error'>${error}</p>").appendTo("#log-work-issue-picker-single-select");
  }, 100);
  """;
} else {
  worklogPreEntryParameters.jsScript = """
  setTimeout(function() {
    AJS.\$("#star-error").remove();
  }, 100);
  """;
}

return worklogPreEntryParameters;