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

Commit

Permalink
Merge pull request #691 from devpanther/config-nested-inheritance
Browse files Browse the repository at this point in the history
chore: merged repo and org config
  • Loading branch information
whilefoo authored Sep 7, 2023
2 parents 7078549 + 2f0631c commit 87c01de
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 235 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@
"ajv": "^8.11.2",
"ajv-formats": "^2.1.1",
"axios": "^1.3.2",
"decimal.js": "^10.4.3",
"copyfiles": "^2.4.1",
"decimal.js": "^10.4.3",
"ethers": "^5.7.2",
"exponential-backoff": "^3.1.1",
"husky": "^8.0.2",
"jimp": "^0.22.4",
"js-yaml": "^4.1.0",
"libsodium-wrappers": "^0.7.11",
"lint-staged": "^13.1.0",
"lodash": "^4.17.21",
"ms": "^2.1.3",
"node-html-parser": "^6.1.5",
"node-html-to-image": "^3.3.0",
Expand All @@ -65,6 +66,7 @@
"@types/eslint": "^8.40.2",
"@types/jest": "^28.1.0",
"@types/libsodium-wrappers": "^0.7.10",
"@types/lodash": "^4.14.197",
"@types/node": "^14.18.37",
"@types/source-map-support": "^0.5.6",
"eslint": "^8.43.0",
Expand Down
176 changes: 1 addition & 175 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import { Incentives } from "./private";
import { Level } from "../adapters/supabase";
import { CommandObj, WideLabel, WideOrgConfig, WideRepoConfig } from "./private";
import { AccessControl } from "../types";

interface Configs {
parsedRepo?: WideRepoConfig;
parsedOrg?: WideOrgConfig;
parsedDefault: WideRepoConfig;
}

export const getNumericLevel = (level: Level) => {
switch (level) {
Expand All @@ -28,169 +19,4 @@ export const getNumericLevel = (level: Level) => {
default:
return -1; // Invalid level
}
};

export const getNetworkId = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => {
if (parsedRepo && parsedRepo["evm-network-id"] !== undefined && !Number.isNaN(Number(parsedRepo["evm-network-id"]))) {
return Number(parsedRepo["evm-network-id"]);
} else if (parsedOrg && parsedOrg["evm-network-id"] !== undefined && !Number.isNaN(Number(parsedOrg["evm-network-id"]))) {
return Number(parsedOrg["evm-network-id"]);
} else {
return Number(parsedDefault["evm-network-id"]);
}
};

export const getBaseMultiplier = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => {
if (parsedRepo && parsedRepo["price-multiplier"] !== undefined && !Number.isNaN(Number(parsedRepo["price-multiplier"]))) {
return Number(parsedRepo["price-multiplier"]);
} else if (parsedOrg && parsedOrg["price-multiplier"] !== undefined && !Number.isNaN(Number(parsedOrg["price-multiplier"]))) {
return Number(parsedOrg["price-multiplier"]);
} else {
return Number(parsedDefault["price-multiplier"]);
}
};

export const getCreatorMultiplier = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => {
if (parsedRepo && parsedRepo["issue-creator-multiplier"] !== undefined && !Number.isNaN(Number(parsedRepo["issue-creator-multiplier"]))) {
return Number(parsedRepo["issue-creator-multiplier"]);
} else if (parsedOrg && parsedOrg["issue-creator-multiplier"] !== undefined && !Number.isNaN(Number(parsedOrg["issue-creator-multiplier"]))) {
return Number(parsedOrg["issue-creator-multiplier"]);
} else {
return Number(parsedDefault["issue-creator-multiplier"]);
}
};

export const getTimeLabels = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): WideLabel[] => {
if (parsedRepo && parsedRepo["time-labels"] !== undefined && Array.isArray(parsedRepo["time-labels"]) && parsedRepo["time-labels"].length > 0) {
return parsedRepo["time-labels"];
} else if (parsedOrg && parsedOrg["time-labels"] !== undefined && Array.isArray(parsedOrg["time-labels"]) && parsedOrg["time-labels"].length > 0) {
return parsedOrg["time-labels"];
} else {
return parsedDefault["time-labels"] as WideLabel[];
}
};

