Skip to content

Commit

Permalink
refactor(core): use portal-client types in helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Feb 9, 2024
1 parent 2923dbd commit db12855
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 150 deletions.
110 changes: 0 additions & 110 deletions packages/core/src/lib/implementation/json-to-gql.ts

This file was deleted.

36 changes: 0 additions & 36 deletions packages/core/src/lib/implementation/json-to-gql.unit.test.ts

This file was deleted.

129 changes: 129 additions & 0 deletions packages/core/src/lib/implementation/report-to-gql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
type AuditReport as PortalAudit,
type CategoryConfig as PortalCategory,
CategoryConfigRefType as PortalCategoryRefType,
type GroupConfig as PortalGroup,
type AuditReportIssue as PortalIssue,
IssueSeverity as PortalIssueSeverity,
IssueSourceType as PortalIssueSourceType,
type PluginReport as PortalPlugin,
type SaveReportMutationVariables,
} from '@code-pushup/portal-client';
import {
AuditReport,
CategoryConfig,
CategoryRef,
type Group,
Issue,
IssueSeverity,
PluginReport,
Report,
} from '@code-pushup/models';

export function reportToGQL(
report: Report,
): Omit<SaveReportMutationVariables, 'organization' | 'project' | 'commit'> {
return {
packageName: report.packageName,
packageVersion: report.version,
commandStartDate: report.date,
commandDuration: report.duration,
plugins: report.plugins.map(pluginToGQL),
categories: report.categories.map(categoryToGQL),
};
}

function pluginToGQL(plugin: PluginReport): PortalPlugin {
return {
slug: plugin.slug,
title: plugin.title,
icon: plugin.icon,
description: plugin.description,
docsUrl: plugin.docsUrl,
audits: plugin.audits.map(auditToGQL),
groups: plugin.groups?.map(groupToGQL),
packageName: plugin.packageName,
packageVersion: plugin.version,
runnerDuration: plugin.duration,
runnerStartDate: plugin.date,
};
}

function groupToGQL(group: Group): PortalGroup {
return {
slug: group.slug,
title: group.title,
description: group.description,
refs: group.refs.map(ref => ({ slug: ref.slug, weight: ref.weight })),
};
}

function auditToGQL(audit: AuditReport): PortalAudit {
return {
slug: audit.slug,
title: audit.title,
description: audit.description,
docsUrl: audit.docsUrl,
score: audit.score,
value: audit.value,
formattedValue: audit.displayValue,
...(audit.details && {
details: {
...(audit.details.issues && {
issues: audit.details.issues.map(issueToGQL),
}),
},
}),
};
}

export function issueToGQL(issue: Issue): PortalIssue {
return {
message: issue.message,
severity: issueSeverityToGQL(issue.severity),
...(issue.source?.file && {
sourceType: PortalIssueSourceType.SourceCode,
sourceFilePath: issue.source.file,
sourceStartLine: issue.source.position?.startLine,
sourceStartColumn: issue.source.position?.startColumn,
sourceEndLine: issue.source.position?.endLine,
sourceEndColumn: issue.source.position?.endColumn,
}),
};
}

function categoryToGQL(category: CategoryConfig): PortalCategory {
return {
slug: category.slug,
title: category.title,
description: category.description,
refs: category.refs.map(ref => ({
plugin: ref.plugin,
type: categoryRefTypeToGQL(ref.type),
weight: ref.weight,
slug: ref.slug,
})),
};
}

function categoryRefTypeToGQL(
type: CategoryRef['type'],
): PortalCategoryRefType {
switch (type) {
case 'audit':
return PortalCategoryRefType.Audit;
case 'group':
return PortalCategoryRefType.Group;
}
}

function issueSeverityToGQL(severity: IssueSeverity): PortalIssueSeverity {
switch (severity) {
case 'info':
return PortalIssueSeverity.Info;
case 'error':
return PortalIssueSeverity.Error;
case 'warning':
return PortalIssueSeverity.Warning;
}
}
26 changes: 26 additions & 0 deletions packages/core/src/lib/implementation/report-to-gql.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe } from 'vitest';
import { issueToGQL } from './report-to-gql';

describe('issueToGQL', () => {
it('transforms issue to GraphQL input type', () => {
expect(
issueToGQL({
message: 'No let, use const instead.',
severity: 'error',
source: {
file: 'cli.ts',
position: { startLine: 5, startColumn: 10, endColumn: 25 },
},
}),
).toStrictEqual({
message: 'No let, use const instead.',
severity: 'Error',
sourceType: 'SourceCode',
sourceFilePath: 'cli.ts',
sourceStartLine: 5,
sourceStartColumn: 10,
sourceEndLine: undefined,
sourceEndColumn: 25,
});
});
});
11 changes: 7 additions & 4 deletions packages/core/src/lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { uploadToPortal } from '@code-pushup/portal-client';
import {
type SaveReportMutationVariables,
uploadToPortal,
} from '@code-pushup/portal-client';
import { PersistConfig, Report, UploadConfig } from '@code-pushup/models';
import { getLatestCommit, loadReport } from '@code-pushup/utils';
import { jsonReportToGql } from './implementation/json-to-gql';
import { reportToGQL } from './implementation/report-to-gql';
import { normalizePersistConfig } from './normalize';
import { GlobalOptions } from './types';

Expand Down Expand Up @@ -31,11 +34,11 @@ export async function upload(
throw new Error('no commit data available');
}

const data = {
const data: SaveReportMutationVariables = {
organization,
project,
commit: commitData.hash,
...jsonReportToGql(report),
...reportToGQL(report),
};

return uploadFn({ apiKey, server, data, timeout });
Expand Down

0 comments on commit db12855

Please sign in to comment.