Skip to content

Commit

Permalink
feat: add repo specific wallet support (ubiquity#747)
Browse files Browse the repository at this point in the history
* feat: add repo specific wallet support

* feat: repo specific wallet support

* feat: repo specific wallet support

* feat: add repo specific wallet support

---------

Co-authored-by: whilefoo <[email protected]>
  • Loading branch information
BeanieMen and whilefoo authored Sep 19, 2023
1 parent e734904 commit dba8308
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/handlers/push/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getBotContext, getLogger } from "../../bindings";
import { createCommitComment, getFileContent } from "../../helpers";
import { CommitsPayload, PushPayload, WideOrgConfigSchema } from "../../types";
import { CommitsPayload, PushPayload, WideConfigSchema } from "../../types";
import { parseYAML } from "../../utils/private";
import { updateBaseRate } from "./update-base";
import { validate } from "../../utils/ajv";
Expand Down Expand Up @@ -87,7 +87,7 @@ export const validateConfigChange = async () => {
if (configFileContent) {
const decodedConfig = Buffer.from(configFileContent, "base64").toString();
const config = parseYAML(decodedConfig);
const { valid, error } = validate(WideOrgConfigSchema, config);
const { valid, error } = validate(WideConfigSchema, config);
if (!valid) {
await createCommitComment(`@${payload.sender.login} Config validation failed! ${error}`, commitSha, BASE_RATE_FILE);
}
Expand Down
7 changes: 1 addition & 6 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const WideConfigSchema = Type.Object(
"register-wallet-with-verification": Type.Optional(Type.Boolean()),
"enable-access-control": Type.Optional(AccessControlSchema),
"stale-bounty-time": Type.Optional(Type.String()),
"private-key-encrypted": Type.Optional(Type.String()),
},
{
additionalProperties: false,
Expand All @@ -166,12 +167,6 @@ export type WideConfig = Static<typeof WideConfigSchema>;

export type WideRepoConfig = WideConfig;

export const WideOrgConfigSchema = Type.Composite([Type.Object({ "private-key-encrypted": Type.Optional(Type.String()) }), WideConfigSchema], {
additionalProperties: false,
});

export type WideOrgConfig = Static<typeof WideOrgConfigSchema>;

export const MergedConfigSchema = Type.Object({
"evm-network-id": Type.Number(),
"price-multiplier": Type.Number(),
Expand Down
18 changes: 13 additions & 5 deletions src/utils/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import merge from "lodash/merge";

import { DefaultConfig } from "../configs";
import { validate } from "./ajv";
import { WideConfig, WideOrgConfig, WideRepoConfig, WideConfigSchema, WideOrgConfigSchema } from "../types";
import { WideConfig, WideRepoConfig, WideConfigSchema } from "../types";

const CONFIG_REPO = "ubiquibot-config";
const CONFIG_PATH = ".github/ubiquibot-config.yml";
Expand Down Expand Up @@ -35,7 +35,7 @@ export const getConfigSuperset = async (context: Context, type: "org" | "repo",

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

Expand Down Expand Up @@ -111,10 +111,10 @@ export const getWideConfig = async (context: Context) => {
const orgConfig = await getConfigSuperset(context, "org", CONFIG_PATH);
const repoConfig = await getConfigSuperset(context, "repo", CONFIG_PATH);

const parsedOrg: WideOrgConfig | undefined = parseYAML(orgConfig);
const parsedOrg: WideRepoConfig | undefined = parseYAML(orgConfig);

if (parsedOrg) {
const { valid, error } = validate(WideOrgConfigSchema, parsedOrg);
const { valid, error } = validate(WideConfigSchema, parsedOrg);
if (!valid) {
throw new Error(`Invalid org config: ${error}`);
}
Expand All @@ -127,7 +127,15 @@ export const getWideConfig = async (context: Context) => {
}
}
const parsedDefault: MergedConfig = DefaultConfig;
const privateKeyDecrypted = parsedOrg && parsedOrg[KEY_NAME] ? await getPrivateKey(parsedOrg[KEY_NAME]) : undefined;

let privateKeyDecrypted;
if (parsedRepo && parsedRepo[KEY_NAME]) {
privateKeyDecrypted = await getPrivateKey(parsedRepo[KEY_NAME]);
} else if (parsedOrg && parsedOrg[KEY_NAME]) {
privateKeyDecrypted = await getPrivateKey(parsedOrg[KEY_NAME]);
} else {
privateKeyDecrypted = undefined;
}

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

0 comments on commit dba8308

Please sign in to comment.