forked from mattermost/mattermost-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_report.js
160 lines (140 loc) · 6 KB
/
save_report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable no-console, no-process-env */
/*
* This is used for saving artifacts to AWS S3, sending data to automation dashboard and
* publishing quick summary to community channels.
*
* Usage: [ENV] node save_report.js
*
* Environment variables:
* BRANCH=[branch] : Branch identifier from CI
* BUILD_ID=[build_id] : Build identifier from CI
* COMMIT_HASH=[commit_hash] : Commit hash from repo
* DEVICE_NAME=[device_name] : Name of the device used for testing
* DEVICE_OS_NAME=[device_os_name] : OS of the device used for testing
* HEADLESS=[boolean] : Headed by default (false) or headless (true)
* IOS=[boolean] : Android by default (false) or iOS (true)
*
* For saving artifacts to AWS S3
* - DETOX_AWS_S3_BUCKET, DETOX_AWS_ACCESS_KEY_ID and DETOX_AWS_SECRET_ACCESS_KEY
* For saving test cases to Test Management
* - ZEPHYR_ENABLE=true|false
* - ZEPHYR_API_KEY=[api_key]
* - JIRA_PROJECT_KEY=[project_key], e.g. "MM",
* - ZEPHYR_FOLDER_ID=[folder_id], e.g. 847997
* For sending hooks to Mattermost channels
* - FULL_REPORT, WEBHOOK_URL and TEST_CYCLE_LINK_PREFIX
* Test type
* - TYPE=[type], e.g. "MASTER", "PR", "RELEASE", "GEKIDOU"
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const fse = require('fs-extra');
const {mergeFiles} = require('junit-report-merger');
const shell = require('shelljs');
const {saveArtifacts} = require('./utils/artifacts');
const {ARTIFACTS_DIR} = require('./utils/constants');
const {
generateJestStareHtmlReport,
mergeJestStareJsonFiles,
} = require('./utils/jest_stare');
const {
convertXmlToJson,
generateShortSummary,
generateTestReport,
getAllTests,
removeOldGeneratedReports,
sendReport,
readJsonFromFile,
writeJsonToFile,
} = require('./utils/report');
const {createTestCycle, createTestExecutions} = require('./utils/test_cases');
require('dotenv').config();
const saveReport = async () => {
const {
DEVICE_NAME,
DEVICE_OS_VERSION,
HEADLESS,
IOS,
TYPE,
WEBHOOK_URL,
ZEPHYR_ENABLE,
ZEPHYR_CYCLE_KEY,
} = process.env;
// Remove old generated reports
removeOldGeneratedReports();
const detox_version = shell.exec('npm list detox').stdout.split('\n')[1].split('@')[1].trim();
const headless = IOS === 'true' ? 'false' : HEADLESS === 'true';
const os_name = os.platform();
const os_version = os.release();
const node_version = process.version;
const npm_version = shell.exec('npm --version').stdout.trim();
const device_name = DEVICE_NAME;
const device_os_version = DEVICE_OS_VERSION;
// Write environment details to file
const environmentDetails = {
detox_version,
device_name,
device_os_version,
headless,
os_name,
os_version,
node_version,
npm_version,
};
writeJsonToFile(environmentDetails, 'environment.json', ARTIFACTS_DIR);
// Merge all XML reports into one single XML report
const platform = process.env.IOS === 'true' ? 'ios' : 'android';
const combinedFilePath = `${ARTIFACTS_DIR}/${platform}-combined.xml`;
await mergeFiles(path.join(__dirname, combinedFilePath), [`${ARTIFACTS_DIR}/${platform}-results*/${platform}-junit*.xml`]);
console.log(`Merged, check ${combinedFilePath}`);
// Read XML from a file
const xml = fse.readFileSync(combinedFilePath);
const {testsuites} = convertXmlToJson(xml);
// Generate short summary, write to file and then send report via webhook
const allTests = getAllTests(testsuites);
const summary = generateShortSummary(allTests);
console.log(summary);
writeJsonToFile(summary, 'summary.json', ARTIFACTS_DIR);
// Generate jest-stare report
const jestStareOutputDir = path.join(__dirname, `${ARTIFACTS_DIR}/jest-stare`);
const jestStareCombinedFilePath = `${jestStareOutputDir}/${platform}-combined.json`;
if (!fs.existsSync(jestStareOutputDir)) {
fs.mkdirSync(jestStareOutputDir, {recursive: true});
}
// "ios-results-*" or "android-results-*" is the path in CI where the parallel detox jobs save the artifacts
await mergeJestStareJsonFiles(jestStareCombinedFilePath, [`${ARTIFACTS_DIR}/${platform}-results*/jest-stare/${platform}-data*.json`]);
generateJestStareHtmlReport(jestStareOutputDir, `${platform}-report.html`, jestStareCombinedFilePath);
if (process.env.CI) {
// Delete folders starting with "ios-results-" or "android-results-" only in CI environment
const entries = fs.readdirSync(ARTIFACTS_DIR, {withFileTypes: true});
for (const entry of entries) {
if (entry.isDirectory() && entry.name.startsWith(`${platform}-results-`)) {
fs.rmSync(path.join(ARTIFACTS_DIR, entry.name), {recursive: true});
}
}
}
const result = await saveArtifacts();
if (result && result.success) {
console.log('Successfully uploaded artifacts to S3:', result.reportLink);
}
// Create or use an existing test cycle
let testCycle = {};
if (ZEPHYR_ENABLE === true) {
const {start, end} = summary.stats;
testCycle = ZEPHYR_CYCLE_KEY ? {key: ZEPHYR_CYCLE_KEY} : await createTestCycle(start, end);
}
// Send test report to "QA: Mobile Test Automation Report" channel via webhook
if (TYPE && TYPE !== 'NONE' && WEBHOOK_URL) {
const environment = readJsonFromFile(`${ARTIFACTS_DIR}/environment.json`);
const data = generateTestReport(summary, result && result.success, result && result.reportLink, environment, testCycle.key);
await sendReport('summary report to Community channel', WEBHOOK_URL, data);
}
// Save test cases to Test Management
if (ZEPHYR_ENABLE === 'true') {
await createTestExecutions(allTests, testCycle);
}
};
saveReport();