Skip to content

Commit

Permalink
Refactor config normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Oct 13, 2024
1 parent 16537e0 commit 2479f28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
20 changes: 12 additions & 8 deletions src/config/consumer-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@ function normalizeConsumerConfig(
definingRepo: string,
config: PartialConsumerConfig,
): ConsumerConfig {
for (const token of Object.values(config.tokens)) {
for (const name in config.tokens) {
const token = config.tokens[name];

token.as ??= undefined;
token.owner ??= definingOwner;
}

for (const secret of Object.values(config.provision.secrets)) {
for (const name in config.provision.secrets) {
const secret = config.provision.secrets[name];

secret.token = normalizeTokenReference(
definingOwner,
definingRepo,
secret.token,
);

secret.github.repositories = Object.fromEntries(
Object.entries(secret.github.repositories).map(([k, v]) => [
normalizePattern(definingOwner, k),
v,
]),
);
const repositories: typeof secret.github.repositories = {};
for (const pattern in secret.github.repositories) {
repositories[normalizePattern(definingOwner, pattern)] =
secret.github.repositories[pattern];
}
secret.github.repositories = repositories;
}

return config as ConsumerConfig;
Expand Down
29 changes: 15 additions & 14 deletions src/config/provider-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function normalizeProviderConfig(
definingOwner: string,
config: ProviderConfig,
): ProviderConfig {
for (const rule of Object.values(config.permissions.rules.repositories)) {
for (const rule of config.permissions.rules.repositories) {
rule.resources = rule.resources.map((resource) =>
normalizePattern(definingOwner, resource),
);
Expand All @@ -39,23 +39,24 @@ function normalizeProviderConfig(
);
}

for (const rule of Object.values(config.provision.rules.secrets)) {
for (const rule of config.provision.rules.secrets) {
rule.requesters = rule.requesters.map((requester) =>
normalizePattern(definingOwner, requester),
);

rule.allow.github.repositories = Object.fromEntries(
Object.entries(rule.allow.github.repositories).map(([k, v]) => [
normalizePattern(definingOwner, k),
v,
]),
);
rule.deny.github.repositories = Object.fromEntries(
Object.entries(rule.deny.github.repositories).map(([k, v]) => [
normalizePattern(definingOwner, k),
v,
]),
);
const allowRepositories: typeof rule.allow.github.repositories = {};
for (const pattern in rule.allow.github.repositories) {
allowRepositories[normalizePattern(definingOwner, pattern)] =
rule.allow.github.repositories[pattern];
}
rule.allow.github.repositories = allowRepositories;

const denyRepositories: typeof rule.deny.github.repositories = {};
for (const pattern in rule.deny.github.repositories) {
denyRepositories[normalizePattern(definingOwner, pattern)] =
rule.deny.github.repositories[pattern];
}
rule.deny.github.repositories = denyRepositories;
}

return config;
Expand Down

0 comments on commit 2479f28

Please sign in to comment.