export const getCommandSettings = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): CommandObj[] => {
if (parsedRepo && parsedRepo["command-settings"] && Array.isArray(parsedRepo["command-settings"]) && parsedRepo["command-settings"].length > 0) {
return parsedRepo["command-settings"];
} else if (parsedOrg && parsedOrg["command-settings"] && Array.isArray(parsedOrg["command-settings"]) && parsedOrg["command-settings"].length > 0) {
return parsedOrg["command-settings"];
} else {
return parsedDefault["command-settings"] as CommandObj[];
}
};

export const getPriorityLabels = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): WideLabel[] => {
if (parsedRepo && parsedRepo["priority-labels"] !== undefined && Array.isArray(parsedRepo["priority-labels"]) && parsedRepo["priority-labels"].length > 0) {
return parsedRepo["priority-labels"];
} else if (
parsedOrg &&
parsedOrg["priority-labels"] !== undefined &&
Array.isArray(parsedOrg["priority-labels"]) &&
parsedOrg["priority-labels"].length > 0
) {
return parsedOrg["priority-labels"];
} else {
return parsedDefault["priority-labels"] as WideLabel[];
}
};

export const getIncentives = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): Incentives => {
if (parsedRepo && parsedRepo["incentives"]) {
return parsedRepo["incentives"];
} else if (parsedOrg && parsedOrg["incentives"]) {
return parsedOrg["incentives"];
} else {
return parsedDefault["incentives"] as Incentives;
}
};

export const getPaymentPermitMaxPrice = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => {
if (parsedRepo && parsedRepo["payment-permit-max-price"] && typeof parsedRepo["payment-permit-max-price"] === "number") {
return Number(parsedRepo["payment-permit-max-price"]);
} else if (parsedOrg && parsedOrg["payment-permit-max-price"] && typeof parsedOrg["payment-permit-max-price"] === "number") {
return Number(parsedOrg["payment-permit-max-price"]);
} else {
return Number(parsedDefault["payment-permit-max-price"]);
}
};

export const getAssistivePricing = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => {
if (parsedRepo && parsedRepo["assistive-pricing"] && typeof parsedRepo["assistive-pricing"] === "boolean") {
return parsedRepo["assistive-pricing"];
} else if (parsedOrg && parsedOrg["assistive-pricing"] && typeof parsedOrg["assistive-pricing"] === "boolean") {
return parsedOrg["assistive-pricing"];
} else {
return parsedDefault["assistive-pricing"] as boolean;
}
};

export const getAnalyticsMode = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => {
if (parsedRepo && parsedRepo["disable-analytics"] !== undefined && typeof parsedRepo["disable-analytics"] === "boolean") {
return parsedRepo["disable-analytics"];
} else if (parsedOrg && parsedOrg["disable-analytics"] !== undefined && typeof parsedOrg["disable-analytics"] === "boolean") {
return parsedOrg["disable-analytics"];
} else {
return parsedDefault["disable-analytics"] as boolean;
}
};

export const getPromotionComment = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): string => {
if (parsedRepo && parsedRepo["promotion-comment"] !== undefined && typeof parsedRepo["promotion-comment"] === "string") {
return parsedRepo["promotion-comment"];
} else if (parsedOrg && parsedOrg["promotion-comment"] !== undefined && typeof parsedOrg["promotion-comment"] === "string") {
return parsedOrg["promotion-comment"];
} else {
return parsedDefault["promotion-comment"] as string;
}
};

