-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
147 lines (124 loc) · 4.02 KB
/
index.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
import axios, { AxiosRequestConfig } from 'axios';
import { Status as CucumberStatus } from 'cucumber';
import FormData from 'form-data';
import {
ICreateLogRequestBody,
IFinishLaunchRequest,
IFinishTestRequest,
INamedFileBuffer,
IPostItemRequest,
IReportPortalPostResponse,
IStartLaunchRequest,
ItemType,
LaunchMode,
LogLevel,
ReportPortalTestStatus
} from './models';
export default class ReportPortalClient {
private readonly statusMap: Map<CucumberStatus, ReportPortalTestStatus> = new Map([
[CucumberStatus.PASSED, 'PASSED'],
[CucumberStatus.FAILED, 'FAILED'],
[CucumberStatus.SKIPPED, 'SKIPPED'],
[CucumberStatus.PENDING, 'FAILED'],
[CucumberStatus.AMBIGUOUS, 'FAILED'],
]);
private readonly requestConfig: AxiosRequestConfig;
constructor(
private readonly baseUrl: string,
authToken: string) {
this.requestConfig = {
headers: {
Authorization: `Bearer ${authToken}`,
Accept: 'application/json',
}
};
}
public startLaunch(name: string, description: string, mode: LaunchMode) {
const request: IStartLaunchRequest = {
name,
start_time: Date.now(),
description,
mode,
tags: []
};
return axios
.post<IReportPortalPostResponse>(`${this.baseUrl}/launch`, request, this.requestConfig)
.then(response => response.data.id);
}
public async finishLaunch(
{ launchId, description, status }: { launchId: string, description?: string, status?: CucumberStatus }) {
const request: IFinishLaunchRequest = {
status: status ? this.statusMap.get(status) || undefined : undefined,
end_time: Date.now(),
description,
tags: []
};
await axios.put(`${this.baseUrl}/launch/${launchId}/finish`, request, this.requestConfig);
}
public async stopLaunch(
{ launchId, description, status }: { launchId: string; description?: string; status?: CucumberStatus; }) {
const request: IFinishLaunchRequest = {
status: status ? this.statusMap.get(status) || undefined : undefined,
end_time: Date.now(),
description,
tags: []
};
await axios.put(`${this.baseUrl}/launch/${launchId}/stop`, request, this.requestConfig);
}
public createItem(
testName: string, testDescription: string, testItemType: ItemType, launchId: string, parentItemId?: string) {
const request: IPostItemRequest = {
description: testDescription,
launch_id: launchId,
name: testName,
parameters: [],
retry: false,
start_time: Date.now(),
tags: [],
type: testItemType,
};
const pathSuffix = (parentItemId && `/${parentItemId}`) || '';
return axios
.post<IReportPortalPostResponse>(`${this.baseUrl}/item${pathSuffix}`, request, this.requestConfig)
.then(response => response.data.id);
}
public async finishItem(itemId: string, status: CucumberStatus) {
const request: IFinishTestRequest = {
end_time: Date.now(),
status: this.statusMap.get(status) || undefined,
tags: [],
};
await axios.put(`${this.baseUrl}/item/${itemId}`, request, this.requestConfig);
}
public async addLogToItem(itemId: string, level: LogLevel, message: string, namedFileBuffer?: INamedFileBuffer) {
const request: ICreateLogRequestBody = {
item_id: itemId,
level,
message,
time: Date.now()
};
const formData = new FormData();
if (namedFileBuffer) {
formData.append(
'file',
namedFileBuffer.buffer,
namedFileBuffer.filename,
);
request.file = { name: namedFileBuffer.filename };
}
formData.append(
'json_request_part',
JSON.stringify([request]),
{ contentType: 'application/json' },
);
return axios
.post<IReportPortalPostResponse>(`${this.baseUrl}/log`, formData, {
...this.requestConfig,
headers: {
...this.requestConfig.headers,
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
}
})
.then(response => response.data.id);
}
}