diff --git a/.changeset/slimy-berries-move.md b/.changeset/slimy-berries-move.md
new file mode 100644
index 000000000..126e417c5
--- /dev/null
+++ b/.changeset/slimy-berries-move.md
@@ -0,0 +1,5 @@
+---
+'@roadiehq/backstage-plugin-github-insights': patch
+---
+
+Fix issue where .environment may be not available
diff --git a/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.test.tsx b/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.test.tsx
index 506a0f153..55f7aa41b 100644
--- a/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.test.tsx
+++ b/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.test.tsx
@@ -15,7 +15,7 @@
*/
import React from 'react';
-import { render, screen } from '@testing-library/react';
+import { render, screen, waitFor } from '@testing-library/react';
import { AnyApiRef, githubAuthApiRef } from '@backstage/core-plugin-api';
import { ConfigReader } from '@backstage/core-app-api';
import { rest } from 'msw';
@@ -31,8 +31,8 @@ import { lightTheme } from '@backstage/theme';
import EnvironmentsCard from '.';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import {
- scmIntegrationsApiRef,
ScmIntegrationsApi,
+ scmIntegrationsApiRef,
} from '@backstage/integration-react';
import { defaultIntegrationsConfig } from '../../../mocks/scmIntegrationsApiMock';
@@ -99,7 +99,7 @@ describe('EnvironmentsCard', () => {
worker.use(
rest.get(
- 'https://api.github.com/repos/mcalus3/backstage/releases',
+ 'https://api.github.com/repos/mcalus3/backstage/environments',
(_, res, ctx) => res(ctx.status(304), ctx.json({})),
),
);
@@ -119,4 +119,38 @@ describe('EnvironmentsCard', () => {
expect(await screen.findAllByText('env1')).toHaveLength(1);
expect(await screen.findAllByText('env2')).toHaveLength(1);
});
+
+ it('should handle gracefully if no environment is found', async () => {
+ worker.restoreHandlers();
+ worker.use(
+ rest.get(
+ 'https://api.github.com/repos/mcalus3/backstage/environments',
+ (_, res, ctx) =>
+ res(
+ ctx.status(404),
+ ctx.json({
+ message: 'Not Found',
+ documentation_url:
+ 'https://docs.github.com/rest/deployments/environments#list-environments',
+ }),
+ ),
+ ),
+ );
+
+ render(
+ wrapInTestApp(
+
+
+
+
+
+
+ ,
+ ),
+ );
+
+ await waitFor(() => {
+ expect(screen.queryByText('Environments')).not.toBeInTheDocument();
+ });
+ });
});
diff --git a/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.tsx b/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.tsx
index 389bd92a8..b1d23f9e8 100644
--- a/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.tsx
+++ b/plugins/frontend/backstage-plugin-github-insights/src/components/Widgets/EnvironmentsCard/EnvironmentsCard.tsx
@@ -63,7 +63,7 @@ const EnvironmentsCard = () => {
);
}
- return value.environments.length && owner && repo ? (
+ return value?.environments?.length && owner && repo ? (