-
Notifications
You must be signed in to change notification settings - Fork 18
/
record.js
executable file
·175 lines (149 loc) · 4.65 KB
/
record.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
// run with "DEBUG=axios" to see debug logs
import axiosDebugLog from "axios-debug-log";
axiosDebugLog({
request: function (debug, config) {
debug(`${config.method.toUpperCase()} ${config.url}`);
},
response: () => {},
error: () => {},
});
import axios from "axios";
import Bottleneck from "bottleneck";
import chalk from "chalk";
import cloneDeep from "lodash/cloneDeep.js";
import { diff, diffString } from "json-diff";
import glob from "glob";
import humanize from "humanize-string";
import minimist from "minimist";
import normalize from "../lib/normalize/index.js";
import read from "../lib/read.js";
import recordScenario from "../lib/record-scenario.js";
import write from "../lib/write.js";
const argv = minimist(process.argv.slice(2), {
boolean: ["update", "test-cached"],
});
const doUpdate = argv.update;
const selectedScenarios = argv._;
const hasSelectedScenarios = selectedScenarios.length > 0;
console.log(`hasSelectedScenarios`);
console.log(hasSelectedScenarios);
const scenarios = hasSelectedScenarios
? selectedScenarios
: glob.sync("scenarios/**/record.js");
const diffs = [];
console.log(`scenarios`);
console.log(scenarios);
// run scenarios one by one
scenarios
.reduce(async (promise, scenarioPath) => {
await promise;
const fixtureName = scenarioPath.replace(
/(^scenarios\/|\/record\.js$)/g,
""
);
const [domain, title] = fixtureName.split("/");
console.log("");
console.log(
`⏯️ ${chalk.bold(domain)}: ${humanize(title.replace(".js", ""))} ...`
);
const request = axios.create({
baseURL: `https://${domain}`,
maxRedirects: 0, // record redirects explicitly
});
// throttle writing requests
// https://developer.github.com/v3/guides/best-practices-for-integrators/#dealing-with-abuse-rate-limits
const limiter = new Bottleneck({
maxConcurrent: 1,
minTime: 3000,
});
request.interceptors.request.use((config) => {
if (
!["POST", "PATCH", "PUT", "DELETE"].includes(
config.method.toUpperCase()
)
) {
return config;
}
return limiter.schedule(async () => config);
});
const oldNormalizedFixtures = await read(fixtureName);
const newRawFixtures = await recordScenario({
request: request,
scenario: await import(`../scenarios/${fixtureName}/record.js`),
});
const scenarioState = {
commitSha: {}, // map original commit sha hashes to normalized commit hashes
ids: {},
};
const newNormalizedFixtures = await Promise.all(
newRawFixtures
.map(cloneDeep)
.filter(hasntIgnoreHeader)
.map(normalize.bind(null, scenarioState))
);
const fixturesDiffs = diff(newNormalizedFixtures, oldNormalizedFixtures);
if (!fixturesDiffs) {
console.log("✅ Fixtures are up-to-date");
return;
}
diffs.push({
name: fixtureName,
changes: fixturesDiffs,
newNormalizedFixtures,
oldNormalizedFixtures,
newRawFixtures,
});
if (fixturesDiffs[0][0] === "-") {
if (doUpdate) {
console.log("📼 New fixtures recorded");
return write(fixtureName, {
normalized: newNormalizedFixtures,
raw: newRawFixtures,
});
}
console.log(`❌ "${fixtureName}" looks like a new fixture`);
return;
}
if (doUpdate) {
console.log("📼 Fixture updates recorded");
return write(fixtureName, {
normalized: newNormalizedFixtures,
raw: newRawFixtures,
});
}
console.log("❌ Fixtures are not up-to-date");
console.log(diffString(oldNormalizedFixtures, newNormalizedFixtures));
console.log(
`💁 Update fixtures with \`${chalk.bold("bin/record.js --update")}\``
);
}, Promise.resolve())
.then(() => {
if (diffs.length === 0) {
console.log("🤖 No fixture changes detected in cron job.");
return;
}
if (doUpdate) {
return;
}
console.log(`${diffs.length} fixtures are out of date. Exit 1`);
process.exit(1);
})
.catch((error) => {
if (!error.response) {
console.log(error);
process.exit(1);
}
console.log(error.toString());
console.log("REQUEST");
console.log(`${error.config.method.toUpperCase()} ${error.config.url}`);
console.log("RESPONSE HEADERS");
console.log(JSON.stringify(error.response.headers, null, 2));
console.log("RESPONSE BODY");
console.log(JSON.stringify(error.response.data, null, 2));
process.exit(1);
});
function hasntIgnoreHeader(fixture) {
const hasIgnoreHeader = "x-octokit-fixture-ignore" in fixture.reqheaders;
return !hasIgnoreHeader;
}