Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
feat: infer type implicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed May 28, 2024
1 parent b85c169 commit 2a02325
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/types/configuration/plugin-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,56 @@ import { githubWebhookEvents } from "./webhook-events";
const pluginNameRegex = new RegExp("^([0-9a-zA-Z-._]+)\\/([0-9a-zA-Z-._]+)(?::([0-9a-zA-Z-._]+))?(?:@([0-9a-zA-Z-._]+(?:\\/[0-9a-zA-Z-._]+)?))?$");

type GithubPlugin = {
type: "github";
owner: string;
repo: string;
workflowId: string;
ref?: string;
};

export function githubPluginType() {
type HttpsPlugin = {
type: "https";
url: string;
};

export type Plugin = GithubPlugin | HttpsPlugin;

function githubPluginType() {
return T.Transform(T.String())
.Decode((value) => {
if (value.startsWith("https://")) {
return {
type: "https",
url: value,
} as Plugin;
}
const matches = value.match(pluginNameRegex);
if (!matches) {
throw new Error(`Invalid plugin name: ${value}`);
}
return {
type: "github",
owner: matches[1],
repo: matches[2],
workflowId: matches[3] || "compute.yml",
ref: matches[4] || undefined,
} as GithubPlugin;
} as Plugin;
})
.Encode((value) => {
return `${value.owner}/${value.repo}${value.workflowId ? ":" + value.workflowId : ""}${value.ref ? "@" + value.ref : ""}`;
if (value.type === "github") {
return `${value.owner}/${value.repo}${value.workflowId ? ":" + value.workflowId : ""}${value.ref ? "@" + value.ref : ""}`;
} else if (value.type === "https") {
return value.url;
} else {
throw new Error(`Invalid plugin type`);
}
});
}

const pluginChainSchema = T.Array(
T.Object({
id: T.Optional(T.String()),
plugin: githubPluginType(),
type: T.Union([T.Literal("github")], { default: "github" }),
with: T.Record(T.String(), T.Unknown()),
}),
{ minItems: 1 }
Expand Down

0 comments on commit 2a02325

Please sign in to comment.