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

Config/context overridden by another webhook #801

Draft
wants to merge 13 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 12 additions & 15 deletions src/bindings/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context } from "probot";
import { createAdapters } from "../adapters";
import { processors, wildcardProcessors } from "../handlers/processors";
import { shouldSkip } from "../helpers";
import { BotConfig, GithubEvent, Payload, PayloadSchema, LogLevel } from "../types";
import { BotContext, GithubEvent, Payload, PayloadSchema, LogLevel } from "../types";
Sadaf-A marked this conversation as resolved.
Show resolved Hide resolved
import { Adapters } from "../types/adapters";
import { ajv } from "../utils";
import { loadConfig } from "./config";
Expand All @@ -12,9 +12,6 @@ import { validateConfigChange } from "../handlers/push";
let botContext: Context = {} as Context;
whilefoo marked this conversation as resolved.
Show resolved Hide resolved
export const getBotContext = () => botContext;

let botConfig: BotConfig = {} as BotConfig;
export const getBotConfig = () => botConfig;

let adapters: Adapters = {} as Adapters;
export const getAdapters = () => adapters;

Expand All @@ -30,7 +27,7 @@ export const getLogger = (): Logger => logger;

const NO_VALIDATION = [GithubEvent.INSTALLATION_ADDED_EVENT as string, GithubEvent.PUSH_EVENT as string];

export const bindEvents = async (context: Context): Promise<void> => {
export const bindEvents = async (context: BotContext): Promise<void> => {
Sadaf-A marked this conversation as resolved.
Show resolved Hide resolved
const { id, name } = context;
botContext = context;
const payload = context.payload as Payload;
Expand All @@ -39,12 +36,12 @@ export const bindEvents = async (context: Context): Promise<void> => {

let botConfigError;
try {
botConfig = await loadConfig(context);
context.botConfig = await loadConfig(context);
} catch (err) {
botConfigError = err;
}

adapters = createAdapters(botConfig);
adapters = createAdapters(context.botConfig);

const options = {
app: "UbiquiBot",
Expand All @@ -53,9 +50,9 @@ export const bindEvents = async (context: Context): Promise<void> => {

logger = new GitHubLogger(
options.app,
botConfig?.log?.logEnvironment ?? "development",
botConfig?.log?.level ?? LogLevel.DEBUG,
botConfig?.log?.retryLimit ?? 0
context.botConfig?.log?.logEnvironment ?? "development",
context.botConfig?.log?.level ?? LogLevel.DEBUG,
context.botConfig?.log?.retryLimit ?? 0
); // contributors will see logs in console while on development env
if (!logger) {
return;
Expand All @@ -74,11 +71,11 @@ export const bindEvents = async (context: Context): Promise<void> => {

logger.info(
`Config loaded! config: ${JSON.stringify({
price: botConfig.price,
unassign: botConfig.unassign,
mode: botConfig.mode,
log: botConfig.log,
wallet: botConfig.wallet,
price: context.botConfig.price,
unassign: context.botConfig.unassign,
mode: context.botConfig.mode,
log: context.botConfig.log,
wallet: context.botConfig.wallet,
})}`
);

Expand Down
5 changes: 5 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Static, Type } from "@sinclair/typebox";
import { LogLevel } from "./log";
import { Context } from "probot";

const LabelItemSchema = Type.Object(
{
Expand Down Expand Up @@ -143,6 +144,10 @@ export const BotConfigSchema = Type.Object({

export type BotConfig = Static<typeof BotConfigSchema>;

export interface BotContext extends Context {
botConfig: BotConfig;
}

export const StreamlinedCommentSchema = Type.Object({
login: Type.Optional(Type.String()),
body: Type.Optional(Type.String()),
Expand Down