|
| 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 | +} |
0 commit comments