forked from w3c/spec-prod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare.js
184 lines (158 loc) · 4.72 KB
/
prepare.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
176
177
178
179
180
181
182
183
184
// @ts-check
const { existsSync } = require("fs");
const {
env,
exit,
formatAsHeading,
pprint,
setOutput,
yesOrNo,
} = require("./utils.js");
main();
async function main() {
/** @type {Inputs} */
const inputs = JSON.parse(env("INPUTS_USER"));
/** @type {GitHubContext} */
const githubContext = JSON.parse(env("INPUTS_GITHUB"));
console.log(formatAsHeading("Provided input"));
pprint(inputs);
console.log();
const normalizedInputs = await processInputs(inputs, githubContext);
console.log(`\n${formatAsHeading("Normalized input")}`);
pprint(normalizedInputs);
// Make processed inputs available to next steps.
setOutput("all", JSON.stringify(normalizedInputs));
}
/**
* @typedef {object} Inputs
* @property {"respec" | "bikeshed"} [inputs.TOOLCHAIN]
* @property {string} [inputs.SOURCE]
* @property {string} [inputs.VALIDATE_LINKS]
* @property {string} [inputs.VALIDATE_MARKUP]
* @property {string} [inputs.GH_PAGES_BRANCH]
* @property {string} [inputs.GH_PAGES_TOKEN]
* @property {string} [inputs.W3C_ECHIDNA_TOKEN]
* @property {string} [inputs.W3C_MANIFEST_URL]
* @property {string} [inputs.W3C_WG_DECISION_URL]
* @property {string} [inputs.W3C_NOTIFICATIONS_CC]
*
* @typedef {{ default_branch: string, has_pages: boolean }} Repository
*
* @typedef {object} GitHubContext
* @property {string} GitHubContext.token
* @property {string} GitHubContext.event_name
* @property {string} GitHubContext.repository
* @property {string} GitHubContext.sha
* @property {string} GitHubContext.actor
* @property {{ repository: Repository }} GitHubContext.event
*
* @param {Inputs} inputs
* @param {GitHubContext} githubContext
*/
async function processInputs(inputs, githubContext) {
return {
...toolchainAndSource(inputs),
validate: validation(inputs),
deploy: {
ghPages: githubPagesDeployment(inputs, githubContext),
w3c: await w3cEchidnaDeployment(inputs, githubContext),
},
};
}
/**
* Figure out "toolchain" and input "source".
* // TODO: refactor this to remove duplicate logic.
* @param {Inputs} inputs
*/
function toolchainAndSource(inputs) {
let toolchain = inputs.TOOLCHAIN;
let source = inputs.SOURCE;
// HARD-CODING
toolchain = "bikeshed";
source = "index.bs";
return { toolchain, source };
}
/**
* Figure out validation requests.
* @param {Inputs} inputs
*/
function validation(inputs) {
const links = yesOrNo(inputs.VALIDATE_LINKS) || false;
const markup = yesOrNo(inputs.VALIDATE_MARKUP) || false;
return { links, markup };
}
/**
* @template T
* @typedef {T extends PromiseLike<infer U> ? U : T} ThenArg<T>
*/
/**
* Figure out GitHub pages deployment.
* @param {Inputs} inputs
* @param {GitHubContext} githubContext
* @typedef {ReturnType<typeof githubPagesDeployment>} GithubPagesDeployOptions
*/
function githubPagesDeployment(inputs, githubContext) {
const { event_name: event, sha, repository, actor } = githubContext;
const {
default_branch: defaultBranch,
has_pages: hasGitHubPagesEnabled,
} = githubContext.event.repository;
const ghPagesBranch = inputs.GH_PAGES_BRANCH;
if (!shouldTryDeploy(event)) {
return false;
}
const askedNotToDeploy = yesOrNo(ghPagesBranch) === false;
if (askedNotToDeploy || !ghPagesBranch) {
return false;
}
const targetBranch = yesOrNo(ghPagesBranch) ? "gh-pages" : ghPagesBranch;
if (defaultBranch === targetBranch) {
exit(`Default branch and "ghPages": "${targetBranch}" cannot be same.`);
}
if (!hasGitHubPagesEnabled) {
console.log(`📣 Please enable GitHub pages in repository settings.`);
}
let token = inputs.GH_PAGES_TOKEN;
if (!token) {
token = githubContext.token;
}
return { targetBranch, token, event, sha, repository, actor };
}
/**
* Figure out GitHub pages deployment.
* @param {Inputs} inputs
* @param {GitHubContext} githubContext
* @typedef {ThenArg<ReturnType<typeof w3cEchidnaDeployment>>} W3CDeployOptions
*/
async function w3cEchidnaDeployment(inputs, githubContext) {
const { event_name: event, repository } = githubContext;
if (!shouldTryDeploy(event)) {
return false;
}
const token = inputs.W3C_ECHIDNA_TOKEN;
const wgDecisionURL = inputs.W3C_WG_DECISION_URL;
if (!token || !wgDecisionURL) {
console.log(
"📣 Skipping deploy to W3C as required inputs were not provided.",
);
return false;
}
let manifest = inputs.W3C_MANIFEST_URL;
if (!manifest) {
if (existsSync("ECHIDNA")) {
const [owner, name] = repository.split("/");
manifest = `https://${owner}.github.io/${name}/ECHIDNA`;
} else {
console.log(`🚧 Echidna manifest file was not found.`);
return false;
}
}
const cc = inputs.W3C_NOTIFICATIONS_CC;
return { manifest, wgDecisionURL, cc, token: token };
}
/**
* @param {string} githubEvent
*/
function shouldTryDeploy(githubEvent) {
return githubEvent === "push";
}