forked from codecov/sourcegraph-codecov
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.ts
More file actions
98 lines (91 loc) · 3.09 KB
/
model.ts
File metadata and controls
98 lines (91 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { CodecovCommitData, codecovGetCommitCoverage } from './api'
import { Endpoint } from './settings'
import {
codecovParamsForRepositoryCommit,
ResolvedDocumentURI,
ResolvedRootURI,
} from './uri'
export interface FileLineCoverage {
[line: string]: LineCoverage
}
export type LineCoverage = number | { hits: number; branches: number } | null
/** Gets the coverage ratio for a commit. */
export async function getCommitCoverageRatio(
{ repo, rev }: Pick<ResolvedRootURI, 'repo' | 'rev'>,
endpoint: Endpoint,
sourcegraph: typeof import('sourcegraph')
): Promise<number | undefined> {
const data = await codecovGetCommitCoverage({
...codecovParamsForRepositoryCommit({ repo, rev }, sourcegraph),
baseURL: endpoint.url,
token: endpoint.token,
})
return data.commit.totals.coverage
}
/** Gets line coverage data for a file at a given commit in a repository. */
export async function getFileLineCoverage(
{ repo, rev, path }: ResolvedDocumentURI,
endpoint: Endpoint,
sourcegraph: typeof import('sourcegraph')
): Promise<FileLineCoverage> {
const data = await codecovGetCommitCoverage({
...codecovParamsForRepositoryCommit({ repo, rev }, sourcegraph),
baseURL: endpoint.url,
token: endpoint.token,
})
return toLineCoverage(data, path)
}
/** Gets the file coverage ratios for all files at a given commit in a repository. */
export async function getFileCoverageRatios(
{ repo, rev }: Pick<ResolvedRootURI, 'repo' | 'rev'>,
endpoint: Endpoint,
sourcegraph: typeof import('sourcegraph')
): Promise<{ [path: string]: number }> {
const data = await codecovGetCommitCoverage({
...codecovParamsForRepositoryCommit({ repo, rev }, sourcegraph),
baseURL: endpoint.url,
token: endpoint.token,
})
const ratios: { [path: string]: number } = {}
for (const [path, fileData] of Object.entries(data.commit.report.files)) {
const ratio = toCoverageRatio(fileData)
if (ratio !== undefined) {
ratios[path] = ratio
}
}
return ratios
}
function toLineCoverage(
data: CodecovCommitData,
path: string
): FileLineCoverage {
const result: FileLineCoverage = {}
const fileData = data.commit.report.files[path]
if (fileData) {
for (const [lineStr, value] of Object.entries(fileData.l)) {
const line = parseInt(lineStr, 10)
if (typeof value === 'number' || value === null) {
result[line] = value
} else if (typeof value === 'string') {
const [hits, branches] = value
.split('/', 2)
.map(v => parseInt(v, 10))
result[line] = { hits, branches }
}
}
}
return result
}
function toCoverageRatio(
fileData: CodecovCommitData['commit']['report']['files'][string]
): number | undefined {
const ratioStr = fileData && fileData.t.c
if (!ratioStr) {
return undefined
}
const ratio = parseFloat(ratioStr)
if (Number.isNaN(ratio)) {
return undefined
}
return ratio
}