Skip to content

Commit

Permalink
feat(core): include audit table in portal upload
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed May 23, 2024
1 parent 76b9772 commit 2b4f3e1
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 12 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"node": ">=18.16"
},
"dependencies": {
"@code-pushup/portal-client": "^0.6.1",
"@code-pushup/portal-client": "^0.7.0",
"@isaacs/cliui": "^8.0.2",
"@poppinss/cliui": "^6.4.0",
"@swc/helpers": "0.5.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependencies": {
"@code-pushup/models": "0.43.1",
"@code-pushup/utils": "0.43.1",
"@code-pushup/portal-client": "^0.6.1",
"@code-pushup/portal-client": "^0.7.0",
"chalk": "^5.3.0"
},
"type": "commonjs",
Expand Down
52 changes: 47 additions & 5 deletions packages/core/src/lib/implementation/report-to-gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import {
IssueSeverity as PortalIssueSeverity,
IssueSourceType as PortalIssueSourceType,
type PluginReport as PortalPlugin,
type AuditReportTable as PortalTable,
TableAlignment as PortalTableAlignment,
type AuditReportTableCell as PortalTableCell,
type AuditReportTableColumn as PortalTableColumn,
type SaveReportMutationVariables,
} from '@code-pushup/portal-client';
import {
import type {
AuditReport,
CategoryConfig,
CategoryRef,
type Group,
Group,
Issue,
IssueSeverity,
PluginReport,
Report,
Table,
TableAlignment,
} from '@code-pushup/models';

export function reportToGQL(
Expand Down Expand Up @@ -69,7 +75,7 @@ function auditToGQL(audit: AuditReport): PortalAudit {
displayValue: formattedValue,
details,
} = audit;
const { issues /*, table */ } = details ?? {};
const { issues, table } = details ?? {};
return {
slug,
title,
Expand All @@ -81,8 +87,7 @@ function auditToGQL(audit: AuditReport): PortalAudit {
...(details && {
details: {
...(issues && { issues: issues.map(issueToGQL) }),
// @TODO add when https://github.com/code-pushup/cli/issues/530 is implemented
// ...(table ? {table} : {}),
...(table && { tables: [tableToGQL(table)] }),
},
}),
};
Expand All @@ -103,6 +108,32 @@ export function issueToGQL(issue: Issue): PortalIssue {
};
}

export function tableToGQL(table: Table): PortalTable {
return {
...(table.title && { title: table.title }),
...(table.columns?.length && {
columns: table.columns.map(
(column): PortalTableColumn =>
typeof column === 'string'
? { alignment: tableAlignmentToGQL(column) }
: {
key: column.key,
label: column.label,
alignment: column.align && tableAlignmentToGQL(column.align),
},
),
}),
rows: table.rows.map((row): PortalTableCell[] =>
Array.isArray(row)
? row.map(content => ({ content: content.toString() }))
: Object.entries(row).map(([key, content]) => ({
key,
content: content.toString(),
})),
),
};
}

function categoryToGQL(category: CategoryConfig): PortalCategory {
return {
slug: category.slug,
Expand Down Expand Up @@ -139,3 +170,14 @@ function issueSeverityToGQL(severity: IssueSeverity): PortalIssueSeverity {
return PortalIssueSeverity.Warning;
}
}

function tableAlignmentToGQL(alignment: TableAlignment): PortalTableAlignment {
switch (alignment) {
case 'left':
return PortalTableAlignment.Left;
case 'center':
return PortalTableAlignment.Center;
case 'right':
return PortalTableAlignment.Right;
}
}
59 changes: 58 additions & 1 deletion packages/core/src/lib/implementation/report-to-gql.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe } from 'vitest';
import { issueToGQL } from './report-to-gql';
import { issueToGQL, tableToGQL } from './report-to-gql';

describe('issueToGQL', () => {
it('transforms issue to GraphQL input type', () => {
Expand All @@ -24,3 +24,60 @@ describe('issueToGQL', () => {
});
});
});

describe('tableToGQL', () => {
it('should transform primitive table to GraphQL input type', () => {
expect(
tableToGQL({
columns: ['left', 'right'],
rows: [
['Script Evaluation', '3,167 ms'],
['Style & Layout', '1,422 ms'],
['Other', '712 ms'],
],
}),
).toStrictEqual({
columns: [{ alignment: 'Left' }, { alignment: 'Right' }],
rows: [
[{ content: 'Script Evaluation' }, { content: '3,167 ms' }],
[{ content: 'Style & Layout' }, { content: '1,422 ms' }],
[{ content: 'Other' }, { content: '712 ms' }],
],
});
});

it('should transform object table to GraphQL input type', () => {
expect(
tableToGQL({
columns: [
{ key: 'category', label: 'Category', align: 'left' },
{ key: 'timeSpent', label: 'Time Spent', align: 'right' },
],
rows: [
{ category: 'Script Evaluation', timeSpent: '3,167 ms' },
{ category: 'Style & Layout', timeSpent: '1,422 ms' },
{ category: 'Other', timeSpent: '712 ms' },
],
}),
).toStrictEqual({
columns: [
{ key: 'category', label: 'Category', alignment: 'Left' },
{ key: 'timeSpent', label: 'Time Spent', alignment: 'Right' },
],
rows: [
[
{ key: 'category', content: 'Script Evaluation' },
{ key: 'timeSpent', content: '3,167 ms' },
],
[
{ key: 'category', content: 'Style & Layout' },
{ key: 'timeSpent', content: '1,422 ms' },
],
[
{ key: 'category', content: 'Other' },
{ key: 'timeSpent', content: '712 ms' },
],
],
});
});
});

0 comments on commit 2b4f3e1

Please sign in to comment.