export type ActionContext = {
with: Record<string, unknown>;
env: Record<string, string>;
workspace: string;
runId: string;
jobId: string;
repoId: string;
commitSha?: string | null;
ref?: string | null;
};
export type ActionResult = {
exitCode: number;
outputs?: Record<string, string>;
stdout?: string;
stderr?: string;
};
export type ActionHandler = {
name: string;
version: string;
run: (ctx: ActionContext) => Promise<ActionResult>;
};
const handlers = new Map<string, ActionHandler>();
const latestByName = new Map<string, string>();
export function registerAction(handler: ActionHandler): void {
if (!handler || !handler.name || !handler.version) return;
const key = `${handler.name}@${handler.version}`;
handlers.set(key, handler);
latestByName.set(handler.name, handler.version);
}
function parseUses(uses: string): { name: string; version: string | null } | null {
if (!uses || typeof uses !== "string") return null;
const trimmed = uses.trim();
if (!trimmed) return null;
const at = trimmed.lastIndexOf("@");
if (at === -1) {
return { name: trimmed, version: null };
}
const name = trimmed.slice(0, at).trim();
const version = trimmed.slice(at + 1).trim();
if (!name) return null;
return { name, version: version || null };
}
export function resolveAction(uses: string): ActionHandler | null {
const parsed = parseUses(uses);
if (!parsed) return null;
if (parsed.version) {
const key = `${parsed.name}@${parsed.version}`;
return handlers.get(key) ?? null;
}
const latest = latestByName.get(parsed.name);
if (latest) {
const key = `${parsed.name}@${latest}`;
const h = handlers.get(key);
if (h) return h;
}
return handlers.get(`${parsed.name}@v1`) ?? null;
}
export function listActions(): { name: string; version: string }[] {
return Array.from(handlers.values()).map((h) => ({
name: h.name,
version: h.version,
}));
}
import { checkoutAction } from "./actions/checkout-action";
import { gatetestAction } from "./actions/gatetest-action";
import { cacheAction } from "./actions/cache-action";
import { uploadArtifactAction } from "./actions/upload-artifact-action";
import { downloadArtifactAction } from "./actions/download-artifact-action";
let registered = false;
export function registerAll(): void {
if (registered) return;
registerAction(checkoutAction);
registerAction(gatetestAction);
registerAction(cacheAction);
registerAction(uploadArtifactAction);
registerAction(downloadArtifactAction);
registered = true;
}
registerAll();
|