Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow specifying skill subscriptions in file-system hierarchy #14

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 57 additions & 20 deletions lib/events/register_skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,27 +377,36 @@ export function fullImageName(
}${digest ? `@${digest}` : ""}`;
}

export async function inlineDatalogResources(
type DatalogSubscription = {
name: string;
query: string;
limit?: number;
};

async function getDatalogSubscriptionFileMatches(
p: project.Project,
skill: AtomistSkillInput,
): Promise<void> {
const datalogSubscriptions = [];
datalogSubscriptions.push(
...(await project.withGlobMatches<{
name: string;
query: string;
limit?: number;
}>(p, "datalog/subscription/*.edn", async file => {
const filePath = p.path(file);
const fileName = path.basename(filePath);
const extName = path.extname(fileName);
return {
query: (await fs.readFile(filePath)).toString(),
name: fileName.replace(extName, ""),
};
})),
);
(skill.datalogSubscriptions || []).forEach(d => {
matchPath: string,
): Promise<Array<DatalogSubscription>> {
return await project.withGlobMatches<{
name: string;
query: string;
limit?: number;
}>(p, matchPath, async file => {
const filePath = p.path(file);
const fileName = path.basename(filePath);
const extName = path.extname(fileName);
return {
query: (await fs.readFile(filePath)).toString(),
name: fileName.replace(extName, ""),
};
});
}

function updateSubscriptions(
datalogSubscriptions: Array<DatalogSubscription>,
updates: Array<DatalogSubscription>,
): void {
updates.forEach(d => {
const eds = datalogSubscriptions.find(ds => d.name === ds.name);
if (eds) {
eds.query = d.query;
Expand All @@ -406,6 +415,34 @@ export async function inlineDatalogResources(
datalogSubscriptions.push(d);
}
});
}

export async function inlineDatalogResources(
p: project.Project,
skill: AtomistYaml["skill"],
): Promise<void> {
const datalogSubscriptions: Array<DatalogSubscription> = [];

// common subscriptions
datalogSubscriptions.push(
...(await getDatalogSubscriptionFileMatches(
p,
"datalog/subscription/*.edn",
)),
);

// subscription paths defined in yaml
for (const subscriptionPath of skill.datalogSubscriptionPaths || []) {
const subscriptionPathMatches = await getDatalogSubscriptionFileMatches(
p,
`datalog/subscription/${subscriptionPath}`,
);
updateSubscriptions(datalogSubscriptions, subscriptionPathMatches);
}
delete skill.datalogSubscriptionPaths;

// subscriptions defined in yaml
updateSubscriptions(datalogSubscriptions, skill.datalogSubscriptions || []);
skill.datalogSubscriptions = datalogSubscriptions;

const schemata = [...(skill.schemata || [])];
Expand Down
3 changes: 2 additions & 1 deletion lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { handle } from "@atomist/skill";
import { AtomistSkillInput } from "@atomist/skill/lib/definition/subscription/typings/types";
import { Project } from "@atomist/skill/lib/project/project";
import * as fs from "fs-extra";
import * as yaml from "js-yaml";
Expand All @@ -23,7 +24,7 @@ import { Configuration } from "./configuration";
import { RegisterSkill } from "./types";

export interface AtomistYaml {
skill: any;
skill: AtomistSkillInput & { datalogSubscriptionPaths?: string[] };
}

export const AtomistYamlFileName = "skill.package.yaml";
Expand Down
Loading