Skip to content

Commit 57e5265

Browse files
blvaCopilot
andauthored
chore: enable noUncheckedIndexedAccess (#285)
Co-authored-by: Copilot <[email protected]>
1 parent fcd62b6 commit 57e5265

30 files changed

+124
-87
lines changed

scripts/apply.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ async function main() {
5858
const httpCode = parseInt(code, 10);
5959
if (httpCode >= 200 && httpCode < 300) {
6060
const response = operation.responses[code];
61-
const responseObject = findObjectFromRef(response, openapi);
62-
if (responseObject.content) {
61+
const responseObject = findObjectFromRef(response, openapi) as OpenAPIV3_1.ResponseObject;
62+
if (responseObject && responseObject.content) {
6363
for (const contentType in responseObject.content) {
6464
const content = responseObject.content[contentType];
65-
hasResponseBody = !!content.schema;
65+
hasResponseBody = !!content?.schema;
6666
}
6767
}
6868
}
@@ -84,7 +84,7 @@ async function main() {
8484
operationId: operation.operationId || "",
8585
requiredParams,
8686
hasResponseBody,
87-
tag: operation.tags[0],
87+
tag: operation.tags?.[0] ?? "",
8888
});
8989
}
9090
}

src/common/atlas/cluster.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ export function formatCluster(cluster: ClusterDescription20240805): Cluster {
5050
};
5151
});
5252

53-
const instanceSize = (regionConfigs.length <= 0 ? undefined : regionConfigs[0].instanceSize) || "UNKNOWN";
54-
53+
const instanceSize = regionConfigs[0]?.instanceSize ?? "UNKNOWN";
5554
const clusterInstanceType = instanceSize == "M0" ? "FREE" : "DEDICATED";
5655

