Skip to content

Commit db12855

Browse files
committed
refactor(core): use portal-client types in helper functions
1 parent 2923dbd commit db12855

5 files changed

Lines changed: 162 additions & 150 deletions

File tree

packages/core/src/lib/implementation/json-to-gql.ts

Lines changed: 0 additions & 110 deletions
This file was deleted.

packages/core/src/lib/implementation/json-to-gql.unit.test.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import {
2+
type AuditReport as PortalAudit,
3+
type CategoryConfig as PortalCategory,
4+
CategoryConfigRefType as PortalCategoryRefType,
5+
type GroupConfig as PortalGroup,
6+
type AuditReportIssue as PortalIssue,
7+
IssueSeverity as PortalIssueSeverity,
8+
IssueSourceType as PortalIssueSourceType,
9+
type PluginReport as PortalPlugin,
10+
type SaveReportMutationVariables,
11+
} from '@code-pushup/portal-client';
12+
import {
13+
AuditReport,
14+
CategoryConfig,
15+
CategoryRef,
16+
type Group,
17+
Issue,
18+
IssueSeverity,
19+
PluginReport,
20+
Report,
21+
} from '@code-pushup/models';
22+
23+
export function reportToGQL(
24+
report: Report,
25+
): Omit<SaveReportMutationVariables, 'organization' | 'project' | 'commit'> {
26+
return {
27+
packageName: report.packageName,
28+
packageVersion: report.version,
29+
commandStartDate: report.date,
30+
commandDuration: report.duration,
31+
plugins: report.plugins.map(pluginToGQL),
32+
categories: report.categories.map(categoryToGQL),
33+
};
34+
}
35+
36+
function pluginToGQL(plugin: PluginReport): PortalPlugin {
37+
return {
38+
slug: plugin.slug,
39+
title: plugin.title,
40+
icon: plugin.icon,
41+
description: plugin.description,
42+
docsUrl: plugin.docsUrl,
43+
audits: plugin.audits.map(auditToGQL),
44+
groups: plugin.groups?.map(groupToGQL),
45+
packageName: plugin.packageName,
46+
packageVersion: plugin.version,
47+
runnerDuration: plugin.duration,
48+
runnerStartDate: plugin.date,
49+
};
50+
}
51+
52+
function groupToGQL(group: Group): PortalGroup {
53+
return {
54+
slug: group.slug,
55+
title: group.title,
56+
description: group.description,
57+
refs: group.refs.map(ref => ({ slug: ref.slug, weight: ref.weight })),
58+
};
59+
}
60+
61+
function auditToGQL(audit: AuditReport): PortalAudit {
62+
return {
63+
slug: audit.slug,
64+
title: audit.title,
65+
description: audit.description,
66+
docsUrl: audit.docsUrl,
67+
score: audit.score,
68+
value: audit.value,
69+
formattedValue: audit.displayValue,
70+
...(audit.details && {
71+
details: {
72+
...(audit.details.issues && {
73+
issues: audit.details.issues.map(issueToGQL),
74+
}),
75+
},
76+
}),
77+
};
78+
}
79+
80+
export function issueToGQL(issue: Issue): PortalIssue {
81+
return {
82+
message: issue.message,
83+
severity: issueSeverityToGQL(issue.severity),
84+
...(issue.source?.file && {
85+
sourceType: PortalIssueSourceType.SourceCode,
86+
sourceFilePath: issue.source.file,
87+
sourceStartLine: issue.source.position?.startLine,
88+
sourceStartColumn: issue.source.position?.startColumn,
89+
sourceEndLine: issue.source.position?.endLine,
90+
sourceEndColumn: issue.source.position?.endColumn,
91+
}),
92+
};
93+
}
94+
95+
function categoryToGQL(category: CategoryConfig): PortalCategory {
96+
return {
97+
slug: category.slug,
98+
title: category.title,
99+
description: category.description,
100+
refs: category.refs.map(ref => ({
101+
plugin: ref.plugin,
102+
type: categoryRefTypeToGQL(ref.type),
103+
weight: ref.weight,
104+
slug: ref.slug,
105+
})),
106+
};
107+
}
108+
109+
function categoryRefTypeToGQL(
110+
type: CategoryRef['type'],
111+
): PortalCategoryRefType {
112+
switch (type) {
113+
case 'audit':
114+
return PortalCategoryRefType.Audit;
115+
case 'group':
116+
return PortalCategoryRefType.Group;
117+
}
118+
}
119+
120+
function issueSeverityToGQL(severity: IssueSeverity): PortalIssueSeverity {
121+
switch (severity) {
122+
case 'info':
123+
return PortalIssueSeverity.Info;
124+
case 'error':
125+
return PortalIssueSeverity.Error;
126+
case 'warning':
127+
return PortalIssueSeverity.Warning;
128+
}
129+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe } from 'vitest';
2+
import { issueToGQL } from './report-to-gql';
3+
4+
describe('issueToGQL', () => {
5+
it('transforms issue to GraphQL input type', () => {
6+
expect(
7+
issueToGQL({
8+
message: 'No let, use const instead.',
9+
severity: 'error',
10+
source: {
11+
file: 'cli.ts',
12+
position: { startLine: 5, startColumn: 10, endColumn: 25 },
13+
},
14+
}),
15+
).toStrictEqual({
16+
message: 'No let, use const instead.',
17+
severity: 'Error',
18+
sourceType: 'SourceCode',
19+
sourceFilePath: 'cli.ts',
20+
sourceStartLine: 5,
21+
sourceStartColumn: 10,
22+
sourceEndLine: undefined,
23+
sourceEndColumn: 25,
24+
});
25+
});
26+
});

packages/core/src/lib/upload.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { uploadToPortal } from '@code-pushup/portal-client';
1+
import {
2+
type SaveReportMutationVariables,
3+
uploadToPortal,
4+
} from '@code-pushup/portal-client';
25
import { PersistConfig, Report, UploadConfig } from '@code-pushup/models';
36
import { getLatestCommit, loadReport } from '@code-pushup/utils';
4-
import { jsonReportToGql } from './implementation/json-to-gql';
7+
import { reportToGQL } from './implementation/report-to-gql';
58
import { normalizePersistConfig } from './normalize';
69
import { GlobalOptions } from './types';
710

@@ -31,11 +34,11 @@ export async function upload(
3134
throw new Error('no commit data available');
3235
}
3336

34-
const data = {
37+
const data: SaveReportMutationVariables = {
3538
organization,
3639
project,
3740
commit: commitData.hash,
38-
...jsonReportToGql(report),
41+
...reportToGQL(report),
3942
};
4043

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

0 commit comments

Comments
 (0)