Skip to content

Commit

Permalink
chore: merge all configs
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jun 1, 2024
1 parent 596fd31 commit 79bed7f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"knip": "knip --config .github/knip.ts",
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts",
"test": "bun test",
"setup-kv": "bun --env-file=.dev.vars deploy/setup_kv_namespace.ts"
},
"setup-kv": "bun --env-file=.dev.vars deploy/setup_kv_namespace.ts"
},
"keywords": [
"typescript",
"template",
Expand All @@ -45,6 +45,7 @@
"@sinclair/typebox": "^0.32.5",
"@ubiquibot/configuration": "2.1.0",
"dotenv": "^16.4.4",
"lodash": "4.17.21",
"smee-client": "^2.0.0",
"yaml": "^2.4.1"
},
Expand Down
50 changes: 27 additions & 23 deletions src/github/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Value } from "@sinclair/typebox/value";
import { generateConfiguration } from "@ubiquibot/configuration";
import { merge } from "lodash";
import YAML from "yaml";
import { GitHubContext } from "../github-context";
import { expressionRegex } from "../types/plugin";
Expand All @@ -17,39 +18,42 @@ export async function getConfig(context: GitHubContext): Promise<PluginConfigura
return defaultConfiguration;
}

let _innerConfig: PluginConfiguration;
// First, try to get the config in the target repo
_innerConfig = parseYaml(
let mergedConfiguration = defaultConfiguration;

const targetRepoConfiguration: PluginConfiguration = parseYaml(
await download({
context,
repository: payload.repository.name,
owner: payload.repository.owner.login,
})
);
// If no config is found, check if there is an Org wide config
if (!_innerConfig) {
_innerConfig = parseYaml(
await download({
context,
repository: UBIQUIBOT_CONFIG_ORG_REPO,
owner: payload.repository.owner.login,
})
);
const orgRepoConfiguration: PluginConfiguration = parseYaml(
await download({
context,
repository: UBIQUIBOT_CONFIG_ORG_REPO,
owner: payload.repository.owner.login,
})
);
if (orgRepoConfiguration) {
try {
const decodedConfiguration = Value.Decode(configSchema, Value.Default(configSchema, orgRepoConfiguration));
mergedConfiguration = merge(mergedConfiguration, decodedConfiguration);
} catch (error) {
console.error("Error decoding organization repository configuration, will ignore.", error);
}
}
// Otherwise, use defaults
if (!_innerConfig) return defaultConfiguration;

let config: PluginConfiguration;
try {
config = Value.Decode(configSchema, Value.Default(configSchema, _innerConfig));
} catch (error) {
console.error("Error decoding config, will use default.", error);
return defaultConfiguration;
if (targetRepoConfiguration) {
try {
const decodedConfiguration = Value.Decode(configSchema, Value.Default(configSchema, targetRepoConfiguration));
mergedConfiguration = merge(mergedConfiguration, decodedConfiguration);
} catch (error) {
console.error("Error decoding target repository configuration, will ignore.", error);
}
}

checkPluginChains(config);
checkPluginChains(mergedConfiguration);

return config;
return mergedConfiguration;
}

function checkPluginChains(config: PluginConfiguration) {
Expand Down

0 comments on commit 79bed7f

Please sign in to comment.