Versions Compared

Key

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

...

In the below example we make use of the ChangeManagementSystemService to allow us to create an incident through FlexDeploy’s internal ServiceNow integration. The only thing that needs to be configured ahead of time is your ServiceNow instance in Topology->Integrations->ChangeManagement

...

Create Incident Script

Code Block
languagegroovy
//Carry out any custom action you wish for this event. Check out the documentation for reference or ideas.
//Optionally filter out events you dont want to execute by returning false on the filter script tab.
def listenerName = "Create Incident";
LOG.info("Running listener: ${listenerName}");

def cmsFields = [:];
cmsFields.short_description = "Deployment failed for ${EVENT.payload.project.projectName}";
cmsFields.description = "Deployment failed for ${EVENT.payload.project.projectName}. Environment ${EVENT.payload.environment.environmentName} executionid ${EVENT.payload.workflowExecutionId} requestor ${EVENT.payload.updatedBy}";
 
LOG.fine("Creating Service Now Incident ${cmsFields}");

def cmsObject = FLEXDEPLOY.createIncidentForWorklowRequest(EVENT.payload.workflowRequest.workflowRequestId, cmsFields, "SERVICENOW",null);
LOG.setMessage("Successfully created Service Now Incident ${cmsObject.getNumber()}");

...

Code Block
languagegroovy
//Carry out any custom action you wish for this event. Check out the documentation for reference or ideas.
//Optionally filter out events you dont want to execute by returning false on the filter script tab.
def listenerName = "Tests Completed";
LOG.info("Running listener: ${listenerName}");

def builder = new StringBuilder();
builder.append("<h1 style=\"padding: 10px 0\">Tests ${EVENT.payload.testRun.runStatus} for ${getProjectLink(EVENT.payload.project.projectName, EVENT.payload.project.projectId)} in ${EVENT.payload.environment.environmentName}</h1>");

builder.append("<table><tr><th style=\"padding: 0 10px 5px 0\">Test Def Name</th><th style=\"padding: 0 10px 5px 0\">Testing Tool</th><th style=\"padding: 0 10px 5px 0\">Test Case Name</th><th style=\"padding: 0 10px 5px 0\">Status</th></tr>");

EVENT.payload.testRuntestSuiteExec.testSets.each{ testSettestSuite ->
  LOG.fine("Processing test setsuite execution ${testSettestSuite.nametestSuiteExecutionId}");
  
  testSettestSuite.testDefinitionstestExecutions.each{ testDeftestExec ->
    LOG.fine("Processing test defexecution ${testDeftestExec.name}");
    

   def testDefName = testDef.name;
    def testingTool = testDeftestExec.testingTooltoolName;
    
    testDeftestExec.resultstestResults.each{ testResult -> 
      def testCaseName = testResult.testCaseName;
      def status = testResult.status;
      
      builder.append("<tr><td style=\"padding-right: 10px\">${testDefName}</td><td style=\"padding-right: 10px\">${testingTool}</td><td style=\"padding-right: 10px\">${testCaseName}</td><td style=\"padding-right: 10px\">${status}</td></tr>");
    }
  }
}

builder.append("</table>");

def htmlMessage = builder.toString();

LOG.fine("Sending test results table to Teams: ${htmlMessage}");
MICROSOFTTEAMS.sendTeamsMessage("TEAMS","FD Developers","Testing",htmlMessage,null);

LOG.setMessage("Successfully sent test results message to teams for ${EVENT.payload.project.projectName}");

def getProjectLink(String pProjectName, Long pProjectId)
{
  String link = String.format("%s/flexdeploy/faces/projects?objecttype=Project&projectid=%s&projectname=%s", FLEXDEPLOY.getFlexDeployBaseUrl(), pProjectId, pProjectName);
  return String.format("<a href=%s>%s</a>", link, pProjectName);
}

...

Code Block
languagegroovy
import flexagon.ff.model2.pojo.communication.communication.EmailAttachment;
import groovy.json.JsonOutput;

//Carry out any custom action you wish for this event. Check out the documentation for reference or ideas.
//Optionally filter out events you dont want to execute by returning false on the filter script tab.
def listenerName = "Email Failed Logs";
LOG.info("Running listener: ${listenerName}");

//retrieve plugin log input stream map
def logs = FLEXDEPLOY.getPluginLogInputStreams(EVENT.payload.workflowExecutionId);
def attachments = [];

//for each entry create and add a new attachment
//prior to 5.4.0.1 logs would be a list instead of a map
logs.each { log -> 
  attachments.add(new EmailAttachment(log.value,"${log.key}.txt","text/plain"));
}

LOG.info("Sending email with ${attachments.size()} attachments.");

//send email
def subject = "${EVENT.payload.project.projectName} failed in ${EVENT.payload.environment.environmentName}";
def message = JsonOutput.prettyPrint(JsonOutput.toJson(EVENT.payload));
def recipients = ["my.user@domain.com"];

EMAIL.sendEmail(subject, message, recipients, attachments);

LOG.setMessage("Sent email message to ${recipients}");

...

Code Block
languagegroovy
return "SUCCESS".equals(EVENT.payload.executionStatus) && "PRODUCTION".equals(EVENT.payload.environment.environmentCode) && EVENT.payload.packageName != null

Set the Assignee of the Work Item on Work Item Creation

To prevent user error when creating a work item, you set the default Assignee for the Work Item to the user who created the work item if it was not set. This webhook will only set the assignee at creation time using the Work Item Created Event.

Webhook Listener

Code Block
languagegroovy
FlxWorkItemDataObject workItem = FLEXDEPLOY.findWorkItemByNumber(EVENT.payload.workItemNumber)

// check if the assignee is not already set
if (!workItem.getAssignee()?.trim())
{
  FLEXDEPLOY.updateWorkItemAssignee(EVENT.payload.workItemNumber, EVENT.payload.createdBy)
}