summaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'hooks')
-rw-r--r--hooks/Dockerfile4
-rw-r--r--hooks/deno.json2
-rw-r--r--hooks/main.ts12
-rw-r--r--[-rwxr-xr-x]hooks/mod.ts80
-rw-r--r--hooks/server/ci.ts55
-rw-r--r--hooks/server/health.ts25
-rw-r--r--hooks/server/job/activity.ts100
-rw-r--r--hooks/server/job/mod.ts2
-rw-r--r--hooks/server/job/queuer.ts78
-rw-r--r--hooks/server/mod.ts3
10 files changed, 281 insertions, 80 deletions
diff --git a/hooks/Dockerfile b/hooks/Dockerfile
index c1ebf7f..d7763a1 100644
--- a/hooks/Dockerfile
+++ b/hooks/Dockerfile
@@ -1,3 +1,5 @@
-FROM oci.liz.coffee/img/liz-ci:release AS hooks
+FROM oci.liz.coffee/@emprespresso/ci-base:release AS hooks
+
+HEALTHCHECK [ "curl --fail http://localhost:9000/health" ]
CMD [ "/app/hooks/mod.ts" ]
diff --git a/hooks/deno.json b/hooks/deno.json
index c4e8fca..cdaf63f 100644
--- a/hooks/deno.json
+++ b/hooks/deno.json
@@ -1,4 +1,4 @@
{
- "name": "@liz-ci/hooks",
+ "name": "@emprespresso/ci-hooks",
"exports": "./mod.ts"
}
diff --git a/hooks/main.ts b/hooks/main.ts
new file mode 100644
index 0000000..21a3f3f
--- /dev/null
+++ b/hooks/main.ts
@@ -0,0 +1,12 @@
+#!/usr/bin/env -S deno run --allow-env --allow-net --allow-run
+
+import { CiHookServer } from "./server/mod.ts";
+
+const server = new CiHookServer();
+
+const SERVER_CONFIG = {
+ host: "0.0.0.0",
+ port: 9000,
+};
+
+Deno.serve(SERVER_CONFIG, (request: Request) => server.serve(request));
diff --git a/hooks/mod.ts b/hooks/mod.ts
index f858ad0..cc15112 100755..100644
--- a/hooks/mod.ts
+++ b/hooks/mod.ts
@@ -1,78 +1,2 @@
-#!/usr/bin/env -S deno run --allow-env --allow-net --allow-run
-
-import {
- getRequiredEnv,
- getStdout,
- loggerWithPrefix,
- validateIdentifier,
-} from "@liz-ci/utils";
-
-const getRequestLogger = (req: Request) => {
- const url = new URL(req.url);
- const traceId = crypto.randomUUID();
- const getPrefix = () =>
- `[${
- new Date().toISOString()
- }] RequestTrace=[${traceId}] @ [${url.pathname}] -X [${req.method}] |`;
- return loggerWithPrefix(getPrefix);
-};
-
-const addr = { port: 9000, hostname: "0.0.0.0" };
-Deno.serve(addr, async (req) => {
- const logger = getRequestLogger(req);
- logger.log("start");
-
- try {
- const { pathname } = new URL(req.url);
- if (pathname === "/health") {
- try {
- getRequiredEnv("LAMINAR_HOST");
- await getStdout(["laminarc", "show-jobs"]);
- return new Response("think im healthy!! lets get to work.\n", {
- status: 200,
- });
- } catch (e) {
- logger.error(e);
- return new Response("i need to eat more vegetables -.-\n", {
- status: 500,
- });
- }
- }
-
- if (req.method !== "POST") {
- return new Response("invalid method", {
- status: 405,
- });
- }
-
- if (pathname === "/checkout_ci") {
- const { remote, rev, refname } = await req.json();
- if (![remote, rev, refname].every(validateIdentifier)) {
- logger.log("invalid reqwest");
- return new Response("invalid reqwest >:D\n", {
- status: 400,
- });
- }
-
- const laminar = await getStdout([
- "laminarc",
- "queue",
- "checkout_ci",
- `remote="${remote}"`,
- `rev="${rev}"`,
- `refname="${refname}"`,
- ]);
- logger.log(`successful queue :D`, laminar);
- return new Response(laminar + "\n", {
- status: 200,
- });
- }
-
- return new Response("idk what that is bro :((\n", { status: 404 });
- } catch (e) {
- logger.error("uncaught exception", e);
- return new Response("womp womp D:\n", { status: 500 });
- } finally {
- logger.log("finish");
- }
-});
+export * from "./server/mod.ts";
+export * from "./main.ts";
diff --git a/hooks/server/ci.ts b/hooks/server/ci.ts
new file mode 100644
index 0000000..8f06124
--- /dev/null
+++ b/hooks/server/ci.ts
@@ -0,0 +1,55 @@
+import {
+ FourOhFourActivityImpl,
+ getRequiredEnv,
+ HealthCheckActivityImpl,
+ type HealthChecker,
+ type IFourOhFourActivity,
+ type IHealthCheckActivity,
+ type ITraceable,
+ PenguenoRequest,
+ type ServerTrace,
+ TraceUtil,
+} from "@emprespresso/pengueno";
+import type { Job } from "@emprespresso/ci-model";
+import {
+ healthCheck as _healthCheck,
+ type IJobHookActivity,
+ type IJobQueuer,
+ JobHookActivityImpl,
+ LaminarJobQueuer,
+} from "@emprespresso/ci-hooks";
+
+export class CiHookServer {
+ constructor(
+ healthCheck: HealthChecker = _healthCheck,
+ jobQueuer: IJobQueuer<ITraceable<Job, ServerTrace>> = new LaminarJobQueuer(
+ getRequiredEnv("LAMINAR_URL").fold((err, val) =>
+ err ? "https://ci.liz.coffee" : val
+ ),
+ ),
+ private readonly healthCheckActivity: IHealthCheckActivity =
+ new HealthCheckActivityImpl(healthCheck),
+ private readonly jobHookActivity: IJobHookActivity =
+ new JobHookActivityImpl(jobQueuer),
+ private readonly fourOhFourActivity: IFourOhFourActivity =
+ new FourOhFourActivityImpl(),
+ ) {}
+
+ private route(req: ITraceable<PenguenoRequest, ServerTrace>) {
+ const url = new URL(req.get().url);
+ if (url.pathname === "/health") {
+ return this.healthCheckActivity.checkHealth(req);
+ }
+ if (url.pathname === "/job") {
+ return this.jobHookActivity.processHook(req);
+ }
+ return this.fourOhFourActivity.fourOhFour(req);
+ }
+
+ public serve(req: Request): Promise<Response> {
+ return PenguenoRequest.from(req)
+ .bimap(TraceUtil.withClassTrace(this))
+ .map(this.route)
+ .get();
+ }
+}
diff --git a/hooks/server/health.ts b/hooks/server/health.ts
new file mode 100644
index 0000000..2f67aa4
--- /dev/null
+++ b/hooks/server/health.ts
@@ -0,0 +1,25 @@
+import {
+ getRequiredEnv,
+ getStdout,
+ type HealthChecker,
+ type HealthCheckInput,
+ HealthCheckOutput,
+ type IEither,
+ type ITraceable,
+ type ServerTrace,
+ TraceUtil,
+} from "@emprespresso/pengueno";
+
+export const healthCheck: HealthChecker = (
+ input: ITraceable<HealthCheckInput, ServerTrace>,
+): Promise<IEither<Error, HealthCheckOutput>> =>
+ input.bimap(TraceUtil.withFunctionTrace(healthCheck))
+ .move(getRequiredEnv("LAMINAR_HOST"))
+ // ensure LAMINAR_HOST is propagated to getStdout for other procedures
+ .map((e) => e.get().moveRight(["laminarc", "show-jobs"]))
+ .map((i) =>
+ i.get().mapRight(i.move.apply)
+ .flatMapAsync(getStdout.apply)
+ .then((gotJobs) => gotJobs.moveRight(HealthCheckOutput.YAASSSLAYQUEEN))
+ )
+ .get();
diff --git a/hooks/server/job/activity.ts b/hooks/server/job/activity.ts
new file mode 100644
index 0000000..14ea459
--- /dev/null
+++ b/hooks/server/job/activity.ts
@@ -0,0 +1,100 @@
+import {
+ Either,
+ ErrorSource,
+ type IActivity,
+ type IEither,
+ type ITraceable,
+ jsonModel,
+ JsonResponse,
+ LogLevel,
+ Metric,
+ PenguenoError,
+ type PenguenoRequest,
+ type ServerTrace,
+ TraceUtil,
+ validateExecutionEntries,
+} from "@emprespresso/pengueno";
+import { isJob, type Job } from "@emprespresso/ci-model";
+import type { IJobQueuer } from "@emprespresso/ci-hooks";
+
+const wellFormedJobMetric = Metric.fromName("Job.WellFormed");
+
+const jobJsonTransformer = (
+ j: ITraceable<unknown, ServerTrace>,
+): IEither<PenguenoError, Job> =>
+ j.bimap(TraceUtil.withMetricTrace(wellFormedJobMetric))
+ .map((tJson) => {
+ if (!isJob(tJson) || !validateExecutionEntries(tJson)) {
+ const err = "seems like a pwetty mawfomed job \\(-.-)/";
+ tJson.trace.addTrace(LogLevel.WARN).trace(err);
+ return Either.left<PenguenoError, Job>(new PenguenoError(err, 400));
+ }
+ return Either.right<PenguenoError, Job>(tJson);
+ })
+ .peek((tJob) =>
+ tJob.trace.trace(
+ tJob.get().fold((err) =>
+ err ? wellFormedJobMetric.failure : wellFormedJobMetric.success
+ ),
+ )
+ )
+ .get();
+
+export interface IJobHookActivity {
+ processHook: IActivity;
+}
+
+const jobHookRequestMetric = Metric.fromName("JobHook.process");
+export class JobHookActivityImpl implements IJobHookActivity {
+ constructor(
+ private readonly queuer: IJobQueuer<ITraceable<Job, ServerTrace>>,
+ ) {}
+
+ private trace(r: ITraceable<PenguenoRequest, ServerTrace>) {
+ return r.bimap(TraceUtil.withClassTrace(this))
+ .bimap(
+ TraceUtil.withMetricTrace(jobHookRequestMetric),
+ );
+ }
+
+ public processHook(r: ITraceable<PenguenoRequest, ServerTrace>) {
+ return this.trace(r).map(jsonModel(jobJsonTransformer))
+ .map(async (tEitherJobJson) => {
+ const eitherJob = await tEitherJobJson.get();
+ return eitherJob.flatMapAsync(async (job) => {
+ const eitherQueued = await tEitherJobJson.move(job)
+ .map(this.queuer.queue)
+ .get();
+ return eitherQueued.mapLeft((e) => new PenguenoError(e.message, 500));
+ });
+ })
+ .peek(
+ TraceUtil.promiseify((tJob) =>
+ tJob.get().fold(
+ (err: PenguenoError | undefined, _val: string | undefined) => {
+ if (!err) {
+ tJob.trace.trace(jobHookRequestMetric.success);
+ tJob.trace.trace(`all queued up and weady to go :D !! ${_val}`);
+ return;
+ }
+ tJob.trace.trace(
+ err.source === ErrorSource.SYSTEM
+ ? jobHookRequestMetric.failure
+ : jobHookRequestMetric.warn,
+ );
+ tJob.trace.addTrace(err.source).trace(`${err}`);
+ },
+ )
+ ),
+ )
+ .map(
+ TraceUtil.promiseify((tEitherQueuedJob) =>
+ new JsonResponse(r, tEitherQueuedJob.get(), {
+ status: tEitherQueuedJob.get()
+ .fold(({ status }, _val) => _val ? 200 : status),
+ })
+ ),
+ )
+ .get();
+ }
+}
diff --git a/hooks/server/job/mod.ts b/hooks/server/job/mod.ts
new file mode 100644
index 0000000..6b4ae85
--- /dev/null
+++ b/hooks/server/job/mod.ts
@@ -0,0 +1,2 @@
+export * from "./activity.ts";
+export * from "./queuer.ts";
diff --git a/hooks/server/job/queuer.ts b/hooks/server/job/queuer.ts
new file mode 100644
index 0000000..6094183
--- /dev/null
+++ b/hooks/server/job/queuer.ts
@@ -0,0 +1,78 @@
+import {
+ getStdout,
+ type IEither,
+ type ITraceable,
+ LogLevel,
+ type Mapper,
+ memoize,
+ Metric,
+ type ServerTrace,
+ TraceUtil,
+} from "@emprespresso/pengueno";
+import type { Job } from "@emprespresso/ci-model";
+
+type QueuePosition = string;
+export class QueueError extends Error {}
+export interface IJobQueuer<TJob> {
+ queue: Mapper<TJob, Promise<IEither<QueueError, QueuePosition>>>;
+}
+
+export class LaminarJobQueuer
+ implements IJobQueuer<ITraceable<Job, ServerTrace>> {
+ constructor(
+ private readonly queuePositionPrefix: string,
+ ) {}
+
+ private static GetJobTypeTrace = (jobType: string) =>
+ `LaminarJobQueue.Queue.${jobType}`;
+ private static JobTypeMetrics = memoize((jobType: string) =>
+ Metric.fromName(LaminarJobQueuer.GetJobTypeTrace(jobType))
+ );
+
+ public queue(j: ITraceable<Job, ServerTrace>) {
+ const { type: jobType } = j.get();
+ const trace = LaminarJobQueuer.GetJobTypeTrace(jobType);
+ const metric = LaminarJobQueuer.JobTypeMetrics(trace);
+
+ return j
+ .bimap(TraceUtil.withTrace(trace))
+ .bimap(TraceUtil.withMetricTrace(metric))
+ .map((j) => {
+ const { type: jobType, arguments: args } = j.get();
+ const laminarCommand = [
+ "laminarc",
+ "queue",
+ jobType,
+ ...Object.entries(args).map(([key, val]) => `"${key}"="${val}"`),
+ ];
+ return laminarCommand;
+ })
+ .peek((c) =>
+ c.trace.trace(
+ `im so excited to see how this queue job will end!! (>ᴗ<): ${c.get().toString()}`,
+ )
+ )
+ .map(getStdout)
+ .peek(
+ TraceUtil.promiseify((q) =>
+ q.trace.trace(
+ q.get().fold((err, _val) => err ? metric.failure : metric.success),
+ )
+ ),
+ )
+ .map(TraceUtil.promiseify((q) =>
+ q.get().mapRight((stdout) => {
+ q.trace.addTrace(LogLevel.DEBUG).trace(`stdout ${stdout}`);
+ const [jobName, jobId] = stdout.split(":");
+ const jobUrl = `${this.queuePositionPrefix}/jobs/${jobName}/${jobId}`;
+
+ q.trace.trace(`all queued up and weady to go~ (˘ω˘) => ${jobUrl}`);
+ return jobUrl;
+ }).mapLeft((err) => {
+ q.trace.addTrace(LogLevel.ERROR).trace(err.toString());
+ return err;
+ })
+ ))
+ .get();
+ }
+}
diff --git a/hooks/server/mod.ts b/hooks/server/mod.ts
new file mode 100644
index 0000000..0a520f9
--- /dev/null
+++ b/hooks/server/mod.ts
@@ -0,0 +1,3 @@
+export * from "./ci.ts";
+export * from "./health.ts";
+export * from "./job/mod.ts";