Skip to content

Commit 8a5f90e

Browse files
committed
feat: update TM base URL retrieval to accept configuration for improved flexibility
1 parent 7a0ce95 commit 8a5f90e

File tree

13 files changed

+50
-34
lines changed

13 files changed

+50
-34
lines changed

src/lib/tm-base-url.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { apiClient } from "./apiClient.js";
22
import logger from "../logger.js";
3+
import { BrowserStackConfig } from "./types.js";
4+
import { getBrowserStackAuth } from "./get-auth.js";
35

46
const TM_BASE_URLS = [
57
"https://test-management.browserstack.com",
@@ -9,18 +11,25 @@ const TM_BASE_URLS = [
911

1012
let cachedBaseUrl: string | null = null;
1113

12-
export async function getTMBaseURL(): Promise<string> {
14+
export async function getTMBaseURL(config: BrowserStackConfig): Promise<string> {
1315
if (cachedBaseUrl) {
1416
logger.debug(`Using cached TM base URL: ${cachedBaseUrl}`);
1517
return cachedBaseUrl;
1618
}
1719

18-
logger.info("No cached TM base URL found, testing available URLs");
20+
logger.info("No cached TM base URL found, testing available URLs with authentication");
21+
22+
const authString = getBrowserStackAuth(config);
23+
const [username, password] = authString.split(":");
1924

2025
for (const baseUrl of TM_BASE_URLS) {
2126
try {
2227
const res = await apiClient.get({
2328
url: `${baseUrl}/api/v2/projects/`,
29+
headers: {
30+
Authorization:
31+
"Basic " + Buffer.from(`${username}:${password}`).toString("base64"),
32+
},
2433
raise_error: false,
2534
});
2635

src/tools/testmanagement-utils/TCG-utils/api.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ export async function fetchFormFields(
2323
projectId: string,
2424
config: BrowserStackConfig,
2525
): Promise<{ default_fields: any; custom_fields: any }> {
26+
const tmBaseUrl = await getTMBaseURL(config);
2627
const res = await apiClient.get({
27-
url: await FORM_FIELDS_URL(projectId),
28+
url: FORM_FIELDS_URL(tmBaseUrl, projectId),
2829
headers: {
2930
"API-TOKEN": getBrowserStackAuth(config),
3031
},
@@ -43,9 +44,9 @@ export async function triggerTestCaseGeneration(
4344
source: string,
4445
config: BrowserStackConfig,
4546
): Promise<string> {
46-
const tmBaseUrl = await getTMBaseURL();
47+
const tmBaseUrl = await getTMBaseURL(config);
4748
const res = await apiClient.post({
48-
url: await TCG_TRIGGER_URL(),
49+
url: TCG_TRIGGER_URL(tmBaseUrl),
4950
headers: {
5051
"API-TOKEN": getBrowserStackAuth(config),
5152
"Content-Type": "application/json",
@@ -80,8 +81,9 @@ export async function fetchTestCaseDetails(
8081
if (testCaseIds.length === 0) {
8182
throw new Error("No testCaseIds provided to fetchTestCaseDetails");
8283
}
84+
const tmBaseUrl = await getTMBaseURL(config);
8385
const res = await apiClient.post({
84-
url: await FETCH_DETAILS_URL(),
86+
url: FETCH_DETAILS_URL(tmBaseUrl),
8587
headers: {
8688
"API-TOKEN": getBrowserStackAuth(config),
8789
"request-source": source,
@@ -109,7 +111,8 @@ export async function pollTestCaseDetails(
109111
): Promise<Record<string, any>> {
110112
const detailMap: Record<string, any> = {};
111113
let done = false;
112-
const TCG_POLL_URL_VALUE = await TCG_POLL_URL();
114+
const tmBaseUrl = await getTMBaseURL(config);
115+
const TCG_POLL_URL_VALUE = TCG_POLL_URL(tmBaseUrl);
113116

114117
while (!done) {
115118
// add a bit of jitter to avoid synchronized polling storms
@@ -160,7 +163,8 @@ export async function pollScenariosTestDetails(
160163
const scenariosMap: Record<string, Scenario> = {};
161164
const detailPromises: Promise<Record<string, any>>[] = [];
162165
let iteratorCount = 0;
163-
const TCG_POLL_URL_VALUE = await TCG_POLL_URL();
166+
const tmBaseUrl = await getTMBaseURL(config);
167+
const TCG_POLL_URL_VALUE = TCG_POLL_URL(tmBaseUrl);
164168

165169
// Promisify interval-style polling using a wrapper
166170
await new Promise<void>((resolve, reject) => {
@@ -283,7 +287,8 @@ export async function bulkCreateTestCases(
283287
const total = Object.keys(scenariosMap).length;
284288
let doneCount = 0;
285289
let testCaseCount = 0;
286-
const BULK_CREATE_URL_VALUE = await BULK_CREATE_URL(projectId, folderId);
290+
const tmBaseUrl = await getTMBaseURL(config);
291+
const BULK_CREATE_URL_VALUE = BULK_CREATE_URL(tmBaseUrl, projectId, folderId);
287292

288293
for (const { id, testcases } of Object.values(scenariosMap)) {
289294
const testCaseLength = testcases.length;
@@ -346,7 +351,7 @@ export async function projectIdentifierToId(
346351
projectId: string,
347352
config: BrowserStackConfig,
348353
): Promise<string> {
349-
const tmBaseUrl = await getTMBaseURL();
354+
const tmBaseUrl = await getTMBaseURL(config);
350355
const url = `${tmBaseUrl}/api/v1/projects/?q=${projectId}`;
351356

352357
const response = await apiClient.get({
@@ -374,7 +379,7 @@ export async function testCaseIdentifierToDetails(
374379
testCaseIdentifier: string,
375380
config: BrowserStackConfig,
376381
): Promise<{ testCaseId: string; folderId: string }> {
377-
const tmBaseUrl = await getTMBaseURL();
382+
const tmBaseUrl = await getTMBaseURL(config);
378383
const url = `${tmBaseUrl}/api/v1/projects/${projectId}/test-cases/search?q[query]=${testCaseIdentifier}`;
379384

380385
const response = await apiClient.get({
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { getTMBaseURL } from "../../../lib/tm-base-url.js";
2-
3-
export const TCG_TRIGGER_URL = async () =>
4-
`${await getTMBaseURL()}/api/v1/integration/tcg/test-generation/suggest-test-cases`;
5-
export const TCG_POLL_URL = async () =>
6-
`${await getTMBaseURL()}/api/v1/integration/tcg/test-generation/test-cases-polling`;
7-
export const FETCH_DETAILS_URL = async () =>
8-
`${await getTMBaseURL()}/api/v1/integration/tcg/test-generation/fetch-test-case-details`;
9-
export const FORM_FIELDS_URL = async (projectId: string) =>
10-
`${await getTMBaseURL()}/api/v1/projects/${projectId}/form-fields-v2`;
11-
export const BULK_CREATE_URL = async (projectId: string, folderId: string) =>
12-
`${await getTMBaseURL()}/api/v1/projects/${projectId}/folder/${folderId}/bulk-test-cases`;
1+
export const TCG_TRIGGER_URL = (baseUrl: string) =>
2+
`${baseUrl}/api/v1/integration/tcg/test-generation/suggest-test-cases`;
3+
4+
export const TCG_POLL_URL = (baseUrl: string) =>
5+
`${baseUrl}/api/v1/integration/tcg/test-generation/test-cases-polling`;
6+
7+
export const FETCH_DETAILS_URL = (baseUrl: string) =>
8+
`${baseUrl}/api/v1/integration/tcg/test-generation/fetch-test-case-details`;
9+
10+
export const FORM_FIELDS_URL = (baseUrl: string, projectId: string) =>
11+
`${baseUrl}/api/v1/projects/${projectId}/form-fields-v2`;
12+
13+
export const BULK_CREATE_URL = (baseUrl: string, projectId: string, folderId: string) =>
14+
`${baseUrl}/api/v1/projects/${projectId}/folder/${folderId}/bulk-test-cases`;

src/tools/testmanagement-utils/add-test-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function addTestResult(
3838
): Promise<CallToolResult> {
3939
try {
4040
const args = AddTestResultSchema.parse(rawArgs);
41-
const tmBaseUrl = await getTMBaseURL();
41+
const tmBaseUrl = await getTMBaseURL(config);
4242
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
4343
args.project_identifier,
4444
)}/test-runs/${encodeURIComponent(args.test_run_id)}/results`;

src/tools/testmanagement-utils/create-lca-steps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export async function createLCASteps(
8282
config,
8383
);
8484

85-
const tmBaseUrl = await getTMBaseURL();
85+
const tmBaseUrl = await getTMBaseURL(config);
8686
const url = `${tmBaseUrl}/api/v1/projects/${projectId}/test-cases/${testCaseId}/lcnc`;
8787

8888
const payload = {

src/tools/testmanagement-utils/create-project-folder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function createProjectOrFolder(
6666
try {
6767
const authString = getBrowserStackAuth(config);
6868
const [username, password] = authString.split(":");
69-
const tmBaseUrl = await getTMBaseURL();
69+
const tmBaseUrl = await getTMBaseURL(config);
7070
const res = await apiClient.post({
7171
url: `${tmBaseUrl}/api/v2/projects`,
7272
headers: {
@@ -97,7 +97,7 @@ export async function createProjectOrFolder(
9797
if (!projId)
9898
throw new Error("Cannot create folder without project_identifier.");
9999
try {
100-
const tmBaseUrl = await getTMBaseURL();
100+
const tmBaseUrl = await getTMBaseURL(config);
101101
const res = await apiClient.post({
102102
url: `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
103103
projId,

src/tools/testmanagement-utils/create-testrun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export async function createTestRun(
6767
};
6868
const args = CreateTestRunSchema.parse(inputArgs);
6969

70-
const tmBaseUrl = await getTMBaseURL();
70+
const tmBaseUrl = await getTMBaseURL(config);
7171
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
7272
args.project_identifier,
7373
)}/test-runs`;

src/tools/testmanagement-utils/list-testcases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function listTestCases(
5050
if (args.priority) params.append("priority", args.priority);
5151
if (args.p !== undefined) params.append("p", args.p.toString());
5252

53-
const tmBaseUrl = await getTMBaseURL();
53+
const tmBaseUrl = await getTMBaseURL(config);
5454
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
5555
args.project_identifier,
5656
)}/test-cases?${params.toString()}`;

src/tools/testmanagement-utils/list-testruns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function listTestRuns(
3636
params.set("run_state", args.run_state);
3737
}
3838

39-
const tmBaseUrl = await getTMBaseURL();
39+
const tmBaseUrl = await getTMBaseURL(config);
4040
const url =
4141
`${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
4242
args.project_identifier,

src/tools/testmanagement-utils/poll-lca-status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function pollLCAStatus(
4040
pollIntervalMs: number = 10 * 1000, // 10 seconds interval
4141
config: BrowserStackConfig,
4242
): Promise<{ resource_path: string; status: string } | null> {
43-
const tmBaseUrl = await getTMBaseURL();
43+
const tmBaseUrl = await getTMBaseURL(config);
4444
const url = `${tmBaseUrl}/api/v1/projects/${projectId}/folder/${folderId}/test-cases/${testCaseId}`;
4545

4646
const startTime = Date.now();

0 commit comments

Comments
 (0)