diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 4c40bc6b0..0846dadb4 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -1,134 +1,108 @@ import { CommentElementPricing } from "../types"; import { WideLabel, WideOrgConfig, WideRepoConfig } from "./private"; -export const getChainId = (parsedRepo: WideRepoConfig | undefined, parsedOrg: WideOrgConfig | undefined, parsedDefaultConfig: WideRepoConfig): number => { +interface Configs { + parsedRepo?: WideRepoConfig; + parsedOrg?: WideOrgConfig; + parsedDefault: WideRepoConfig; +} + +export const getChainId = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => { if (parsedRepo && parsedRepo["chain-id"] && !Number.isNaN(Number(parsedRepo["chain-id"]))) { return Number(parsedRepo["chain-id"]); } else if (parsedOrg && parsedOrg["chain-id"] && !Number.isNaN(Number(parsedOrg["chain-id"]))) { return Number(parsedOrg["chain-id"]); } else { - return Number(parsedDefaultConfig["chain-id"]!); + return Number(parsedDefault["chain-id"]!); } }; -export const getBaseMultiplier = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): number => { +export const getBaseMultiplier = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => { if (parsedRepo && parsedRepo["base-multiplier"] && !Number.isNaN(Number(parsedRepo["base-multiplier"]))) { return Number(parsedRepo["base-multiplier"]); } else if (parsedOrg && parsedOrg["base-multiplier"] && !Number.isNaN(Number(parsedOrg["base-multiplier"]))) { return Number(parsedOrg["base-multiplier"]); } else { - return Number(parsedDefaultConfig["base-multiplier"]!); + return Number(parsedDefault["base-multiplier"]!); } }; -export const getCreatorMultiplier = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): number => { +export const getCreatorMultiplier = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => { if (parsedRepo && parsedRepo["issue-creator-multiplier"] && !Number.isNaN(Number(parsedRepo["issue-creator-multiplier"]))) { return Number(parsedRepo["issue-creator-multiplier"]); } else if (parsedOrg && parsedOrg["issue-creator-multiplier"] && !Number.isNaN(Number(parsedOrg["issue-creator-multiplier"]))) { return Number(parsedOrg["issue-creator-multiplier"]); } else { - return Number(parsedDefaultConfig["issue-creator-multiplier"]); + return Number(parsedDefault["issue-creator-multiplier"]); } }; -export const getTimeLabels = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): WideLabel[] => { +export const getTimeLabels = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): WideLabel[] => { if (parsedRepo && parsedRepo["time-labels"] && Array.isArray(parsedRepo["time-labels"]) && parsedRepo["time-labels"].length > 0) { return parsedRepo["time-labels"]; } else if (parsedOrg && parsedOrg["time-labels"] && Array.isArray(parsedOrg["time-labels"]) && parsedOrg["time-labels"].length > 0) { return parsedOrg["time-labels"]; } else { - return parsedDefaultConfig["time-labels"]!; + return parsedDefault["time-labels"]!; } }; -export const getPriorityLabels = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): WideLabel[] => { +export const getPriorityLabels = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): WideLabel[] => { if (parsedRepo && parsedRepo["priority-labels"] && Array.isArray(parsedRepo["priority-labels"]) && parsedRepo["priority-labels"].length > 0) { return parsedRepo["priority-labels"]; } else if (parsedOrg && parsedOrg["priority-labels"] && Array.isArray(parsedOrg["priority-labels"]) && parsedOrg["priority-labels"].length > 0) { return parsedOrg["priority-labels"]; } else { - return parsedDefaultConfig["priority-labels"]!; + return parsedDefault["priority-labels"]!; } }; -export const getCommentItemPrice = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): CommentElementPricing => { +export const getCommentItemPrice = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): CommentElementPricing => { if (parsedRepo && parsedRepo["comment-element-pricing"]) { return parsedRepo["comment-element-pricing"]; } else if (parsedOrg && parsedOrg["comment-element-pricing"]) { return parsedOrg["comment-element-pricing"]; } else { - return parsedDefaultConfig["comment-element-pricing"]!; + return parsedDefault["comment-element-pricing"]!; } }; -export const getAutoPayMode = (parsedRepo: WideRepoConfig | undefined, parsedOrg: WideOrgConfig | undefined, parsedDefaultConfig: WideRepoConfig): boolean => { +export const getAutoPayMode = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => { if (parsedRepo && parsedRepo["auto-pay-mode"] && typeof parsedRepo["auto-pay-mode"] === "boolean") { return parsedRepo["auto-pay-mode"]; } else if (parsedOrg && parsedOrg["auto-pay-mode"] && typeof parsedOrg["auto-pay-mode"] === "boolean") { return parsedOrg["auto-pay-mode"]; } else { - return parsedDefaultConfig["auto-pay-mode"]!; + return parsedDefault["auto-pay-mode"]!; } }; -export const getAnalyticsMode = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): boolean => { +export const getAnalyticsMode = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => { if (parsedRepo && parsedRepo["analytics-mode"] && typeof parsedRepo["analytics-mode"] === "boolean") { return parsedRepo["analytics-mode"]; } else if (parsedOrg && parsedOrg["analytics-mode"] && typeof parsedOrg["analytics-mode"] === "boolean") { return parsedOrg["analytics-mode"]; } else { - return parsedDefaultConfig["analytics-mode"]!; + return parsedDefault["analytics-mode"]!; } }; -export const getIncentiveMode = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): boolean => { +export const getIncentiveMode = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): boolean => { if (parsedRepo && parsedRepo["incentive-mode"] && typeof parsedRepo["incentive-mode"] === "boolean") { return parsedRepo["incentive-mode"]; } else if (parsedOrg && parsedOrg["incentive-mode"] && typeof parsedOrg["incentive-mode"] === "boolean") { return parsedOrg["incentive-mode"]; } else { - return parsedDefaultConfig["incentive-mode"]!; + return parsedDefault["incentive-mode"]!; } }; -export const getBountyHunterMax = ( - parsedRepo: WideRepoConfig | undefined, - parsedOrg: WideOrgConfig | undefined, - parsedDefaultConfig: WideRepoConfig -): number => { +export const getBountyHunterMax = ({ parsedRepo, parsedOrg, parsedDefault }: Configs): number => { if (parsedRepo && parsedRepo["max-concurrent-bounties"] && !Number.isNaN(Number(parsedRepo["max-concurrent-bounties"]))) { return Number(parsedRepo["max-concurrent-bounties"]); } else if (parsedOrg && parsedOrg["max-concurrent-bounties"] && !Number.isNaN(Number(parsedOrg!["max-concurrent-bounties"]))) { return Number(parsedOrg["max-concurrent-bounties"]); } else { - return Number(parsedDefaultConfig["max-concurrent-bounties"]!); + return Number(parsedDefault["max-concurrent-bounties"]!); } }; diff --git a/src/utils/private.ts b/src/utils/private.ts index 9b481998b..a0031c2db 100644 --- a/src/utils/private.ts +++ b/src/utils/private.ts @@ -78,13 +78,9 @@ export const parseYAML = (data: any): any | undefined => { } }; -export const getDefaultConfig = (): WideRepoConfig | undefined => { - try { - const defaultConfig = readFileSync(`${__dirname}/../../ubiquibot-config-default.yml`, "utf8"); - return parseYAML(defaultConfig) as WideRepoConfig; - } catch (error: any) { - return undefined; - } +export const getDefaultConfig = (): WideRepoConfig => { + const defaultConfig = readFileSync(`${__dirname}/../../ubiquibot-config-default.yml`, "utf8"); + return parseYAML(defaultConfig); }; export const getPrivateKey = async (cipherText: string): Promise => { @@ -133,21 +129,22 @@ export const getWideConfig = async (context: Context) => { const parsedOrg: WideOrgConfig | undefined = parseYAML(orgConfig); const parsedRepo: WideRepoConfig | undefined = parseYAML(repoConfig); - const parsedDefault: WideRepoConfig = getDefaultConfig()!; + const parsedDefault: WideRepoConfig = getDefaultConfig(); const privateKeyDecrypted = parsedOrg && parsedOrg[KEY_NAME] ? await getPrivateKey(parsedOrg[KEY_NAME]) : undefined; + const configs = { parsedRepo, parsedOrg, parsedDefault }; const configData = { - chainId: getChainId(parsedRepo, parsedOrg, parsedDefault), + chainId: getChainId(configs), privateKey: privateKeyDecrypted ?? "", - baseMultiplier: getBaseMultiplier(parsedRepo, parsedOrg, parsedDefault), - issueCreatorMultiplier: getCreatorMultiplier(parsedRepo, parsedOrg, parsedDefault), - timeLabels: getTimeLabels(parsedRepo, parsedOrg, parsedDefault), - priorityLabels: getPriorityLabels(parsedRepo, parsedOrg, parsedDefault), - autoPayMode: getAutoPayMode(parsedRepo, parsedOrg, parsedDefault), - analyticsMode: getAnalyticsMode(parsedRepo, parsedOrg, parsedDefault), - bountyHunterMax: getBountyHunterMax(parsedRepo, parsedOrg, parsedDefault), - incentiveMode: getIncentiveMode(parsedRepo, parsedOrg, parsedDefault), - commentElementPricing: getCommentItemPrice(parsedRepo, parsedOrg, parsedDefault), + baseMultiplier: getBaseMultiplier(configs), + issueCreatorMultiplier: getCreatorMultiplier(configs), + timeLabels: getTimeLabels(configs), + priorityLabels: getPriorityLabels(configs), + autoPayMode: getAutoPayMode(configs), + analyticsMode: getAnalyticsMode(configs), + bountyHunterMax: getBountyHunterMax(configs), + incentiveMode: getIncentiveMode(configs), + commentElementPricing: getCommentItemPrice(configs), }; return configData;