-
Notifications
You must be signed in to change notification settings - Fork 533
/
usage-tracking.ts
57 lines (54 loc) · 1.51 KB
/
usage-tracking.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
import { makeRequest } from '../../../../../lib/request';
import config from '../../../../../lib/config';
import { getAuthHeader } from '../../../../../lib/api-token';
import { CustomError } from '../../../../../lib/errors';
export async function trackUsage(
formattedResults: TrackableResult[],
org: string, // e.g. "my.org"
): Promise<void> {
const trackingData = formattedResults.map((res) => {
return {
isPrivate: res.meta.isPrivate,
issuesPrevented: res.result.cloudConfigResults.length,
};
});
const trackingResponse = await makeRequest({
method: 'POST',
headers: {
Authorization: getAuthHeader(),
},
url: `${config.API}/track-iac-usage/cli`,
body: { results: trackingData },
qs: { org },
gzip: true,
json: true,
});
switch (trackingResponse.res.statusCode) {
case 200:
break;
case 429:
throw new TestLimitReachedError();
default:
throw new CustomError(
'An error occurred while attempting to track test usage: ' +
JSON.stringify(trackingResponse.res.body),
);
}
}
export class TestLimitReachedError extends CustomError {
constructor() {
super(
'Test limit reached! You have exceeded your infrastructure as code test allocation for this billing period.',
);
}
}
// Sub-interface of FormattedResult that we really only use to make test
// fixtures easier to create.
export interface TrackableResult {
meta: {
isPrivate: boolean;
};
result: {
cloudConfigResults: any[];
};
}