|
| 1 | +const request = require('superagent'); |
| 2 | + |
| 3 | +function main() { |
| 4 | + if (process.env.TEST_FRAMEWORK === undefined) { |
| 5 | + throw new Error('TEST_FRAMEWORK should be defined either as "jest" or "mocha"'); |
| 6 | + } else if (process.env.HOOK_URL === undefined) { |
| 7 | + throw new Error('HOOK_URL should be defined as Microsoft teams channel hook url'); |
| 8 | + } |
| 9 | + const file = process.argv[2]; |
| 10 | + if (!file) throw new Error('File path is not given'); |
| 11 | + |
| 12 | + const platform = process.env.TEST_FRAMEWORK; |
| 13 | + const hookUrl = process.env.HOOK_URL; |
| 14 | + const project = process.env.PROJECT_NAME; |
| 15 | + const environment = process.env.PROJECT_ENVIRONMENT; |
| 16 | + const type = process.env.PROJECT_TEST_TYPE; |
| 17 | + const buildUrl = process.env.BUILD_URL; |
| 18 | + |
| 19 | + let total = 0; |
| 20 | + let passed = 0; |
| 21 | + let failed = 0; |
| 22 | + let skipped = 0; |
| 23 | + |
| 24 | + // logic to get results |
| 25 | + if (platform === 'jest') { |
| 26 | + const testData = require(file); |
| 27 | + total = testData['numTotalTests']; |
| 28 | + passed= testData['numPassedTests']; |
| 29 | + failed = testData['numFailedTests']; |
| 30 | + skipped = total - passed - failed; |
| 31 | + } else if (platform === 'mocha') { |
| 32 | + // TODO: Implement this |
| 33 | + } |
| 34 | + |
| 35 | + let results = `**${total} Tests**: ✔${passed} passed`; |
| 36 | + if (failed > 0) results += `, ❗${failed} failed`; |
| 37 | + if (skipped > 0) results += `, ➖${skipped} skipped`; |
| 38 | + |
| 39 | + request.post(hookUrl) |
| 40 | + .send( |
| 41 | + { |
| 42 | + "@type": "MessageCard", |
| 43 | + "@context": "http://schema.org/extensions", |
| 44 | + "themeColor": "33F0FF", |
| 45 | + "summary": `Test report for ${project}`, |
| 46 | + "sections": [ |
| 47 | + { |
| 48 | + |
| 49 | + "activityTitle": `## ${project}, ${environment}, ${type}`, |
| 50 | + // "activitySubtitle": "Status of build #90", |
| 51 | + "facts": [ |
| 52 | + { |
| 53 | + "name": "Environment", |
| 54 | + "value": environment, |
| 55 | + }, |
| 56 | + // { |
| 57 | + // "name": "Type", |
| 58 | + // "value": "UI", |
| 59 | + // }, |
| 60 | + { |
| 61 | + "name": "Status", |
| 62 | + "value": results, |
| 63 | + }, |
| 64 | + ], |
| 65 | + "markdown": true, |
| 66 | + }, |
| 67 | + ], |
| 68 | + "potentialAction": [ |
| 69 | + { |
| 70 | + "@type": "OpenUri", |
| 71 | + "name": "View Report", |
| 72 | + "targets": [ |
| 73 | + { "os": "default", "uri": buildUrl }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ) |
| 79 | + .then( (res) => { |
| 80 | + console.log(`Post results to Microsoft Teams with status: ${res.status}`); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +main(); |
0 commit comments