Skip to content

Commit

Permalink
chore: use ubiquity-logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jul 4, 2024
1 parent 023a3eb commit 88b5431
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 241 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@octokit/webhooks": "^13.1.0",
"@sinclair/typebox": "^0.32.5",
"@supabase/supabase-js": "2.42.0",
"@ubiquity-dao/ubiquibot-logger": "^1.2.0",
"dotenv": "^16.4.4",
"ms": "^2.1.3",
"typebox-validators": "^0.3.5"
Expand Down Expand Up @@ -85,4 +86,4 @@
]
},
"packageManager": "[email protected]"
}
}
200 changes: 0 additions & 200 deletions src/adapters/supabase/pretty-logs.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
});
commitHash = hashResponse.data.sha;
} catch (e) {
logger.error("Error while getting commit hash", e);
logger.error("Error while getting commit hash", { error: e as Error });
}

// check max assigned issues
Expand All @@ -40,7 +40,7 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
logger.info(`Opened Pull Requests with approved reviews or with no reviews but over 24 hours have passed: ${JSON.stringify(openedPullRequests)}`);

const assignedIssues = await getAssignedIssues(context, sender.login);
logger.info("Max issue allowed is", maxConcurrentTasks);
logger.info("Max issue allowed is", { maxConcurrentTasks });

// check for max and enforce max

Expand Down Expand Up @@ -75,13 +75,12 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
const duration: number = calculateDurations(labels).shift() ?? 0;

const { id, login } = sender;
const toCreate = { duration, priceLabel, revision: commitHash?.substring(0, 7) };
const logMessage = logger.info("Task assigned successfully", { duration, priceLabel, revision: commitHash?.substring(0, 7) });

const assignmentComment = await generateAssignmentComment(context, issue.created_at, issue.number, id, duration);
const metadata = structuredMetadata.create<typeof toCreate>("Assignment", toCreate);
const metadata = structuredMetadata.create("Assignment", logMessage);

// add assignee

if (!assignees.map((i) => i?.login).includes(login)) {
await addAssignees(context, issue.number, [login]);
}
Expand Down
22 changes: 9 additions & 13 deletions src/handlers/shared/structured-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { LogLevel } from "../../adapters/supabase/pretty-logs";

