|
| 1 | +/** |
| 2 | + * Copyright 2022 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +// [START all] |
| 19 | +// [START import] |
| 20 | +// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and set up triggers. |
| 21 | +const { onTestMatrixCompleted } = require('firebase-functions/v2/testLab'); |
| 22 | +const { logger } = require('firebase-functions'); |
| 23 | +// The Axios client to send web requests to Slack. |
| 24 | +const axios = require('axios'); |
| 25 | +// [END import] |
| 26 | + |
| 27 | +// [START postToSlack] |
| 28 | +function postToSlack(title, details) { |
| 29 | + return axios.post( |
| 30 | + process.env.SLACK_WEBHOOK_URL, |
| 31 | + { |
| 32 | + blocks: [ |
| 33 | + { |
| 34 | + type: 'section', |
| 35 | + text: { |
| 36 | + type: 'mrkdwn', |
| 37 | + text: title |
| 38 | + } |
| 39 | + }, |
| 40 | + { |
| 41 | + type: 'divider' |
| 42 | + }, |
| 43 | + { |
| 44 | + type: 'section', |
| 45 | + text: { |
| 46 | + type: 'mrkdwn', |
| 47 | + text: details |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + ); |
| 53 | +} |
| 54 | +// [END postToSlack] |
| 55 | + |
| 56 | +// [START getSlackmoji] |
| 57 | +function getSlackmoji(term) { |
| 58 | + switch (term) { |
| 59 | + case 'SUCCESS': |
| 60 | + return ':tada:'; |
| 61 | + case 'FAILURE': |
| 62 | + return ':broken_heart:'; |
| 63 | + case 'INCONCLUSIVE': |
| 64 | + return ':question:'; |
| 65 | + case 'SKIPPED': |
| 66 | + return ':arrow_heading_down:'; |
| 67 | + case 'VALIDATING': |
| 68 | + return ':thought_balloon:'; |
| 69 | + case 'PENDING': |
| 70 | + return ':soon:'; |
| 71 | + case 'FINISHED': |
| 72 | + return ':white_check_mark:'; |
| 73 | + case 'ERROR': |
| 74 | + return ':red_circle:'; |
| 75 | + case 'INVALID': |
| 76 | + return ':large_orange_diamond:'; |
| 77 | + default: |
| 78 | + return ''; |
| 79 | + } |
| 80 | +} |
| 81 | +// [END getSlackmoji] |
| 82 | + |
| 83 | +// [START posttestresultstoslack] |
| 84 | +exports.posttestresultstoslack = onTestMatrixCompleted( |
| 85 | + { secrets: ["SLACK_WEBHOOK_URL"] }, |
| 86 | + async (event) => { |
| 87 | + // Obtain Test Matrix properties from the CloudEvent |
| 88 | + const { testMatrixId, state, outcomeSummary } = event.data; |
| 89 | + |
| 90 | + // Create the title of the message |
| 91 | + const title = `${getSlackmoji(state)} ${getSlackmoji( |
| 92 | + outcomeSummary |
| 93 | + )} ${testMatrixId}`; |
| 94 | + |
| 95 | + // Create the details of the message |
| 96 | + const details = `Status: *${state}* ${getSlackmoji( |
| 97 | + state |
| 98 | + )}\nOutcome: *${outcomeSummary}* ${getSlackmoji(outcomeSummary)} |
| 99 | + `; |
| 100 | + |
| 101 | + // Post the message to slack |
| 102 | + const slackResponse = await postToSlack(title, details); |
| 103 | + |
| 104 | + // Log the response |
| 105 | + logger.log(JSON.stringify(slackResponse.data)); |
| 106 | + }); |
| 107 | +// [END posttestresultstoslack] |
| 108 | +// [END all] |
0 commit comments