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 all commits
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
50 changes: 25 additions & 25 deletions src/adapters/supabase/helpers/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createClient, SupabaseClient } from "@supabase/supabase-js";
import { getAdapters, getLogger } from "../../../bindings";
import { Issue, UserProfile } from "../../../types";
import { BotContext, Issue, UserProfile } from "../../../types";
import { Database } from "../types";
import { InsertPermit, Permit } from "../../../helpers";
import { BigNumber, BigNumberish } from "ethers";
Expand Down Expand Up @@ -54,27 +54,27 @@ export const getLastWeeklyTime = async (): Promise<Date | undefined> => {
/**
* @dev Updates the last weekly update timestamp
*/
export const updateLastWeeklyTime = async (time: Date): Promise<void> => {
export const updateLastWeeklyTime = async (context: BotContext, time: Date): Promise<void> => {
const logger = getLogger();
const { supabase } = getAdapters();

const { data, error } = await supabase.from("weekly").select("last_time");
if (error) {
logger.error(`Checking last time failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Checking last time failed, error: ${JSON.stringify(error)}`);
throw new Error(`Checking last time failed, error: ${JSON.stringify(error)}`);
}

if (data && data.length > 0) {
const { data, error } = await supabase.from("weekly").update({ last_time: time.toUTCString() }).neq("last_time", time.toUTCString());
if (error) {
logger.error(`Updating last time failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Updating last time failed, error: ${JSON.stringify(error)}`);
throw new Error(`Updating last time failed, error: ${JSON.stringify(error)}`);
}
logger.info(`Updating last time is done, data: ${data}`);
} else {
const { data, error } = await supabase.from("weekly").insert({ last_time: time.toUTCString() });
if (error) {
logger.error(`Creating last time failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Creating last time failed, error: ${JSON.stringify(error)}`);
throw new Error(`Creating last time failed, error: ${JSON.stringify(error)}`);
}
logger.info(`Creating last time is done, data: ${data}`);
Expand Down Expand Up @@ -138,12 +138,12 @@ const getDbDataFromUserProfile = (userProfile: UserProfile, additions?: UserProf
* Performs an UPSERT on the issues table.
* @param issue The issue entity fetched from github event.
*/
export const upsertIssue = async (issue: Issue, additions: IssueAdditions): Promise<void> => {
export const upsertIssue = async (context: BotContext, issue: Issue, additions: IssueAdditions): Promise<void> => {
const logger = getLogger();
const { supabase } = getAdapters();
const { data, error } = await supabase.from("issues").select("id").eq("issue_number", issue.number);
if (error) {
logger.error(`Checking issue failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Checking issue failed, error: ${JSON.stringify(error)}`);
throw new Error(`Checking issue failed, error: ${JSON.stringify(error)}`);
}

Expand All @@ -154,14 +154,14 @@ export const upsertIssue = async (issue: Issue, additions: IssueAdditions): Prom
.upsert({ id: key, ...getDbDataFromIssue(issue, additions) })
.select();
if (_error) {
logger.error(`Upserting an issue failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Upserting an issue failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Upserting an issue failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Upserting an issue done, { data: ${_data}, error: ${_error}`);
} else {
const { data: _data, error: _error } = await supabase.from("issues").insert(getDbDataFromIssue(issue, additions));
if (_error) {
logger.error(`Creating a new issue record failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Creating a new issue record failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Creating a new issue record failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Creating a new issue record done, { data: ${_data}, error: ${_error}`);
Expand All @@ -172,26 +172,26 @@ export const upsertIssue = async (issue: Issue, additions: IssueAdditions): Prom
* Performs an UPSERT on the users table.
* @param user The user entity fetched from github event.
*/
export const upsertUser = async (user: UserProfile): Promise<void> => {
export const upsertUser = async (context: BotContext, user: UserProfile): Promise<void> => {
const logger = getLogger();
const { supabase } = getAdapters();
const { data, error } = await supabase.from("users").select("user_login").eq("user_login", user.login);
if (error) {
logger.error(`Checking user failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Checking user failed, error: ${JSON.stringify(error)}`);
throw new Error(`Checking user failed, error: ${JSON.stringify(error)}`);
}

if (data && data.length > 0) {
const { data: _data, error: _error } = await supabase.from("users").upsert(getDbDataFromUserProfile(user)).select();
if (_error) {
logger.error(`Upserting a user failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Upserting a user failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Upserting a user failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Upserting a user done, { data: ${JSON.stringify(_data)} }`);
} else {
const { data: _data, error: _error } = await supabase.from("users").insert(getDbDataFromUserProfile(user));
if (_error) {
logger.error(`Creating a new user record failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Creating a new user record failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Creating a new user record failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Creating a new user record done, { data: ${JSON.stringify(_data)} }`);
Expand All @@ -203,13 +203,13 @@ export const upsertUser = async (user: UserProfile): Promise<void> => {
* @param username The user name you want to upsert a wallet address for
* @param address The account address
*/
export const upsertWalletAddress = async (username: string, address: string): Promise<void> => {
export const upsertWalletAddress = async (context: BotContext, username: string, address: string): Promise<void> => {
const logger = getLogger();
const { supabase } = getAdapters();

const { data, error } = await supabase.from("wallets").select("user_name").eq("user_name", username);
if (error) {
logger.error(`Checking wallet address failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Checking wallet address failed, error: ${JSON.stringify(error)}`);
throw new Error(`Checking wallet address failed, error: ${JSON.stringify(error)}`);
}

Expand All @@ -220,7 +220,7 @@ export const upsertWalletAddress = async (username: string, address: string): Pr
updated_at: new Date().toUTCString(),
});
if (_error) {
logger.error(`Upserting a wallet address failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Upserting a wallet address failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Upserting a wallet address failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Upserting a wallet address done, { data: ${JSON.stringify(_data)} }`);
Expand All @@ -232,7 +232,7 @@ export const upsertWalletAddress = async (username: string, address: string): Pr
updated_at: new Date().toUTCString(),
});
if (error) {
logger.error(`Creating a new wallet_table record failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Creating a new wallet_table record failed, error: ${JSON.stringify(error)}`);
throw new Error(`Creating a new wallet_table record failed, error: ${JSON.stringify(error)}`);
}
logger.info(`Creating a new wallet_table record done, { data: ${JSON.stringify(data)}, address: $address }`);
Expand All @@ -244,13 +244,13 @@ export const upsertWalletAddress = async (username: string, address: string): Pr
* @param username The user name you want to upsert a wallet address for
* @param address The account multiplier
*/
export const upsertWalletMultiplier = async (username: string, multiplier: string, reason: string, org_id: string): Promise<void> => {
export const upsertWalletMultiplier = async (context: BotContext, username: string, multiplier: string, reason: string, org_id: string): Promise<void> => {
const logger = getLogger();
const { supabase } = getAdapters();

const { data, error } = await supabase.from("multiplier").select("user_id").eq("user_id", `${username}_${org_id}`);
if (error) {
logger.error(`Checking wallet multiplier failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Checking wallet multiplier failed, error: ${JSON.stringify(error)}`);
throw new Error(`Checking wallet multiplier failed, error: ${JSON.stringify(error)}`);
}

Expand All @@ -262,7 +262,7 @@ export const upsertWalletMultiplier = async (username: string, multiplier: strin
updated_at: new Date().toUTCString(),
});
if (_error) {
logger.error(`Upserting a wallet multiplier failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Upserting a wallet multiplier failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Upserting a wallet multiplier failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Upserting a wallet multiplier done, { data: ${JSON.stringify(_data)} }`);
Expand All @@ -275,7 +275,7 @@ export const upsertWalletMultiplier = async (username: string, multiplier: strin
updated_at: new Date().toUTCString(),
});
if (_error) {
logger.error(`Creating a new multiplier record failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Creating a new multiplier record failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Creating a new multiplier record failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Creating a new multiplier record done, { data: ${JSON.stringify(_data)} }`);
Expand All @@ -289,13 +289,13 @@ export const upsertWalletMultiplier = async (username: string, multiplier: strin
* @param access Access granting
* @param bool Disabling or enabling
*/
export const upsertAccessControl = async (username: string, repository: string, access: string, bool: boolean): Promise<void> => {
export const upsertAccessControl = async (context: BotContext, username: string, repository: string, access: string, bool: boolean): Promise<void> => {
const logger = getLogger();
const { supabase } = getAdapters();

const { data, error } = await supabase.from("access").select("user_name").eq("user_name", username).eq("repository", repository);
if (error) {
logger.error(`Checking access control failed, error: ${JSON.stringify(error)}`);
logger.error(context, `Checking access control failed, error: ${JSON.stringify(error)}`);
throw new Error(`Checking access control failed, error: ${JSON.stringify(error)}`);
}

Expand All @@ -309,7 +309,7 @@ export const upsertAccessControl = async (username: string, repository: string,
if (data && data.length > 0) {
const { data: _data, error: _error } = await supabase.from("access").upsert(properties);
if (_error) {
logger.error(`Upserting a access control failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Upserting a access control failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Upserting a access control failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Upserting a access control done, { data: ${JSON.stringify(_data)} }`);
Expand All @@ -323,7 +323,7 @@ export const upsertAccessControl = async (username: string, repository: string,
...properties,
});
if (_error) {
logger.error(`Creating a new access control record failed, error: ${JSON.stringify(_error)}`);
logger.error(context, `Creating a new access control record failed, error: ${JSON.stringify(_error)}`);
throw new Error(`Creating a new access control record failed, error: ${JSON.stringify(_error)}`);
}
logger.info(`Creating a new access control record done, { data: ${JSON.stringify(_data)} }`);
Expand Down
24 changes: 13 additions & 11 deletions src/adapters/supabase/helpers/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { getAdapters, getBotContext, Logger } from "../../../bindings";
import { Payload, LogLevel, LogNotification } from "../../../types";
import { getAdapters, Logger } from "../../../bindings";
import { BotContext, Payload, LogLevel, LogNotification } from "../../../types";
import { getOrgAndRepoFromPath } from "../../../utils/private";
import jwt from "jsonwebtoken";
interface Log {
Expand Down Expand Up @@ -44,14 +44,18 @@ export class GitHubLogger implements Logger {
private retryDelay = 1000; // Delay between retries in milliseconds
private throttleCount = 0;
private retryLimit = 0; // Retries disabled by default

private context;
private logNotification;

constructor(app: string, logEnvironment: string, maxLevel: LogLevel, retryLimit: number, logNotification: LogNotification) {
constructor(context: BotContext, app: string, logEnvironment: string, maxLevel: LogLevel, retryLimit: number, logNotification: LogNotification) {
this.app = app;
this.logEnvironment = logEnvironment;
this.maxLevel = getNumericLevel(maxLevel);
this.retryLimit = retryLimit;
this.supabase = getAdapters().supabase;

this.context = context;
this.logNotification = logNotification;
}

Expand Down Expand Up @@ -83,8 +87,7 @@ export class GitHubLogger implements Logger {
}
}

private sendDataWithJwt(message: string | object, errorPayload?: string | object) {
const context = getBotContext();
private sendDataWithJwt(context: BotContext, message: string | object, errorPayload?: string | object) {
const payload = context.payload as Payload;

const { comment, issue, repository } = payload;
Expand Down Expand Up @@ -195,8 +198,7 @@ export class GitHubLogger implements Logger {
private save(logMessage: string | object, level: LogLevel, errorPayload?: string | object) {
if (getNumericLevel(level) > this.maxLevel) return; // only return errors lower than max level

const context = getBotContext();
const payload = context.payload as Payload;
const payload = this.context.payload as Payload;
const timestamp = new Date().toUTCString();

const { comment, issue, repository } = payload;
Expand Down Expand Up @@ -230,9 +232,9 @@ export class GitHubLogger implements Logger {
this.save(message, LogLevel.INFO, errorPayload);
}

warn(message: string | object, errorPayload?: string | object) {
warn(context: BotContext, message: string | object, errorPayload?: string | object) {
this.save(message, LogLevel.WARN, errorPayload);
this.sendDataWithJwt(message, errorPayload)
this.sendDataWithJwt(context, message, errorPayload)
.then((response) => {
this.save(`Log Notification Success: ${response}`, LogLevel.DEBUG, "");
})
Expand All @@ -245,9 +247,9 @@ export class GitHubLogger implements Logger {
this.save(message, LogLevel.DEBUG, errorPayload);
}

error(message: string | object, errorPayload?: string | object) {
error(context: BotContext, message: string | object, errorPayload?: string | object) {
this.save(message, LogLevel.ERROR, errorPayload);
this.sendDataWithJwt(message, errorPayload)
this.sendDataWithJwt(context, message, errorPayload)
.then((response) => {
this.save(`Log Notification Success: ${response}`, LogLevel.DEBUG, "");
})
Expand Down
11 changes: 6 additions & 5 deletions src/adapters/telegram/helpers/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Input } from "telegraf";
import { getAdapters, getBotConfig } from "../../../bindings";
import { getAdapters } from "../../../bindings";
import { BotContext } from "../../../types";
import { TLMessageFormattedPayload, TLMessagePayload, TLPhotoPayload } from "../types/payload";

export function messageFormatter(messagePayload: TLMessagePayload) {
Expand All @@ -14,10 +15,10 @@ export function messageFormatter(messagePayload: TLMessagePayload) {
return msgObj;
}

export async function telegramFormattedNotifier(messagePayload: TLMessageFormattedPayload) {
export async function telegramFormattedNotifier(context: BotContext, messagePayload: TLMessageFormattedPayload) {
const {
telegram: { delay },
} = getBotConfig();
} = context.botConfig;
const { telegram } = getAdapters();
const { chatIds, text, parseMode } = messagePayload;

Expand All @@ -40,14 +41,14 @@ export async function telegramFormattedNotifier(messagePayload: TLMessageFormatt
sendHandler();
}

export async function telegramNotifier(messagePayload: TLMessagePayload) {
export async function telegramNotifier(context: BotContext, messagePayload: TLMessagePayload) {
const messageString = messageFormatter(messagePayload);
const messageObj: TLMessageFormattedPayload = {
chatIds: messagePayload.chatIds,
text: messageString,
parseMode: "HTML",
};
await telegramFormattedNotifier(messageObj);
await telegramFormattedNotifier(context, messageObj);
}

export async function telegramPhotoNotifier(messagePayload: TLPhotoPayload) {
Expand Down
9 changes: 3 additions & 6 deletions src/bindings/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ms from "ms";

import { BotConfig, BotConfigSchema, LogLevel } from "../types";
import { BotContext, BotConfig, BotConfigSchema, LogLevel } from "../types";
import { getPayoutConfigByNetworkId } from "../helpers";
import { ajv } from "../utils";
import { Context } from "probot";
import { getScalarKey, getWideConfig } from "../utils/private";

export const loadConfig = async (context: Context): Promise<BotConfig> => {
export const loadConfig = async (context: BotContext): Promise<BotConfig> => {
const {
baseMultiplier,
timeLabels,
Expand Down Expand Up @@ -65,9 +64,7 @@ export const loadConfig = async (context: Context): Promise<BotConfig> => {
permitBaseUrl: process.env.PERMIT_BASE_URL || permitBaseUrl,
},
unassign: {
timeRangeForMaxIssue: process.env.DEFAULT_TIME_RANGE_FOR_MAX_ISSUE
? Number(process.env.DEFAULT_TIME_RANGE_FOR_MAX_ISSUE)
: timeRangeForMaxIssue,
timeRangeForMaxIssue: process.env.DEFAULT_TIME_RANGE_FOR_MAX_ISSUE ? Number(process.env.DEFAULT_TIME_RANGE_FOR_MAX_ISSUE) : timeRangeForMaxIssue,
timeRangeForMaxIssueEnabled: process.env.DEFAULT_TIME_RANGE_FOR_MAX_ISSUE_ENABLED
? process.env.DEFAULT_TIME_RANGE_FOR_MAX_ISSUE_ENABLED == "true"
: timeRangeForMaxIssueEnabled,
Expand Down
Loading
Loading