-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcheck-or-update-webhooks.mts
119 lines (100 loc) · 3.08 KB
/
check-or-update-webhooks.mts
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
import { writeFileSync, readFileSync } from "fs";
import { diff, diffString } from "json-diff";
import * as prettier from "prettier";
import {
State,
WorkableWebhook,
applyWorkarounds,
getActionsAndExamplesFromPayloads,
getHtml,
getSections,
toWebhook,
} from "./index.mjs";
const isNotNull = <T extends any>(value: T | null): value is T =>
value !== null;
export const checkOrUpdateWebhooks = async ({
cached,
checkOnly,
ghe,
githubAE,
updateAll,
}: State): Promise<void> => {
if (updateAll) {
await checkOrUpdateWebhooks({
cached,
checkOnly,
ghe: "",
});
await checkOrUpdateWebhooks({
cached,
checkOnly,
githubAE: true,
});
}
if (ghe === "") {
const gheVersions = ["3.1", "3.2", "3.3", "3.4", "3.5"];
for (let gheVersion of gheVersions) {
await checkOrUpdateWebhooks({
cached,
checkOnly,
ghe: gheVersion,
});
}
return;
}
const [baseUrl, folderName] = ghe
? [
`https://docs.github.com/en/enterprise-server@${ghe}/developers/webhooks-and-events/webhooks/webhook-events-and-payloads`,
`ghes-${ghe.replace(".", "")}`,
]
: githubAE
? [
"https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhooks/webhook-events-and-payloads",
"github.ae",
]
: [
"https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhooks/webhook-events-and-payloads",
"api.github.com",
];
const currentWebhooks = JSON.parse(
readFileSync(`./payload-examples/${folderName}/index.json`).toString(),
);
const html = await getHtml({ cached, baseUrl, folderName });
const sections = getSections(html);
const webhooksFromScrapingDocs = sections.map(toWebhook).filter(isNotNull);
const webhooksFromPayloadExamplesByName =
getActionsAndExamplesFromPayloads(folderName);
const webhooks = webhooksFromScrapingDocs.map((webhook) => {
const name = webhook.name;
if (!(name in webhooksFromPayloadExamplesByName)) {
console.warn(`[${folderName}] No payload examples for ${name}`);
return webhook;
}
const webhookFromPayloadExamples = webhooksFromPayloadExamplesByName[name];
return {
name,
description: webhook.description,
actions: Array.from(
new Set(webhook.actions.concat(webhookFromPayloadExamples.actions)),
),
properties: webhook.properties,
examples: webhook.examples.concat(webhookFromPayloadExamples.examples),
};
});
applyWorkarounds(webhooks as WorkableWebhook[]);
if (!diff(currentWebhooks, webhooks)) {
console.log(`✅ webhooks ${folderName} are up-to-date`);
return;
}
console.log(`❌ webhooks ${folderName} are not up-to-date`);
console.log(diffString(currentWebhooks, webhooks));
if (checkOnly) {
process.exitCode = 1;
return;
}
writeFileSync(
`./payload-examples/${folderName}/index.json`,
await prettier.format(JSON.stringify(webhooks), { parser: "json" }),
);
console.log(`✏️ ${folderName}/index.json, written`);
};