Skip to content

Commit 1a66531

Browse files
Merge pull request #354 from lt-zeeshan/DOT-5874
DOT-5874: Refactor PDF result fetching and enhance error handling for project ID
2 parents 52ed5f8 + a516bd2 commit 1a66531

File tree

4 files changed

+35
-41
lines changed

4 files changed

+35
-41
lines changed

src/lib/httpClient.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -686,22 +686,20 @@ export default class httpClient {
686686
}
687687

688688
async fetchPdfResults(ctx: Context): Promise<any> {
689-
const params: Record<string, string> = {
690-
projectToken: this.projectToken
691-
};
689+
const params: Record<string, string> = {};
692690

693-
// Use buildId if available, otherwise use buildName
694-
if (ctx.build.id) {
695-
params.buildId = ctx.build.id;
696-
} else if (ctx.build.name) {
697-
params.buildName = ctx.build.name;
691+
if (ctx.build.projectId) {
692+
params.project_id = ctx.build.projectId;
693+
} else {
694+
throw new Error('Project ID not found to fetch PDF results');
698695
}
696+
params.build_id = ctx.build.id;
699697

700698
const auth = Buffer.from(`${this.username}:${this.accessKey}`).toString('base64');
701699

702700
try {
703701
const response = await axios.request({
704-
url: ctx.env.SMARTUI_UPLOAD_URL + '/automation/smart-ui/screenshot/build/status',
702+
url: ctx.env.SMARTUI_UPLOAD_URL + '/smartui/2.0/build/screenshots',
705703
method: 'GET',
706704
params: params,
707705
headers: {

src/lib/utils.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -526,55 +526,54 @@ export function startPdfPolling(ctx: Context) {
526526
}
527527

528528
let attempts = 0;
529-
const maxAttempts = 30; // 5 minutes (10 seconds * 30)
529+
const maxAttempts = 60; // 5 minutes (10 seconds * 30)
530530

531531
console.log(chalk.yellow('Waiting for results...'));
532532

533533
const interval = setInterval(async () => {
534534
attempts++;
535535

536536
try {
537-
const response = await ctx.client.fetchPdfResults(ctx, ctx.log);
537+
const response = await ctx.client.fetchPdfResults(ctx);
538538

539-
if (response.status === 'success' && response.data && response.data.Screenshots) {
539+
if (response.screenshots) {
540540
clearInterval(interval);
541541

542-
const pdfGroups = groupScreenshotsByPdf(response.data.Screenshots);
543-
542+
const pdfGroups = groupScreenshotsByPdf(response.screenshots);
544543
const pdfsWithMismatches = countPdfsWithMismatches(pdfGroups);
545-
const pagesWithMismatches = countPagesWithMismatches(response.data.Screenshots);
544+
const pagesWithMismatches = countPagesWithMismatches(response.screenshots);
546545

547546
console.log(chalk.green('\n✓ PDF Test Results:'));
548-
console.log(chalk.green(`Build Name: ${response.data.buildName}`));
549-
console.log(chalk.green(`Project Name: ${response.data.projectName}`));
547+
console.log(chalk.green(`Build Name: ${response.build.name}`));
548+
console.log(chalk.green(`Project Name: ${response.project.name}`));
550549
console.log(chalk.green(`Total PDFs: ${Object.keys(pdfGroups).length}`));
551-
console.log(chalk.green(`Total Pages: ${response.data.Screenshots.length}`));
550+
console.log(chalk.green(`Total Pages: ${response.screenshots.length}`));
552551

553552
if (pdfsWithMismatches > 0 || pagesWithMismatches > 0) {
554-
console.log(chalk.yellow(`${pdfsWithMismatches} PDFs and ${pagesWithMismatches} Pages in build ${response.data.buildName} have changes present.`));
553+
console.log(chalk.yellow(`${pdfsWithMismatches} PDFs and ${pagesWithMismatches} Pages in build ${response.build.name} have changes present.`));
555554
} else {
556555
console.log(chalk.green('All PDFs match the baseline.'));
557556
}
558557

559558
Object.entries(pdfGroups).forEach(([pdfName, pages]) => {
560-
const hasMismatch = pages.some(page => page.mismatchPercentage > 0);
559+
const hasMismatch = pages.some(page => page.mismatch_percentage > 0);
561560
const statusColor = hasMismatch ? chalk.yellow : chalk.green;
562561

563562
console.log(statusColor(`\n📄 ${pdfName} (${pages.length} pages)`));
564563

565564
pages.forEach(page => {
566-
const pageStatusColor = page.mismatchPercentage > 0 ? chalk.yellow : chalk.green;
567-
console.log(pageStatusColor(` - Page ${getPageNumber(page.screenshotName)}: ${page.status} (Mismatch: ${page.mismatchPercentage}%)`));
565+
const pageStatusColor = page.mismatch_percentage > 0 ? chalk.yellow : chalk.green;
566+
console.log(pageStatusColor(` - Page ${getPageNumber(page.screenshot_name)}: ${page.status} (Mismatch: ${page.mismatch_percentage}%)`));
568567
});
569568
});
570569

571570
const formattedResults = {
572-
status: response.status,
571+
status: 'success',
573572
data: {
574-
buildId: response.data.buildId,
575-
buildName: response.data.buildName,
576-
projectName: response.data.projectName,
577-
buildStatus: response.data.buildStatus,
573+
buildId: response.build.id,
574+
buildName: response.build.name,
575+
projectName: response.project.name,
576+
buildStatus: response.build.build_satus,
578577
pdfs: formatPdfsForOutput(pdfGroups)
579578
}
580579
};
@@ -588,12 +587,6 @@ export function startPdfPolling(ctx: Context) {
588587
}
589588

590589
return;
591-
} else if (response.status === 'error') {
592-
clearInterval(interval);
593-
console.log(chalk.red(`\nError fetching results: ${response.message || 'Unknown error'}`));
594-
return;
595-
} else {
596-
process.stdout.write(chalk.yellow('.'));
597590
}
598591

599592
if (attempts >= maxAttempts) {
@@ -625,7 +618,7 @@ function groupScreenshotsByPdf(screenshots: any[]): Record<string, any[]> {
625618

626619
screenshots.forEach(screenshot => {
627620
// screenshot name format: "pdf-name.pdf#page-number"
628-
const pdfName = screenshot.screenshotName.split('#')[0];
621+
const pdfName = screenshot.screenshot_name.split('#')[0];
629622

630623
if (!pdfGroups[pdfName]) {
631624
pdfGroups[pdfName] = [];
@@ -641,7 +634,7 @@ function countPdfsWithMismatches(pdfGroups: Record<string, any[]>): number {
641634
let count = 0;
642635

643636
Object.values(pdfGroups).forEach(pages => {
644-
if (pages.some(page => page.mismatchPercentage > 0)) {
637+
if (pages.some(page => page.mismatch_percentage > 0)) {
645638
count++;
646639
}
647640
});
@@ -650,7 +643,7 @@ function countPdfsWithMismatches(pdfGroups: Record<string, any[]>): number {
650643
}
651644

652645
function countPagesWithMismatches(screenshots: any[]): number {
653-
return screenshots.filter(screenshot => screenshot.mismatchPercentage > 0).length;
646+
return screenshots.filter(screenshot => screenshot.mismatch_percentage > 0).length;
654647
}
655648

656649
function formatPdfsForOutput(pdfGroups: Record<string, any[]>): any[] {
@@ -659,12 +652,11 @@ function formatPdfsForOutput(pdfGroups: Record<string, any[]>): any[] {
659652
pdfName,
660653
pageCount: pages.length,
661654
pages: pages.map(page => ({
662-
pageNumber: getPageNumber(page.screenshotName),
663-
screenshotId: page.screenshotId,
664-
mismatchPercentage: page.mismatchPercentage,
665-
threshold: page.threshold,
655+
pageNumber: getPageNumber(page.screenshot_name),
656+
screenshotId: page.captured_image_id,
657+
mismatchPercentage: page.mismatch_percentage,
666658
status: page.status,
667-
screenshotUrl: page.screenshotUrl
659+
screenshotUrl: page.shareable_link
668660
}))
669661
};
670662
});

src/tasks/uploadPdfs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ async function uploadPdfs(ctx: Context, pdfPath: string): Promise<void> {
5555
ctx.build.id = response.buildId;
5656
ctx.log.debug(`PDF upload successful. Build ID: ${ctx.build.id}`);
5757
}
58+
if (response && response.projectId) {
59+
ctx.build.projectId = response.projectId;
60+
}
5861
} catch (error : any) {
5962
throw new Error(error.message);
6063
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export interface Build {
181181
baseline: boolean;
182182
useKafkaFlow: boolean;
183183
hasDiscoveryError: boolean;
184+
projectId?: string;
184185
}
185186

186187
export interface WebConfig {

0 commit comments

Comments
 (0)