Skip to content

Commit

Permalink
feat: runtime info populated for worker and node environments
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Dec 20, 2024
1 parent cb81852 commit c8f45f0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 48 deletions.
8 changes: 5 additions & 3 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "./context";
import { LogReturn, Metadata } from "@ubiquity-os/ubiquity-os-logger";
import { getPluginName, getRunUrl, getVersion } from "./helpers/get-run-info";
import { Context } from "./context";
import { PluginRuntimeInfo } from "./helpers/runtime-info";
import { sanitizeMetadata } from "./util";

const HEADER_NAME = "UbiquityOS";
Expand Down Expand Up @@ -67,8 +67,10 @@ async function createStructuredMetadataWithMessage(context: Context, message: Lo
} else {
instigatorName = context.payload.sender?.login || HEADER_NAME;
}
const runUrl = PluginRuntimeInfo.getInstance().runUrl;
const version = await PluginRuntimeInfo.getInstance().version;

const ubiquityMetadataHeader = `<!-- ${HEADER_NAME} - ${await getPluginName()} - @${instigatorName} - ${getRunUrl()} - ${callingFnName} - ${await getVersion(context)}`;
const ubiquityMetadataHeader = `<!-- ${HEADER_NAME} - @${instigatorName} - ${runUrl} - ${callingFnName} - ${version}`;

let metadataSerialized: string;
const metadataSerializedVisible = ["```json", jsonPretty, "```"].join("\n");
Expand Down
45 changes: 0 additions & 45 deletions src/helpers/get-run-info.ts

This file was deleted.

47 changes: 47 additions & 0 deletions src/helpers/runtime-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import github from "@actions/github";
import { getRuntimeKey } from "hono/adapter";
import simpleGit from "simple-git";

export abstract class PluginRuntimeInfo {
private static _instance: PluginRuntimeInfo | null = null;
protected _env: Record<string, string> = {};

protected constructor(env?: Record<string, string>) {
if (env) {
this._env = env;
}
}

public static getInstance(env?: Record<string, string>) {
if (!PluginRuntimeInfo._instance) {
PluginRuntimeInfo._instance = getRuntimeKey() === "workerd" ? new CfRuntimeInfo(env) : new NodeRuntimeInfo(env);
}
return PluginRuntimeInfo._instance;
}

public abstract get version(): Promise<string>;
public abstract get runUrl(): string;
}

export class CfRuntimeInfo extends PluginRuntimeInfo {
public get version(): Promise<string> {
return Promise.resolve(this._env.CF_VERSION_METADATA ?? "[missing CF_VERSION_METADATA]");
}
public get runUrl(): string {
const accountId = this._env.CF_ACCOUNT_ID;
const workerName = this._env.CF_WORKER_NAME;
const toTime = Date.now() + 60000;
const fromTime = Date.now() - 60000;
const timeParam = encodeURIComponent(`{"type":"absolute","to":${toTime},"from":${fromTime}}`);
return `https://dash.cloudflare.com/${accountId}/workers/services/view/${workerName}/production/observability/logs?granularity=0&time=${timeParam}`;
}
}

export class NodeRuntimeInfo extends PluginRuntimeInfo {
public get version() {
return simpleGit().revparse(["--short", "HEAD"]);
}
public get runUrl() {
return github.context.payload.repository ? `${github.context.payload.repository?.html_url}/actions/runs/${github.context.runId}` : "http://localhost";
}
}
11 changes: 11 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { env as honoEnv } from "hono/adapter";
import { HTTPException } from "hono/http-exception";
import { postComment } from "./comment";
import { Context } from "./context";
import { PluginRuntimeInfo } from "./helpers/runtime-info";
import { customOctokit } from "./octokit";
import { verifySignature } from "./signature";
import { Manifest } from "./types/manifest";
Expand Down Expand Up @@ -79,6 +80,16 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
env = ctx.env as TEnv;
}

const workerUrl = new URL(inputs.ref).origin;
let workerName;

if (workerUrl.includes("localhost")) {
workerName = "localhost";
} else {
workerName = `${workerUrl.split("//")[1].split(".")[0]}`;
}
PluginRuntimeInfo.getInstance({ ...env, CF_WORKER_NAME: workerName });

let command: TCommand | null = null;
if (inputs.command && pluginOptions.commandSchema) {
try {
Expand Down

0 comments on commit c8f45f0

Please sign in to comment.