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

Infer type implicitly #18

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function githubPluginType() {
function githubPluginType(): Plugin {

It might be safer to declare the type here instead of typecasting it below

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
Loading