|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const https = require('https'); |
| 19 | + |
| 20 | +/** |
| 21 | + * Sends result of E2E staging test to JSCore chat. |
| 22 | + */ |
| 23 | +async function notifyTestResults() { |
| 24 | + // JSCore chat webhook URL. |
| 25 | + if (!process.env.WEBHOOK_URL) { |
| 26 | + console.log(`Couldn't find WEBHOOK_URL env variable.`); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // URL of this workflow run. |
| 31 | + const workflowUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 32 | + let status = 'did not log a status correctly'; |
| 33 | + if (process.argv.includes('fail')) { |
| 34 | + status = 'failed'; |
| 35 | + } |
| 36 | + if (process.argv.includes('success')) { |
| 37 | + status = 'succeeded'; |
| 38 | + } |
| 39 | + |
| 40 | + let message = `E2E Tests ${status}`; |
| 41 | + |
| 42 | + // Add version if it can find it in the workflow_dispatch event data. |
| 43 | + if (process.env.GITHUB_EVENT_PATH) { |
| 44 | + const wrPayload = require(process.env.GITHUB_EVENT_PATH); |
| 45 | + if (wrPayload.inputs && wrPayload.inputs.versionOrTag) { |
| 46 | + message += ` for release ${wrPayload.inputs.versionOrTag}.`; |
| 47 | + } else { |
| 48 | + console.log(`Couldn't find versionOrTag in event payload.`); |
| 49 | + } |
| 50 | + } else { |
| 51 | + console.log(`Couldn't find event payload.`); |
| 52 | + } |
| 53 | + |
| 54 | + message += ` ${workflowUrl}`; |
| 55 | + |
| 56 | + const data = JSON.stringify({ |
| 57 | + text: message |
| 58 | + }); |
| 59 | + |
| 60 | + const options = { |
| 61 | + method: 'POST', |
| 62 | + headers: { |
| 63 | + 'Content-Type': 'application/json' |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + return new Promise((resolve, reject) => { |
| 68 | + console.log(`Sending message to chat: ${message}`); |
| 69 | + const req = https.request(process.env.WEBHOOK_URL, options, res => { |
| 70 | + res.on('data', d => { |
| 71 | + process.stdout.write(d); |
| 72 | + }); |
| 73 | + res.on('end', resolve); |
| 74 | + }); |
| 75 | + |
| 76 | + req.on('error', error => reject(error)); |
| 77 | + |
| 78 | + req.write(data); |
| 79 | + req.end(); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +notifyTestResults(); |
0 commit comments