Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Oct 1, 2024
1 parent 690f8fa commit 885e25e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
27 changes: 25 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43720,6 +43720,7 @@ function getWorkflowRunHtmlUrl(context) {




const context = enhancedContext();
/**
* GitHub Actions bot user
Expand Down Expand Up @@ -43749,6 +43750,7 @@ function run(action) {
}
function getInput(name, options_schema, schema) {
let options;
// noinspection SuspiciousTypeOfGuard
if (options_schema instanceof ZodType) {
schema = options_schema;
}
Expand All @@ -43760,10 +43762,31 @@ function getInput(name, options_schema, schema) {
return undefined;
if (!schema)
return input;
const parseResult = schema.safeParse(input);
let parseResult = schema.safeParse(input);
if (parseResult.error) {
const initialIssue = parseResult.error.issues.at(0);
if (initialIssue?.code === "invalid_type" &&
initialIssue.received === "string" &&
initialIssue.expected !== "string") {
// try parse as yaml/json
parseResult = z.string().transform((val, ctx) => {
try {
return dist.parse(val);
}
catch {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: initialIssue.expected,
received: 'unknown',
});
return z.NEVER;
}
}).pipe(schema).safeParse(input);
}
}
if (parseResult.error) {
const issues = parseResult.error.issues.map(formatZodIssue);
throw new Error(`Invalid value for input '${name}': ${input}\n` +
throw new Error(`Invalid input value for \`${name}\`, received \`${input}\`\n` +
issues.map((it) => ` - ${it}`).join('\n'));
}
return parseResult.data;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as github from '@actions/github';
import {Deployment} from '@octokit/graphql-schema';
import {GitHub} from "@actions/github/lib/utils";
import {getWorkflowRunHtmlUrl} from "./github.js";
import YAML from "yaml";

export const context = enhancedContext()

Expand Down Expand Up @@ -104,6 +105,7 @@ export function getInput<T extends ZodSchema>(
name: string, options_schema?: InputOptions | T, schema?: T
): string | z.infer<T> | undefined {
let options: InputOptions | undefined
// noinspection SuspiciousTypeOfGuard
if (options_schema instanceof ZodSchema) {
schema = options_schema
} else {
Expand All @@ -114,10 +116,32 @@ export function getInput<T extends ZodSchema>(
if (!input) return undefined
if (!schema) return input

const parseResult = schema.safeParse(input)
let parseResult = schema.safeParse(input)
if (parseResult.error) {
const initialIssue = parseResult.error.issues.at(0);
if (initialIssue?.code === "invalid_type" &&
initialIssue.received === "string" &&
initialIssue.expected !== "string"
) {
// try parse as yaml/json
parseResult = z.string().transform((val, ctx) => {
try {
return YAML.parse(val);
} catch {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: initialIssue.expected,
received: 'unknown',
})
return z.NEVER;
}
}).pipe(schema).safeParse(input);
}
}

if (parseResult.error) {
const issues = parseResult.error.issues.map(formatZodIssue)
throw new Error(`Invalid value for input '${name}': ${input}\n` +
throw new Error(`Invalid input value for \`${name}\`, received \`${input}\`\n` +
issues.map((it) => ` - ${it}`).join('\n'))
}

Expand Down

0 comments on commit 885e25e

Please sign in to comment.