|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const axios = require('axios'); |
| 4 | +const test_output = fs.readFileSync(path.resolve('test-output.log'), { encoding: 'utf8' }); |
| 5 | +const test_ouput_json = JSON.parse(test_output); |
| 6 | +const ansi_regex = new RegExp( |
| 7 | + [ |
| 8 | + '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', |
| 9 | + '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', |
| 10 | + ].join('|'), |
| 11 | + 'g' |
| 12 | +); |
| 13 | + |
| 14 | +const failed_suits = test_ouput_json.testResults.filter(suite => suite.status === 'failed'); |
| 15 | + |
| 16 | +const errors = { |
| 17 | + service: [], |
| 18 | + test: [], |
| 19 | +}; |
| 20 | + |
| 21 | +failed_suits.map(suite => { |
| 22 | + const failed_tests = suite.assertionResults.filter(test => test.status === 'failed'); |
| 23 | + const error_suite = { |
| 24 | + name: suite.name.split('node-sdk/test')[1], |
| 25 | + service: [], |
| 26 | + test: [], |
| 27 | + }; |
| 28 | + |
| 29 | + failed_tests.map(result => { |
| 30 | + const message_clean = result.failureMessages.join('\n').replace(ansi_regex, ''); |
| 31 | + error_suite[message_clean.indexOf(/^Received: 5/m) > 0 ? 'service' : 'test'].push( |
| 32 | + `${result.fullName}\n${message_clean}` |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + errors.service.push(`${error_suite.name}\n${error_suite.service.join('\n')}`); |
| 37 | + errors.test.push(`${error_suite.name}\n${error_suite.test.join('\n')}`); |
| 38 | +}); |
| 39 | + |
| 40 | +let body = ''; |
| 41 | +if (errors.service.length > 0) { |
| 42 | + body = `${body}## Service Failures\n${errors.service.join('\n')}\n`; |
| 43 | +} |
| 44 | + |
| 45 | +if (errors.test.length > 0) { |
| 46 | + body = `${body}## Possible Test Failures\n${errors.test.join('\n')}\n`; |
| 47 | +} |
| 48 | + |
| 49 | +if (process.env.TRAVIS_PULL_REQUEST && process.env.TRAVIS_PULL_REQUEST !== 'false') { |
| 50 | + // Send the result to the pull request if it is a pull request. |
| 51 | + axios |
| 52 | + .post( |
| 53 | + `https://api.github.com/repos/${process.env.TRAVIS_REPO_SLUG}/issues/${ |
| 54 | + process.env.TRAVIS_PULL_REQUEST |
| 55 | + }/comments`, |
| 56 | + { |
| 57 | + body: body, |
| 58 | + }, |
| 59 | + { |
| 60 | + headers: { |
| 61 | + 'User-Agent': 'watson-github-bot', |
| 62 | + Authorization: `token ${process.env.GH_TOKEN}`, |
| 63 | + }, |
| 64 | + } |
| 65 | + ) |
| 66 | + .catch(error => { |
| 67 | + console.error(error); // eslint-disable-line |
| 68 | + }) |
| 69 | + .then(() => { |
| 70 | + if (errors.test.length > 0) { |
| 71 | + process.exit(1); // eslint-disable-line |
| 72 | + } |
| 73 | + }); |
| 74 | +} else { |
| 75 | + // Write to stdout |
| 76 | + console.log(body); // eslint-disable-line |
| 77 | + |
| 78 | + if (errors.test.length > 0) { |
| 79 | + process.exit(1); // eslint-disable-line |
| 80 | + } |
| 81 | +} |
0 commit comments