This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (120 loc) · 3.25 KB
/
index.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
const compareSnapshots = require('./compareSnapshots');
const makeRequest = require('./makeRequest');
function validateArguments({ sha1, sha2, apiKey, apiSecret, threshold }) {
if (!sha1) {
throw new Error('Missing `sha1` argument');
}
if (!sha2) {
throw new Error('Missing `sha2` argument');
}
if (!apiKey) {
throw new Error('Missing `apiKey` argument');
}
if (!apiSecret) {
throw new Error('Missing `apiSecret` argument');
}
if (!threshold) {
throw new Error('Missing `threshold` argument');
}
if (typeof threshold !== 'number' || threshold > 1 || threshold < 0) {
throw new Error(
'Argument `threshold` needs to be a number between 0 and 1',
);
}
}
function ignore({ before, after, apiKey, apiSecret, endpoint }) {
return makeRequest(
{
url: `${endpoint}/api/ignored-diffs`,
method: 'POST',
json: true,
body: {
snapshot1Id: before.id,
snapshot2Id: after.id,
},
},
{ apiKey, apiSecret },
);
}
module.exports = async function happoDeepCompare(
args,
{ HAPPO_API_KEY, HAPPO_API_SECRET } = process.env,
log = console.log,
) {
args.apiKey = args.apiKey || HAPPO_API_KEY;
args.apiSecret = args.apiSecret || HAPPO_API_SECRET;
args.threshold = args.threshold || 0.05;
args.endpoint = args.endpoint || 'https://happo.io';
const {
sha1,
sha2,
project,
apiKey,
apiSecret,
endpoint,
link,
message,
author,
threshold,
} = args;
validateArguments(args);
log(`Comparing ${sha1} with ${sha2}...`);
// First, make a comparison without passing `link` (this will prevent any
// github status posting from happening).
const firstCompareResult = await makeRequest(
{
url: `${endpoint}/api/reports/${sha1}/compare/${sha2}`,
method: 'POST',
json: true,
body: {
project,
},
},
{ apiKey, apiSecret },
);
const resolved = [];
log(
`Found ${
firstCompareResult.diffs.length
} diffs to deep-compare using threshold ${threshold}`,
);
await Promise.all(
firstCompareResult.diffs.map(async ([before, after]) => {
const diff = await compareSnapshots({ before, after, endpoint });
if (diff < threshold) {
log(
`✓ ${after.component} - ${after.variant} - ${
after.target
} diff (${diff}) is within threshold`,
);
await ignore({ before, after, apiKey, apiSecret, endpoint });
resolved.push([before, after]);
} else {
log(
`✗ ${after.component} - ${after.variant} - ${
after.target
} diff (${diff}) is larger than threshold`,
);
}
}),
);
// Make second compare call to finalize the deep compare. The second call will
// cause a status to be posted to the PR (if applicable). Any ignored diffs
// from the first call will be excluded from the result.
const secondCompareResult = await makeRequest(
{
url: `${endpoint}/api/reports/${sha1}/compare/${sha2}`,
method: 'POST',
json: true,
body: {
link,
message,
author,
project,
},
},
{ apiKey, apiSecret },
);
log(secondCompareResult.summary);
return Object.assign({ resolved }, secondCompareResult);
};