type Metadata<T extends object> = T & {
revision?: string;
logMessage?: {
type?: LogLevel;
message?: string;
};
[key: string]: unknown;
};
import { L as LogReturn } from "@ubiquity-dao/ubiquibot-logger/dist/pretty-logs--Bv8yJMk";
function createStructuredMetadata(className: string, logReturn: LogReturn | null) {
let logMessage, metadata
if (logReturn) {
logMessage = logReturn.logMessage;
metadata = logReturn.metadata;
}

function createStructuredMetadata<T extends object>(className: string, metadata: Metadata<T>) {
const jsonPretty = JSON.stringify(metadata, null, 2);
const stackLine = new Error().stack?.split("\n")[2] ?? "";
const caller = stackLine.match(/at (\S+)/)?.[1] ?? "";
const ubiquityMetadataHeader = `<!-- Ubiquity - ${className} - ${caller} - ${metadata.revision}`;
const ubiquityMetadataHeader = `<!-- Ubiquity - ${className} - ${caller} - ${metadata?.revision}`;

let metadataSerialized: string;
const metadataSerializedVisible = ["```json", jsonPretty, "```"].join("\n");
const metadataSerializedHidden = [ubiquityMetadataHeader, jsonPretty, "-->"].join("\n");

if (metadata.logMessage?.type === LogLevel.FATAL) {
if (logMessage?.type === "fatal") {
// if the log message is fatal, then we want to show the metadata
metadataSerialized = [metadataSerializedVisible, metadataSerializedHidden].join("\n");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Env, PluginInputs } from "./types";
import { Octokit } from "@octokit/rest";
import { createClient } from "@supabase/supabase-js";
import { createAdapters } from "./adapters";
import { PrettyLogs } from "./adapters/supabase/pretty-logs";
import { Logs } from "@ubiquity-dao/ubiquibot-logger";

export async function startStopTask(inputs: PluginInputs, env: Env) {
const octokit = new Octokit({ auth: inputs.authToken });
Expand All @@ -17,7 +17,7 @@ export async function startStopTask(inputs: PluginInputs, env: Env) {
config: inputs.settings,
octokit,
env,
logger: new PrettyLogs(),
logger: new Logs("info"),
adapters: {} as ReturnType<typeof createAdapters>,
};

Expand Down
4 changes: 2 additions & 2 deletions src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Octokit } from "@octokit/rest";
import { StartStopSettings } from "./plugin-input";
import { createAdapters } from "../adapters";
import { Env } from "./env";
import { PrettyLogs } from "../adapters/supabase/pretty-logs";
import { Logs } from "@ubiquity-dao/ubiquibot-logger/.";

export type SupportedEventsU = "issue_comment.created";

Expand All @@ -18,5 +18,5 @@ export interface Context<T extends SupportedEventsU = SupportedEventsU, TU exten
adapters: ReturnType<typeof createAdapters>;
config: StartStopSettings;
env: Env;
logger: PrettyLogs;
logger: Logs
}
14 changes: 7 additions & 7 deletions src/utils/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getAssignedIssues(context: Context, username: string): Pro
({ data: issues }) => issues.filter((issue) => !issue.pull_request && issue.assignee && issue.assignee.login === username)
);
} catch (err: unknown) {
context.logger.error("Fetching assigned issues failed!", err);
context.logger.error("Fetching assigned issues failed!", { error: err as Error });
return [];
}
}
Expand All @@ -41,7 +41,7 @@ export async function addCommentToIssue(context: Context, message: string | null
body: comment,
});
} catch (e: unknown) {
context.logger.error("Adding a comment failed!", e);
context.logger.error("Adding a comment failed!", { error: e as Error });
}
}

Expand All @@ -57,7 +57,7 @@ export async function closePullRequest(context: Context, pullNumber: number) {
state: "closed",
});
} catch (err: unknown) {
context.logger.error("Closing pull requests failed!", err);
context.logger.error("Closing pull requests failed!", { error: err as Error });
}
}

Expand All @@ -77,7 +77,7 @@ export async function closePullRequestForAnIssue(context: Context, issueNumber:
return logger.info(`No linked pull requests to close`);
}

logger.info(`Opened prs`, linkedPullRequests);
logger.info(`Opened prs`, { message: JSON.stringify(linkedPullRequests) });
let comment = `These linked pull requests are closed: `;
for (let i = 0; i < linkedPullRequests.length; i++) {
await closePullRequest(context, linkedPullRequests[i].number);
Expand All @@ -98,7 +98,7 @@ export async function addAssignees(context: Context, issueNo: number, assignees:
assignees,
});
} catch (e: unknown) {
throw context.logger.error("Adding the assignee failed", { assignee: assignees, issueNo, error: e });
throw context.logger.error("Adding the assignee failed", { assignee: assignees, issueNo, error: e as Error });
}
}

Expand All @@ -113,7 +113,7 @@ export async function getAllPullRequests(context: Context, state: "open" | "clos
per_page: 100,
});
} catch (err: unknown) {
context.logger.error("Fetching all pull requests failed!", err);
context.logger.error("Fetching all pull requests failed!", { error: err as Error });
return [];
}
}
Expand All @@ -135,7 +135,7 @@ export async function getAllPullRequestReviews(context: Context, pullNumber: num
},
});
} catch (err: unknown) {
context.logger.error("Fetching all pull request reviews failed!", err);
context.logger.error("Fetching all pull request reviews failed!", { error: err as Error });
return [];
}
}
Expand Down
Loading

0 comments on commit 88b5431

Please sign in to comment.