forked from codecov/sourcegraph-codecov
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri.ts
More file actions
102 lines (90 loc) · 2.73 KB
/
uri.ts
File metadata and controls
102 lines (90 loc) · 2.73 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
99
100
101
102
import { CodecovGetCommitCoverageArgs } from './api'
import { Endpoint, Settings } from './settings'
/**
* A resolved URI without an identified path.
*/
export interface ResolvedRootURI {
repo: string
rev: string
}
/**
* A resolved URI with an identified path in a repository at a specific revision.
*/
export interface ResolvedDocumentURI extends ResolvedRootURI {
path: string
}
/**
* Resolve a URI of the form git://github.com/owner/repo?rev to an absolute reference.
*/
export function resolveRootURI(uri: string): ResolvedRootURI {
const url = new URL(uri)
if (url.protocol !== 'git:') {
throw new Error(`Unsupported protocol: ${url.protocol}`)
}
const repo = (url.host + url.pathname).replace(/^\/*/, '')
const rev = url.search.slice(1)
if (!rev) {
throw new Error('Could not determine revision')
}
return { repo, rev }
}
/**
* Resolve a URI of the form git://github.com/owner/repo?rev#path to an absolute reference.
*/
export function resolveDocumentURI(uri: string): ResolvedDocumentURI {
return {
...resolveRootURI(uri),
path: new URL(uri).hash.slice(1),
}
}
export interface KnownHost {
name: string
service: string
}
/**
* Returns the URL parameters used to access the Codecov API for the URI's repository.
*
* Currently only GitHub.com repositories are supported.
*/
export function codecovParamsForRepositoryCommit(
uri: Pick<ResolvedRootURI, 'repo' | 'rev'>,
sourcegraph: typeof import('sourcegraph')
): Pick<
CodecovGetCommitCoverageArgs,
'baseURL' | 'service' | 'owner' | 'repo' | 'sha'
> {
try {
const endpoints:
| Readonly<Endpoint[]>
| undefined = sourcegraph.configuration
.get<Settings>()
.get('codecov.endpoints')
const baseURL: string =
(endpoints && endpoints[0] && endpoints[0].url) || ''
const knownHosts: KnownHost[] = [
{ name: 'github.com', service: 'gh' },
{ name: 'gitlab.com', service: 'gl' },
{ name: 'bitbucket.org', service: 'bb' },
]
const knownHost: KnownHost | undefined = knownHosts.find(knownHost =>
uri.repo.includes(knownHost.name)
)
let service: string =
(endpoints && endpoints[0] && endpoints[0].service) || 'gh'
const [, owner, repo] = uri.repo.split('/', 4)
service = (knownHost && knownHost.service) || service
return {
baseURL,
service,
owner,
repo,
sha: uri.rev,
}
} catch (err) {
throw new Error(
`extension does not yet support the repository ${JSON.stringify(
uri.repo
)}`
)
}
}