Contents:

  1. HPE QC / ALM Workflow Properties and Examples
    1. Workflow Properties
    2. Template JavaScript Workflow Script
    3. Sample Script: Only QA Managers Can Close Defects

HPE QC / ALM Workflow Properties and Examples

Workflow Properties

The HPE QC / ALM connector provides the following properties:

Template JavaScript Workflow Script

To get started writing a workflow script in JavaScript, you can copy this template script:

// import the Arrays class from Java (optional)
importClass(java.util.Arrays);

// Declare the standard message type constants
var MSG_INFO = 0;
var MSG_WARNING = 1;
var MSG_ERROR = 2;

// Declare constants for the properties exposed by the HPE QC / ALM connector
var PROP_USER_ID = "USER_ID";
var PROP_DISPLAY_NAME = "DISPLAY_NAME";
var PROP_GROUPS = "USER_GROUPS";
var PROP_DOMAIN = "DOMAIN";
var PROP_PROJECT = "PROJECT";

// Declare constants for the ids of fields you are interested in
var BG_ACTUAL_FIX_TIME = "BG_ACTUAL_FIX_TIME";
var BG_ESTIMATED_FIX_TIME = "BG_ESTIMATED_FIX_TIME";
var BG_REPRODUCIBLE = "BG_REPRODUCIBLE";
var BG_PRIORITY = "BG_PRIORITY";

// Declare any other constants
var QA_MANAGER = "QA Manager";

function handleEditorOpened(dom) {
    // this is how you can get metadata from the task
    var taskId = dom.getTask().getTaskId();
    var displayName = dom.getTask().getProperty(PROP_DISPLAY_NAME);
    var domain = dom.getTask().getProperty(PROP_DOMAIN);
    var project = dom.getTask().getProperty(PROP_PROJECT);
    var groups = dom.getTask().getProperty(PROP_GROUPS);
    if (groups != null) {
        var groupMessageSet = false;
        for(var i in groups) {
            if(groups[i] == QA_MANAGER) {
                dom.getTaskUi().setMessage(
                    "You are a member of the QA Manager group", MSG_INFO);
                groupMessageSet = true;
            }
        }
        if(!groupMessageSet) {
            var groupsAsString = Arrays.toString(groups);
            dom.getTaskUi().setMessage(
                "Click to see your group membership", 
                "You are a member of the following groups: " + groupsAsString,
                MSG_INFO);
        }
    }
    
    // get the IField objects containing the current values of the fields 
    // the getField method returns null if the field is not present for that task 
    var actualFixTime = dom.getTask().getField(BG_ACTUAL_FIX_TIME);
    var estimatedFixTime = dom.getTask().getField(BG_ESTIMATED_FIX_TIME);
    var reproducible = dom.getTask().getField(BG_REPRODUCIBLE);
    var priority = dom.getTask().getField(BG_PRIORITY);
    
    // change field visibility, values, etc. as desired
    if(actualFixTime != null) {
        actualFixTime.setVisible(false);
    }
    if(estimatedFixTime != null) {
        estimatedFixTime.setInt(1);
        estimatedFixTime.setRequired(true);
    }
    if(reproducible != null) {
        reproducible.setReadOnly(true);
    }
}

function handleSingleValueChanged(dom, fieldId, oldValue, newValue) {
    // change field visibility, values, etc. as desired
    if(fieldId.equals(BG_PRIORITY)) {
        if(newValue != null && newValue.equals("P1")) {
            // set a warning message in the task editor
            dom.getTaskUi().setMessage(
                "Priority P1 should only be used for critical tasks", MSG_WARNING);
        }
    }
}

function handleFieldChanged(dom, fieldId, oldValues, newValues) {
    if(!fieldId.equals(BG_PRIORITY)) {
        // set an information message in the task editor
        dom.getTaskUi().setMessage("You edited the field \"" + 
            dom.getTask().getField(fieldId).getLabel() + "\"", MSG_INFO);
    }
}

function handleSubmit(dom, changedFieldIds) {
    // return false to prevent the user from submitting their changes
    return true;
}

function dispose() {
    // if necessary, do any needed cleanup here
}

Sample Script: Only QA Managers Can Close Defects

This sample script prevents a user from changing the status of a defect to “Closed” unless they are part of the “QA Manager” group:

// Declare the standard message type constants
var MSG_INFO = 0;
var MSG_WARNING = 1;
var MSG_ERROR = 2;

// Declare constants for the properties exposed by the HPE QC / ALM connector
var PROP_USER_ID = "USER_ID";
var PROP_DISPLAY_NAME = "DISPLAY_NAME";
var PROP_GROUPS = "USER_GROUPS";
var PROP_DOMAIN = "DOMAIN";
var PROP_PROJECT = "PROJECT";

var BG_STATUS = "BG_STATUS";
var CLOSED = "Closed";
var DEFECT = "Defect";
var QA_MANAGER = "QA Manager";

function handleSingleValueChanged(dom, fieldId, oldValue, newValue) {
    // warn the user when they try to make an illegal change
    var task = dom.getTask();
    if(task.getTaskType() == DEFECT && fieldId == BG_STATUS && newValue.getString() == CLOSED) {
        if(!isUserQAManager(dom)) {
            dom.getTaskUi().setMessage("Only a QA Manager can close a defect.", MSG_ERROR);
            return;
        }
    }
    dom.getTaskUi().clearMessage();
}

function handleSubmit(dom, changedFieldIds) {
    // prevent the user from submitting an illegal change 
    var task = dom.getTask();
    var statusField = task.getField(BG_STATUS);
    if(task.getTaskType() == DEFECT && statusField.getString() == CLOSED) {
        for(var i in changedFieldIds) {
            var changedFieldId = changedFieldIds[i];
            if(changedFieldId == BG_STATUS) {// the status field has been changed
                if(!isUserQAManager(dom)) {
                    dom.getTaskUi().setMessage(
                        "Submit disallowed: only a QA Manager can close a defect.", MSG_ERROR);
                    return false;// block the submission 
                }
            }
        }
    }
    return true;// allow the submission
}

function isUserQAManager(dom) {
    var groups = dom.getTask().getProperty(PROP_GROUPS);
    for(var i in groups) {
        if(groups[i] == QA_MANAGER) {
            return true;
        }
    }
    return false;
}