-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreporter.ts
204 lines (174 loc) · 5.27 KB
/
reporter.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
PutObjectCommand,
PutObjectCommandInput,
S3Client,
} from "@aws-sdk/client-s3";
import type {
AwsCredentialIdentity,
Provider,
Endpoint,
EndpointV2,
UserAgent,
} from "@smithy/types";
import type { Reporter } from "@playwright/test/reporter";
import { createReadStream } from "fs";
import { readdir } from "fs/promises";
import path from "path";
import mime from "mime";
export interface S3ReporterOptions {
/**
* AWS credentials required for authentication.
* @property {string} accessKeyId - AWS access key ID.
* @property {string} secretAccessKey - AWS secret access key.
*/
credentials: AwsCredentialIdentity | Provider<AwsCredentialIdentity>;
/**
* The endpoint URL of the S3 service.
* Optional. If not specified, the default AWS endpoint is used.
* @default s3.<region>.amazonaws.com
*/
endpoint?:
| string
| Endpoint
| Provider<Endpoint>
| EndpointV2
| Provider<EndpointV2>;
/**
* Flag to enable or disable SSL for the connection.
* Optional. Defaults to true if not provided.
* @default true
*/
sslEnabled?: boolean;
/**
* AWS region where the S3 bucket is located.
* Optional. If not specified, the default region is used.
*/
region?: string | Provider<string>;
/**
* A custom user agent string to be used in requests to AWS services.
* Optional.
*/
customUserAgent?: string | UserAgent;
/**
* The maximum number of attempts to make for a request.
* Optional. If not specified, the default retry strategy is used.
*/
maxAttempts?: number | Provider<number>;
/**
* The name of the S3 bucket where reports will be uploaded.
*/
bucketName: string;
/**
* The base key (prefix) under which the reports will be uploaded in the bucket.
* Optional. If not provided, files are uploaded to the root of the bucket.
*/
baseUploadKey?: string;
/**
* Flag to enable or disable the upload of test results.
* Optional. Defaults to false if not provided.
* @default false
*/
uploadTestResults?: boolean;
/**
* Flag to enable or disable the upload of the report.
* Optional. Defaults to false if not provided.
* @default false
*/
uploadReport?: boolean;
}
class S3Reporter implements Reporter {
constructor(protected options: S3ReporterOptions) {}
async onExit(): Promise<void> {
console.log(`[${S3Reporter.name}] Discovering files...`);
const {
credentials,
endpoint,
sslEnabled,
region,
customUserAgent,
maxAttempts,
bucketName,
baseUploadKey,
uploadTestResults,
uploadReport,
} = this.options;
const s3 = new S3Client({
credentials,
endpoint,
forcePathStyle: true,
tls: sslEnabled,
region,
customUserAgent,
maxAttempts,
});
const files: string[] = [];
if (uploadTestResults) {
const testResultsFiles = await this.getFiles("test-results");
files.push(...testResultsFiles);
console.log(
`[${S3Reporter.name}] Discovered ${testResultsFiles.length} files in test-results/.`,
);
}
if (uploadReport) {
const playwrightReportFiles = await this.getFiles("playwright-report");
files.push(...playwrightReportFiles);
console.log(
`[${S3Reporter.name}] Discovered ${playwrightReportFiles.length} files in playwright-report/.`,
);
}
console.log(`[${S3Reporter.name}] Uploading ${files.length} files...`);
let totalUploadErrors = 0;
const uploads = files.map(async (filePath) => {
const metaData: Record<string, string> = {};
let sourceDirectory = "";
if (filePath.includes("test-results")) {
sourceDirectory = "test-results";
}
if (filePath.includes("playwright-report")) {
sourceDirectory = "playwright-report";
}
const key = path
.join(
baseUploadKey ?? "",
sourceDirectory,
path.relative(sourceDirectory, filePath),
)
.split(/[\\/]/g)
.join("/");
try {
const putObjectParams: PutObjectCommandInput = {
Bucket: bucketName,
Key: key,
Body: createReadStream(filePath),
Metadata: metaData,
ContentType: mime.getType(filePath),
};
const putObjectCommand = new PutObjectCommand(putObjectParams);
const response = await s3.send(putObjectCommand);
console.log(
`[${S3Reporter.name}] File uploaded successfully: ${key} (${response.$metadata.httpStatusCode})`,
);
} catch (error) {
console.error(`[${S3Reporter.name}] Error uploading file: `, error);
totalUploadErrors += 1;
}
});
await Promise.all(uploads);
console.log(
`[${S3Reporter.name}] Upload completed with ${totalUploadErrors} errors!`,
);
}
protected async getFiles(directory: string): Promise<string[]> {
const _directories = await readdir(directory, { withFileTypes: true });
const _files = await Promise.all(
_directories.map((_directory) => {
const resolvedPath = path.resolve(directory, _directory.name);
return _directory.isDirectory()
? this.getFiles(resolvedPath)
: resolvedPath;
}),
);
return Array.prototype.concat(..._files);
}
}
export default S3Reporter;