Skip to content

Commit 30a89c4

Browse files
malwilleygeorge-sentry
authored andcommitted
fix(aci): Handle invalid project IDs in monitor form (#112220)
1 parent c441214 commit 30a89c4

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

static/app/views/detectors/new-setting.spec.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import {
77
import {OrganizationFixture} from 'sentry-fixture/organization';
88
import {ProjectFixture} from 'sentry-fixture/project';
99

10-
import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
10+
import {
11+
render,
12+
screen,
13+
userEvent,
14+
waitFor,
15+
within,
16+
} from 'sentry-test/reactTestingLibrary';
1117
import {selectEvent} from 'sentry-test/selectEvent';
1218

1319
import {OrganizationStore} from 'sentry/stores/organizationStore';
@@ -103,6 +109,27 @@ describe('DetectorEdit', () => {
103109
});
104110
});
105111

112+
it('selects the first project when an invalid project is provided in the URL', async () => {
113+
render(<DetectorNewSettings />, {
114+
organization,
115+
initialRouterConfig: {
116+
...initialRouterConfig,
117+
location: {
118+
...initialRouterConfig.location,
119+
query: {detectorType: 'metric_issue', project: 'not-a-project-id'},
120+
},
121+
},
122+
});
123+
124+
await screen.findByText('New Monitor');
125+
126+
// Verify the project dropdown has the first project selected
127+
const projectSection = screen
128+
.getByText(/Choose the Project and Environment/)
129+
.closest('section')!;
130+
expect(within(projectSection).getByText(project.slug)).toBeInTheDocument();
131+
});
132+
106133
describe('Metric Detector', () => {
107134
const metricRouterConfig = {
108135
...initialRouterConfig,

static/app/views/detectors/new-settings.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ export default function DetectorNewSettings() {
4040
);
4141
}
4242

43-
const project = projectId
44-
? projects.find(p => p.id === projectId)
45-
: orderBy(projects, ['isMember', 'isBookmarked'], ['desc', 'desc'])[0];
43+
const sortedProjects = orderBy(
44+
projects,
45+
['isMember', 'isBookmarked'],
46+
['desc', 'desc']
47+
);
48+
const project =
49+
(projectId ? projects.find(p => p.id === projectId) : null) ?? sortedProjects[0];
4650

4751
if (!project) {
4852
return <LoadingError message={t('Project not found')} />;

0 commit comments

Comments
 (0)