5756
return {

src/logger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export const LogId = {
3232

3333
mongodbConnectFailure: mongoLogId(1_004_001),
3434
mongodbDisconnectFailure: mongoLogId(1_004_002),
35+
36+
toolUpdateFailure: mongoLogId(1_005_001),
3537
} as const;
3638

3739
abstract class LoggerBase {

src/tools/atlas/create/createProject.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export class CreateProjectTool extends AtlasToolBase {
2828
"No organizations were found in your MongoDB Atlas account. Please create an organization first."
2929
);
3030
}
31-
organizationId = organizations.results[0].id;
31+
const firstOrg = organizations.results[0];
32+
if (!firstOrg?.id) {
33+
throw new Error(
34+
"The first organization found does not have an ID. Please check your Atlas account."
35+
);
36+
}
37+
organizationId = firstOrg.id;
3238
assumedOrg = true;
3339
} catch {
3440
throw new Error(

src/tools/atlas/read/listProjects.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export class ListProjectsTool extends AtlasToolBase {
2121

2222
const orgs: Record<string, string> = orgData.results
2323
.map((org) => [org.id || "", org.name])
24-
.reduce((acc, [id, name]) => ({ ...acc, [id]: name }), {});
24+
.filter(([id]) => id)
25+
.reduce((acc, [id, name]) => ({ ...acc, [id as string]: name }), {});
2526

2627
const data = orgId
2728
? await this.session.apiClient.listOrganizationProjects({
@@ -41,7 +42,8 @@ export class ListProjectsTool extends AtlasToolBase {
4142
const rows = data.results
4243
.map((project) => {
4344
const createdAt = project.created ? new Date(project.created).toLocaleString() : "N/A";
44-
return `${project.name} | ${project.id} | ${orgs[project.orgId]} | ${project.orgId} | ${createdAt}`;
45+
const orgName = orgs[project.orgId] ?? "N/A";
46+
return `${project.name} | ${project.id} | ${orgName} | ${project.orgId} | ${createdAt}`;
4547
})
4648
.join("\n");
4749
const formattedProjects = `Project Name | Project ID | Organization Name | Organization ID | Created At

src/tools/tool.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ export abstract class ToolBase {
9393
this.update = (updates: { name?: string; description?: string; inputSchema?: AnyZodObject }) => {
9494
const tools = server["_registeredTools"] as { [toolName: string]: RegisteredTool };
9595
const existingTool = tools[this.name];
96+
97+
if (!existingTool) {
98+
logger.warning(LogId.toolUpdateFailure, "tool", `Tool ${this.name} not found in update`);
99+
return;
100+
}
101+
96102
existingTool.annotations = this.annotations;
97103

98104
if (updates.name && updates.name !== this.name) {

tests/integration/tools/atlas/accessLists.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describeWithAtlas("ip access lists", (integration) => {
6767
})) as CallToolResult;
6868
expect(response.content).toBeArray();
6969
expect(response.content).toHaveLength(1);
70-
expect(response.content[0].text).toContain("IP/CIDR ranges added to access list");
70+
expect(response.content[0]?.text).toContain("IP/CIDR ranges added to access list");
7171
});
7272
});
7373

@@ -90,7 +90,7 @@ describeWithAtlas("ip access lists", (integration) => {
9090
expect(response.content).toBeArray();
9191
expect(response.content).toHaveLength(1);
9292
for (const value of values) {
93-
expect(response.content[0].text).toContain(value);
93+
expect(response.content[0]?.text).toContain(value);
9494
}
9595
});
9696
});

tests/integration/tools/atlas/alerts.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describeWithAtlas("alerts", (integration) => {
2323
expect(response.content).toBeArray();
2424
expect(response.content).toHaveLength(1);
2525

26-
const data = parseTable(response.content[0].text as string);
26+
const data = parseTable(response.content[0]?.text as string);
2727
expect(data).toBeArray();
2828

2929
// Since we can't guarantee alerts will exist, we just verify the table structure

tests/integration/tools/atlas/atlasHelpers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export function parseTable(text: string): Record<string, string>[] {
7575
.map((cells) => {
7676
const row: Record<string, string> = {};
7777
cells.forEach((cell, index) => {
78-
row[headers[index]] = cell;
78+
if (headers) {
79+
row[headers[index] ?? ""] = cell;
80+
}
7981
});
8082
return row;
8183
});
@@ -87,14 +89,14 @@ async function createProject(apiClient: ApiClient): Promise<Group> {
8789
const projectName: string = `testProj-` + randomId;
8890

8991
const orgs = await apiClient.listOrganizations();
90-
if (!orgs?.results?.length || !orgs.results[0].id) {
92+
if (!orgs?.results?.length || !orgs.results[0]?.id) {
9193
throw new Error("No orgs found");
9294
}
9395

9496
const group = await apiClient.createProject({
9597
body: {
9698
name: projectName,
97-
orgId: orgs.results[0].id,
99+
orgId: orgs.results[0]?.id ?? "",
98100
} as Group,
99101
});
100102

tests/integration/tools/atlas/clusters.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describeWithAtlas("clusters", (integration) => {
8888
})) as CallToolResult;
8989
expect(response.content).toBeArray();
9090
expect(response.content).toHaveLength(2);
91-
expect(response.content[0].text).toContain("has been created");
91+
expect(response.content[0]?.text).toContain("has been created");
9292
});
9393
});
9494

@@ -113,7 +113,7 @@ describeWithAtlas("clusters", (integration) => {
113113
})) as CallToolResult;
114114
expect(response.content).toBeArray();
115115
expect(response.content).toHaveLength(1);
116-
expect(response.content[0].text).toContain(`${clusterName} | `);
116+
expect(response.content[0]?.text).toContain(`${clusterName} | `);
117117
});
118118
});
119119

@@ -135,7 +135,7 @@ describeWithAtlas("clusters", (integration) => {
135135
.callTool({ name: "atlas-list-clusters", arguments: { projectId } })) as CallToolResult;
136136
expect(response.content).toBeArray();
137137
expect(response.content).toHaveLength(2);
138-
expect(response.content[1].text).toContain(`${clusterName} | `);
138+
expect(response.content[1]?.text).toContain(`${clusterName} | `);
139139
});
140140
});
141141

@@ -178,7 +178,7 @@ describeWithAtlas("clusters", (integration) => {
178178
})) as CallToolResult;
179179
expect(response.content).toBeArray();
180180
expect(response.content).toHaveLength(1);
181-
expect(response.content[0].text).toContain(`Connected to cluster "${clusterName}"`);
181+
expect(response.content[0]?.text).toContain(`Connected to cluster "${clusterName}"`);
182182
});
183183
});
184184
});

0 commit comments

Comments
 (0)