Skip to content

Commit

Permalink
fix: merge now happens on plugins keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jun 10, 2024
1 parent 5a7df72 commit f548451
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 54 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@octokit/webhooks": "^12.0.10",
"@sinclair/typebox": "^0.32.5",
"dotenv": "^16.4.4",
"lodash": "4.17.21",
"smee-client": "^2.0.0",
"typebox-validators": "0.3.5",
"yaml": "^2.4.1"
Expand Down
33 changes: 11 additions & 22 deletions src/github/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { emitterEventNames } from "@octokit/webhooks";
import { Value } from "@sinclair/typebox/value";
import { isArray, mergeWith } from "lodash";
import YAML from "yaml";
import { GitHubContext } from "../github-context";
import { expressionRegex } from "../types/plugin";
Expand Down Expand Up @@ -36,27 +34,18 @@ async function getConfigurationFromRepo(context: GitHubContext, repository: stri
return null;
}

type UsesType = PluginConfiguration["plugins"]["*"][0]["uses"];

function mergeEventNames(arrays: UsesType[]) {
const mergedMap = new Map<string, unknown>();

arrays.flat().forEach((item) => {
const pluginKey = JSON.stringify(item.plugin);
mergedMap.set(pluginKey, item); // Override the content if the key exists
});

return Array.from(mergedMap.values());
}

function customMerge(objValue: UsesType, srcValue: UsesType, key: string) {
if ((emitterEventNames as readonly string[]).includes(key) && isArray(objValue) && isArray(srcValue)) {
return mergeEventNames([objValue, srcValue]);
}
}

/**
* Merge configurations based on their 'plugins' keys
*/
function mergeConfigurations(configuration1: PluginConfiguration, configuration2: PluginConfiguration): PluginConfiguration {
return mergeWith({}, configuration1, configuration2, customMerge);
const mergedConfiguration = { ...configuration1 };
for (const key of Object.keys(configuration2.plugins)) {
const pluginKey = key as keyof PluginConfiguration["plugins"];
if (configuration2.plugins[pluginKey]?.length) {
mergedConfiguration.plugins[pluginKey] = configuration2.plugins[pluginKey];
}
}
return mergedConfiguration;
}

export async function getConfig(context: GitHubContext): Promise<PluginConfiguration> {
Expand Down
58 changes: 27 additions & 31 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,8 @@ describe("Worker tests", () => {
expect(pluginChain[0].uses[0].plugin).toBe("https://plugin-a.internal");
expect(pluginChain[0].uses[0].with).toEqual({});
});
it("Should merge the configuration when found", async () => {
const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
id: "",
payload: {
repository: {
owner: { login: "ubiquity" },
name: "conversation-rewards",
},
} as unknown as GitHubContext<"issues.closed">["payload"],
octokit: {
rest: {
repos: {
getContent() {
return {
data: `
incentives:
enabled: false`,
};
},
},
},
},
eventHandler: {} as GitHubEventHandler,
} as unknown as GitHubContext);
expect(cfg).toBeTruthy();
expect(cfg.incentives.enabled).toBeFalse();
});
it("Should merge organization and repository configuration", async () => {
const workflowId = "compute.yml";
const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
Expand Down Expand Up @@ -214,6 +186,12 @@ plugins:
return {
data: `
plugins:
'issues.assigned':
- uses:
- plugin: uses-1/plugin-1
type: github
with:
settings1: 'enabled'
'*':
- uses:
- plugin: repo-1/plugin-1
Expand All @@ -232,14 +210,32 @@ plugins:
},
eventHandler: {} as GitHubEventHandler,
} as unknown as GitHubContext);
expect(cfg.plugins["issues.assigned"]).toEqual([
{
uses: [
{
plugin: {
owner: "uses-1",
repo: "plugin-1",
workflowId,
},
type: "github",
with: {
settings1: "enabled",
},
},
],
skipBotEvents: true,
},
]);
expect(cfg.plugins["*"]).toEqual([
{
uses: [
{
plugin: {
owner: "repo-3",
repo: "plugin-3",
workflowId: "compute.yml",
workflowId,
},
type: "github",
with: {
Expand All @@ -255,7 +251,7 @@ plugins:
plugin: {
owner: "repo-1",
repo: "plugin-1",
workflowId: "compute.yml",
workflowId,
},
type: "github",
with: {
Expand Down

0 comments on commit f548451

Please sign in to comment.