Skip to content

Commit 91da6bd

Browse files
committed
check-run: use Typia for type checking
This not only makes the validation more robust, easier to read (and verify the logic), but also prepares for persisting the parameters in an Action state so that subsequent calls to the Action can update, say, the text, without having to re-specify all of the parameters. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent d18d4f9 commit 91da6bd

File tree

1 file changed

+46
-54
lines changed

1 file changed

+46
-54
lines changed

lib/ci-helper.ts

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -285,64 +285,56 @@ export class CIHelper {
285285
}
286286
}
287287

288-
protected static validateConclusion = typia.createValidate<ConclusionType>();
289-
290288
protected async createOrUpdateCheckRun(): Promise<void> {
291-
const { owner, repo, pull_number } = getPullRequestOrCommentKeyFromURL(core.getInput("pr-url"));
292-
let check_run_id = ((id?: string) => (!id ? undefined : Number.parseInt(id, 10)))(
293-
core.getInput("check-run-id"),
294-
);
295-
const name = core.getInput("name") || undefined;
296-
const title = core.getInput("title") || undefined;
297-
const summary = core.getInput("summary") || undefined;
298-
const text = core.getInput("text") || undefined;
299-
const detailsURL = core.getInput("details-url") || undefined;
300-
const conclusion = core.getInput("conclusion") || undefined;
301-
302-
if (!check_run_id) {
303-
const problems = [];
304-
if (!name) problems.push(`name is required`);
305-
if (!title) problems.push(`title is required`);
306-
if (!summary) problems.push(`summary is required`);
307-
if (conclusion) {
308-
const result = CIHelper.validateConclusion(conclusion);
309-
if (!result.success) problems.push(result.errors);
289+
type CheckRunParameters = {
290+
owner: string;
291+
repo: string;
292+
pull_number: number;
293+
check_run_id?: number;
294+
name: string;
295+
output?: {
296+
title: string;
297+
summary: string;
298+
text?: string;
299+
};
300+
detailsURL?: string;
301+
conclusion?: ConclusionType;
302+
};
303+
const params = {} as CheckRunParameters;
304+
305+
const validateCheckRunParameters = () => {
306+
const result = typia.createValidate<CheckRunParameters>()(params);
307+
if (!result.success) {
308+
throw new Error(
309+
`Invalid check-run state:\n- ${result.errors
310+
.map((e) => `${e.path} (value: ${e.value}, expected: ${e.expected}): ${e.description}`)
311+
.join("\n- ")}`,
312+
);
310313
}
311-
if (problems.length) throw new Error(`Could not create Check Run:${JSON.stringify(problems, null, 2)}`);
312-
313-
({ id: check_run_id } = await this.github.createCheckRun({
314-
owner,
315-
repo,
316-
pull_number,
317-
name: name!,
318-
output: {
319-
title: title!,
320-
summary: summary!,
321-
text,
322-
},
323-
detailsURL,
324-
conclusion: conclusion as ConclusionType | undefined,
325-
}));
326-
core.setOutput("check-run-id", check_run_id);
327-
} else {
328-
const problems = [];
329-
if (name) problems.push(`Specify either check-run-id or name but not both`);
330-
if (!summary && (title || text)) problems.push(`title or text require a summary`);
331-
if (problems.length) throw new Error(`Could not create Check Run:${JSON.stringify(problems, null, 2)}`);
314+
};
332315

316+
["pr-url", "check-run-id", "name", "title", "summary", "text", "details-url", "conclusion"]
317+
.map((name) => [name.replaceAll("-", "_"), core.getInput(name)] as const)
318+
.forEach(([key, value]) => {
319+
if (!value) return;
320+
if (key === "pr-url") Object.assign(params, getPullRequestOrCommentKeyFromURL(value));
321+
else if (key === "check-run-id") params.check_run_id = Number.parseInt(value, 10);
322+
else if (key === "details-url") params.detailsURL = value;
323+
else if (key === "title" || key === "summary" || key === "text") {
324+
if (!params.output) Object.assign(params, { output: {} });
325+
(params.output as { [key: string]: string })[key] = value;
326+
} else (params as unknown as { [key: string]: string })[key.replaceAll("-", "_")] = value;
327+
});
328+
validateCheckRunParameters();
329+
330+
if (params.check_run_id === undefined) {
331+
({ id: params.check_run_id } = await this.github.createCheckRun(params));
332+
core.setOutput("check-run-id", params.check_run_id);
333+
} else {
333334
await this.github.updateCheckRun({
334-
owner,
335-
repo,
336-
check_run_id,
337-
output: summary
338-
? {
339-
title,
340-
summary,
341-
text,
342-
}
343-
: undefined,
344-
detailsURL,
345-
conclusion: conclusion as ConclusionType | undefined,
335+
...params,
336+
// needed to pacify TypeScript's concerns about the ID being potentially undefined
337+
check_run_id: params.check_run_id,
346338
});
347339
}
348340
}

0 commit comments

Comments
 (0)