-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): use portal-client types in helper functions
- Loading branch information
1 parent
2923dbd
commit db12855
Showing
5 changed files
with
162 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
packages/core/src/lib/implementation/json-to-gql.unit.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
packages/core/src/lib/implementation/report-to-gql.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters