Skip to content

Commit 13a7a3a

Browse files
authored
fix(ai-integrations): add null check for catalogKeys (#644)
* fix(ai-integrations): add null check for catalogKeys Signed-off-by: John Collier <jcollier@redhat.com> * Add changeset Signed-off-by: John Collier <jcollier@redhat.com> * Include undefined check just incase Signed-off-by: John Collier <jcollier@redhat.com> * Add test Signed-off-by: John Collier <jcollier@redhat.com> --------- Signed-off-by: John Collier <jcollier@redhat.com>
1 parent d6bbaa9 commit 13a7a3a

5 files changed

Lines changed: 66 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-catalog-backend-module-model-catalog': patch
3+
---
4+
5+
Add null check when retrieving keys from catalog bridge

workspaces/ai-integrations/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,23 @@ To run the rhdh model catalog backend module in development mode:
1313
2. Run `yarn install` to install dependencies
1414

1515
3. Run `yarn dev` to launch the module
16+
17+
## Building the plugin archive
18+
19+
**Note:** We suggest building on an x86 environment (we've found the plugin archive does not successfully deploy when built on an arm64 environment)
20+
21+
The ai-experience plugins are packaged as part of an OCI archive.
22+
23+
To build the plugin archive, ensure you have `docker` or `podman` installed on your system, and run:
24+
25+
```
26+
npx --yes @janus-idp/cli@$latest package package-dynamic-plugins --tag "${PLUGIN_CONTAINER_TAG}"
27+
```
28+
29+
Where `PLUGIN_CONTAINER_TAG` is the image tag you would like to use (e.g. `quay.io/<your-user>/ai-experience:latest`)
30+
31+
If you would like to build with `docker`, add the `--user-docker` tag like so:
32+
33+
```
34+
npx --yes @janus-idp/cli@$latest package package-dynamic-plugins --tag "${PLUGIN_CONTAINER_TAG}" --use-docker
35+
```

workspaces/ai-integrations/plugins/catalog-backend-module-model-catalog/src/providers/ModelCatalogResourceEntityProvider.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,29 @@ describe('ModelCatalogResourceEntityProvider', () => {
141141
(connection.applyMutation as jest.Mock).mock.calls,
142142
).toMatchSnapshot();
143143
});
144+
145+
it('should connect and run should resolve even if the keys returned are null', async () => {
146+
(fetchModelCatalogKeys as jest.Mock).mockReturnValue(Promise.resolve(null));
147+
148+
const mcprovider = ModelCatalogResourceEntityProvider.fromConfig(
149+
{
150+
config: mockServices.rootConfig({ data: CONFIG }),
151+
logger: mockServices.logger.mock(),
152+
},
153+
{
154+
schedule,
155+
},
156+
);
157+
158+
for await (const k of mcprovider) {
159+
await k.connect(connection);
160+
await schedule.runAll();
161+
}
162+
163+
expect(connection.applyMutation).toHaveBeenCalledTimes(1);
164+
expect(fetchModelCatalogFromKey).toHaveBeenCalledTimes(0);
165+
expect(
166+
(connection.applyMutation as jest.Mock).mock.calls,
167+
).toMatchSnapshot();
168+
});
144169
});

workspaces/ai-integrations/plugins/catalog-backend-module-model-catalog/src/providers/ModelCatalogResourceEntityProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ export class ModelCatalogResourceEntityProvider implements EntityProvider {
154154
);
155155

156156
/** [5]: Fetch the model catalog keys from the bridge and fetch the corresponding catalog entries. */
157-
const catalogKeys = await fetchModelCatalogKeys(this.baseUrl);
157+
let catalogKeys = await fetchModelCatalogKeys(this.baseUrl);
158+
// If no models are registered yet, the catalogKeys array may be null, so handle it by setting it to be an emptyList
159+
if (catalogKeys === null || catalogKeys === undefined) {
160+
catalogKeys = [];
161+
}
158162
let entityList: Entity[] = [];
159163

160164
await Promise.all(

workspaces/ai-integrations/plugins/catalog-backend-module-model-catalog/src/providers/__snapshots__/ModelCatalogResourceEntityProvider.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ exports[`ModelCatalogResourceEntityProvider should connect and run should resolv
3333
],
3434
]
3535
`;
36+
37+
exports[`ModelCatalogResourceEntityProvider should connect and run should resolve even if the keys returned are null 1`] = `
38+
[
39+
[
40+
{
41+
"entities": [],
42+
"type": "full",
43+
},
44+
],
45+
]
46+
`;

0 commit comments

Comments
 (0)