|
| 1 | +#!/usr/local/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2018 Zachary Kniebel. All rights reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains the |
| 7 | + * property of Zachary Kniebel. The intellectual and technical |
| 8 | + * concepts contained herein are proprietary to Zachary Kniebel and |
| 9 | + * may be covered by U.S. and Foreign Patents, patents in process, |
| 10 | + * and are protected by trade secret or copyright law. Dissemination |
| 11 | + * of this information or reproduction of this material is strictly |
| 12 | + * forbidden unless prior written permission is obtained from Zachary |
| 13 | + * Kniebel (contact@zacharykniebel.com). |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * DEPENDENCIES |
| 20 | + */ |
| 21 | + |
| 22 | +// third-party |
| 23 | +var request = require("request"); |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * CONSTANTS |
| 28 | + */ |
| 29 | + |
| 30 | +const COMPLETIONHANDLER_ID = "MSTeams_Notifier"; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * FUNCTIONS |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * Executes the completion handler with the given output directory path |
| 39 | + * @param {string} outputDirectoryPath the path to the output directory |
| 40 | + * @param {object} configurationLoader the configuration loader module |
| 41 | + * @param {object} metaball holds the metadata from the generation |
| 42 | + * @param {object} logger the logger |
| 43 | + * @param {Array<*>} params array of custom parameters |
| 44 | + */ |
| 45 | +var _execute = function (outputDirectoryPath, configurationLoader, metaball, logger, params) { |
| 46 | + if (!params || !params.Url) { |
| 47 | + logger.error("MS Teams notifier requires a params object with a \"Url\" parameter as an argument"); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + var basicInfoItems = []; |
| 52 | + if (metaball.ProjectName) { |
| 53 | + basicInfoItems.push({ |
| 54 | + "name": "Project Title", |
| 55 | + "value": metaball.ProjectName |
| 56 | + }); |
| 57 | + } |
| 58 | + if (metaball.EnvironmentName) { |
| 59 | + basicInfoItems.push({ |
| 60 | + "name": "Environment Name", |
| 61 | + "value": metaball.EnvironmentName |
| 62 | + }); |
| 63 | + } |
| 64 | + if (metaball.DocumentationTitle) { |
| 65 | + basicInfoItems.push({ |
| 66 | + "name": "Documentation Title", |
| 67 | + "value": metaball.DocumentationTitle |
| 68 | + }); |
| 69 | + } |
| 70 | + if (metaball.StartTime > 0) { |
| 71 | + var dateStr = new Date(metaball.StartTime).toUTCString(); |
| 72 | + basicInfoItems.push({ |
| 73 | + "name": "Started", |
| 74 | + "value": dateStr |
| 75 | + }); |
| 76 | + } |
| 77 | + if (metaball.EndTime > 0) { |
| 78 | + var dateStr = new Date(metaball.EndTime).toUTCString(); |
| 79 | + basicInfoItems.push({ |
| 80 | + "name": "Completed", |
| 81 | + "value": dateStr |
| 82 | + }); |
| 83 | + } |
| 84 | + if (metaball.CommitAuthor) { |
| 85 | + basicInfoItems.push({ |
| 86 | + "name": "Commit Author", |
| 87 | + "value": metaball.CommitAuthor |
| 88 | + }); |
| 89 | + } |
| 90 | + if (metaball.CommitHash) { |
| 91 | + basicInfoItems.push({ |
| 92 | + "name": "Commit Hash", |
| 93 | + "value": metaball.CommitHash |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + var themeColor = "32CD32"; |
| 98 | + var validationErrorsLayerItems = []; |
| 99 | + if (metaball.ValidationErrorsDetected) { |
| 100 | + validationErrorsLayerItems = metaball.ValidationErrors |
| 101 | + .filter(function(layer) { return layer.Entries.length; }) |
| 102 | + .map(function(layer) { |
| 103 | + var moduleNames = layer.Entries |
| 104 | + .map(function(entry) { return entry.ModuleName; }) |
| 105 | + .filter(function(entry, idx, arr) { return arr.indexOf(entry) === idx}) // select distinct |
| 106 | + .join(", "); |
| 107 | + |
| 108 | + return { |
| 109 | + "name": layer.Name, |
| 110 | + "value": moduleNames |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + themeColor = "F00000"; |
| 115 | + } |
| 116 | + |
| 117 | + var actionItems = []; |
| 118 | + if (metaball.DeployLink) { |
| 119 | + actionItems.push({ |
| 120 | + "@type": "OpenUri", |
| 121 | + "name": "View Documentation", |
| 122 | + "targets": [ |
| 123 | + { |
| 124 | + "os": "default", |
| 125 | + "uri": metaball.DeployLink |
| 126 | + } |
| 127 | + ] |
| 128 | + }); |
| 129 | + } |
| 130 | + if (metaball.CommitLink) { |
| 131 | + actionItems.push({ |
| 132 | + "@type": "OpenUri", |
| 133 | + "name": "View Commit", |
| 134 | + "targets": [ |
| 135 | + { |
| 136 | + "os": "default", |
| 137 | + "uri": metaball.CommitLink |
| 138 | + } |
| 139 | + ] |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + var card = { |
| 144 | + "@type": "MessageCard", |
| 145 | + "@context": "https://schema.org/extensions", |
| 146 | + "summary": "SitecoreDXG Generation Notification", |
| 147 | + "themeColor": themeColor, |
| 148 | + "title": "SitecoreDXG Generation Report", |
| 149 | + "sections": [ |
| 150 | + { |
| 151 | + "facts": basicInfoItems |
| 152 | + }, |
| 153 | + { |
| 154 | + "activityTitle": "**Validation Errors Detected**", |
| 155 | + "activitySubtitle": "Invalid dependencies were found in the following modules by layer:", |
| 156 | + "facts": validationErrorsLayerItems |
| 157 | + } |
| 158 | + ], |
| 159 | + "potentialAction": actionItems |
| 160 | + }; |
| 161 | + |
| 162 | + request.post({ |
| 163 | + url: params.Url, |
| 164 | + headers: {'content-type' : 'application/json'}, |
| 165 | + method: 'POST', |
| 166 | + json: card |
| 167 | + }, function optionalCallback(err, res) { |
| 168 | + if (err) { |
| 169 | + logger.error(`Error occurred while sending MS Teams notification: ${err}`); |
| 170 | + } else if (res.statusCode != 200) { |
| 171 | + logger.error(`Error occurred while posting MS Teams notification: { StatusCode: ${res.statusCode}, Message: "${res.statusMessage}", Readable: ${res.readable} Body: "${res.body}" }`); |
| 172 | + } else { |
| 173 | + logger.info("MS Teams notification sent"); |
| 174 | + } |
| 175 | + }); |
| 176 | +}; |
| 177 | + |
| 178 | +/** |
| 179 | + * Registers the completion handler with the given CompletionHandlerManager - this function is required on all completion handler modules |
| 180 | + * @param {CompletionHandlerManager} completionHandlerManager the trigger manager to register the trigger for |
| 181 | + */ |
| 182 | +var registerCompletionHandler = function (completionHandlerManager) { |
| 183 | + completionHandlerManager.registerCompletionHandler(COMPLETIONHANDLER_ID, _execute); |
| 184 | +}; |
| 185 | + |
| 186 | +exports.COMPLETIONHANDLER_ID = COMPLETIONHANDLER_ID; |
| 187 | +exports.registerCompletionHandler = registerCompletionHandler; |
0 commit comments