export const getIncentiveMode = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => {
if (parsedRepo && parsedRepo["comment-incentives"] !== undefined && typeof parsedRepo["comment-incentives"] === "boolean") {
return parsedRepo["comment-incentives"];
} else if (parsedOrg && parsedOrg["comment-incentives"] !== undefined && typeof parsedOrg["comment-incentives"] === "boolean") {
return parsedOrg["comment-incentives"];
} else {
return parsedDefault["comment-incentives"] as boolean;
}
};

export const getBountyHunterMax = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => {
if (parsedRepo && parsedRepo["max-concurrent-assigns"] !== undefined && !Number.isNaN(Number(parsedRepo["max-concurrent-assigns"]))) {
return Number(parsedRepo["max-concurrent-assigns"]);
} else if (parsedOrg && parsedOrg["max-concurrent-assigns"] !== undefined && !Number.isNaN(Number(parsedOrg["max-concurrent-assigns"]))) {
return Number(parsedOrg["max-concurrent-assigns"]);
} else {
return Number(parsedDefault["max-concurrent-assigns"]);
}
};

export const getDefaultLabels = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): string[] => {
if (parsedRepo && parsedRepo["default-labels"] !== undefined) {
return parsedRepo["default-labels"];
} else if (parsedOrg && parsedOrg["default-labels"] !== undefined) {
return parsedOrg["default-labels"];
} else {
return parsedDefault["default-labels"] as string[];
}
};

export const getRegisterWalletWithVerification = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => {
if (parsedRepo && parsedRepo["register-wallet-with-verification"] !== undefined && typeof parsedRepo["register-wallet-with-verification"] === "boolean") {
return Boolean(parsedRepo["register-wallet-with-verification"]);
} else if (parsedOrg && parsedOrg["register-wallet-with-verification"] !== undefined && typeof parsedOrg["register-wallet-with-verification"] === "boolean") {
return Boolean(parsedOrg["register-wallet-with-verification"]);
} else {
return Boolean(parsedDefault["register-wallet-with-verification"]);
}
};

export const getEnableAccessControl = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): AccessControl => {
if (parsedRepo && parsedRepo["enable-access-control"]) {
return parsedRepo["enable-access-control"];
} else if (parsedOrg && parsedOrg["enable-access-control"]) {
return parsedOrg["enable-access-control"];
} else {
return parsedDefault["enable-access-control"] as AccessControl;
}
};
};
95 changes: 45 additions & 50 deletions src/utils/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@ import _sodium from "libsodium-wrappers";
import YAML from "yaml";
import { AccessControl, Payload } from "../types";
import { Context } from "probot";
import {
getAnalyticsMode,
getPaymentPermitMaxPrice,
getBaseMultiplier,
getCreatorMultiplier,
getBountyHunterMax,
getIncentiveMode,
getNetworkId,
getPriorityLabels,
getTimeLabels,
getDefaultLabels,
getPromotionComment,
getIncentives,
getAssistivePricing,
getCommandSettings,
getRegisterWalletWithVerification,
getEnableAccessControl,
} from "./helpers";
import merge from "lodash/merge";

import DEFAULT_CONFIG_JSON from "../../ubiquibot-config-default.json";

Expand Down Expand Up @@ -69,22 +52,22 @@ export interface CommandObj {
}

export interface WideConfig {
"evm-network-id"?: number;
"price-multiplier"?: number;
"evm-network-id": number;
"price-multiplier": number;
"issue-creator-multiplier": number;
"time-labels"?: WideLabel[];
"priority-labels"?: WideLabel[];
"payment-permit-max-price"?: number;
"command-settings"?: CommandObj[];
"promotion-comment"?: string;
"disable-analytics"?: boolean;
"comment-incentives"?: boolean;
"assistive-pricing"?: boolean;
"max-concurrent-assigns"?: number;
incentives?: Incentives;
"default-labels"?: string[];
"register-wallet-with-verification"?: boolean;
"enable-access-control"?: AccessControl;
"time-labels": WideLabel[];
"priority-labels": WideLabel[];
"payment-permit-max-price": number;
"command-settings": CommandObj[];
"promotion-comment": string;
"disable-analytics": boolean;
"comment-incentives": boolean;
"assistive-pricing": boolean;
"max-concurrent-assigns": number;
incentives: Incentives;
"default-labels": string[];
"register-wallet-with-verification": boolean;
"enable-access-control": AccessControl;
}

export type WideRepoConfig = WideConfig;
Expand All @@ -93,6 +76,12 @@ export interface WideOrgConfig extends WideConfig {
"private-key-encrypted"?: string;
}

export interface MergedConfigs {
parsedRepo: WideRepoConfig | undefined;
parsedOrg: WideOrgConfig | undefined;
parsedDefault: WideRepoConfig;
}

export const parseYAML = (data?: string): WideConfig | undefined => {
try {
if (data) {
Expand Down Expand Up @@ -157,6 +146,10 @@ export const getScalarKey = async (X25519_PRIVATE_KEY: string | undefined): Prom
}
};

const mergeConfigs = (configs: MergedConfigs) => {
return merge({}, configs.parsedDefault, configs.parsedOrg, configs.parsedRepo);
};

export const getWideConfig = async (context: Context) => {
const orgConfig = await getConfigSuperset(context, "org", CONFIG_PATH);
const repoConfig = await getConfigSuperset(context, "repo", CONFIG_PATH);
Expand All @@ -166,25 +159,27 @@ export const getWideConfig = async (context: Context) => {
const parsedDefault: WideRepoConfig = DEFAULT_CONFIG_JSON;
const privateKeyDecrypted = parsedOrg && parsedOrg[KEY_NAME] ? await getPrivateKey(parsedOrg[KEY_NAME]) : undefined;

const configs = { parsedRepo, parsedOrg, parsedDefault };
const configs: MergedConfigs = { parsedDefault, parsedOrg, parsedRepo };
const mergedConfigData: WideConfig = mergeConfigs(configs);

const configData = {
networkId: getNetworkId(configs),
networkId: mergedConfigData["evm-network-id"],
privateKey: privateKeyDecrypted ?? "",
assistivePricing: getAssistivePricing(configs),
commandSettings: getCommandSettings(configs),
baseMultiplier: getBaseMultiplier(configs),
issueCreatorMultiplier: getCreatorMultiplier(configs),
timeLabels: getTimeLabels(configs),
priorityLabels: getPriorityLabels(configs),
paymentPermitMaxPrice: getPaymentPermitMaxPrice(configs),
disableAnalytics: getAnalyticsMode(configs),
bountyHunterMax: getBountyHunterMax(configs),
incentiveMode: getIncentiveMode(configs),
incentives: getIncentives(configs),
defaultLabels: getDefaultLabels(configs),
promotionComment: getPromotionComment(configs),
registerWalletWithVerification: getRegisterWalletWithVerification(configs),
enableAccessControl: getEnableAccessControl(configs),
assistivePricing: mergedConfigData["assistive-pricing"],
commandSettings: mergedConfigData["command-settings"],
baseMultiplier: mergedConfigData["price-multiplier"],
issueCreatorMultiplier: mergedConfigData["issue-creator-multiplier"],
timeLabels: mergedConfigData["time-labels"],
priorityLabels: mergedConfigData["priority-labels"],
paymentPermitMaxPrice: mergedConfigData["payment-permit-max-price"],
disableAnalytics: mergedConfigData["disable-analytics"],
bountyHunterMax: mergedConfigData["max-concurrent-assigns"],
incentiveMode: mergedConfigData["comment-incentives"],
incentives: mergedConfigData["incentives"],
defaultLabels: mergedConfigData["default-labels"],
promotionComment: mergedConfigData["promotion-comment"],
registerWalletWithVerification: mergedConfigData["register-wallet-with-verification"],
enableAccessControl: mergedConfigData["enable-access-control"]
};

return configData;
Expand Down
Loading

0 comments on commit 87c01de

Please sign in to comment.